rsa_decrypt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * RSA simple decryption program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  59. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  60. #endif /* MBEDTLS_PLATFORM_C */
  61. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
  62. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  63. defined(MBEDTLS_CTR_DRBG_C)
  64. #include "mbedtls/rsa.h"
  65. #include "mbedtls/entropy.h"
  66. #include "mbedtls/ctr_drbg.h"
  67. #include <string.h>
  68. #endif
  69. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  70. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  71. !defined(MBEDTLS_CTR_DRBG_C)
  72. int main( void )
  73. {
  74. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  75. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  76. "MBEDTLS_CTR_DRBG_C not defined.\n");
  77. mbedtls_exit( 0 );
  78. }
  79. #else
  80. int main( int argc, char *argv[] )
  81. {
  82. FILE *f;
  83. int ret = 1;
  84. int exit_code = MBEDTLS_EXIT_FAILURE;
  85. unsigned c;
  86. size_t i;
  87. mbedtls_rsa_context rsa;
  88. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  89. mbedtls_entropy_context entropy;
  90. mbedtls_ctr_drbg_context ctr_drbg;
  91. unsigned char result[1024];
  92. unsigned char buf[512];
  93. const char *pers = "rsa_decrypt";
  94. ((void) argv);
  95. memset(result, 0, sizeof( result ) );
  96. if( argc != 1 )
  97. {
  98. mbedtls_printf( "usage: rsa_decrypt\n" );
  99. #if defined(_WIN32)
  100. mbedtls_printf( "\n" );
  101. #endif
  102. mbedtls_exit( exit_code );
  103. }
  104. mbedtls_printf( "\n . Seeding the random number generator..." );
  105. fflush( stdout );
  106. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  107. mbedtls_ctr_drbg_init( &ctr_drbg );
  108. mbedtls_entropy_init( &entropy );
  109. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  110. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  111. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  112. ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  113. &entropy, (const unsigned char *) pers,
  114. strlen( pers ) );
  115. if( ret != 0 )
  116. {
  117. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  118. ret );
  119. goto exit;
  120. }
  121. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  122. fflush( stdout );
  123. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  124. {
  125. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  126. " ! Please run rsa_genkey first\n\n" );
  127. goto exit;
  128. }
  129. if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
  130. ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
  131. ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
  132. ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
  133. ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
  134. ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
  135. ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
  136. ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
  137. {
  138. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  139. ret );
  140. fclose( f );
  141. goto exit;
  142. }
  143. fclose( f );
  144. if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
  145. {
  146. mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
  147. ret );
  148. goto exit;
  149. }
  150. if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
  151. {
  152. mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
  153. ret );
  154. goto exit;
  155. }
  156. /*
  157. * Extract the RSA encrypted value from the text file
  158. */
  159. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  160. {
  161. mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  162. goto exit;
  163. }
  164. i = 0;
  165. while( fscanf( f, "%02X", &c ) > 0 &&
  166. i < (int) sizeof( buf ) )
  167. buf[i++] = (unsigned char) c;
  168. fclose( f );
  169. if( i != rsa.len )
  170. {
  171. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  172. goto exit;
  173. }
  174. /*
  175. * Decrypt the encrypted RSA data and print the result.
  176. */
  177. mbedtls_printf( "\n . Decrypting the encrypted data" );
  178. fflush( stdout );
  179. ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
  180. &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
  181. buf, result, 1024 );
  182. if( ret != 0 )
  183. {
  184. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
  185. ret );
  186. goto exit;
  187. }
  188. mbedtls_printf( "\n . OK\n\n" );
  189. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  190. exit_code = MBEDTLS_EXIT_SUCCESS;
  191. exit:
  192. mbedtls_ctr_drbg_free( &ctr_drbg );
  193. mbedtls_entropy_free( &entropy );
  194. mbedtls_rsa_free( &rsa );
  195. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  196. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  197. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  198. #if defined(_WIN32)
  199. mbedtls_printf( " + Press Enter to exit this program.\n" );
  200. fflush( stdout ); getchar();
  201. #endif
  202. mbedtls_exit( exit_code );
  203. }
  204. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */