1 /*
2 * RSA simple data encryption program
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_printf printf
35 #define mbedtls_exit exit
36 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39
40 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
41 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
42 defined(MBEDTLS_CTR_DRBG_C)
43 #include "mbedtls/error.h"
44 #include "mbedtls/pk.h"
45 #include "mbedtls/entropy.h"
46 #include "mbedtls/ctr_drbg.h"
47
48 #include <stdio.h>
49 #include <string.h>
50 #endif
51
52 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
53 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
54 !defined(MBEDTLS_CTR_DRBG_C)
main(void)55 int main( void )
56 {
57 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
58 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
59 "MBEDTLS_CTR_DRBG_C not defined.\n");
60 return( 0 );
61 }
62 #else
63
64 #if defined(MBEDTLS_CHECK_PARAMS)
65 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)66 void mbedtls_param_failed( const char *failure_condition,
67 const char *file,
68 int line )
69 {
70 mbedtls_printf( "%s:%i: Input param failed - %s\n",
71 file, line, failure_condition );
72 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
73 }
74 #endif
75
main(int argc,char * argv[])76 int main( int argc, char *argv[] )
77 {
78 FILE *f;
79 int ret = 1;
80 int exit_code = MBEDTLS_EXIT_FAILURE;
81 size_t i, olen = 0;
82 mbedtls_pk_context pk;
83 mbedtls_entropy_context entropy;
84 mbedtls_ctr_drbg_context ctr_drbg;
85 unsigned char input[1024];
86 unsigned char buf[512];
87 const char *pers = "mbedtls_pk_encrypt";
88
89 mbedtls_ctr_drbg_init( &ctr_drbg );
90 mbedtls_entropy_init( &entropy );
91 mbedtls_pk_init( &pk );
92
93 if( argc != 3 )
94 {
95 mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
96
97 #if defined(_WIN32)
98 mbedtls_printf( "\n" );
99 #endif
100
101 goto exit;
102 }
103
104 mbedtls_printf( "\n . Seeding the random number generator..." );
105 fflush( stdout );
106
107 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
108 &entropy, (const unsigned char *) pers,
109 strlen( pers ) ) ) != 0 )
110 {
111 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
112 -ret );
113 goto exit;
114 }
115
116 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
117 fflush( stdout );
118
119 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
120 {
121 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
122 goto exit;
123 }
124
125 if( strlen( argv[2] ) > 100 )
126 {
127 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
128 goto exit;
129 }
130
131 memcpy( input, argv[2], strlen( argv[2] ) );
132
133 /*
134 * Calculate the RSA encryption of the hash.
135 */
136 mbedtls_printf( "\n . Generating the encrypted value" );
137 fflush( stdout );
138
139 if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
140 buf, &olen, sizeof(buf),
141 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
142 {
143 mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
144 -ret );
145 goto exit;
146 }
147
148 /*
149 * Write the signature into result-enc.txt
150 */
151 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
152 {
153 mbedtls_printf( " failed\n ! Could not create %s\n\n",
154 "result-enc.txt" );
155 ret = 1;
156 goto exit;
157 }
158
159 for( i = 0; i < olen; i++ )
160 {
161 mbedtls_fprintf( f, "%02X%s", buf[i],
162 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
163 }
164
165 fclose( f );
166
167 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
168
169 exit_code = MBEDTLS_EXIT_SUCCESS;
170
171 exit:
172
173 mbedtls_pk_free( &pk );
174 mbedtls_entropy_free( &entropy );
175 mbedtls_ctr_drbg_free( &ctr_drbg );
176
177 #if defined(MBEDTLS_ERROR_C)
178 if( exit_code != MBEDTLS_EXIT_SUCCESS )
179 {
180 mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
181 mbedtls_printf( " ! Last error was: %s\n", buf );
182 }
183 #endif
184
185 #if defined(_WIN32)
186 mbedtls_printf( " + Press Enter to exit this program.\n" );
187 fflush( stdout ); getchar();
188 #endif
189
190 return( exit_code );
191 }
192 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
193 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
194