blob: aa202b00e14b921ea773ab2c8f59609d3861bc4b [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker940f9ce2013-09-18 15:34:57 +020031#include "polarssl/error.h"
32#include "polarssl/pk.h"
33#include "polarssl/entropy.h"
34#include "polarssl/ctr_drbg.h"
35
36#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
37 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
38 !defined(POLARSSL_CTR_DRBG_C)
39int main( int argc, char *argv[] )
40{
41 ((void) argc);
42 ((void) argv);
43
44 printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
45 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
46 "POLARSSL_CTR_DRBG_C 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, olen = 0;
55 pk_context pk;
56 entropy_context entropy;
57 ctr_drbg_context ctr_drbg;
58 unsigned char input[1024];
59 unsigned char buf[512];
60 const char *pers = "pk_encrypt";
61
62 ret = 1;
63
64 if( argc != 3 )
65 {
66 printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" );
67
68#if defined(_WIN32)
69 printf( "\n" );
70#endif
71
72 goto exit;
73 }
74
75 printf( "\n . Seeding the random number generator..." );
76 fflush( stdout );
77
78 entropy_init( &entropy );
79 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
80 (const unsigned char *) pers,
81 strlen( pers ) ) ) != 0 )
82 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020083 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020084 goto exit;
85 }
86
87 printf( "\n . Reading public key from '%s'", argv[1] );
88 fflush( stdout );
89
90 pk_init( &pk );
91
92 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
93 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020094 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
98 if( strlen( argv[2] ) > 100 )
99 {
100 printf( " Input data larger than 100 characters.\n\n" );
101 goto exit;
102 }
103
104 memcpy( input, argv[2], strlen( argv[2] ) );
105
106 /*
107 * Calculate the RSA encryption of the hash.
108 */
109 printf( "\n . Generating the encrypted value" );
110 fflush( stdout );
111
112 if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
113 buf, &olen, sizeof(buf),
114 ctr_drbg_random, &ctr_drbg ) ) != 0 )
115 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200116 printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200117 goto exit;
118 }
119
120 /*
121 * Write the signature into result-enc.txt
122 */
123 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
124 {
125 ret = 1;
126 printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
127 goto exit;
128 }
129
130 for( i = 0; i < olen; i++ )
131 fprintf( f, "%02X%s", buf[i],
132 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
133
134 fclose( f );
135
136 printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
137
138exit:
139
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200140#if defined(POLARSSL_ERROR_C)
141 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
142 printf( " ! Last error was: %s\n", buf );
143#endif
144
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145#if defined(_WIN32)
146 printf( " + Press Enter to exit this program.\n" );
147 fflush( stdout ); getchar();
148#endif
149
150 return( ret );
151}
152#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
153 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */