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