blob: 14789401ff316e2409628b1cfb16f5b1b7e04923 [file] [log] [blame] [raw]
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001/*
2 * Test application that shows some PolarSSL and OpenSSL compatibility
3 *
4 * Copyright (C) 2011-2012 Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000031
32#include <string.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36#include <sys/stat.h>
37
38#include <openssl/rsa.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010039#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000040#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010041#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000042#include <openssl/pem.h>
43#include <openssl/bio.h>
Paul Bakkere6ee41f2012-05-19 08:43:48 +000044
Paul Bakker36713e82013-09-17 13:25:29 +020045#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000046#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000047#include "polarssl/entropy.h"
48#include "polarssl/ctr_drbg.h"
49
Paul Bakkered27a042013-04-18 22:46:23 +020050#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020051 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020052int main( int argc, char *argv[] )
53{
54 ((void) argc);
55 ((void) argv);
56
57 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020058 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020059 return( 0 );
60}
61#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000062int main( int argc, char *argv[] )
63{
64 int ret;
65 FILE *key_file;
66 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020067 pk_context p_pk;
68 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000069 RSA *o_rsa;
70 entropy_context entropy;
71 ctr_drbg_context ctr_drbg;
72 unsigned char input[1024];
73 unsigned char p_pub_encrypted[512];
74 unsigned char o_pub_encrypted[512];
75 unsigned char p_pub_decrypted[512];
76 unsigned char o_pub_decrypted[512];
77 unsigned char p_priv_encrypted[512];
78 unsigned char o_priv_encrypted[512];
79 unsigned char p_priv_decrypted[512];
80 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000082
83 entropy_init( &entropy );
84 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020085 (const unsigned char *) pers,
86 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000087 {
88 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
89 goto exit;
90 }
91 ERR_load_crypto_strings();
92
93 ret = 1;
94
95 if( argc != 3 )
96 {
97 printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
98
99#ifdef WIN32
100 printf( "\n" );
101#endif
102
103 goto exit;
104 }
105
106 printf( " . Reading private key from %s into PolarSSL ...", argv[1] );
107 fflush( stdout );
108
Paul Bakker36713e82013-09-17 13:25:29 +0200109 pk_init( &p_pk );
110 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000111 {
112 ret = 1;
113 printf( " failed\n ! Could not load key.\n\n" );
114 goto exit;
115 }
Paul Bakker36713e82013-09-17 13:25:29 +0200116
117 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
118 {
119 ret = 1;
120 printf( " failed\n ! Key is not an RSA key\n" );
121 goto exit;
122 }
123
124 p_rsa = pk_rsa( p_pk );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000125
126 printf( " passed\n");
127
128 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
129 fflush( stdout );
130
131 key_file = fopen( argv[1], "r" );
132 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
133 fclose(key_file);
134 if( o_rsa == NULL )
135 {
136 ret = 1;
137 printf( " failed\n ! Could not load key.\n\n" );
138 goto exit;
139 }
140
141 printf( " passed\n");
142 printf( "\n" );
143
144 if( strlen( argv[1] ) > 100 )
145 {
146 printf( " Input data larger than 100 characters.\n\n" );
147 goto exit;
148 }
149
150 memcpy( input, argv[2], strlen( argv[2] ) );
151
152 /*
153 * Calculate the RSA encryption with public key.
154 */
155 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." );
156 fflush( stdout );
157
Paul Bakker36713e82013-09-17 13:25:29 +0200158 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 +0000159 {
160 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
161 goto exit;
162 }
163 else
164 printf( " passed\n");
165
166 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
167 fflush( stdout );
168
Paul Bakker36713e82013-09-17 13:25:29 +0200169 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 +0000170 {
171 unsigned long code = ERR_get_error();
172 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
173 goto exit;
174 }
175 else
176 printf( " passed\n");
177
178 /*
179 * Calculate the RSA encryption with private key.
180 */
181 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." );
182 fflush( stdout );
183
Paul Bakker36713e82013-09-17 13:25:29 +0200184 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 +0000185 {
186 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
187 goto exit;
188 }
189 else
190 printf( " passed\n");
191
192 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
193 fflush( stdout );
194
Paul Bakker36713e82013-09-17 13:25:29 +0200195 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 +0000196 {
197 unsigned long code = ERR_get_error();
198 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
199 goto exit;
200 }
201 else
202 printf( " passed\n");
203
204 printf( "\n" );
205
206 /*
207 * Calculate the RSA decryption with private key.
208 */
209 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." );
210 fflush( stdout );
211
Paul Bakker36713e82013-09-17 13:25:29 +0200212 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 +0000213 {
214 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
215 }
216 else
217 printf( " passed\n");
218
219 printf( " . Generating the RSA decrypted value for PolarSSL (PUBLIC) with OpenSSL (PRIVATE) ..." );
220 fflush( stdout );
221
Paul Bakker36713e82013-09-17 13:25:29 +0200222 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 +0000223 {
224 unsigned long code = ERR_get_error();
225 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
226 }
227 else
228 printf( " passed\n");
229
230 /*
231 * Calculate the RSA decryption with public key.
232 */
233 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." );
234 fflush( stdout );
235
Paul Bakker36713e82013-09-17 13:25:29 +0200236 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 +0000237 {
238 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
239 }
240 else
241 printf( " passed\n");
242
243 printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." );
244 fflush( stdout );
245
Paul Bakker36713e82013-09-17 13:25:29 +0200246 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 +0000247 {
248 unsigned long code = ERR_get_error();
249 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
250 }
251 else
252 printf( " passed\n");
253
254 printf( "\n" );
255 printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted );
256 printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
257 printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted );
258 printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
259
260exit:
Paul Bakker1ffefac2013-09-28 15:23:03 +0200261 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000262
263#ifdef WIN32
264 printf( " + Press Enter to exit this program.\n" );
265 fflush( stdout ); getchar();
266#endif
267
268 return( ret );
269}
Paul Bakkered27a042013-04-18 22:46:23 +0200270#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200271 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */