pk_decrypt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Public key-based 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_PK_PARSE_C) && \
  62. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  63. defined(MBEDTLS_CTR_DRBG_C)
  64. #include "mbedtls/error.h"
  65. #include "mbedtls/pk.h"
  66. #include "mbedtls/entropy.h"
  67. #include "mbedtls/ctr_drbg.h"
  68. #include <stdio.h>
  69. #include <string.h>
  70. #endif
  71. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
  72. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  73. !defined(MBEDTLS_CTR_DRBG_C)
  74. int main( void )
  75. {
  76. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
  77. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  78. "MBEDTLS_CTR_DRBG_C not defined.\n");
  79. mbedtls_exit( 0 );
  80. }
  81. #else
  82. int main( int argc, char *argv[] )
  83. {
  84. FILE *f;
  85. int ret = 1;
  86. unsigned c;
  87. int exit_code = MBEDTLS_EXIT_FAILURE;
  88. size_t i, olen = 0;
  89. mbedtls_pk_context pk;
  90. mbedtls_entropy_context entropy;
  91. mbedtls_ctr_drbg_context ctr_drbg;
  92. unsigned char result[1024];
  93. unsigned char buf[512];
  94. const char *pers = "mbedtls_pk_decrypt";
  95. ((void) argv);
  96. mbedtls_pk_init( &pk );
  97. mbedtls_entropy_init( &entropy );
  98. mbedtls_ctr_drbg_init( &ctr_drbg );
  99. memset(result, 0, sizeof( result ) );
  100. if( argc != 2 )
  101. {
  102. mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
  103. #if defined(_WIN32)
  104. mbedtls_printf( "\n" );
  105. #endif
  106. goto exit;
  107. }
  108. mbedtls_printf( "\n . Seeding the random number generator..." );
  109. fflush( stdout );
  110. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  111. &entropy, (const unsigned char *) pers,
  112. strlen( pers ) ) ) != 0 )
  113. {
  114. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  115. -ret );
  116. goto exit;
  117. }
  118. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  119. fflush( stdout );
  120. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  121. {
  122. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -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. ret = 1;
  132. goto exit;
  133. }
  134. i = 0;
  135. while( fscanf( f, "%02X", &c ) > 0 &&
  136. i < (int) sizeof( buf ) )
  137. {
  138. buf[i++] = (unsigned char) c;
  139. }
  140. fclose( f );
  141. /*
  142. * Decrypt the encrypted RSA data and print the result.
  143. */
  144. mbedtls_printf( "\n . Decrypting the encrypted data" );
  145. fflush( stdout );
  146. if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
  147. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  148. {
  149. mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
  150. -ret );
  151. goto exit;
  152. }
  153. mbedtls_printf( "\n . OK\n\n" );
  154. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  155. exit_code = MBEDTLS_EXIT_SUCCESS;
  156. exit:
  157. mbedtls_pk_free( &pk );
  158. mbedtls_entropy_free( &entropy );
  159. mbedtls_ctr_drbg_free( &ctr_drbg );
  160. #if defined(MBEDTLS_ERROR_C)
  161. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  162. {
  163. mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
  164. mbedtls_printf( " ! Last error was: %s\n", buf );
  165. }
  166. #endif
  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_PK_PARSE_C && MBEDTLS_FS_IO &&
  174. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */