blob: 0b54a17ae7c58fefdf8cf7c457dc1a79ca57f4c3 [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"
36#include "polarssl/havege.h"
37
38#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
39 !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000040int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000041{
Paul Bakkercce9d772011-11-18 14:26:47 +000042 ((void) argc);
43 ((void) argv);
44
Paul Bakker7bc05ff2011-08-09 10:30:36 +000045 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
46 "POLARSSL_HAVEGE_C and/or POLARSSL_FS_IO not defined.\n");
47 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;
56 havege_state hs;
57 unsigned char input[1024];
58 unsigned char buf[512];
59
60 havege_init( &hs );
61
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 Bakkerd246ed32011-10-06 13:18:27 +000075 printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000076 fflush( stdout );
77
Paul Bakkerd246ed32011-10-06 13:18:27 +000078 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000079 {
80 ret = 1;
Paul Bakkerd246ed32011-10-06 13:18:27 +000081 printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000082 " ! Please run rsa_genkey first\n\n" );
83 goto exit;
84 }
85
86 rsa_init( &rsa, RSA_PKCS_V15, 0 );
87
Paul Bakkerd246ed32011-10-06 13:18:27 +000088 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000090 {
91 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
92 goto exit;
93 }
94
95 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
96
97 fclose( f );
98
99 if( strlen( argv[1] ) > 100 )
100 {
101 printf( " Input data larger than 100 characters.\n\n" );
102 goto exit;
103 }
104
105 memcpy( input, argv[1], strlen( argv[1] ) );
106
107 /*
108 * Calculate the RSA encryption of the hash.
109 */
110 printf( "\n . Generating the RSA encrypted value" );
111 fflush( stdout );
112
Paul Bakkerd246ed32011-10-06 13:18:27 +0000113 if( ( ret = rsa_pkcs1_encrypt( &rsa, havege_rand, &hs, RSA_PUBLIC, strlen( argv[1] ), input, buf ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000114 {
115 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
116 goto exit;
117 }
118
119 /*
120 * Write the signature into result-enc.txt
121 */
122 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
123 {
124 ret = 1;
125 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
126 goto exit;
127 }
128
129 for( i = 0; i < rsa.len; i++ )
130 fprintf( f, "%02X%s", buf[i],
131 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
132
133 fclose( f );
134
135 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
136
137exit:
138
Paul Bakkercce9d772011-11-18 14:26:47 +0000139#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000140 printf( " + Press Enter to exit this program.\n" );
141 fflush( stdout ); getchar();
142#endif
143
144 return( ret );
145}
146#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_HAVEGE_C &&
147 POLARSSL_FS_IO */