Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 2 | * Test application that shows some mbed TLS and OpenSSL compatibility |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2011-2012 ARM Limited, All Rights Reserved |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <unistd.h> |
| 33 | #include <sys/stat.h> |
| 34 | |
| 35 | #include <openssl/rsa.h> |
Paul Bakker | 5eb264c | 2014-01-23 15:43:07 +0100 | [diff] [blame] | 36 | #ifndef OPENSSL_NO_ENGINE |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 37 | #include <openssl/engine.h> |
Paul Bakker | 5eb264c | 2014-01-23 15:43:07 +0100 | [diff] [blame] | 38 | #endif |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 39 | #include <openssl/pem.h> |
| 40 | #include <openssl/bio.h> |
| 41 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 42 | #include "polarssl/pk.h" |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 43 | #include "polarssl/x509.h" |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 44 | #include "polarssl/entropy.h" |
| 45 | #include "polarssl/ctr_drbg.h" |
| 46 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 47 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 48 | !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 49 | int main( int argc, char *argv[] ) |
| 50 | { |
| 51 | ((void) argc); |
| 52 | ((void) argv); |
| 53 | |
| 54 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 55 | "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 56 | return( 0 ); |
| 57 | } |
| 58 | #else |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 59 | int main( int argc, char *argv[] ) |
| 60 | { |
| 61 | int ret; |
| 62 | FILE *key_file; |
| 63 | size_t olen; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 64 | pk_context p_pk; |
| 65 | rsa_context *p_rsa; |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 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 Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 78 | const char *pers = "o_p_test_example"; |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 79 | |
| 80 | entropy_init( &entropy ); |
| 81 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 82 | (const unsigned char *) pers, |
| 83 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 84 | { |
| 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 | |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 103 | printf( " . Reading private key from %s into mbed TLS ...", argv[1] ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 104 | fflush( stdout ); |
| 105 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 106 | pk_init( &p_pk ); |
| 107 | if( pk_parse_keyfile( &p_pk, argv[1], NULL ) != 0 ) |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 108 | { |
| 109 | ret = 1; |
| 110 | printf( " failed\n ! Could not load key.\n\n" ); |
| 111 | goto exit; |
| 112 | } |
| 113 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 114 | if( !pk_can_do( &p_pk, POLARSSL_PK_RSA ) ) |
| 115 | { |
| 116 | ret = 1; |
| 117 | printf( " failed\n ! Key is not an RSA key\n" ); |
| 118 | goto exit; |
| 119 | } |
| 120 | |
| 121 | p_rsa = pk_rsa( p_pk ); |
| 122 | |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 123 | printf( " passed\n"); |
| 124 | |
| 125 | printf( " . Reading private key from %s into OpenSSL ...", argv[1] ); |
| 126 | fflush( stdout ); |
| 127 | |
| 128 | key_file = fopen( argv[1], "r" ); |
| 129 | o_rsa = PEM_read_RSAPrivateKey(key_file, 0, 0, 0); |
| 130 | fclose(key_file); |
| 131 | if( o_rsa == NULL ) |
| 132 | { |
| 133 | ret = 1; |
| 134 | printf( " failed\n ! Could not load key.\n\n" ); |
| 135 | goto exit; |
| 136 | } |
| 137 | |
| 138 | printf( " passed\n"); |
| 139 | printf( "\n" ); |
| 140 | |
| 141 | if( strlen( argv[1] ) > 100 ) |
| 142 | { |
| 143 | printf( " Input data larger than 100 characters.\n\n" ); |
| 144 | goto exit; |
| 145 | } |
| 146 | |
| 147 | memcpy( input, argv[2], strlen( argv[2] ) ); |
| 148 | |
| 149 | /* |
| 150 | * Calculate the RSA encryption with public key. |
| 151 | */ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 152 | printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PUBLIC) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 153 | fflush( stdout ); |
| 154 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 155 | 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] | 156 | { |
| 157 | printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); |
| 158 | goto exit; |
| 159 | } |
| 160 | else |
| 161 | printf( " passed\n"); |
| 162 | |
| 163 | printf( " . Generating the RSA encrypted value with OpenSSL (PUBLIC) ..." ); |
| 164 | fflush( stdout ); |
| 165 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | unsigned long code = ERR_get_error(); |
| 169 | printf( " failed\n ! RSA_public_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 170 | goto exit; |
| 171 | } |
| 172 | else |
| 173 | printf( " passed\n"); |
| 174 | |
| 175 | /* |
| 176 | * Calculate the RSA encryption with private key. |
| 177 | */ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 178 | printf( " . Generating the RSA encrypted value with mbed TLS (RSA_PRIVATE) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 179 | fflush( stdout ); |
| 180 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 181 | 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] | 182 | { |
| 183 | printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret ); |
| 184 | goto exit; |
| 185 | } |
| 186 | else |
| 187 | printf( " passed\n"); |
| 188 | |
| 189 | printf( " . Generating the RSA encrypted value with OpenSSL (PRIVATE) ..." ); |
| 190 | fflush( stdout ); |
| 191 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 192 | 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] | 193 | { |
| 194 | unsigned long code = ERR_get_error(); |
| 195 | printf( " failed\n ! RSA_private_encrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 196 | goto exit; |
| 197 | } |
| 198 | else |
| 199 | printf( " passed\n"); |
| 200 | |
| 201 | printf( "\n" ); |
| 202 | |
| 203 | /* |
| 204 | * Calculate the RSA decryption with private key. |
| 205 | */ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 206 | printf( " . Generating the RSA decrypted value for OpenSSL (PUBLIC) with mbed TLS (PRIVATE) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 207 | fflush( stdout ); |
| 208 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); |
| 212 | } |
| 213 | else |
| 214 | printf( " passed\n"); |
| 215 | |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 216 | printf( " . Generating the RSA decrypted value for mbed TLS (PUBLIC) with OpenSSL (PRIVATE) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 217 | fflush( stdout ); |
| 218 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 219 | 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] | 220 | { |
| 221 | unsigned long code = ERR_get_error(); |
| 222 | printf( " failed\n ! RSA_private_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 223 | } |
| 224 | else |
| 225 | printf( " passed\n"); |
| 226 | |
| 227 | /* |
| 228 | * Calculate the RSA decryption with public key. |
| 229 | */ |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 230 | printf( " . Generating the RSA decrypted value for OpenSSL (PRIVATE) with mbed TLS (PUBLIC) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 231 | fflush( stdout ); |
| 232 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
| 235 | printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); |
| 236 | } |
| 237 | else |
| 238 | printf( " passed\n"); |
| 239 | |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 240 | printf( " . Generating the RSA decrypted value for mbed TLS (PRIVATE) with OpenSSL (PUBLIC) ..." ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 241 | fflush( stdout ); |
| 242 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 243 | 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] | 244 | { |
| 245 | unsigned long code = ERR_get_error(); |
| 246 | printf( " failed\n ! RSA_public_decrypt returned %d %s\n\n", ret, ERR_error_string( code, NULL ) ); |
| 247 | } |
| 248 | else |
| 249 | printf( " passed\n"); |
| 250 | |
| 251 | printf( "\n" ); |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 252 | printf( "String value (OpenSSL Public Encrypt, mbed TLS Private Decrypt): '%s'\n", p_pub_decrypted ); |
| 253 | printf( "String value (mbed TLS Public Encrypt, OpenSSL Private Decrypt): '%s'\n", o_pub_decrypted ); |
| 254 | printf( "String value (OpenSSL Private Encrypt, mbed TLS Public Decrypt): '%s'\n", p_priv_decrypted ); |
| 255 | printf( "String value (mbed TLS Private Encrypt, OpenSSL Public Decrypt): '%s'\n", o_priv_decrypted ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 256 | |
| 257 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 258 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 259 | entropy_free( &entropy ); |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 260 | |
| 261 | #ifdef WIN32 |
| 262 | printf( " + Press Enter to exit this program.\n" ); |
| 263 | fflush( stdout ); getchar(); |
| 264 | #endif |
| 265 | |
| 266 | return( ret ); |
| 267 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 268 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 269 | POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */ |