blob: 69ffbcdaed6a87bedcb39418999e03c4a4ad6620 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdio.h>
31
Paul Bakker7bc05ff2011-08-09 10:30:36 +000032#include "polarssl/rsa.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000033#include "polarssl/entropy.h"
34#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000035
36#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000037 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
38 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000039int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000040{
Paul Bakkercce9d772011-11-18 14:26:47 +000041 ((void) argc);
42 ((void) argv);
43
Paul Bakker7bc05ff2011-08-09 10:30:36 +000044 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000045 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
46 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000047 return( 0 );
48}
49#else
50int main( int argc, char *argv[] )
51{
52 FILE *f;
53 int ret;
54 size_t i;
55 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000056 entropy_context entropy;
57 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000058 unsigned char input[1024];
59 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020060 const char *pers = "rsa_encrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000061
62 ret = 1;
63
64 if( argc != 2 )
65 {
66 printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
67
Paul Bakkercce9d772011-11-18 14:26:47 +000068#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000069 printf( "\n" );
70#endif
71
72 goto exit;
73 }
74
Paul Bakker508ad5a2011-12-04 17:09:26 +000075 printf( "\n . Seeding the random number generator..." );
76 fflush( stdout );
77
78 entropy_init( &entropy );
79 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020080 (const unsigned char *) pers,
81 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000082 {
83 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
84 goto exit;
85 }
86
Paul Bakkerd246ed32011-10-06 13:18:27 +000087 printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000088 fflush( stdout );
89
Paul Bakkerd246ed32011-10-06 13:18:27 +000090 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000091 {
92 ret = 1;
Paul Bakkerd246ed32011-10-06 13:18:27 +000093 printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000094 " ! Please run rsa_genkey first\n\n" );
95 goto exit;
96 }
97
98 rsa_init( &rsa, RSA_PKCS_V15, 0 );
99
Paul Bakkerd246ed32011-10-06 13:18:27 +0000100 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
101 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000102 {
103 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
104 goto exit;
105 }
106
107 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
108
109 fclose( f );
110
111 if( strlen( argv[1] ) > 100 )
112 {
113 printf( " Input data larger than 100 characters.\n\n" );
114 goto exit;
115 }
116
117 memcpy( input, argv[1], strlen( argv[1] ) );
118
119 /*
120 * Calculate the RSA encryption of the hash.
121 */
122 printf( "\n . Generating the RSA encrypted value" );
123 fflush( stdout );
124
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
126 RSA_PUBLIC, strlen( argv[1] ),
127 input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000128 {
129 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
130 goto exit;
131 }
132
133 /*
134 * Write the signature into result-enc.txt
135 */
136 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
137 {
138 ret = 1;
139 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
140 goto exit;
141 }
142
143 for( i = 0; i < rsa.len; i++ )
144 fprintf( f, "%02X%s", buf[i],
145 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
146
147 fclose( f );
148
149 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
150
151exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200152 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200153 entropy_free( &entropy );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000154
Paul Bakkercce9d772011-11-18 14:26:47 +0000155#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000156 printf( " + Press Enter to exit this program.\n" );
157 fflush( stdout ); getchar();
158#endif
159
160 return( ret );
161}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
163 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */