blob: 5e427626805cd8c702e00fda05273eab2adf967b [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * RSA simple data encryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +02007 *
Paul Bakker940f9ce2013-09-18 15:34:57 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_PK_PARSE_C) &&\
38 defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) &&\
39 defined(POLARSSL_CTR_DRBG_C)
Paul Bakker940f9ce2013-09-18 15:34:57 +020040#include "polarssl/error.h"
41#include "polarssl/pk.h"
42#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
44
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <string.h>
47#endif
48
Paul Bakker940f9ce2013-09-18 15:34:57 +020049#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
50 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
51 !defined(POLARSSL_CTR_DRBG_C)
52int main( int argc, char *argv[] )
53{
54 ((void) argc);
55 ((void) argv);
56
Rich Evansf90016a2015-01-19 14:26:37 +000057 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020058 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
59 "POLARSSL_CTR_DRBG_C not defined.\n");
60 return( 0 );
61}
62#else
63int main( int argc, char *argv[] )
64{
65 FILE *f;
66 int ret;
67 size_t i, olen = 0;
68 pk_context pk;
69 entropy_context entropy;
70 ctr_drbg_context ctr_drbg;
71 unsigned char input[1024];
72 unsigned char buf[512];
73 const char *pers = "pk_encrypt";
74
75 ret = 1;
76
77 if( argc != 3 )
78 {
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020080
81#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020083#endif
84
85 goto exit;
86 }
87
Rich Evansf90016a2015-01-19 14:26:37 +000088 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +020089 fflush( stdout );
90
91 entropy_init( &entropy );
92 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
93 (const unsigned char *) pers,
94 strlen( pers ) ) ) != 0 )
95 {
Rich Evansf90016a2015-01-19 14:26:37 +000096 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020097 goto exit;
98 }
99
Rich Evansf90016a2015-01-19 14:26:37 +0000100 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200101 fflush( stdout );
102
103 pk_init( &pk );
104
105 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
106 {
Rich Evansf90016a2015-01-19 14:26:37 +0000107 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200108 goto exit;
109 }
110
111 if( strlen( argv[2] ) > 100 )
112 {
Rich Evansf90016a2015-01-19 14:26:37 +0000113 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200114 goto exit;
115 }
116
117 memcpy( input, argv[2], strlen( argv[2] ) );
118
119 /*
120 * Calculate the RSA encryption of the hash.
121 */
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_printf( "\n . Generating the encrypted value" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200123 fflush( stdout );
124
125 if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
126 buf, &olen, sizeof(buf),
127 ctr_drbg_random, &ctr_drbg ) ) != 0 )
128 {
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 goto exit;
131 }
132
133 /*
134 * Write the signature into result-enc.txt
135 */
136 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
137 {
138 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200140 goto exit;
141 }
142
143 for( i = 0; i < olen; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
146
147 fclose( f );
148
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200150
151exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200152 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200153 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200154
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200155#if defined(POLARSSL_ERROR_C)
156 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000157 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200158#endif
159
Paul Bakker940f9ce2013-09-18 15:34:57 +0200160#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200162 fflush( stdout ); getchar();
163#endif
164
165 return( ret );
166}
167#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
168 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */