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