blob: b904a9f8d00eabd0223ca566909a7708e70aec9d [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
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakkere6ee41f2012-05-19 08:43:48 +000035#include <string.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <sys/stat.h>
40
41#include <openssl/rsa.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010042#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000043#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010044#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000045#include <openssl/pem.h>
46#include <openssl/bio.h>
47
Paul Bakker36713e82013-09-17 13:25:29 +020048#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000049#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000050#include "polarssl/entropy.h"
51#include "polarssl/ctr_drbg.h"
52
Paul Bakkered27a042013-04-18 22:46:23 +020053#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020054 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020055int main( int argc, char *argv[] )
56{
57 ((void) argc);
58 ((void) argv);
59
Rich Evansf90016a2015-01-19 14:26:37 +000060 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020061 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020062 return( 0 );
63}
64#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000065int main( int argc, char *argv[] )
66{
67 int ret;
68 FILE *key_file;
69 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020070 pk_context p_pk;
71 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000072 RSA *o_rsa;
73 entropy_context entropy;
74 ctr_drbg_context ctr_drbg;
75 unsigned char input[1024];
76 unsigned char p_pub_encrypted[512];
77 unsigned char o_pub_encrypted[512];
78 unsigned char p_pub_decrypted[512];
79 unsigned char o_pub_decrypted[512];
80 unsigned char p_priv_encrypted[512];
81 unsigned char o_priv_encrypted[512];
82 unsigned char p_priv_decrypted[512];
83 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020084 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000085
86 entropy_init( &entropy );
87 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020088 (const unsigned char *) pers,
89 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000090 {
Rich Evansf90016a2015-01-19 14:26:37 +000091 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +000092 goto exit;
93 }
94 ERR_load_crypto_strings();
95
96 ret = 1;
97
98 if( argc != 3 )
99 {
Rich Evansf90016a2015-01-19 14:26:37 +0000100 polarssl_printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000101
102#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000104#endif
105
106 goto exit;
107 }
108
Rich Evansf90016a2015-01-19 14:26:37 +0000109 polarssl_printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000110 fflush( stdout );
111
Paul Bakker36713e82013-09-17 13:25:29 +0200112 pk_init( &p_pk );
113 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000114 {
115 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000117 goto exit;
118 }
119
Paul Bakker36713e82013-09-17 13:25:29 +0200120 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
121 {
122 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000123 polarssl_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker36713e82013-09-17 13:25:29 +0200124 goto exit;
125 }
126
127 p_rsa = pk_rsa( p_pk );
128
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000130
Rich Evansf90016a2015-01-19 14:26:37 +0000131 polarssl_printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000132 fflush( stdout );
133
134 key_file = fopen( argv[1], "r" );
135 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
136 fclose(key_file);
137 if( o_rsa == NULL )
138 {
139 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000140 polarssl_printf( " failed\n ! Could not load key.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000141 goto exit;
142 }
143
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " passed\n");
145 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000146
147 if( strlen( argv[1] ) > 100 )
148 {
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_printf( " Input data larger than 100 characters.\n\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000150 goto exit;
151 }
152
153 memcpy( input, argv[2], strlen( argv[2] ) );
154
155 /*
156 * Calculate the RSA encryption with public key.
157 */
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000159 fflush( stdout );
160
Paul Bakker36713e82013-09-17 13:25:29 +0200161 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 +0000162 {
Rich Evansf90016a2015-01-19 14:26:37 +0000163 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000164 goto exit;
165 }
166 else
Rich Evansf90016a2015-01-19 14:26:37 +0000167 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000168
Rich Evansf90016a2015-01-19 14:26:37 +0000169 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000170 fflush( stdout );
171
Paul Bakker36713e82013-09-17 13:25:29 +0200172 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 +0000173 {
174 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000175 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 +0000176 goto exit;
177 }
178 else
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000180
181 /*
182 * Calculate the RSA encryption with private key.
183 */
Rich Evansf90016a2015-01-19 14:26:37 +0000184 polarssl_printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000185 fflush( stdout );
186
Paul Bakker36713e82013-09-17 13:25:29 +0200187 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 +0000188 {
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000190 goto exit;
191 }
192 else
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000194
Rich Evansf90016a2015-01-19 14:26:37 +0000195 polarssl_printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000196 fflush( stdout );
197
Paul Bakker36713e82013-09-17 13:25:29 +0200198 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 +0000199 {
200 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000201 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 +0000202 goto exit;
203 }
204 else
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000206
Rich Evansf90016a2015-01-19 14:26:37 +0000207 polarssl_printf( "\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000208
209 /*
210 * Calculate the RSA decryption with private key.
211 */
Rich Evansf90016a2015-01-19 14:26:37 +0000212 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000213 fflush( stdout );
214
Paul Bakker36713e82013-09-17 13:25:29 +0200215 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 +0000216 {
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000218 }
219 else
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000221
Rich Evansf90016a2015-01-19 14:26:37 +0000222 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000223 fflush( stdout );
224
Paul Bakker36713e82013-09-17 13:25:29 +0200225 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 +0000226 {
227 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000228 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 +0000229 }
230 else
Rich Evansf90016a2015-01-19 14:26:37 +0000231 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000232
233 /*
234 * Calculate the RSA decryption with public key.
235 */
Rich Evansf90016a2015-01-19 14:26:37 +0000236 polarssl_printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000237 fflush( stdout );
238
Paul Bakker36713e82013-09-17 13:25:29 +0200239 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 +0000240 {
Rich Evansf90016a2015-01-19 14:26:37 +0000241 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000242 }
243 else
Rich Evansf90016a2015-01-19 14:26:37 +0000244 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000245
Rich Evansf90016a2015-01-19 14:26:37 +0000246 polarssl_printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000247 fflush( stdout );
248
Paul Bakker36713e82013-09-17 13:25:29 +0200249 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 +0000250 {
251 unsigned long code = ERR_get_error();
Rich Evansf90016a2015-01-19 14:26:37 +0000252 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 +0000253 }
254 else
Rich Evansf90016a2015-01-19 14:26:37 +0000255 polarssl_printf( " passed\n");
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000256
Rich Evansf90016a2015-01-19 14:26:37 +0000257 polarssl_printf( "\n" );
258 polarssl_printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
259 polarssl_printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
260 polarssl_printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
261 polarssl_printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000262
263exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200264 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200265 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000266
267#ifdef WIN32
Rich Evansf90016a2015-01-19 14:26:37 +0000268 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000269 fflush( stdout ); getchar();
270#endif
271
272 return( ret );
273}
Paul Bakkered27a042013-04-18 22:46:23 +0200274#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200275 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */