rsa_decrypt.c 6.1 KB

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