rsa_decrypt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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
  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 return_val, exit_val, c;
  59. size_t i;
  60. mbedtls_rsa_context rsa;
  61. mbedtls_entropy_context entropy;
  62. mbedtls_ctr_drbg_context ctr_drbg;
  63. unsigned char result[1024];
  64. unsigned char buf[512];
  65. const char *pers = "rsa_decrypt";
  66. ((void) argv);
  67. memset(result, 0, sizeof( result ) );
  68. exit_val = MBEDTLS_EXIT_SUCCESS;
  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( MBEDTLS_EXIT_FAILURE );
  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. return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  83. &entropy, (const unsigned char *) pers,
  84. strlen( pers ) );
  85. if( return_val != 0 )
  86. {
  87. exit_val = MBEDTLS_EXIT_FAILURE;
  88. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  89. return_val );
  90. goto exit;
  91. }
  92. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  93. fflush( stdout );
  94. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  95. {
  96. exit_val = MBEDTLS_EXIT_FAILURE;
  97. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  98. " ! Please run rsa_genkey first\n\n" );
  99. goto exit;
  100. }
  101. if( ( return_val = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
  102. ( return_val = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
  103. ( return_val = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
  104. ( return_val = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
  105. ( return_val = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
  106. ( return_val = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
  107. ( return_val = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
  108. ( return_val = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
  109. {
  110. exit_val = MBEDTLS_EXIT_FAILURE;
  111. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  112. return_val );
  113. fclose( f );
  114. goto exit;
  115. }
  116. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  117. fclose( f );
  118. /*
  119. * Extract the RSA encrypted value from the text file
  120. */
  121. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  122. {
  123. exit_val = MBEDTLS_EXIT_FAILURE;
  124. mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  125. goto exit;
  126. }
  127. i = 0;
  128. while( fscanf( f, "%02X", &c ) > 0 &&
  129. i < (int) sizeof( buf ) )
  130. buf[i++] = (unsigned char) c;
  131. fclose( f );
  132. if( i != rsa.len )
  133. {
  134. exit_val = MBEDTLS_EXIT_FAILURE;
  135. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  136. goto exit;
  137. }
  138. /*
  139. * Decrypt the encrypted RSA data and print the result.
  140. */
  141. mbedtls_printf( "\n . Decrypting the encrypted data" );
  142. fflush( stdout );
  143. return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
  144. &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
  145. buf, result, 1024 );
  146. if( return_val != 0 )
  147. {
  148. exit_val = MBEDTLS_EXIT_FAILURE;
  149. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
  150. return_val );
  151. goto exit;
  152. }
  153. mbedtls_printf( "\n . OK\n\n" );
  154. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  155. exit:
  156. mbedtls_ctr_drbg_free( &ctr_drbg );
  157. mbedtls_entropy_free( &entropy );
  158. mbedtls_rsa_free( &rsa );
  159. #if defined(_WIN32)
  160. mbedtls_printf( " + Press Enter to exit this program.\n" );
  161. fflush( stdout ); getchar();
  162. #endif
  163. return( exit_val );
  164. }
  165. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */