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
39
40 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
41 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
42 defined(MBEDTLS_CTR_DRBG_C)
43 #include "mbedtls/rsa.h"
44 #include "mbedtls/entropy.h"
45 #include "mbedtls/ctr_drbg.h"
46
47 #include <string.h>
48 #endif
49
50 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
51 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
52 !defined(MBEDTLS_CTR_DRBG_C)
main(void)53 int main( void )
54 {
55 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
56 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
57 "MBEDTLS_CTR_DRBG_C not defined.\n");
58 return( 0 );
59 }
60 #else
main(int argc,char * argv[])61 int main( int argc, char *argv[] )
62 {
63 FILE *f;
64 int return_val, exit_val;
65 size_t i;
66 mbedtls_rsa_context rsa;
67 mbedtls_entropy_context entropy;
68 mbedtls_ctr_drbg_context ctr_drbg;
69 unsigned char input[1024];
70 unsigned char buf[512];
71 const char *pers = "rsa_encrypt";
72
73 exit_val = MBEDTLS_EXIT_SUCCESS;
74
75 if( argc != 2 )
76 {
77 mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
78
79 #if defined(_WIN32)
80 mbedtls_printf( "\n" );
81 #endif
82
83 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
84 }
85
86 mbedtls_printf( "\n . Seeding the random number generator..." );
87 fflush( stdout );
88
89 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
90 mbedtls_ctr_drbg_init( &ctr_drbg );
91 mbedtls_entropy_init( &entropy );
92
93 return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
94 &entropy, (const unsigned char *) pers,
95 strlen( pers ) );
96 if( return_val != 0 )
97 {
98 exit_val = MBEDTLS_EXIT_FAILURE;
99 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
100 return_val );
101 goto exit;
102 }
103
104 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
105 fflush( stdout );
106
107 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
108 {
109 exit_val = MBEDTLS_EXIT_FAILURE;
110 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
111 " ! Please run rsa_genkey first\n\n" );
112 goto exit;
113 }
114
115 if( ( return_val = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
116 ( return_val = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
117 {
118 exit_val = MBEDTLS_EXIT_FAILURE;
119 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
120 return_val );
121 fclose( f );
122 goto exit;
123 }
124
125 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
126
127 fclose( f );
128
129 if( strlen( argv[1] ) > 100 )
130 {
131 exit_val = MBEDTLS_EXIT_FAILURE;
132 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
133 goto exit;
134 }
135
136 memcpy( input, argv[1], strlen( argv[1] ) );
137
138 /*
139 * Calculate the RSA encryption of the hash.
140 */
141 mbedtls_printf( "\n . Generating the RSA encrypted value" );
142 fflush( stdout );
143
144 return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
145 &ctr_drbg, MBEDTLS_RSA_PUBLIC,
146 strlen( argv[1] ), input, buf );
147 if( return_val != 0 )
148 {
149 exit_val = MBEDTLS_EXIT_FAILURE;
150 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
151 return_val );
152 goto exit;
153 }
154
155 /*
156 * Write the signature into result-enc.txt
157 */
158 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
159 {
160 exit_val = MBEDTLS_EXIT_FAILURE;
161 mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
162 goto exit;
163 }
164
165 for( i = 0; i < rsa.len; i++ )
166 mbedtls_fprintf( f, "%02X%s", buf[i],
167 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
168
169 fclose( f );
170
171 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
172
173 exit:
174 mbedtls_ctr_drbg_free( &ctr_drbg );
175 mbedtls_entropy_free( &entropy );
176 mbedtls_rsa_free( &rsa );
177
178 #if defined(_WIN32)
179 mbedtls_printf( " + Press Enter to exit this program.\n" );
180 fflush( stdout ); getchar();
181 #endif
182
183 return( exit_val );
184 }
185 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
186 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
187