Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * RSA simple data encryption program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [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 | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [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 | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 32 | #include "polarssl/error.h" |
| 33 | #include "polarssl/pk.h" |
| 34 | #include "polarssl/entropy.h" |
| 35 | #include "polarssl/ctr_drbg.h" |
| 36 | |
| 37 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \ |
| 38 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \ |
| 39 | !defined(POLARSSL_CTR_DRBG_C) |
| 40 | int main( int argc, char *argv[] ) |
| 41 | { |
| 42 | ((void) argc); |
| 43 | ((void) argv); |
| 44 | |
| 45 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or " |
| 46 | "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or " |
| 47 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 48 | return( 0 ); |
| 49 | } |
| 50 | #else |
| 51 | int main( int argc, char *argv[] ) |
| 52 | { |
| 53 | FILE *f; |
| 54 | int ret; |
| 55 | size_t i, olen = 0; |
| 56 | pk_context pk; |
| 57 | entropy_context entropy; |
| 58 | ctr_drbg_context ctr_drbg; |
| 59 | unsigned char input[1024]; |
| 60 | unsigned char buf[512]; |
| 61 | const char *pers = "pk_encrypt"; |
| 62 | |
| 63 | ret = 1; |
| 64 | |
| 65 | if( argc != 3 ) |
| 66 | { |
| 67 | printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" ); |
| 68 | |
| 69 | #if defined(_WIN32) |
| 70 | printf( "\n" ); |
| 71 | #endif |
| 72 | |
| 73 | goto exit; |
| 74 | } |
| 75 | |
| 76 | printf( "\n . Seeding the random number generator..." ); |
| 77 | fflush( stdout ); |
| 78 | |
| 79 | entropy_init( &entropy ); |
| 80 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 81 | (const unsigned char *) pers, |
| 82 | strlen( pers ) ) ) != 0 ) |
| 83 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 84 | printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | printf( "\n . Reading public key from '%s'", argv[1] ); |
| 89 | fflush( stdout ); |
| 90 | |
| 91 | pk_init( &pk ); |
| 92 | |
| 93 | if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) |
| 94 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 95 | printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 96 | goto exit; |
| 97 | } |
| 98 | |
| 99 | if( strlen( argv[2] ) > 100 ) |
| 100 | { |
| 101 | printf( " Input data larger than 100 characters.\n\n" ); |
| 102 | goto exit; |
| 103 | } |
| 104 | |
| 105 | memcpy( input, argv[2], strlen( argv[2] ) ); |
| 106 | |
| 107 | /* |
| 108 | * Calculate the RSA encryption of the hash. |
| 109 | */ |
| 110 | printf( "\n . Generating the encrypted value" ); |
| 111 | fflush( stdout ); |
| 112 | |
| 113 | if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ), |
| 114 | buf, &olen, sizeof(buf), |
| 115 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
| 116 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 117 | printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 118 | goto exit; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Write the signature into result-enc.txt |
| 123 | */ |
| 124 | if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) |
| 125 | { |
| 126 | ret = 1; |
| 127 | printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); |
| 128 | goto exit; |
| 129 | } |
| 130 | |
| 131 | for( i = 0; i < olen; i++ ) |
| 132 | fprintf( f, "%02X%s", buf[i], |
| 133 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
| 134 | |
| 135 | fclose( f ); |
| 136 | |
| 137 | printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); |
| 138 | |
| 139 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 140 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 141 | entropy_free( &entropy ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 143 | #if defined(POLARSSL_ERROR_C) |
| 144 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 145 | printf( " ! Last error was: %s\n", buf ); |
| 146 | #endif |
| 147 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 148 | #if defined(_WIN32) |
| 149 | printf( " + Press Enter to exit this program.\n" ); |
| 150 | fflush( stdout ); getchar(); |
| 151 | #endif |
| 152 | |
| 153 | return( ret ); |
| 154 | } |
| 155 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C && |
| 156 | POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |