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