blob: 2f88209d73730842fad8304720fc3c0dc7c65a85 [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple data encryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker7bc05ff2011-08-09 10:30:36 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7bc05ff2011-08-09 10:30:36 +00007 *
Paul Bakker7bc05ff2011-08-09 10:30:36 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker7bc05ff2011-08-09 10:30:36 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) &&\
38 defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) &&\
39 defined(POLARSSL_CTR_DRBG_C)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000040#include "polarssl/rsa.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000041#include "polarssl/entropy.h"
42#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <stdio.h>
45#include <string.h>
46#endif
47
Paul Bakker7bc05ff2011-08-09 10:30:36 +000048#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
50 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000051int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000052{
Paul Bakkercce9d772011-11-18 14:26:47 +000053 ((void) argc);
54 ((void) argv);
55
Rich Evansf90016a2015-01-19 14:26:37 +000056 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000057 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
58 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000059 return( 0 );
60}
61#else
62int main( int argc, char *argv[] )
63{
64 FILE *f;
65 int ret;
66 size_t i;
67 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 entropy_context entropy;
69 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000070 unsigned char input[1024];
71 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072 const char *pers = "rsa_encrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000073
74 ret = 1;
75
76 if( argc != 2 )
77 {
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000079
Paul Bakkercce9d772011-11-18 14:26:47 +000080#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000081 polarssl_printf( "\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000082#endif
83
84 goto exit;
85 }
86
Rich Evansf90016a2015-01-19 14:26:37 +000087 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +000088 fflush( stdout );
89
90 entropy_init( &entropy );
91 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020092 (const unsigned char *) pers,
93 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000094 {
Rich Evansf90016a2015-01-19 14:26:37 +000095 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000096 goto exit;
97 }
98
Rich Evansf90016a2015-01-19 14:26:37 +000099 polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000100 fflush( stdout );
101
Paul Bakkerd246ed32011-10-06 13:18:27 +0000102 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000103 {
104 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000105 polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000106 " ! Please run rsa_genkey first\n\n" );
107 goto exit;
108 }
109
110 rsa_init( &rsa, RSA_PKCS_V15, 0 );
111
Paul Bakkerd246ed32011-10-06 13:18:27 +0000112 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
113 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000114 {
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000116 goto exit;
117 }
118
119 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
120
121 fclose( f );
122
123 if( strlen( argv[1] ) > 100 )
124 {
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000126 goto exit;
127 }
128
129 memcpy( input, argv[1], strlen( argv[1] ) );
130
131 /*
132 * Calculate the RSA encryption of the hash.
133 */
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( "\n . Generating the RSA encrypted value" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000135 fflush( stdout );
136
Paul Bakker508ad5a2011-12-04 17:09:26 +0000137 if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
138 RSA_PUBLIC, strlen( argv[1] ),
139 input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000140 {
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000142 goto exit;
143 }
144
145 /*
146 * Write the signature into result-enc.txt
147 */
148 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
149 {
150 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000151 polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000152 goto exit;
153 }
154
155 for( i = 0; i < rsa.len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000157 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
158
159 fclose( f );
160
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000162
163exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200164 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200165 entropy_free( &entropy );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000166
Paul Bakkercce9d772011-11-18 14:26:47 +0000167#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000168 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000169 fflush( stdout ); getchar();
170#endif
171
172 return( ret );
173}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000174#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
175 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */