• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  RSA simple data encryption program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13 
14 #include "mbedtls/platform.h"
15 
16 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
17     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
18     defined(MBEDTLS_CTR_DRBG_C)
19 #include "mbedtls/error.h"
20 #include "mbedtls/pk.h"
21 #include "mbedtls/entropy.h"
22 #include "mbedtls/ctr_drbg.h"
23 
24 #include <stdio.h>
25 #include <string.h>
26 #endif
27 
28 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) ||  \
29     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
30     !defined(MBEDTLS_CTR_DRBG_C)
main(void)31 int main(void)
32 {
33     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
34                    "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
35                    "MBEDTLS_CTR_DRBG_C not defined.\n");
36     mbedtls_exit(0);
37 }
38 #else
39 
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43     FILE *f;
44     int ret = 1;
45     int exit_code = MBEDTLS_EXIT_FAILURE;
46     size_t i, olen = 0;
47     mbedtls_pk_context pk;
48     mbedtls_entropy_context entropy;
49     mbedtls_ctr_drbg_context ctr_drbg;
50     unsigned char input[1024];
51     unsigned char buf[512];
52     const char *pers = "mbedtls_pk_encrypt";
53 
54     mbedtls_ctr_drbg_init(&ctr_drbg);
55     mbedtls_entropy_init(&entropy);
56     mbedtls_pk_init(&pk);
57 
58 #if defined(MBEDTLS_USE_PSA_CRYPTO)
59     psa_status_t status = psa_crypto_init();
60     if (status != PSA_SUCCESS) {
61         mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
62                         (int) status);
63         goto exit;
64     }
65 #endif /* MBEDTLS_USE_PSA_CRYPTO */
66 
67     if (argc != 3) {
68         mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
69 
70 #if defined(_WIN32)
71         mbedtls_printf("\n");
72 #endif
73 
74         goto exit;
75     }
76 
77     mbedtls_printf("\n  . Seeding the random number generator...");
78     fflush(stdout);
79 
80     if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
81                                      &entropy, (const unsigned char *) pers,
82                                      strlen(pers))) != 0) {
83         mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
84                        (unsigned int) -ret);
85         goto exit;
86     }
87 
88     mbedtls_printf("\n  . Reading public key from '%s'", argv[1]);
89     fflush(stdout);
90 
91     if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
92         mbedtls_printf(" failed\n  ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
93                        (unsigned int) -ret);
94         goto exit;
95     }
96 
97     if (strlen(argv[2]) > 100) {
98         mbedtls_printf(" Input data larger than 100 characters.\n\n");
99         goto exit;
100     }
101 
102     memcpy(input, argv[2], strlen(argv[2]));
103 
104     /*
105      * Calculate the RSA encryption of the hash.
106      */
107     mbedtls_printf("\n  . Generating the encrypted value");
108     fflush(stdout);
109 
110     if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]),
111                                   buf, &olen, sizeof(buf),
112                                   mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
113         mbedtls_printf(" failed\n  ! mbedtls_pk_encrypt returned -0x%04x\n",
114                        (unsigned int) -ret);
115         goto exit;
116     }
117 
118     /*
119      * Write the signature into result-enc.txt
120      */
121     if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
122         mbedtls_printf(" failed\n  ! Could not create %s\n\n",
123                        "result-enc.txt");
124         ret = 1;
125         goto exit;
126     }
127 
128     for (i = 0; i < olen; i++) {
129         mbedtls_fprintf(f, "%02X%s", buf[i],
130                         (i + 1) % 16 == 0 ? "\r\n" : " ");
131     }
132 
133     fclose(f);
134 
135     mbedtls_printf("\n  . Done (created \"%s\")\n\n", "result-enc.txt");
136 
137     exit_code = MBEDTLS_EXIT_SUCCESS;
138 
139 exit:
140 
141     mbedtls_pk_free(&pk);
142     mbedtls_entropy_free(&entropy);
143     mbedtls_ctr_drbg_free(&ctr_drbg);
144 #if defined(MBEDTLS_USE_PSA_CRYPTO)
145     mbedtls_psa_crypto_free();
146 #endif /* MBEDTLS_USE_PSA_CRYPTO */
147 
148 #if defined(MBEDTLS_ERROR_C)
149     if (exit_code != MBEDTLS_EXIT_SUCCESS) {
150         mbedtls_strerror(ret, (char *) buf, sizeof(buf));
151         mbedtls_printf("  !  Last error was: %s\n", buf);
152     }
153 #endif
154 
155 #if defined(_WIN32)
156     mbedtls_printf("  + Press Enter to exit this program.\n");
157     fflush(stdout); getchar();
158 #endif
159 
160     mbedtls_exit(exit_code);
161 }
162 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
163           MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
164