blob: c444c1afa1acc890e46e9e11f4fb8923f2951177 [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker7bc05ff2011-08-09 10:30:36 +000031#include "polarssl/rsa.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000032#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000034
35#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000036 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
37 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000038int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000039{
Paul Bakkercce9d772011-11-18 14:26:47 +000040 ((void) argc);
41 ((void) argv);
42
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000044 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
45 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000046 return( 0 );
47}
48#else
49int main( int argc, char *argv[] )
50{
51 FILE *f;
52 int ret;
53 size_t i;
54 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000055 entropy_context entropy;
56 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000057 unsigned char input[1024];
58 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020059 const char *pers = "rsa_encrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000060
61 ret = 1;
62
63 if( argc != 2 )
64 {
65 printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
66
Paul Bakkercce9d772011-11-18 14:26:47 +000067#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000068 printf( "\n" );
69#endif
70
71 goto exit;
72 }
73
Paul Bakker508ad5a2011-12-04 17:09:26 +000074 printf( "\n . Seeding the random number generator..." );
75 fflush( stdout );
76
77 entropy_init( &entropy );
78 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 (const unsigned char *) pers,
80 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000081 {
82 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
83 goto exit;
84 }
85
Paul Bakkerd246ed32011-10-06 13:18:27 +000086 printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000087 fflush( stdout );
88
Paul Bakkerd246ed32011-10-06 13:18:27 +000089 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000090 {
91 ret = 1;
Paul Bakkerd246ed32011-10-06 13:18:27 +000092 printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000093 " ! Please run rsa_genkey first\n\n" );
94 goto exit;
95 }
96
97 rsa_init( &rsa, RSA_PKCS_V15, 0 );
98
Paul Bakkerd246ed32011-10-06 13:18:27 +000099 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
100 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000101 {
102 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
103 goto exit;
104 }
105
106 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
107
108 fclose( f );
109
110 if( strlen( argv[1] ) > 100 )
111 {
112 printf( " Input data larger than 100 characters.\n\n" );
113 goto exit;
114 }
115
116 memcpy( input, argv[1], strlen( argv[1] ) );
117
118 /*
119 * Calculate the RSA encryption of the hash.
120 */
121 printf( "\n . Generating the RSA encrypted value" );
122 fflush( stdout );
123
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
125 RSA_PUBLIC, strlen( argv[1] ),
126 input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000127 {
128 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
129 goto exit;
130 }
131
132 /*
133 * Write the signature into result-enc.txt
134 */
135 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
136 {
137 ret = 1;
138 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
139 goto exit;
140 }
141
142 for( i = 0; i < rsa.len; i++ )
143 fprintf( f, "%02X%s", buf[i],
144 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
145
146 fclose( f );
147
148 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
149
150exit:
151
Paul Bakkercce9d772011-11-18 14:26:47 +0000152#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000153 printf( " + Press Enter to exit this program.\n" );
154 fflush( stdout ); getchar();
155#endif
156
157 return( ret );
158}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000159#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
160 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */