rsa_decrypt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * RSA simple decryption program
  3. *
  4. * Copyright The Mbed TLS Contributors
  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.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_exit exit
  31. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  32. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  33. #endif /* MBEDTLS_PLATFORM_C */
  34. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
  35. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  36. defined(MBEDTLS_CTR_DRBG_C)
  37. #include "mbedtls/rsa.h"
  38. #include "mbedtls/entropy.h"
  39. #include "mbedtls/ctr_drbg.h"
  40. #include <string.h>
  41. #endif
  42. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  43. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  44. !defined(MBEDTLS_CTR_DRBG_C)
  45. int main( void )
  46. {
  47. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  48. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  49. "MBEDTLS_CTR_DRBG_C not defined.\n");
  50. mbedtls_exit( 0 );
  51. }
  52. #else
  53. int main( int argc, char *argv[] )
  54. {
  55. FILE *f;
  56. int ret = 1;
  57. int exit_code = MBEDTLS_EXIT_FAILURE;
  58. unsigned c;
  59. size_t i;
  60. mbedtls_rsa_context rsa;
  61. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  62. mbedtls_entropy_context entropy;
  63. mbedtls_ctr_drbg_context ctr_drbg;
  64. unsigned char result[1024];
  65. unsigned char buf[512];
  66. const char *pers = "rsa_decrypt";
  67. ((void) argv);
  68. memset(result, 0, sizeof( result ) );
  69. if( argc != 1 )
  70. {
  71. mbedtls_printf( "usage: rsa_decrypt\n" );
  72. #if defined(_WIN32)
  73. mbedtls_printf( "\n" );
  74. #endif
  75. mbedtls_exit( exit_code );
  76. }
  77. mbedtls_printf( "\n . Seeding the random number generator..." );
  78. fflush( stdout );
  79. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  80. mbedtls_ctr_drbg_init( &ctr_drbg );
  81. mbedtls_entropy_init( &entropy );
  82. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  83. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  84. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  85. ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  86. &entropy, (const unsigned char *) pers,
  87. strlen( pers ) );
  88. if( ret != 0 )
  89. {
  90. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  91. ret );
  92. goto exit;
  93. }
  94. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  95. fflush( stdout );
  96. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  97. {
  98. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  99. " ! Please run rsa_genkey first\n\n" );
  100. goto exit;
  101. }
  102. if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
  103. ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
  104. ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
  105. ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
  106. ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
  107. ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
  108. ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
  109. ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
  110. {
  111. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  112. ret );
  113. fclose( f );
  114. goto exit;
  115. }
  116. fclose( f );
  117. if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
  118. {
  119. mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
  120. ret );
  121. goto exit;
  122. }
  123. if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
  124. {
  125. mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
  126. ret );
  127. goto exit;
  128. }
  129. /*
  130. * Extract the RSA encrypted value from the text file
  131. */
  132. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  133. {
  134. mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  135. goto exit;
  136. }
  137. i = 0;
  138. while( fscanf( f, "%02X", (unsigned int*) &c ) > 0 &&
  139. i < (int) sizeof( buf ) )
  140. buf[i++] = (unsigned char) c;
  141. fclose( f );
  142. if( i != rsa.len )
  143. {
  144. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  145. goto exit;
  146. }
  147. /*
  148. * Decrypt the encrypted RSA data and print the result.
  149. */
  150. mbedtls_printf( "\n . Decrypting the encrypted data" );
  151. fflush( stdout );
  152. ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
  153. &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
  154. buf, result, 1024 );
  155. if( ret != 0 )
  156. {
  157. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
  158. ret );
  159. goto exit;
  160. }
  161. mbedtls_printf( "\n . OK\n\n" );
  162. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  163. exit_code = MBEDTLS_EXIT_SUCCESS;
  164. exit:
  165. mbedtls_ctr_drbg_free( &ctr_drbg );
  166. mbedtls_entropy_free( &entropy );
  167. mbedtls_rsa_free( &rsa );
  168. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  169. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  170. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  171. #if defined(_WIN32)
  172. mbedtls_printf( " + Press Enter to exit this program.\n" );
  173. fflush( stdout ); getchar();
  174. #endif
  175. mbedtls_exit( exit_code );
  176. }
  177. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */