blob: 19fef9c91b994f42cc0926f0323810067a32616b [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) &&\
37 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>
55#include <sys/stat.h>
56#include <unistd.h>
57#endif
58
Paul Bakkered27a042013-04-18 22:46:23 +020059#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020060 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020061int main( int argc, char *argv[] )
62{
63 ((void) argc);
64 ((void) argv);
65
Rich Evansf90016a2015-01-19 14:26:37 +000066 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020067 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020068 return( 0 );
69}
70#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000071int main( int argc, char *argv[] )
72{
73 int ret;
74 FILE *key_file;
75 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020076 pk_context p_pk;
77 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000078 RSA *o_rsa;
79 entropy_context entropy;
80 ctr_drbg_context ctr_drbg;
81 unsigned char input[1024];
82 unsigned char p_pub_encrypted[512];
83 unsigned char o_pub_encrypted[512];
84 unsigned char p_pub_decrypted[512];
85 unsigned char o_pub_decrypted[512];
86 unsigned char p_priv_encrypted[512];
87 unsigned char o_priv_encrypted[512];
88 unsigned char p_priv_decrypted[512];
89 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020090 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000091
92 entropy_init( &entropy );
93 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 (const unsigned char *) pers,
95 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000096 {
Rich Evansf90016a2015-01-19 14:26:37 +000097 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +000098 goto exit;
99 }
100 ERR_load_crypto_strings();
101
102 ret = 1;
103
104 if( argc != 3 )
105 {
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000107
108#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000109 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000110#endif
111
112 goto exit;
113 }
114
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000116 fflush( stdout );
117
Paul Bakker36713e82013-09-17 13:25:29 +0200118 pk_init( &p_pk );
119 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000120 {
121 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000123 goto exit;
124 }
125
Paul Bakker36713e82013-09-17 13:25:29 +0200126 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
127 {
128 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200130 goto exit;
131 }
132
133 p_rsa = pk_rsa( p_pk );
134
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000136
Rich Evansf90016a2015-01-19 14:26:37 +0000137 polarssl_printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000138 fflush( stdout );
139
140 key_file = fopen( argv[1], "r" );
141 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
142 fclose(key_file);
143 if( o_rsa == NULL )
144 {
145 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000147 goto exit;
148 }
149
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( " passed\n");
151 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000152
153 if( strlen( argv[1] ) > 100 )
154 {
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000156 goto exit;
157 }
158
159 memcpy( input, argv[2], strlen( argv[2] ) );
160
161 /*
162 * Calculate the RSA encryption with public key.
163 */
Rich Evansf90016a2015-01-19 14:26:37 +0000164 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000165 fflush( stdout );
166
Paul Bakker36713e82013-09-17 13:25:29 +0200167 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 +0000168 {
Rich Evansf90016a2015-01-19 14:26:37 +0000169 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000170 goto exit;
171 }
172 else
Rich Evansf90016a2015-01-19 14:26:37 +0000173 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000174
Rich Evansf90016a2015-01-19 14:26:37 +0000175 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000176 fflush( stdout );
177
Paul Bakker36713e82013-09-17 13:25:29 +0200178 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 +0000179 {
180 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000181 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 +0000182 goto exit;
183 }
184 else
Rich Evansf90016a2015-01-19 14:26:37 +0000185 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000186
187 /*
188 * Calculate the RSA encryption with private key.
189 */
Rich Evansf90016a2015-01-19 14:26:37 +0000190 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000191 fflush( stdout );
192
Paul Bakker36713e82013-09-17 13:25:29 +0200193 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 +0000194 {
Rich Evansf90016a2015-01-19 14:26:37 +0000195 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000196 goto exit;
197 }
198 else
Rich Evansf90016a2015-01-19 14:26:37 +0000199 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000200
Rich Evansf90016a2015-01-19 14:26:37 +0000201 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000202 fflush( stdout );
203
Paul Bakker36713e82013-09-17 13:25:29 +0200204 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 +0000205 {
206 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000207 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 +0000208 goto exit;
209 }
210 else
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000212
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000214
215 /*
216 * Calculate the RSA decryption with private key.
217 */
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000219 fflush( stdout );
220
Paul Bakker36713e82013-09-17 13:25:29 +0200221 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 +0000222 {
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000224 }
225 else
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000227
Rich Evansf90016a2015-01-19 14:26:37 +0000228 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000229 fflush( stdout );
230
Paul Bakker36713e82013-09-17 13:25:29 +0200231 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 +0000232 {
233 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000234 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 +0000235 }
236 else
Rich Evansf90016a2015-01-19 14:26:37 +0000237 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000238
239 /*
240 * Calculate the RSA decryption with public key.
241 */
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000243 fflush( stdout );
244
Paul Bakker36713e82013-09-17 13:25:29 +0200245 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 +0000246 {
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000248 }
249 else
Rich Evansf90016a2015-01-19 14:26:37 +0000250 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000251
Rich Evansf90016a2015-01-19 14:26:37 +0000252 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000253 fflush( stdout );
254
Paul Bakker36713e82013-09-17 13:25:29 +0200255 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 +0000256 {
257 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000258 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 +0000259 }
260 else
Rich Evansf90016a2015-01-19 14:26:37 +0000261 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000262
Rich Evansf90016a2015-01-19 14:26:37 +0000263 polarssl_printf( "\n" );
264 polarssl_printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
265 polarssl_printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
266 polarssl_printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
267 polarssl_printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000268
269exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200270 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200271 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000272
273#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000274 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000275 fflush( stdout ); getchar();
276#endif
277
278 return( ret );
279}
Paul Bakkered27a042013-04-18 22:46:23 +0200280#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200281 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */