| 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 | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #endif | 
|  | 34 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \ | 
|  | 36 | defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \ | 
|  | 37 | defined(MBEDTLS_CTR_DRBG_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/rsa.h" | 
|  | 39 | #include "mbedtls/entropy.h" | 
|  | 40 | #include "mbedtls/ctr_drbg.h" | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 41 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 42 | #include <stdio.h> | 
|  | 43 | #include <string.h> | 
|  | 44 | #endif | 
|  | 45 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \ | 
|  | 47 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \ | 
|  | 48 | !defined(MBEDTLS_CTR_DRBG_C) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 49 | int main( void ) | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 50 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " | 
|  | 52 | "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or " | 
|  | 53 | "MBEDTLS_CTR_DRBG_C not defined.\n"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 54 | return( 0 ); | 
|  | 55 | } | 
|  | 56 | #else | 
|  | 57 | int main( int argc, char *argv[] ) | 
|  | 58 | { | 
|  | 59 | FILE *f; | 
|  | 60 | int ret, c; | 
|  | 61 | size_t i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_rsa_context rsa; | 
|  | 63 | mbedtls_entropy_context entropy; | 
|  | 64 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 65 | unsigned char result[1024]; | 
|  | 66 | unsigned char buf[512]; | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 67 | const char *pers = "rsa_decrypt"; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 68 | ((void) argv); | 
|  | 69 |  | 
| Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 70 | memset(result, 0, sizeof( result ) ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 71 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 72 | ret = 1; | 
| Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 73 |  | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 74 | if( argc != 1 ) | 
|  | 75 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | mbedtls_printf( "usage: rsa_decrypt\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) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | mbedtls_printf( "\n" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 80 | #endif | 
|  | 81 |  | 
|  | 82 | goto exit; | 
|  | 83 | } | 
|  | 84 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | mbedtls_printf( "\n  . Seeding the random number generator..." ); | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 86 | fflush( stdout ); | 
|  | 87 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_entropy_init( &entropy ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 89 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 90 | (const unsigned char *) pers, | 
|  | 91 | strlen( pers ) ) ) != 0 ) | 
|  | 92 | { | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 93 | mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret ); | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 94 | goto exit; | 
|  | 95 | } | 
|  | 96 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | mbedtls_printf( "\n  . Reading private key from rsa_priv.txt" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 98 | fflush( stdout ); | 
|  | 99 |  | 
| Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 100 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 101 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_printf( " failed\n  ! Could not open rsa_priv.txt\n" \ | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 103 | "  ! Please run rsa_genkey first\n\n" ); | 
|  | 104 | goto exit; | 
|  | 105 | } | 
|  | 106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 108 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 || | 
|  | 110 | ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 || | 
|  | 111 | ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 || | 
|  | 112 | ( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 || | 
|  | 113 | ( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 || | 
|  | 114 | ( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 || | 
|  | 115 | ( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || | 
|  | 116 | ( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 117 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 119 | goto exit; | 
|  | 120 | } | 
|  | 121 |  | 
| Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 122 | rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 123 |  | 
|  | 124 | fclose( f ); | 
|  | 125 |  | 
|  | 126 | /* | 
|  | 127 | * Extract the RSA encrypted value from the text file | 
|  | 128 | */ | 
|  | 129 | ret = 1; | 
|  | 130 |  | 
|  | 131 | if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) | 
|  | 132 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 134 | goto exit; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | i = 0; | 
|  | 138 |  | 
|  | 139 | while( fscanf( f, "%02X", &c ) > 0 && | 
|  | 140 | i < (int) sizeof( buf ) ) | 
|  | 141 | buf[i++] = (unsigned char) c; | 
|  | 142 |  | 
|  | 143 | fclose( f ); | 
|  | 144 |  | 
|  | 145 | if( i != rsa.len ) | 
|  | 146 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_printf( "\n  ! Invalid RSA signature format\n\n" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 148 | goto exit; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | /* | 
|  | 152 | * Decrypt the encrypted RSA data and print the result. | 
|  | 153 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | mbedtls_printf( "\n  . Decrypting the encrypted data" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 155 | fflush( stdout ); | 
|  | 156 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | if( ( ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, | 
|  | 158 | MBEDTLS_RSA_PRIVATE, &i, buf, result, | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 159 | 1024 ) ) != 0 ) | 
|  | 160 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", ret ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 162 | goto exit; | 
|  | 163 | } | 
|  | 164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_printf( "\n  . OK\n\n" ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 166 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | mbedtls_printf( "The decrypted result is: '%s'\n\n", result ); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 168 |  | 
|  | 169 | ret = 0; | 
|  | 170 |  | 
|  | 171 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | mbedtls_ctr_drbg_free( &ctr_drbg ); | 
|  | 173 | mbedtls_entropy_free( &entropy ); | 
| 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) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | mbedtls_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 |  | 
|  | 180 | return( ret ); | 
|  | 181 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */ |