blob: dd488d7acb51ed253b8d9f34ec393d6cefff6ec7 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <sys/stat.h>
35
36#include <openssl/rsa.h>
37#include <openssl/engine.h>
38#include <openssl/pem.h>
39#include <openssl/bio.h>
40
41#include "polarssl/config.h"
42
Paul Bakker36713e82013-09-17 13:25:29 +020043#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000044#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000045#include "polarssl/entropy.h"
46#include "polarssl/ctr_drbg.h"
47
Paul Bakkered27a042013-04-18 22:46:23 +020048#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020049 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020050int main( int argc, char *argv[] )
51{
52 ((void) argc);
53 ((void) argv);
54
55 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020056 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020057 return( 0 );
58}
59#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000060int main( int argc, char *argv[] )
61{
62 int ret;
63 FILE *key_file;
64 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020065 pk_context p_pk;
66 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000067 RSA *o_rsa;
68 entropy_context entropy;
69 ctr_drbg_context ctr_drbg;
70 unsigned char input[1024];
71 unsigned char p_pub_encrypted[512];
72 unsigned char o_pub_encrypted[512];
73 unsigned char p_pub_decrypted[512];
74 unsigned char o_pub_decrypted[512];
75 unsigned char p_priv_encrypted[512];
76 unsigned char o_priv_encrypted[512];
77 unsigned char p_priv_decrypted[512];
78 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000080
81 entropy_init( &entropy );
82 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 (const unsigned char *) pers,
84 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000085 {
86 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
87 goto exit;
88 }
89 ERR_load_crypto_strings();
90
91 ret = 1;
92
93 if( argc != 3 )
94 {
95 printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
96
97#ifdef WIN32
98 printf( "\n" );
99#endif
100
101 goto exit;
102 }
103
104 printf( " . Reading private key from %s into PolarSSL ...", argv[1] );
105 fflush( stdout );
106
Paul Bakker36713e82013-09-17 13:25:29 +0200107 pk_init( &p_pk );
108 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000109 {
110 ret = 1;
111 printf( " failed\n ! Could not load key.\n\n" );
112 goto exit;
113 }
114
Paul Bakker36713e82013-09-17 13:25:29 +0200115 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
116 {
117 ret = 1;
118 printf( " failed\n ! Key is not an RSA key\n" );
119 goto exit;
120 }
121
122 p_rsa = pk_rsa( p_pk );
123
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000124 printf( " passed\n");
125
126 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
127 fflush( stdout );
128
129 key_file = fopen( argv[1], "r" );
130 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
131 fclose(key_file);
132 if( o_rsa == NULL )
133 {
134 ret = 1;
135 printf( " failed\n ! Could not load key.\n\n" );
136 goto exit;
137 }
138
139 printf( " passed\n");
140 printf( "\n" );
141
142 if( strlen( argv[1] ) > 100 )
143 {
144 printf( " Input data larger than 100 characters.\n\n" );
145 goto exit;
146 }
147
148 memcpy( input, argv[2], strlen( argv[2] ) );
149
150 /*
151 * Calculate the RSA encryption with public key.
152 */
153 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." );
154 fflush( stdout );
155
Paul Bakker36713e82013-09-17 13:25:29 +0200156 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 +0000157 {
158 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
159 goto exit;
160 }
161 else
162 printf( " passed\n");
163
164 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
165 fflush( stdout );
166
Paul Bakker36713e82013-09-17 13:25:29 +0200167 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 +0000168 {
169 unsigned long code = ERR_get_error();
170 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
171 goto exit;
172 }
173 else
174 printf( " passed\n");
175
176 /*
177 * Calculate the RSA encryption with private key.
178 */
179 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." );
180 fflush( stdout );
181
Paul Bakker36713e82013-09-17 13:25:29 +0200182 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 +0000183 {
184 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
185 goto exit;
186 }
187 else
188 printf( " passed\n");
189
190 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
191 fflush( stdout );
192
Paul Bakker36713e82013-09-17 13:25:29 +0200193 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 +0000194 {
195 unsigned long code = ERR_get_error();
196 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
197 goto exit;
198 }
199 else
200 printf( " passed\n");
201
202 printf( "\n" );
203
204 /*
205 * Calculate the RSA decryption with private key.
206 */
207 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." );
208 fflush( stdout );
209
Paul Bakker36713e82013-09-17 13:25:29 +0200210 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 +0000211 {
212 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
213 }
214 else
215 printf( " passed\n");
216
217 printf( " . Generating the RSA decrypted value for PolarSSL (PUBLIC) with OpenSSL (PRIVATE) ..." );
218 fflush( stdout );
219
Paul Bakker36713e82013-09-17 13:25:29 +0200220 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 +0000221 {
222 unsigned long code = ERR_get_error();
223 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
224 }
225 else
226 printf( " passed\n");
227
228 /*
229 * Calculate the RSA decryption with public key.
230 */
231 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." );
232 fflush( stdout );
233
Paul Bakker36713e82013-09-17 13:25:29 +0200234 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 +0000235 {
236 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
237 }
238 else
239 printf( " passed\n");
240
241 printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." );
242 fflush( stdout );
243
Paul Bakker36713e82013-09-17 13:25:29 +0200244 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 +0000245 {
246 unsigned long code = ERR_get_error();
247 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
248 }
249 else
250 printf( " passed\n");
251
252 printf( "\n" );
253 printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted );
254 printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
255 printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted );
256 printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
257
258exit:
259
260#ifdef WIN32
261 printf( " + Press Enter to exit this program.\n" );
262 fflush( stdout ); getchar();
263#endif
264
265 return( ret );
266}
Paul Bakkered27a042013-04-18 22:46:23 +0200267#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200268 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */