pk_decrypt.c 4.5 KB

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