blob: 83dcef2dff3f1db49fe252a5616adf697f1bad54 [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple data encryption program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker7bc05ff2011-08-09 10:30:36 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
33#include "polarssl/config.h"
34
35#include "polarssl/rsa.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000038
39#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000040 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
41 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000042int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043{
Paul Bakkercce9d772011-11-18 14:26:47 +000044 ((void) argc);
45 ((void) argv);
46
Paul Bakker7bc05ff2011-08-09 10:30:36 +000047 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000048 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
49 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000050 return( 0 );
51}
52#else
53int main( int argc, char *argv[] )
54{
55 FILE *f;
56 int ret;
57 size_t i;
58 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000059 entropy_context entropy;
60 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000061 unsigned char input[1024];
62 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020063 const char *pers = "rsa_encrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000064
65 ret = 1;
66
67 if( argc != 2 )
68 {
69 printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
70
Paul Bakkercce9d772011-11-18 14:26:47 +000071#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000072 printf( "\n" );
73#endif
74
75 goto exit;
76 }
77
Paul Bakker508ad5a2011-12-04 17:09:26 +000078 printf( "\n . Seeding the random number generator..." );
79 fflush( stdout );
80
81 entropy_init( &entropy );
82 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 (const unsigned char *) pers,
84 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000085 {
86 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
87 goto exit;
88 }
89
Paul Bakkerd246ed32011-10-06 13:18:27 +000090 printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000091 fflush( stdout );
92
Paul Bakkerd246ed32011-10-06 13:18:27 +000093 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000094 {
95 ret = 1;
Paul Bakkerd246ed32011-10-06 13:18:27 +000096 printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000097 " ! Please run rsa_genkey first\n\n" );
98 goto exit;
99 }
100
101 rsa_init( &rsa, RSA_PKCS_V15, 0 );
102
Paul Bakkerd246ed32011-10-06 13:18:27 +0000103 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
104 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000105 {
106 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
107 goto exit;
108 }
109
110 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
111
112 fclose( f );
113
114 if( strlen( argv[1] ) > 100 )
115 {
116 printf( " Input data larger than 100 characters.\n\n" );
117 goto exit;
118 }
119
120 memcpy( input, argv[1], strlen( argv[1] ) );
121
122 /*
123 * Calculate the RSA encryption of the hash.
124 */
125 printf( "\n . Generating the RSA encrypted value" );
126 fflush( stdout );
127
Paul Bakker508ad5a2011-12-04 17:09:26 +0000128 if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
129 RSA_PUBLIC, strlen( argv[1] ),
130 input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000131 {
132 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
133 goto exit;
134 }
135
136 /*
137 * Write the signature into result-enc.txt
138 */
139 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
140 {
141 ret = 1;
142 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
143 goto exit;
144 }
145
146 for( i = 0; i < rsa.len; i++ )
147 fprintf( f, "%02X%s", buf[i],
148 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
149
150 fclose( f );
151
152 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
153
154exit:
155
Paul Bakkercce9d772011-11-18 14:26:47 +0000156#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000157 printf( " + Press Enter to exit this program.\n" );
158 fflush( stdout ); getchar();
159#endif
160
161 return( ret );
162}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000163#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
164 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */