blob: f0ade14eca30fd134b35603e2741776bfae2eeb6 [file] [log] [blame]
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001/*
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +00002 * Test application that shows some mbed TLS and OpenSSL compatibility
Paul Bakkere6ee41f2012-05-19 08:43:48 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2011-2012 ARM Limited, All Rights Reserved
Paul Bakkere6ee41f2012-05-19 08:43:48 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere6ee41f2012-05-19 08:43:48 +00007 *
Paul Bakkere6ee41f2012-05-19 08:43:48 +00008 * 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 Bakkere6ee41f2012-05-19 08:43:48 +000028
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_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000036#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000037 defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO)
Paul Bakkere6ee41f2012-05-19 08:43:48 +000038#include <openssl/rsa.h>
Rich Evans18b78c72015-02-11 14:06:19 +000039
Paul Bakker5eb264c2014-01-23 15:43:07 +010040#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000041#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010042#endif
Rich Evans18b78c72015-02-11 14:06:19 +000043
Paul Bakkere6ee41f2012-05-19 08:43:48 +000044#include <openssl/pem.h>
45#include <openssl/bio.h>
46
Paul Bakker36713e82013-09-17 13:25:29 +020047#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000048#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000049#include "polarssl/entropy.h"
50#include "polarssl/ctr_drbg.h"
51
Rich Evans18b78c72015-02-11 14:06:19 +000052#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000055#endif
56
Paul Bakkered27a042013-04-18 22:46:23 +020057#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020058 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000059int main( void )
Paul Bakkered27a042013-04-18 22:46:23 +020060{
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020062 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020063 return( 0 );
64}
65#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000066int main( int argc, char *argv[] )
67{
68 int ret;
69 FILE *key_file;
70 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020071 pk_context p_pk;
72 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000073 RSA *o_rsa;
74 entropy_context entropy;
75 ctr_drbg_context ctr_drbg;
76 unsigned char input[1024];
77 unsigned char p_pub_encrypted[512];
78 unsigned char o_pub_encrypted[512];
79 unsigned char p_pub_decrypted[512];
80 unsigned char o_pub_decrypted[512];
81 unsigned char p_priv_encrypted[512];
82 unsigned char o_priv_encrypted[512];
83 unsigned char p_priv_decrypted[512];
84 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020085 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000086
87 entropy_init( &entropy );
88 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020089 (const unsigned char *) pers,
90 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000091 {
Rich Evansf90016a2015-01-19 14:26:37 +000092 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +000093 goto exit;
94 }
95 ERR_load_crypto_strings();
96
97 ret = 1;
98
99 if( argc != 3 )
100 {
Rich Evansf90016a2015-01-19 14:26:37 +0000101 polarssl_printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000102
103#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000104 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000105#endif
106
107 goto exit;
108 }
109
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000111 fflush( stdout );
112
Paul Bakker36713e82013-09-17 13:25:29 +0200113 pk_init( &p_pk );
114 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000115 {
116 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000117 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000118 goto exit;
119 }
120
Paul Bakker36713e82013-09-17 13:25:29 +0200121 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
122 {
123 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200125 goto exit;
126 }
127
128 p_rsa = pk_rsa( p_pk );
129
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000131
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000133 fflush( stdout );
134
135 key_file = fopen( argv[1], "r" );
136 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
137 fclose(key_file);
138 if( o_rsa == NULL )
139 {
140 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000142 goto exit;
143 }
144
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_printf( " passed\n");
146 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000147
148 if( strlen( argv[1] ) > 100 )
149 {
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000151 goto exit;
152 }
153
154 memcpy( input, argv[2], strlen( argv[2] ) );
155
156 /*
157 * Calculate the RSA encryption with public key.
158 */
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000160 fflush( stdout );
161
Paul Bakker36713e82013-09-17 13:25:29 +0200162 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[2] ), input, p_pub_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000163 {
Rich Evansf90016a2015-01-19 14:26:37 +0000164 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000165 goto exit;
166 }
167 else
Rich Evansf90016a2015-01-19 14:26:37 +0000168 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000169
Rich Evansf90016a2015-01-19 14:26:37 +0000170 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000171 fflush( stdout );
172
Paul Bakker36713e82013-09-17 13:25:29 +0200173 if( ( ret = RSA_public_encrypt( strlen( argv[2] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000174 {
175 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000176 polarssl_printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000177 goto exit;
178 }
179 else
Rich Evansf90016a2015-01-19 14:26:37 +0000180 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000181
182 /*
183 * Calculate the RSA encryption with private key.
184 */
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000186 fflush( stdout );
187
Paul Bakker36713e82013-09-17 13:25:29 +0200188 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[2] ), input, p_priv_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000189 {
Rich Evansf90016a2015-01-19 14:26:37 +0000190 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000191 goto exit;
192 }
193 else
Rich Evansf90016a2015-01-19 14:26:37 +0000194 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000195
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000197 fflush( stdout );
198
Paul Bakker36713e82013-09-17 13:25:29 +0200199 if( ( ret = RSA_private_encrypt( strlen( argv[2] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000200 {
201 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000202 polarssl_printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000203 goto exit;
204 }
205 else
Rich Evansf90016a2015-01-19 14:26:37 +0000206 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000207
Rich Evansf90016a2015-01-19 14:26:37 +0000208 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000209
210 /*
211 * Calculate the RSA decryption with private key.
212 */
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000214 fflush( stdout );
215
Paul Bakker36713e82013-09-17 13:25:29 +0200216 if( ( ret = rsa_pkcs1_decrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, &olen, o_pub_encrypted, p_pub_decrypted, 1024 ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000217 {
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000219 }
220 else
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000222
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000224 fflush( stdout );
225
Paul Bakker36713e82013-09-17 13:25:29 +0200226 if( ( ret = RSA_private_decrypt( p_rsa->len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000227 {
228 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000230 }
231 else
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000233
234 /*
235 * Calculate the RSA decryption with public key.
236 */
Rich Evansf90016a2015-01-19 14:26:37 +0000237 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000238 fflush( stdout );
239
Paul Bakker36713e82013-09-17 13:25:29 +0200240 if( ( ret = rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000241 {
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000243 }
244 else
Rich Evansf90016a2015-01-19 14:26:37 +0000245 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000246
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000248 fflush( stdout );
249
Paul Bakker36713e82013-09-17 13:25:29 +0200250 if( ( ret = RSA_public_decrypt( p_rsa->len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000251 {
252 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000254 }
255 else
Rich Evansf90016a2015-01-19 14:26:37 +0000256 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000257
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_printf( "\n" );
259 polarssl_printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
260 polarssl_printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
261 polarssl_printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
262 polarssl_printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000263
264exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200265 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200266 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000267
268#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000269 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000270 fflush( stdout ); getchar();
271#endif
272
273 return( ret );
274}
Paul Bakkered27a042013-04-18 22:46:23 +0200275#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200276 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */