blob: 3a6c693bb0959bf406fdb77835e626a98f50b795 [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000027
28#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <sys/stat.h>
33
34#include <openssl/rsa.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010035#ifndef OPENSSL_NO_ENGINE
Paul Bakkere6ee41f2012-05-19 08:43:48 +000036#include <openssl/engine.h>
Paul Bakker5eb264c2014-01-23 15:43:07 +010037#endif
Paul Bakkere6ee41f2012-05-19 08:43:48 +000038#include <openssl/pem.h>
39#include <openssl/bio.h>
40
Paul Bakker36713e82013-09-17 13:25:29 +020041#include "polarssl/pk.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000042#include "polarssl/x509.h"
Paul Bakkere6ee41f2012-05-19 08:43:48 +000043#include "polarssl/entropy.h"
44#include "polarssl/ctr_drbg.h"
45
Paul Bakkered27a042013-04-18 22:46:23 +020046#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020047 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkered27a042013-04-18 22:46:23 +020048int main( int argc, char *argv[] )
49{
50 ((void) argc);
51 ((void) argv);
52
53 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020054 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakkered27a042013-04-18 22:46:23 +020055 return( 0 );
56}
57#else
Paul Bakkere6ee41f2012-05-19 08:43:48 +000058int main( int argc, char *argv[] )
59{
60 int ret;
61 FILE *key_file;
62 size_t olen;
Paul Bakker36713e82013-09-17 13:25:29 +020063 pk_context p_pk;
64 rsa_context *p_rsa;
Paul Bakkere6ee41f2012-05-19 08:43:48 +000065 RSA *o_rsa;
66 entropy_context entropy;
67 ctr_drbg_context ctr_drbg;
68 unsigned char input[1024];
69 unsigned char p_pub_encrypted[512];
70 unsigned char o_pub_encrypted[512];
71 unsigned char p_pub_decrypted[512];
72 unsigned char o_pub_decrypted[512];
73 unsigned char p_priv_encrypted[512];
74 unsigned char o_priv_encrypted[512];
75 unsigned char p_priv_decrypted[512];
76 unsigned char o_priv_decrypted[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020077 const char *pers = "o_p_test_example";
Paul Bakkere6ee41f2012-05-19 08:43:48 +000078
79 entropy_init( &entropy );
80 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 (const unsigned char *) pers,
82 strlen( pers ) ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +000083 {
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
Paul Bakker36713e82013-09-17 13:25:29 +0200105 pk_init( &p_pk );
106 if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000107 {
108 ret = 1;
109 printf( " failed\n ! Could not load key.\n\n" );
110 goto exit;
111 }
112
Paul Bakker36713e82013-09-17 13:25:29 +0200113 if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) )
114 {
115 ret = 1;
116 printf( " failed\n ! Key is not an RSA key\n" );
117 goto exit;
118 }
119
120 p_rsa = pk_rsa( p_pk );
121
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000122 printf( " passed\n");
123
124 printf( " . Reading private key from %s into OpenSSL ...", argv[1] );
125 fflush( stdout );
126
127 key_file = fopen( argv[1], "r" );
128 o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0);
129 fclose(key_file);
130 if( o_rsa == NULL )
131 {
132 ret = 1;
133 printf( " failed\n ! Could not load key.\n\n" );
134 goto exit;
135 }
136
137 printf( " passed\n");
138 printf( "\n" );
139
140 if( strlen( argv[1] ) > 100 )
141 {
142 printf( " Input data larger than 100 characters.\n\n" );
143 goto exit;
144 }
145
146 memcpy( input, argv[2], strlen( argv[2] ) );
147
148 /*
149 * Calculate the RSA encryption with public key.
150 */
151 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." );
152 fflush( stdout );
153
Paul Bakker36713e82013-09-17 13:25:29 +0200154 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[2] ), input, p_pub_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000155 {
156 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
157 goto exit;
158 }
159 else
160 printf( " passed\n");
161
162 printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." );
163 fflush( stdout );
164
Paul Bakker36713e82013-09-17 13:25:29 +0200165 if( ( ret = RSA_public_encrypt( strlen( argv[2] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000166 {
167 unsigned long code = ERR_get_error();
168 printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
169 goto exit;
170 }
171 else
172 printf( " passed\n");
173
174 /*
175 * Calculate the RSA encryption with private key.
176 */
177 printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." );
178 fflush( stdout );
179
Paul Bakker36713e82013-09-17 13:25:29 +0200180 if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[2] ), input, p_priv_encrypted ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000181 {
182 printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
183 goto exit;
184 }
185 else
186 printf( " passed\n");
187
188 printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." );
189 fflush( stdout );
190
Paul Bakker36713e82013-09-17 13:25:29 +0200191 if( ( ret = RSA_private_encrypt( strlen( argv[2] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000192 {
193 unsigned long code = ERR_get_error();
194 printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
195 goto exit;
196 }
197 else
198 printf( " passed\n");
199
200 printf( "\n" );
201
202 /*
203 * Calculate the RSA decryption with private key.
204 */
205 printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." );
206 fflush( stdout );
207
Paul Bakker36713e82013-09-17 13:25:29 +0200208 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 +0000209 {
210 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
211 }
212 else
213 printf( " passed\n");
214
215 printf( " . Generating the RSA decrypted value for PolarSSL (PUBLIC) with OpenSSL (PRIVATE) ..." );
216 fflush( stdout );
217
Paul Bakker36713e82013-09-17 13:25:29 +0200218 if( ( ret = RSA_private_decrypt( p_rsa->len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000219 {
220 unsigned long code = ERR_get_error();
221 printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
222 }
223 else
224 printf( " passed\n");
225
226 /*
227 * Calculate the RSA decryption with public key.
228 */
229 printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." );
230 fflush( stdout );
231
Paul Bakker36713e82013-09-17 13:25:29 +0200232 if( ( ret = rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000233 {
234 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
235 }
236 else
237 printf( " passed\n");
238
239 printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." );
240 fflush( stdout );
241
Paul Bakker36713e82013-09-17 13:25:29 +0200242 if( ( ret = RSA_public_decrypt( p_rsa->len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 )
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000243 {
244 unsigned long code = ERR_get_error();
245 printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) );
246 }
247 else
248 printf( " passed\n");
249
250 printf( "\n" );
251 printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted );
252 printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted );
253 printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted );
254 printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted );
255
256exit:
Paul Bakker1ffefac2013-09-28 15:23:03 +0200257 entropy_free( &entropy );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000258
259#ifdef WIN32
260 printf( " + Press Enter to exit this program.\n" );
261 fflush( stdout ); getchar();
262#endif
263
264 return( ret );
265}
Paul Bakkered27a042013-04-18 22:46:23 +0200266#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C &&
Paul Bakker36713e82013-09-17 13:25:29 +0200267 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */