decrypt_tests.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * RSA simple decryption program
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_PLATFORM_C)
  29. #include "mbedtls/platform.h"
  30. #else
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #define mbedtls_printf printf
  34. #define mbedtls_exit exit
  35. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  36. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  37. #endif
  38. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
  39. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  40. defined(MBEDTLS_CTR_DRBG_C)
  41. #include "mbedtls/rsa.h"
  42. #include "mbedtls/entropy.h"
  43. #include "mbedtls/ctr_drbg.h"
  44. #include <string.h>
  45. #endif
  46. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_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_RSA_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 return_val, exit_val, c;
  61. size_t i;
  62. mbedtls_rsa_context rsa;
  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 = "rsa_decrypt";
  68. ((void) argv);
  69. memset(result, 0, sizeof( result ) );
  70. exit_val = MBEDTLS_EXIT_SUCCESS;
  71. if( argc != 1 )
  72. {
  73. mbedtls_printf( "Uso: rsa_decrypt\n" );
  74. #if defined(_WIN32)
  75. mbedtls_printf( "\n" );
  76. #endif
  77. mbedtls_exit( MBEDTLS_EXIT_FAILURE );
  78. }
  79. mbedtls_printf( "\n . Seeding the random number generator..." );
  80. fflush( stdout );
  81. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  82. mbedtls_ctr_drbg_init( &ctr_drbg );
  83. mbedtls_entropy_init( &entropy );
  84. return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  85. &entropy, (const unsigned char *) pers,
  86. strlen( pers ) );
  87. if( return_val != 0 )
  88. {
  89. exit_val = MBEDTLS_EXIT_FAILURE;
  90. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  91. return_val );
  92. goto exit;
  93. }
  94. mbedtls_printf( "\n . Leyendo llave publica desde rsa_pub.txt" );
  95. fflush( stdout );
  96. /*NOTA: En este programa estamos intentado descifrar con la clave publica
  97. previamente el mensaje se habia cifrado con la clave privada. */
  98. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  99. {
  100. exit_val = MBEDTLS_EXIT_FAILURE;
  101. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
  102. " ! Please run rsa_genkey first\n\n" );
  103. goto exit;
  104. }
  105. if( ( return_val = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
  106. ( return_val = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 )
  107. {
  108. exit_val = MBEDTLS_EXIT_FAILURE;
  109. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  110. return_val );
  111. fclose( f );
  112. goto exit;
  113. }
  114. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  115. fclose( f );
  116. /*
  117. * Extract the RSA encrypted value from the text file
  118. */
  119. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  120. {
  121. exit_val = MBEDTLS_EXIT_FAILURE;
  122. mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  123. goto exit;
  124. }
  125. i = 0;
  126. while( fscanf( f, "%02X", &c ) > 0 &&
  127. i < (int) sizeof( buf ) )
  128. buf[i++] = (unsigned char) c;
  129. fclose( f );
  130. if( i != rsa.len )
  131. {
  132. exit_val = MBEDTLS_EXIT_FAILURE;
  133. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  134. goto exit;
  135. }
  136. /*
  137. * Decrypt the encrypted RSA data and print the result.
  138. */
  139. mbedtls_printf( "\n . Decrypting the encrypted data (con la clave publica)" );
  140. fflush( stdout );
  141. return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
  142. &ctr_drbg, MBEDTLS_RSA_PUBLIC, &i,
  143. buf, result, 1024 );
  144. if( return_val != 0 )
  145. {
  146. exit_val = MBEDTLS_EXIT_FAILURE;
  147. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned (con clave publica) %d\n\n",
  148. return_val );
  149. goto exit;
  150. }
  151. mbedtls_printf( "\n . OK\n\n" );
  152. mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
  153. exit:
  154. mbedtls_ctr_drbg_free( &ctr_drbg );
  155. mbedtls_entropy_free( &entropy );
  156. mbedtls_rsa_free( &rsa );
  157. #if defined(_WIN32)
  158. mbedtls_printf( " + Press Enter to exit this program.\n" );
  159. fflush( stdout ); getchar();
  160. #endif
  161. return( exit_val );
  162. }
  163. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */