1 /*
2  *  RSA simple data encryption program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: GPL-2.0
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29 
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
32 #else
33 #include <stdio.h>
34 #include <stdlib.h>
35 #define mbedtls_fprintf         fprintf
36 #define mbedtls_printf          printf
37 #define mbedtls_exit            exit
38 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
39 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
40 #endif /* MBEDTLS_PLATFORM_C */
41 
42 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
43     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
44     defined(MBEDTLS_CTR_DRBG_C)
45 #include "mbedtls/rsa.h"
46 #include "mbedtls/entropy.h"
47 #include "mbedtls/ctr_drbg.h"
48 
49 #include <string.h>
50 #endif
51 
52 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_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_RSA_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
main(int argc,char * argv[])63 int main( int argc, char *argv[] )
64 {
65     FILE *f;
66     int ret = 1;
67     int exit_code = MBEDTLS_EXIT_FAILURE;
68     size_t i;
69     mbedtls_rsa_context rsa;
70     mbedtls_entropy_context entropy;
71     mbedtls_ctr_drbg_context ctr_drbg;
72     unsigned char input[1024];
73     unsigned char buf[512];
74     const char *pers = "rsa_encrypt";
75     mbedtls_mpi N, E;
76 
77     if( argc != 2 )
78     {
79         mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
80 
81 #if defined(_WIN32)
82         mbedtls_printf( "\n" );
83 #endif
84 
85         mbedtls_exit( exit_code );
86     }
87 
88     mbedtls_printf( "\n  . Seeding the random number generator..." );
89     fflush( stdout );
90 
91     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
92     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
93     mbedtls_ctr_drbg_init( &ctr_drbg );
94     mbedtls_entropy_init( &entropy );
95 
96     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
97                                  &entropy, (const unsigned char *) pers,
98                                  strlen( pers ) );
99     if( ret != 0 )
100     {
101         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
102                         ret );
103         goto exit;
104     }
105 
106     mbedtls_printf( "\n  . Reading public key from rsa_pub.txt" );
107     fflush( stdout );
108 
109     if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
110     {
111         mbedtls_printf( " failed\n  ! Could not open rsa_pub.txt\n" \
112                 "  ! Please run rsa_genkey first\n\n" );
113         goto exit;
114     }
115 
116     if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
117         ( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
118     {
119         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
120                         ret );
121         fclose( f );
122         goto exit;
123     }
124     fclose( f );
125 
126     if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
127     {
128         mbedtls_printf( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
129                         ret );
130         goto exit;
131     }
132 
133     if( strlen( argv[1] ) > 100 )
134     {
135         mbedtls_printf( " Input data larger than 100 characters.\n\n" );
136         goto exit;
137     }
138 
139     memcpy( input, argv[1], strlen( argv[1] ) );
140 
141     /*
142      * Calculate the RSA encryption of the hash.
143      */
144     mbedtls_printf( "\n  . Generating the RSA encrypted value" );
145     fflush( stdout );
146 
147     ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
148                                      &ctr_drbg, MBEDTLS_RSA_PUBLIC,
149                                      strlen( argv[1] ), input, buf );
150     if( ret != 0 )
151     {
152         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
153                         ret );
154         goto exit;
155     }
156 
157     /*
158      * Write the signature into result-enc.txt
159      */
160     if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
161     {
162         mbedtls_printf( " failed\n  ! Could not create %s\n\n", "result-enc.txt" );
163         goto exit;
164     }
165 
166     for( i = 0; i < rsa.len; i++ )
167         mbedtls_fprintf( f, "%02X%s", buf[i],
168                  ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
169 
170     fclose( f );
171 
172     mbedtls_printf( "\n  . Done (created \"%s\")\n\n", "result-enc.txt" );
173 
174     exit_code = MBEDTLS_EXIT_SUCCESS;
175 
176 exit:
177     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
178     mbedtls_ctr_drbg_free( &ctr_drbg );
179     mbedtls_entropy_free( &entropy );
180     mbedtls_rsa_free( &rsa );
181 
182 #if defined(_WIN32)
183     mbedtls_printf( "  + Press Enter to exit this program.\n" );
184     fflush( stdout ); getchar();
185 #endif
186 
187     return( exit_code );
188 }
189 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
190           MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
191