Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 31 | |
| 32 | #include <string.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <unistd.h> |
| 36 | #include <sys/stat.h> |
| 37 | |
| 38 | #include <openssl/rsa.h> |
Paul Bakker | 5eb264c | 2014-01-23 15:43:07 +0100 | [diff] [blame] | 39 | #ifndef OPENSSL_NO_ENGINE |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 40 | #include <openssl/engine.h> |
Paul Bakker | 5eb264c | 2014-01-23 15:43:07 +0100 | [diff] [blame] | 41 | #endif |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 42 | #include <openssl/pem.h> |
| 43 | #include <openssl/bio.h> |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 45 | #include "polarssl/pk.h" |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 46 | #include "polarssl/x509.h" |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 47 | #include "polarssl/entropy.h" |
| 48 | #include "polarssl/ctr_drbg.h" |
| 49 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 50 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 51 | !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 52 | int main( int argc, char *argv[] ) |
| 53 | { |
| 54 | ((void) argc); |
| 55 | ((void) argv); |
| 56 | |
| 57 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 58 | "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 59 | return( 0 ); |
| 60 | } |
| 61 | #else |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 62 | int main( int argc, char *argv[] ) |
| 63 | { |
| 64 | int ret; |
| 65 | FILE *key_file; |
| 66 | size_t olen; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 67 | pk_context p_pk; |
| 68 | rsa_context *p_rsa; |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 69 | RSA *o_rsa; |
| 70 | entropy_context entropy; |
| 71 | ctr_drbg_context ctr_drbg; |
| 72 | unsigned char input[1024]; |
| 73 | unsigned char p_pub_encrypted[512]; |
| 74 | unsigned char o_pub_encrypted[512]; |
| 75 | unsigned char p_pub_decrypted[512]; |
| 76 | unsigned char o_pub_decrypted[512]; |
| 77 | unsigned char p_priv_encrypted[512]; |
| 78 | unsigned char o_priv_encrypted[512]; |
| 79 | unsigned char p_priv_decrypted[512]; |
| 80 | unsigned char o_priv_decrypted[512]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 81 | const char *pers = "o_p_test_example"; |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 82 | |
| 83 | entropy_init( &entropy ); |
| 84 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 85 | (const unsigned char *) pers, |
| 86 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 87 | { |
| 88 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 89 | goto exit; |
| 90 | } |
| 91 | ERR_load_crypto_strings(); |
| 92 | |
| 93 | ret = 1; |
| 94 | |
| 95 | if( argc != 3 ) |
| 96 | { |
| 97 | printf( "usage: o_p_test <keyfile with private_key> <string of max 100 characters>\n" ); |
| 98 | |
| 99 | #ifdef WIN32 |
| 100 | printf( "\n" ); |
| 101 | #endif |
| 102 | |
| 103 | goto exit; |
| 104 | } |
| 105 | |
| 106 | printf( " . Reading private key from %s into PolarSSL ...", argv[1] ); |
| 107 | fflush( stdout ); |
| 108 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 109 | pk_init( &p_pk ); |
| 110 | if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 111 | { |
| 112 | ret = 1; |
| 113 | printf( " failed\n ! Could not load key.\n\n" ); |
| 114 | goto exit; |
| 115 | } |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 116 | |
| 117 | if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) ) |
| 118 | { |
| 119 | ret = 1; |
| 120 | printf( " failed\n ! Key is not an RSA key\n" ); |
| 121 | goto exit; |
| 122 | } |
| 123 | |
| 124 | p_rsa = pk_rsa( p_pk ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 125 | |
| 126 | printf( " passed\n"); |
| 127 | |
| 128 | printf( " . Reading private key from %s into OpenSSL ...", argv[1] ); |
| 129 | fflush( stdout ); |
| 130 | |
| 131 | key_file = fopen( argv[1], "r" ); |
| 132 | o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0); |
| 133 | fclose(key_file); |
| 134 | if( o_rsa == NULL ) |
| 135 | { |
| 136 | ret = 1; |
| 137 | printf( " failed\n ! Could not load key.\n\n" ); |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | printf( " passed\n"); |
| 142 | printf( "\n" ); |
| 143 | |
| 144 | if( strlen( argv[1] ) > 100 ) |
| 145 | { |
| 146 | printf( " Input data larger than 100 characters.\n\n" ); |
| 147 | goto exit; |
| 148 | } |
| 149 | |
| 150 | memcpy( input, argv[2], strlen( argv[2] ) ); |
| 151 | |
| 152 | /* |
| 153 | * Calculate the RSA encryption with public key. |
| 154 | */ |
| 155 | printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PUBLIC) ..." ); |
| 156 | fflush( stdout ); |
| 157 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 158 | if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PUBLIC, strlen( argv[2] ), input, p_pub_encrypted ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 159 | { |
| 160 | printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); |
| 161 | goto exit; |
| 162 | } |
| 163 | else |
| 164 | printf( " passed\n"); |
| 165 | |
| 166 | printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." ); |
| 167 | fflush( stdout ); |
| 168 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 169 | if( ( ret = RSA_public_encrypt( strlen( argv[2] ), input, o_pub_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 170 | { |
| 171 | unsigned long code = ERR_get_error(); |
| 172 | printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 173 | goto exit; |
| 174 | } |
| 175 | else |
| 176 | printf( " passed\n"); |
| 177 | |
| 178 | /* |
| 179 | * Calculate the RSA encryption with private key. |
| 180 | */ |
| 181 | printf( " . Generating the RSA encrypted value with PolarSSL (RSA_PRIVATE) ..." ); |
| 182 | fflush( stdout ); |
| 183 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 184 | if( ( ret = rsa_pkcs1_encrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, strlen( argv[2] ), input, p_priv_encrypted ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 185 | { |
| 186 | printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); |
| 187 | goto exit; |
| 188 | } |
| 189 | else |
| 190 | printf( " passed\n"); |
| 191 | |
| 192 | printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." ); |
| 193 | fflush( stdout ); |
| 194 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 195 | if( ( ret = RSA_private_encrypt( strlen( argv[2] ), input, o_priv_encrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 196 | { |
| 197 | unsigned long code = ERR_get_error(); |
| 198 | printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 199 | goto exit; |
| 200 | } |
| 201 | else |
| 202 | printf( " passed\n"); |
| 203 | |
| 204 | printf( "\n" ); |
| 205 | |
| 206 | /* |
| 207 | * Calculate the RSA decryption with private key. |
| 208 | */ |
| 209 | printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with PolarSSL (PRIVATE) ..." ); |
| 210 | fflush( stdout ); |
| 211 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 212 | if( ( ret = rsa_pkcs1_decrypt( p_rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE, &olen, o_pub_encrypted, p_pub_decrypted, 1024 ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 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 (PUBLIC) with OpenSSL (PRIVATE) ..." ); |
| 220 | fflush( stdout ); |
| 221 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 222 | if( ( ret = RSA_private_decrypt( p_rsa->len, p_pub_encrypted, o_pub_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 223 | { |
| 224 | unsigned long code = ERR_get_error(); |
| 225 | printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 226 | } |
| 227 | else |
| 228 | printf( " passed\n"); |
| 229 | |
| 230 | /* |
| 231 | * Calculate the RSA decryption with public key. |
| 232 | */ |
| 233 | printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with PolarSSL (PUBLIC) ..." ); |
| 234 | fflush( stdout ); |
| 235 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 236 | if( ( ret = rsa_pkcs1_decrypt( p_rsa, NULL, NULL, RSA_PUBLIC, &olen, o_priv_encrypted, p_priv_decrypted, 1024 ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 237 | { |
| 238 | printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); |
| 239 | } |
| 240 | else |
| 241 | printf( " passed\n"); |
| 242 | |
| 243 | printf( " . Generating the RSA decrypted value for PolarSSL (PRIVATE) with OpenSSL (PUBLIC) ..." ); |
| 244 | fflush( stdout ); |
| 245 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 246 | if( ( ret = RSA_public_decrypt( p_rsa->len, p_priv_encrypted, o_priv_decrypted, o_rsa, RSA_PKCS1_PADDING ) ) == -1 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 247 | { |
| 248 | unsigned long code = ERR_get_error(); |
| 249 | printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 250 | } |
| 251 | else |
| 252 | printf( " passed\n"); |
| 253 | |
| 254 | printf( "\n" ); |
| 255 | printf( "String value (OpenSSL Public Encrypt, PolarSSL Private Decrypt): '%s'\n", p_pub_decrypted ); |
| 256 | printf( "String value (PolarSSL Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted ); |
| 257 | printf( "String value (OpenSSL Private Encrypt, PolarSSL Public Decrypt): '%s'\n", p_priv_decrypted ); |
| 258 | printf( "String value (PolarSSL Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted ); |
| 259 | |
| 260 | exit: |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 261 | entropy_free( &entropy ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 262 | |
| 263 | #ifdef WIN32 |
| 264 | printf( " + Press Enter to exit this program.\n" ); |
| 265 | fflush( stdout ); getchar(); |
| 266 | #endif |
| 267 | |
| 268 | return( ret ); |
| 269 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 270 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 271 | POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */ |