1 /*
2 * RSA simple data encryption program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include "mbedtls/build_info.h"
21
22 #if defined(MBEDTLS_PLATFORM_C)
23 #include "mbedtls/platform.h"
24 #else
25 #include <stdio.h>
26 #include <stdlib.h>
27 #define mbedtls_fprintf fprintf
28 #define mbedtls_printf printf
29 #define mbedtls_exit exit
30 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
31 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
32 #endif /* MBEDTLS_PLATFORM_C */
33
34 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
35 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
36 defined(MBEDTLS_CTR_DRBG_C)
37 #include "mbedtls/error.h"
38 #include "mbedtls/pk.h"
39 #include "mbedtls/entropy.h"
40 #include "mbedtls/ctr_drbg.h"
41
42 #include <stdio.h>
43 #include <string.h>
44 #endif
45
46 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
47 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
48 !defined(MBEDTLS_CTR_DRBG_C)
main(void)49 int main( void )
50 {
51 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
52 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
53 "MBEDTLS_CTR_DRBG_C not defined.\n");
54 mbedtls_exit( 0 );
55 }
56 #else
57
58
main(int argc,char * argv[])59 int main( int argc, char *argv[] )
60 {
61 FILE *f;
62 int ret = 1;
63 int exit_code = MBEDTLS_EXIT_FAILURE;
64 size_t i, olen = 0;
65 mbedtls_pk_context pk;
66 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
68 unsigned char input[1024];
69 unsigned char buf[512];
70 const char *pers = "mbedtls_pk_encrypt";
71
72 mbedtls_ctr_drbg_init( &ctr_drbg );
73 mbedtls_entropy_init( &entropy );
74 mbedtls_pk_init( &pk );
75
76 if( argc != 3 )
77 {
78 mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
79
80 #if defined(_WIN32)
81 mbedtls_printf( "\n" );
82 #endif
83
84 goto exit;
85 }
86
87 mbedtls_printf( "\n . Seeding the random number generator..." );
88 fflush( stdout );
89
90 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
91 &entropy, (const unsigned char *) pers,
92 strlen( pers ) ) ) != 0 )
93 {
94 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
95 (unsigned int) -ret );
96 goto exit;
97 }
98
99 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
100 fflush( stdout );
101
102 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
103 {
104 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
105 goto exit;
106 }
107
108 if( strlen( argv[2] ) > 100 )
109 {
110 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
111 goto exit;
112 }
113
114 memcpy( input, argv[2], strlen( argv[2] ) );
115
116 /*
117 * Calculate the RSA encryption of the hash.
118 */
119 mbedtls_printf( "\n . Generating the encrypted value" );
120 fflush( stdout );
121
122 if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
123 buf, &olen, sizeof(buf),
124 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
125 {
126 mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
127 (unsigned int) -ret );
128 goto exit;
129 }
130
131 /*
132 * Write the signature into result-enc.txt
133 */
134 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
135 {
136 mbedtls_printf( " failed\n ! Could not create %s\n\n",
137 "result-enc.txt" );
138 ret = 1;
139 goto exit;
140 }
141
142 for( i = 0; i < olen; i++ )
143 {
144 mbedtls_fprintf( f, "%02X%s", buf[i],
145 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
146 }
147
148 fclose( f );
149
150 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
151
152 exit_code = MBEDTLS_EXIT_SUCCESS;
153
154 exit:
155
156 mbedtls_pk_free( &pk );
157 mbedtls_entropy_free( &entropy );
158 mbedtls_ctr_drbg_free( &ctr_drbg );
159
160 #if defined(MBEDTLS_ERROR_C)
161 if( exit_code != MBEDTLS_EXIT_SUCCESS )
162 {
163 mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
164 mbedtls_printf( " ! Last error was: %s\n", buf );
165 }
166 #endif
167
168 #if defined(_WIN32)
169 mbedtls_printf( " + Press Enter to exit this program.\n" );
170 fflush( stdout ); getchar();
171 #endif
172
173 mbedtls_exit( exit_code );
174 }
175 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
176 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
177