blob: 3230eb0bafb8e51569c6080e973368fb2ca2a519 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * RSA simple data encryption program
3 *
4 * Copyright (C) 2006-2011, Brainspark B.V.
5 *
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/error.h"
36#include "polarssl/pk.h"
37#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
39
40#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
41 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
42 !defined(POLARSSL_CTR_DRBG_C)
43int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
48 printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
49 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
50 "POLARSSL_CTR_DRBG_C not defined.\n");
51 return( 0 );
52}
53#else
54int main( int argc, char *argv[] )
55{
56 FILE *f;
57 int ret;
58 size_t i, olen = 0;
59 pk_context pk;
60 entropy_context entropy;
61 ctr_drbg_context ctr_drbg;
62 unsigned char input[1024];
63 unsigned char buf[512];
64 const char *pers = "pk_encrypt";
65
66 ret = 1;
67
68 if( argc != 3 )
69 {
70 printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" );
71
72#if defined(_WIN32)
73 printf( "\n" );
74#endif
75
76 goto exit;
77 }
78
79 printf( "\n . Seeding the random number generator..." );
80 fflush( stdout );
81
82 entropy_init( &entropy );
83 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
84 (const unsigned char *) pers,
85 strlen( pers ) ) ) != 0 )
86 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020087 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020088 goto exit;
89 }
90
91 printf( "\n . Reading public key from '%s'", argv[1] );
92 fflush( stdout );
93
94 pk_init( &pk );
95
96 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
97 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020098 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020099 goto exit;
100 }
101
102 if( strlen( argv[2] ) > 100 )
103 {
104 printf( " Input data larger than 100 characters.\n\n" );
105 goto exit;
106 }
107
108 memcpy( input, argv[2], strlen( argv[2] ) );
109
110 /*
111 * Calculate the RSA encryption of the hash.
112 */
113 printf( "\n . Generating the encrypted value" );
114 fflush( stdout );
115
116 if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
117 buf, &olen, sizeof(buf),
118 ctr_drbg_random, &ctr_drbg ) ) != 0 )
119 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200120 printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 goto exit;
122 }
123
124 /*
125 * Write the signature into result-enc.txt
126 */
127 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
128 {
129 ret = 1;
130 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
131 goto exit;
132 }
133
134 for( i = 0; i < olen; i++ )
135 fprintf( f, "%02X%s", buf[i],
136 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
137
138 fclose( f );
139
140 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
141
142exit:
143
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200144#if defined(POLARSSL_ERROR_C)
145 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
146 printf( " ! Last error was: %s\n", buf );
147#endif
148
Paul Bakker940f9ce2013-09-18 15:34:57 +0200149#if defined(_WIN32)
150 printf( " + Press Enter to exit this program.\n" );
151 fflush( stdout ); getchar();
152#endif
153
154 return( ret );
155}
156#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
157 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */