rsa_decrypt.c 6.2 KB

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