blob: aa6ca0e2735e5a44fa3859477f614ca5bbd42900 [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 Bakker508ad5a2011-12-04 17:09:26 +000063 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,
83 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
84 {
85 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
86 goto exit;
87 }
88
Paul Bakkerd246ed32011-10-06 13:18:27 +000089 printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000090 fflush( stdout );
91
Paul Bakkerd246ed32011-10-06 13:18:27 +000092 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000093 {
94 ret = 1;
Paul Bakkerd246ed32011-10-06 13:18:27 +000095 printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000096 " ! Please run rsa_genkey first\n\n" );
97 goto exit;
98 }
99
100 rsa_init( &rsa, RSA_PKCS_V15, 0 );
101
Paul Bakkerd246ed32011-10-06 13:18:27 +0000102 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
103 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000104 {
105 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
106 goto exit;
107 }
108
109 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
110
111 fclose( f );
112
113 if( strlen( argv[1] ) > 100 )
114 {
115 printf( " Input data larger than 100 characters.\n\n" );
116 goto exit;
117 }
118
119 memcpy( input, argv[1], strlen( argv[1] ) );
120
121 /*
122 * Calculate the RSA encryption of the hash.
123 */
124 printf( "\n . Generating the RSA encrypted value" );
125 fflush( stdout );
126
Paul Bakker508ad5a2011-12-04 17:09:26 +0000127 if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
128 RSA_PUBLIC, strlen( argv[1] ),
129 input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000130 {
131 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
132 goto exit;
133 }
134
135 /*
136 * Write the signature into result-enc.txt
137 */
138 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
139 {
140 ret = 1;
141 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
142 goto exit;
143 }
144
145 for( i = 0; i < rsa.len; i++ )
146 fprintf( f, "%02X%s", buf[i],
147 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
148
149 fclose( f );
150
151 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
152
153exit:
154
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 */