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