blob: 7f0f00b7e7473d1337d73c2023cd9d97a4ca4412 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <sys/stat.h>
34
35#include <openssl/rsa.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010036#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000037#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010038#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000039#include <openssl/pem.h>
40#include <openssl/bio.h>
41
Paul Bakker36713e82013-09-17 13:25:29 +020042#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000043#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000044#include "polarssl/entropy.h"
45#include "polarssl/ctr_drbg.h"
46
Paul Bakkered27a042013-04-18 22:46:23 +020047#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020048 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020049int main( int argc, char *argv[] )
50{
51 ((void) argc);
52 ((void) argv);
53
54 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020055 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020056 return( 0 );
57}
58#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000059int main( int argc, char *argv[] )
60{
61 int ret;
62 FILE *key_file;
63 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020064 pk_context p_pk;
65 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000066 RSA *o_rsa;
67 entropy_context entropy;
68 ctr_drbg_context ctr_drbg;
69 unsigned char input[1024];
70 unsigned char p_pub_encrypted[512];
71 unsigned char o_pub_encrypted[512];
72 unsigned char p_pub_decrypted[512];
73 unsigned char o_pub_decrypted[512];
74 unsigned char p_priv_encrypted[512];
75 unsigned char o_priv_encrypted[512];
76 unsigned char p_priv_decrypted[512];
77 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020078 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000079
80 entropy_init( &entropy );
81 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020082 (const unsigned char *) pers,
83 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000084 {
85 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
86 goto exit;
87 }
88 ERR_load_crypto_strings();
89
90 ret = 1;
91
92 if( argc != 3 )
93 {
94 printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
95
96#ifdef WIN32
97 printf( "\n" );
98#endif
99
100 goto exit;
101 }
102
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000103 printf( " . Reading private key from %s into mbed TLS ...", argv[1] );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000104 fflush( stdout );
105
Paul Bakker36713e82013-09-17 13:25:29 +0200106 pk_init( &p_pk );
107 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000108 {
109 ret = 1;
110 printf( " failed\n ! Could not load key.\n\n" );
111 goto exit;
112 }
113
Paul Bakker36713e82013-09-17 13:25:29 +0200114 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
115 {
116 ret = 1;
117 printf( " failed\n ! Key is not an RSA key\n" );
118 goto exit;
119 }
120
121 p_rsa = pk_rsa( p_pk );
122
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000123 printf( " passed\n");
124
125 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
126 fflush( stdout );
127
128 key_file = fopen( argv[1], "r" );
129 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
130 fclose(key_file);
131 if( o_rsa == NULL )
132 {
133 ret = 1;
134 printf( " failed\n ! Could not load key.\n\n" );
135 goto exit;
136 }
137
138 printf( " passed\n");
139 printf( "\n" );
140
141 if( strlen( argv[1] ) > 100 )
142 {
143 printf( " Input data larger than 100 characters.\n\n" );
144 goto exit;
145 }
146
147 memcpy( input, argv[2], strlen( argv[2] ) );
148
149 /*
150 * Calculate the RSA encryption with public key.
151 */
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000152 printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000153 fflush( stdout );
154
Paul Bakker36713e82013-09-17 13:25:29 +0200155 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 +0000156 {
157 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
158 goto exit;
159 }
160 else
161 printf( " passed\n");
162
163 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
164 fflush( stdout );
165
Paul Bakker36713e82013-09-17 13:25:29 +0200166 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 +0000167 {
168 unsigned long code = ERR_get_error();
169 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
170 goto exit;
171 }
172 else
173 printf( " passed\n");
174
175 /*
176 * Calculate the RSA encryption with private key.
177 */
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000178 printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000179 fflush( stdout );
180
Paul Bakker36713e82013-09-17 13:25:29 +0200181 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 +0000182 {
183 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
184 goto exit;
185 }
186 else
187 printf( " passed\n");
188
189 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
190 fflush( stdout );
191
Paul Bakker36713e82013-09-17 13:25:29 +0200192 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 +0000193 {
194 unsigned long code = ERR_get_error();
195 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
196 goto exit;
197 }
198 else
199 printf( " passed\n");
200
201 printf( "\n" );
202
203 /*
204 * Calculate the RSA decryption with private key.
205 */
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000206 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000207 fflush( stdout );
208
Paul Bakker36713e82013-09-17 13:25:29 +0200209 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 +0000210 {
211 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
212 }
213 else
214 printf( " passed\n");
215
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000216 printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000217 fflush( stdout );
218
Paul Bakker36713e82013-09-17 13:25:29 +0200219 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 +0000220 {
221 unsigned long code = ERR_get_error();
222 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
223 }
224 else
225 printf( " passed\n");
226
227 /*
228 * Calculate the RSA decryption with public key.
229 */
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000230 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000231 fflush( stdout );
232
Paul Bakker36713e82013-09-17 13:25:29 +0200233 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 +0000234 {
235 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
236 }
237 else
238 printf( " passed\n");
239
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000240 printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000241 fflush( stdout );
242
Paul Bakker36713e82013-09-17 13:25:29 +0200243 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 +0000244 {
245 unsigned long code = ERR_get_error();
246 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
247 }
248 else
249 printf( " passed\n");
250
251 printf( "\n" );
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000252 printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted );
253 printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
254 printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted );
255 printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000256
257exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200258 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200259 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000260
261#ifdef WIN32
262 printf( " + Press Enter to exit this program.\n" );
263 fflush( stdout ); getchar();
264#endif
265
266 return( ret );
267}
Paul Bakkered27a042013-04-18 22:46:23 +0200268#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200269 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */