Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSA simple decryption program |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #define mbedtls_printf printf |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 33 | #define mbedtls_exit exit |
| 34 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 35 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \ |
| 39 | defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \ |
| 40 | defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/rsa.h" |
| 42 | #include "mbedtls/entropy.h" |
| 43 | #include "mbedtls/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 <string.h> |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 46 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ |
| 50 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 51 | !defined(MBEDTLS_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 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " |
| 55 | "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or " |
| 56 | "MBEDTLS_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 | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 63 | int return_val, exit_val, c; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 64 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | mbedtls_rsa_context rsa; |
| 66 | mbedtls_entropy_context entropy; |
| 67 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 68 | unsigned char result[1024]; |
| 69 | unsigned char buf[512]; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 70 | const char *pers = "rsa_decrypt"; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 71 | ((void) argv); |
| 72 | |
Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 73 | memset(result, 0, sizeof( result ) ); |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 74 | exit_val = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 75 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 76 | if( argc != 1 ) |
| 77 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | mbedtls_printf( "usage: rsa_decrypt\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 79 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 80 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | mbedtls_printf( "\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 84 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 88 | fflush( stdout ); |
| 89 | |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 90 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 91 | mbedtls_entropy_init( &entropy ); |
| 92 | |
| 93 | return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 94 | &entropy, (const unsigned char *) pers, |
| 95 | strlen( pers ) ); |
| 96 | if( return_val != 0 ) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 97 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 98 | exit_val = MBEDTLS_EXIT_FAILURE; |
| 99 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", |
| 100 | return_val ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
| 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | mbedtls_printf( "\n . Reading private key from rsa_priv.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_priv.txt", "rb" ) ) == NULL ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 108 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 109 | exit_val = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 111 | " ! Please run rsa_genkey first\n\n" ); |
| 112 | goto exit; |
| 113 | } |
| 114 | |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 115 | if( ( return_val = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 116 | ( return_val = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 117 | ( return_val = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 118 | ( return_val = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 119 | ( return_val = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 120 | ( return_val = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 121 | ( return_val = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 122 | ( return_val = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 123 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 124 | exit_val = MBEDTLS_EXIT_FAILURE; |
| 125 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", |
| 126 | return_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 127 | goto exit; |
| 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 130 | rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 131 | |
| 132 | fclose( f ); |
| 133 | |
| 134 | /* |
| 135 | * Extract the RSA encrypted value from the text file |
| 136 | */ |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 137 | if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) |
| 138 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 139 | exit_val = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | i = 0; |
| 145 | |
| 146 | while( fscanf( f, "%02X", &c ) > 0 && |
| 147 | i < (int) sizeof( buf ) ) |
| 148 | buf[i++] = (unsigned char) c; |
| 149 | |
| 150 | fclose( f ); |
| 151 | |
| 152 | if( i != rsa.len ) |
| 153 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 154 | exit_val = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | mbedtls_printf( "\n ! Invalid RSA signature format\n\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 156 | goto exit; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Decrypt the encrypted RSA data and print the result. |
| 161 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_printf( "\n . Decrypting the encrypted data" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 163 | fflush( stdout ); |
| 164 | |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 165 | return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, |
| 166 | &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i, |
| 167 | buf, result, 1024 ); |
| 168 | if( return_val != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 169 | { |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 170 | exit_val = MBEDTLS_EXIT_FAILURE; |
| 171 | mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", |
| 172 | return_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 173 | goto exit; |
| 174 | } |
| 175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | mbedtls_printf( "\n . OK\n\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | mbedtls_printf( "The decrypted result is: '%s'\n\n", result ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 179 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 180 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 182 | mbedtls_entropy_free( &entropy ); |
Janos Follath | f713b0a | 2016-03-17 15:21:39 +0000 | [diff] [blame] | 183 | mbedtls_rsa_free( &rsa ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 184 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 185 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 187 | fflush( stdout ); getchar(); |
| 188 | #endif |
| 189 | |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 190 | return( exit_val ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 191 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */ |
Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame^] | 193 | |