blob: fe84aee770f554c7ae7abae7cfa415098340fef3 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * RSA simple data encryption program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
38 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
39 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/error.h"
41#include "mbedtls/pk.h"
42#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <string.h>
47#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
50 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
51 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000052int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
55 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
56 "MBEDTLS_CTR_DRBG_C not defined.\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020057 return( 0 );
58}
59#else
60int main( int argc, char *argv[] )
61{
62 FILE *f;
63 int ret;
64 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_pk_context pk;
66 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020068 unsigned char input[1024];
69 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 const char *pers = "mbedtls_pk_encrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020071
72 ret = 1;
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020073 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker940f9ce2013-09-18 15:34:57 +020074
75 if( argc != 3 )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020078
79#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020081#endif
82
83 goto exit;
84 }
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +020087 fflush( stdout );
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020090 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker940f9ce2013-09-18 15:34:57 +020091 (const unsigned char *) pers,
92 strlen( pers ) ) ) != 0 )
93 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020094 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020099 fflush( stdout );
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_init( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200106 goto exit;
107 }
108
109 if( strlen( argv[2] ) > 100 )
110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112 goto exit;
113 }
114
115 memcpy( input, argv[2], strlen( argv[2] ) );
116
117 /*
118 * Calculate the RSA encryption of the hash.
119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( "\n . Generating the encrypted value" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 fflush( stdout );
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
Paul Bakker940f9ce2013-09-18 15:34:57 +0200124 buf, &olen, sizeof(buf),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128 goto exit;
129 }
130
131 /*
132 * Write the signature into result-enc.txt
133 */
134 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
135 {
136 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200138 goto exit;
139 }
140
141 for( i = 0; i < olen; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_fprintf( f, "%02X%s", buf[i],
Paul Bakker940f9ce2013-09-18 15:34:57 +0200143 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
144
145 fclose( f );
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200148
149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_ctr_drbg_free( &ctr_drbg );
151 mbedtls_entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_ERROR_C)
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200154 if( ret != 0 )
155 {
156 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
157 mbedtls_printf( " ! Last error was: %s\n", buf );
158 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200159#endif
160
Paul Bakker940f9ce2013-09-18 15:34:57 +0200161#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200163 fflush( stdout ); getchar();
164#endif
165
166 return( ret );
167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
169 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */