blob: 187372af9e31eaff447541aba85351b3d17e622c [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
43#include "polarssl/x509.h"
44#include "polarssl/rsa.h"
45#include "polarssl/entropy.h"
46#include "polarssl/ctr_drbg.h"
47
48int main( int argc, char *argv[] )
49{
50 int ret;
51 FILE *key_file;
52 size_t olen;
53 rsa_context p_rsa;
54 RSA *o_rsa;
55 entropy_context entropy;
56 ctr_drbg_context ctr_drbg;
57 unsigned char input[1024];
58 unsigned char p_pub_encrypted[512];
59 unsigned char o_pub_encrypted[512];
60 unsigned char p_pub_decrypted[512];
61 unsigned char o_pub_decrypted[512];
62 unsigned char p_priv_encrypted[512];
63 unsigned char o_priv_encrypted[512];
64 unsigned char p_priv_decrypted[512];
65 unsigned char o_priv_decrypted[512];
66 char *pers = "o_p_test_example";
67
68 entropy_init( &entropy );
69 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
70 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
71 {
72 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
73 goto exit;
74 }
75 ERR_load_crypto_strings();
76
77 ret = 1;
78
79 if( argc != 3 )
80 {
81 printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" );
82
83#ifdef WIN32
84 printf( "\n" );
85#endif
86
87 goto exit;
88 }
89
90 printf( " . Reading private key from %s into PolarSSL ...", argv[1] );
91 fflush( stdout );
92
93 rsa_init( &p_rsa, RSA_PKCS_V15, 0 );
94 if( x509parse_keyfile( &p_rsa, argv[1], NULL ) != 0 )
95 {
96 ret = 1;
97 printf( " failed\n ! Could not load key.\n\n" );
98 goto exit;
99 }
100
101 printf( " passed\n");
102
103 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
104 fflush( stdout );
105
106 key_file = fopen( argv[1], "r" );
107 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
108 fclose(key_file);
109 if( o_rsa == NULL )
110 {
111 ret = 1;
112 printf( " failed\n ! Could not load key.\n\n" );
113 goto exit;
114 }
115
116 printf( " passed\n");
117 printf( "\n" );
118
119 if( strlen( argv[1] ) > 100 )
120 {
121 printf( " Input data larger than 100 characters.\n\n" );
122 goto exit;
123 }
124
125 memcpy( input, argv[2], strlen( argv[2] ) );
126
127 /*
128 * Calculate the RSA encryption with public key.
129 */
130 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." );
131 fflush( stdout );
132
133 if( ( ret = rsa_pkcs1_encrypt( &p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[1] ), input, p_pub_encrypted ) ) != 0 )
134 {
135 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
136 goto exit;
137 }
138 else
139 printf( " passed\n");
140
141 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
142 fflush( stdout );
143
144 if( ( ret = RSA_public_encrypt( strlen( argv[1] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
145 {
146 unsigned long code = ERR_get_error();
147 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
148 goto exit;
149 }
150 else
151 printf( " passed\n");
152
153 /*
154 * Calculate the RSA encryption with private key.
155 */
156 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." );
157 fflush( stdout );
158
159 if( ( ret = rsa_pkcs1_encrypt( &p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[1] ), input, p_priv_encrypted ) ) != 0 )
160 {
161 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
162 goto exit;
163 }
164 else
165 printf( " passed\n");
166
167 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
168 fflush( stdout );
169
170 if( ( ret = RSA_private_encrypt( strlen( argv[1] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
171 {
172 unsigned long code = ERR_get_error();
173 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
174 goto exit;
175 }
176 else
177 printf( " passed\n");
178
179 printf( "\n" );
180
181 /*
182 * Calculate the RSA decryption with private key.
183 */
184 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." );
185 fflush( stdout );
186
187 if( ( ret = rsa_pkcs1_decrypt( &p_rsa, RSA_PRIVATE, &olen, o_pub_encrypted, p_pub_decrypted, 1024 ) ) != 0 )
188 {
189 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
190 }
191 else
192 printf( " passed\n");
193
194 printf( " . Generating the RSA decrypted value for PolarSSL (PUBLIC) with OpenSSL (PRIVATE) ..." );
195 fflush( stdout );
196
197 if( ( ret = RSA_private_decrypt( p_rsa.len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
198 {
199 unsigned long code = ERR_get_error();
200 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
201 }
202 else
203 printf( " passed\n");
204
205 /*
206 * Calculate the RSA decryption with public key.
207 */
208 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." );
209 fflush( stdout );
210
211 if( ( ret = rsa_pkcs1_decrypt( &p_rsa, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 )
212 {
213 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
214 }
215 else
216 printf( " passed\n");
217
218 printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." );
219 fflush( stdout );
220
221 if( ( ret = RSA_public_decrypt( p_rsa.len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
222 {
223 unsigned long code = ERR_get_error();
224 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
225 }
226 else
227 printf( " passed\n");
228
229 printf( "\n" );
230 printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted );
231 printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
232 printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted );
233 printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
234
235exit:
236
237#ifdef WIN32
238 printf( " + Press Enter to exit this program.\n" );
239 fflush( stdout ); getchar();
240#endif
241
242 return( ret );
243}