Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [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 | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +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 | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 35 | #define polarssl_exit exit |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 38 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \ |
| 39 | defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 40 | defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 41 | #include "polarssl/rsa.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 42 | #include "polarssl/entropy.h" |
| 43 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 44 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <string.h> |
| 47 | #endif |
| 48 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 49 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 50 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \ |
| 51 | !defined(POLARSSL_CTR_DRBG_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 52 | int main( void ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 53 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 54 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 55 | "POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or " |
| 56 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 57 | return( 0 ); |
| 58 | } |
| 59 | #else |
| 60 | int main( int argc, char *argv[] ) |
| 61 | { |
| 62 | FILE *f; |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 63 | int return_val, exit_val; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 64 | size_t i; |
| 65 | rsa_context rsa; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 66 | entropy_context entropy; |
| 67 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 68 | unsigned char input[1024]; |
| 69 | unsigned char buf[512]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 70 | const char *pers = "rsa_encrypt"; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 71 | |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 72 | exit_val = 0; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 73 | |
| 74 | if( argc != 2 ) |
| 75 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 76 | polarssl_printf( "usage: rsa_encrypt <string of max 100 characters>\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 77 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 78 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 79 | polarssl_printf( "\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 82 | polarssl_exit( 1 ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 85 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 86 | fflush( stdout ); |
| 87 | |
| 88 | entropy_init( &entropy ); |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 89 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 90 | |
| 91 | return_val = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 92 | (const unsigned char *) pers, |
| 93 | strlen( pers ) ); |
| 94 | |
| 95 | if( return_val != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 96 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 97 | exit_val = 1; |
| 98 | polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", |
| 99 | return_val ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 100 | goto exit; |
| 101 | } |
| 102 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 103 | polarssl_printf( "\n . Reading public key from rsa_pub.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 104 | fflush( stdout ); |
| 105 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 106 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 107 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 108 | exit_val = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 109 | polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 110 | " ! Please run rsa_genkey first\n\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 111 | goto exit; |
| 112 | } |
| 113 | |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 114 | if( ( return_val = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 115 | ( return_val = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 116 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 117 | exit_val = 1; |
| 118 | polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", |
| 119 | return_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 120 | goto exit; |
| 121 | } |
| 122 | |
| 123 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 124 | |
| 125 | fclose( f ); |
| 126 | |
| 127 | if( strlen( argv[1] ) > 100 ) |
| 128 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 129 | exit_val = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 130 | polarssl_printf( " Input data larger than 100 characters.\n\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 131 | goto exit; |
| 132 | } |
| 133 | |
| 134 | memcpy( input, argv[1], strlen( argv[1] ) ); |
| 135 | |
| 136 | /* |
| 137 | * Calculate the RSA encryption of the hash. |
| 138 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 139 | polarssl_printf( "\n . Generating the RSA encrypted value" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 140 | fflush( stdout ); |
| 141 | |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 142 | if( ( return_val = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg, |
| 143 | RSA_PUBLIC, strlen( argv[1] ), |
| 144 | input, buf ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 145 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 146 | exit_val = 1; |
| 147 | polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", |
| 148 | return_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 149 | goto exit; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Write the signature into result-enc.txt |
| 154 | */ |
| 155 | if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) |
| 156 | { |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 157 | exit_val = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 158 | polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 159 | goto exit; |
| 160 | } |
| 161 | |
| 162 | for( i = 0; i < rsa.len; i++ ) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 163 | polarssl_fprintf( f, "%02X%s", buf[i], |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 164 | ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); |
| 165 | |
| 166 | fclose( f ); |
| 167 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 168 | polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 169 | |
| 170 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 171 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 172 | entropy_free( &entropy ); |
Janos Follath | fa4a88a | 2016-03-17 15:21:39 +0000 | [diff] [blame] | 173 | rsa_free( &rsa ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 174 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 175 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 176 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 177 | fflush( stdout ); getchar(); |
| 178 | #endif |
| 179 | |
Simon Butcher | de62b6e | 2016-04-12 17:36:34 +0100 | [diff] [blame^] | 180 | return( exit_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 181 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 182 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C && |
| 183 | POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |