pk_decrypt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Public key-based 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_PK_PARSE_C) && \
  35. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  36. defined(MBEDTLS_CTR_DRBG_C)
  37. #include "mbedtls/error.h"
  38. #include "mbedtls/pk.h"
  39. #include "mbedtls/entropy.h"
  40. #include "mbedtls/ctr_drbg.h"
  41. #include <stdio.h>
  42. #include <string.h>
  43. #endif
  44. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_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_PK_PARSE_C and/or "
  50. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  51. "MBEDTLS_CTR_DRBG_C not defined.\n");
  52. mbedtls_exit( 0 );
  53. }
  54. #else
  55. int main( int argc, char *argv[] )
  56. {
  57. FILE *f;
  58. int ret = 1;
  59. unsigned c;
  60. int exit_code = MBEDTLS_EXIT_FAILURE;
  61. size_t i, olen = 0;
  62. mbedtls_pk_context pk;
  63. mbedtls_entropy_context entropy;
  64. mbedtls_ctr_drbg_context ctr_drbg;
  65. unsigned char result[1024];
  66. unsigned char buf[512];
  67. const char *pers = "mbedtls_pk_decrypt";
  68. ((void) argv);
  69. mbedtls_pk_init( &pk );
  70. mbedtls_entropy_init( &entropy );
  71. mbedtls_ctr_drbg_init( &ctr_drbg );
  72. memset(result, 0, sizeof( result ) );
  73. if( argc != 2 )
  74. {
  75. mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
  76. #if defined(_WIN32)
  77. mbedtls_printf( "\n" );
  78. #endif
  79. goto exit;
  80. }
  81. mbedtls_printf( "\n . Seeding the random number generator..." );
  82. fflush( stdout );
  83. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  84. &entropy, (const unsigned char *) pers,
  85. strlen( pers ) ) ) != 0 )
  86. {
  87. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  88. (unsigned int) -ret );
  89. goto exit;
  90. }
  91. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  92. fflush( stdout );
  93. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  94. {
  95. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
  96. goto exit;
  97. }
  98. /*
  99. * Extract the RSA encrypted value from the text file
  100. */
  101. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  102. {
  103. mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  104. ret = 1;
  105. goto exit;
  106. }
  107. i = 0;
  108. while( fscanf( f, "%02X", (unsigned int*) &c ) > 0 &&
  109. i < (int) sizeof( buf ) )
  110. {
  111. buf[i++] = (unsigned char) c;
  112. }
  113. fclose( f );
  114. /*
  115. * Decrypt the encrypted RSA data and print the result.
  116. */
  117. mbedtls_printf( "\n . Decrypting the encrypted data" );
  118. fflush( stdout );
  119. if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
  120. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  121. {
  122. mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
  123. (unsigned int) -ret );
  124. goto exit;
  125. }
  126. mbedtls_printf( "\n . OK\n\n" );
  127. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  128. exit_code = MBEDTLS_EXIT_SUCCESS;
  129. exit:
  130. mbedtls_pk_free( &pk );
  131. mbedtls_entropy_free( &entropy );
  132. mbedtls_ctr_drbg_free( &ctr_drbg );
  133. #if defined(MBEDTLS_ERROR_C)
  134. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  135. {
  136. mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
  137. mbedtls_printf( " ! Last error was: %s\n", buf );
  138. }
  139. #endif
  140. #if defined(_WIN32)
  141. mbedtls_printf( " + Press Enter to exit this program.\n" );
  142. fflush( stdout ); getchar();
  143. #endif
  144. mbedtls_exit( exit_code );
  145. }
  146. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  147. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */