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