blob: 5d7d1e036eaf6dca970915fb24ebeec77497e5cd [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 {
87 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
88 printf( " failed\n ! ctr_drbg_init returned -0x%04x - %s\n\n", -ret, buf );
89 goto exit;
90 }
91
92 printf( "\n . Reading public key from '%s'", argv[1] );
93 fflush( stdout );
94
95 pk_init( &pk );
96
97 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
98 {
99 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
100 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x - %s\n\n", -ret, buf );
101 goto exit;
102 }
103
104 if( strlen( argv[2] ) > 100 )
105 {
106 printf( " Input data larger than 100 characters.\n\n" );
107 goto exit;
108 }
109
110 memcpy( input, argv[2], strlen( argv[2] ) );
111
112 /*
113 * Calculate the RSA encryption of the hash.
114 */
115 printf( "\n . Generating the encrypted value" );
116 fflush( stdout );
117
118 if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
119 buf, &olen, sizeof(buf),
120 ctr_drbg_random, &ctr_drbg ) ) != 0 )
121 {
122 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
123 printf( " failed\n ! pk_encrypt returned -0x%04x - %s\n\n", -ret, buf );
124 goto exit;
125 }
126
127 /*
128 * Write the signature into result-enc.txt
129 */
130 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
131 {
132 ret = 1;
133 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
134 goto exit;
135 }
136
137 for( i = 0; i < olen; i++ )
138 fprintf( f, "%02X%s", buf[i],
139 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
140
141 fclose( f );
142
143 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
144
145exit:
146
147#if defined(_WIN32)
148 printf( " + Press Enter to exit this program.\n" );
149 fflush( stdout ); getchar();
150#endif
151
152 return( ret );
153}
154#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
155 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */