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