blob: 9f298493904ced37fac52005280c48f60b65963e [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker940f9ce2013-09-18 15:34:57 +020038#include <string.h>
39#include <stdio.h>
40
Paul Bakker940f9ce2013-09-18 15:34:57 +020041#include "polarssl/error.h"
42#include "polarssl/pk.h"
43#include "polarssl/entropy.h"
44#include "polarssl/ctr_drbg.h"
45
46#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
47 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
48 !defined(POLARSSL_CTR_DRBG_C)
49int main( int argc, char *argv[] )
50{
51 ((void) argc);
52 ((void) argv);
53
Rich Evansf90016a2015-01-19 14:26:37 +000054 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020055 "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
56 "POLARSSL_CTR_DRBG_C not defined.\n");
57 return( 0 );
58}
59#else
60int main( int argc, char *argv[] )
61{
62 FILE *f;
63 int ret;
64 size_t i, olen = 0;
65 pk_context pk;
66 entropy_context entropy;
67 ctr_drbg_context ctr_drbg;
68 unsigned char input[1024];
69 unsigned char buf[512];
70 const char *pers = "pk_encrypt";
71
72 ret = 1;
73
74 if( argc != 3 )
75 {
Rich Evansf90016a2015-01-19 14:26:37 +000076 polarssl_printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020077
78#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020080#endif
81
82 goto exit;
83 }
84
Rich Evansf90016a2015-01-19 14:26:37 +000085 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +020086 fflush( stdout );
87
88 entropy_init( &entropy );
89 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
90 (const unsigned char *) pers,
91 strlen( pers ) ) ) != 0 )
92 {
Rich Evansf90016a2015-01-19 14:26:37 +000093 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 goto exit;
95 }
96
Rich Evansf90016a2015-01-19 14:26:37 +000097 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020098 fflush( stdout );
99
100 pk_init( &pk );
101
102 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
103 {
Rich Evansf90016a2015-01-19 14:26:37 +0000104 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200105 goto exit;
106 }
107
108 if( strlen( argv[2] ) > 100 )
109 {
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200111 goto exit;
112 }
113
114 memcpy( input, argv[2], strlen( argv[2] ) );
115
116 /*
117 * Calculate the RSA encryption of the hash.
118 */
Rich Evansf90016a2015-01-19 14:26:37 +0000119 polarssl_printf( "\n . Generating the encrypted value" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 fflush( stdout );
121
122 if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
123 buf, &olen, sizeof(buf),
124 ctr_drbg_random, &ctr_drbg ) ) != 0 )
125 {
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200127 goto exit;
128 }
129
130 /*
131 * Write the signature into result-enc.txt
132 */
133 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
134 {
135 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000136 polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200137 goto exit;
138 }
139
140 for( i = 0; i < olen; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
143
144 fclose( f );
145
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200147
148exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200149 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200150 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200152#if defined(POLARSSL_ERROR_C)
153 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200155#endif
156
Paul Bakker940f9ce2013-09-18 15:34:57 +0200157#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200159 fflush( stdout ); getchar();
160#endif
161
162 return( ret );
163}
164#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
165 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */