blob: bf54752c9c24efbd053461d34f0178a2e2365f3e [file] [log] [blame]
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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000027
28#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <sys/stat.h>
33
34#include <openssl/rsa.h>
35#include <openssl/engine.h>
36#include <openssl/pem.h>
37#include <openssl/bio.h>
38
Paul Bakker36713e82013-09-17 13:25:29 +020039#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000040#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000041#include "polarssl/entropy.h"
42#include "polarssl/ctr_drbg.h"
43
Paul Bakkered27a042013-04-18 22:46:23 +020044#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020045 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020046int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
51 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020052 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020053 return( 0 );
54}
55#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000056int main( int argc, char *argv[] )
57{
58 int ret;
59 FILE *key_file;
60 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020061 pk_context p_pk;
62 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000063 RSA *o_rsa;
64 entropy_context entropy;
65 ctr_drbg_context ctr_drbg;
66 unsigned char input[1024];
67 unsigned char p_pub_encrypted[512];
68 unsigned char o_pub_encrypted[512];
69 unsigned char p_pub_decrypted[512];
70 unsigned char o_pub_decrypted[512];
71 unsigned char p_priv_encrypted[512];
72 unsigned char o_priv_encrypted[512];
73 unsigned char p_priv_decrypted[512];
74 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000076
77 entropy_init( &entropy );
78 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 (const unsigned char *) pers,
80 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000081 {
82 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
83 goto exit;
84 }
85 ERR_load_crypto_strings();
86
87 ret = 1;
88
89 if( argc != 3 )
90 {
91 printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
92
93#ifdef WIN32
94 printf( "\n" );
95#endif
96
97 goto exit;
98 }
99
100 printf( " . Reading private key from %s into PolarSSL ...", argv[1] );
101 fflush( stdout );
102
Paul Bakker36713e82013-09-17 13:25:29 +0200103 pk_init( &p_pk );
104 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000105 {
106 ret = 1;
107 printf( " failed\n ! Could not load key.\n\n" );
108 goto exit;
109 }
110
Paul Bakker36713e82013-09-17 13:25:29 +0200111 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
112 {
113 ret = 1;
114 printf( " failed\n ! Key is not an RSA key\n" );
115 goto exit;
116 }
117
118 p_rsa = pk_rsa( p_pk );
119
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000120 printf( " passed\n");
121
122 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
123 fflush( stdout );
124
125 key_file = fopen( argv[1], "r" );
126 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
127 fclose(key_file);
128 if( o_rsa == NULL )
129 {
130 ret = 1;
131 printf( " failed\n ! Could not load key.\n\n" );
132 goto exit;
133 }
134
135 printf( " passed\n");
136 printf( "\n" );
137
138 if( strlen( argv[1] ) > 100 )
139 {
140 printf( " Input data larger than 100 characters.\n\n" );
141 goto exit;
142 }
143
144 memcpy( input, argv[2], strlen( argv[2] ) );
145
146 /*
147 * Calculate the RSA encryption with public key.
148 */
149 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." );
150 fflush( stdout );
151
Paul Bakker36713e82013-09-17 13:25:29 +0200152 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 +0000153 {
154 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
155 goto exit;
156 }
157 else
158 printf( " passed\n");
159
160 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
161 fflush( stdout );
162
Paul Bakker36713e82013-09-17 13:25:29 +0200163 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 +0000164 {
165 unsigned long code = ERR_get_error();
166 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
167 goto exit;
168 }
169 else
170 printf( " passed\n");
171
172 /*
173 * Calculate the RSA encryption with private key.
174 */
175 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." );
176 fflush( stdout );
177
Paul Bakker36713e82013-09-17 13:25:29 +0200178 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 +0000179 {
180 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
181 goto exit;
182 }
183 else
184 printf( " passed\n");
185
186 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
187 fflush( stdout );
188
Paul Bakker36713e82013-09-17 13:25:29 +0200189 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 +0000190 {
191 unsigned long code = ERR_get_error();
192 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
193 goto exit;
194 }
195 else
196 printf( " passed\n");
197
198 printf( "\n" );
199
200 /*
201 * Calculate the RSA decryption with private key.
202 */
203 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." );
204 fflush( stdout );
205
Paul Bakker36713e82013-09-17 13:25:29 +0200206 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 +0000207 {
208 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
209 }
210 else
211 printf( " passed\n");
212
213 printf( " . Generating the RSA decrypted value for PolarSSL (PUBLIC) with OpenSSL (PRIVATE) ..." );
214 fflush( stdout );
215
Paul Bakker36713e82013-09-17 13:25:29 +0200216 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 +0000217 {
218 unsigned long code = ERR_get_error();
219 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
220 }
221 else
222 printf( " passed\n");
223
224 /*
225 * Calculate the RSA decryption with public key.
226 */
227 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." );
228 fflush( stdout );
229
Paul Bakker36713e82013-09-17 13:25:29 +0200230 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 +0000231 {
232 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
233 }
234 else
235 printf( " passed\n");
236
237 printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." );
238 fflush( stdout );
239
Paul Bakker36713e82013-09-17 13:25:29 +0200240 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 +0000241 {
242 unsigned long code = ERR_get_error();
243 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
244 }
245 else
246 printf( " passed\n");
247
248 printf( "\n" );
249 printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted );
250 printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
251 printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted );
252 printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
253
254exit:
255
256#ifdef WIN32
257 printf( " + Press Enter to exit this program.\n" );
258 fflush( stdout ); getchar();
259#endif
260
261 return( ret );
262}
Paul Bakkered27a042013-04-18 22:46:23 +0200263#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200264 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */