pk_decrypt.c 4.8 KB

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