rsa_encrypt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * RSA simple data encryption 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_fprintf fprintf
  34. #define mbedtls_printf printf
  35. #define mbedtls_exit exit
  36. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  37. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  38. #endif
  39. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
  40. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
  41. defined(MBEDTLS_CTR_DRBG_C)
  42. #include "mbedtls/rsa.h"
  43. #include "mbedtls/entropy.h"
  44. #include "mbedtls/ctr_drbg.h"
  45. #include <string.h>
  46. #endif
  47. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  48. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
  49. !defined(MBEDTLS_CTR_DRBG_C)
  50. int main( void )
  51. {
  52. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  53. "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
  54. "MBEDTLS_CTR_DRBG_C not defined.\n");
  55. return( 0 );
  56. }
  57. #else
  58. int main( int argc, char *argv[] )
  59. {
  60. FILE *f;
  61. int return_val, exit_val;
  62. size_t i;
  63. mbedtls_rsa_context rsa;
  64. mbedtls_entropy_context entropy;
  65. mbedtls_ctr_drbg_context ctr_drbg;
  66. unsigned char input[1024];
  67. unsigned char buf[512];
  68. const char *pers = "rsa_encrypt";
  69. exit_val = MBEDTLS_EXIT_SUCCESS;
  70. if( argc != 2 )
  71. {
  72. mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
  73. #if defined(_WIN32)
  74. mbedtls_printf( "\n" );
  75. #endif
  76. mbedtls_exit( MBEDTLS_EXIT_FAILURE );
  77. }
  78. mbedtls_printf( "\n . Seeding the random number generator..." );
  79. fflush( stdout );
  80. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  81. mbedtls_ctr_drbg_init( &ctr_drbg );
  82. mbedtls_entropy_init( &entropy );
  83. return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  84. &entropy, (const unsigned char *) pers,
  85. strlen( pers ) );
  86. if( return_val != 0 )
  87. {
  88. exit_val = MBEDTLS_EXIT_FAILURE;
  89. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  90. return_val );
  91. goto exit;
  92. }
  93. //mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
  94. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  95. fflush( stdout );
  96. /*NOTA: en este programa estamos intendo cifrar con la clave privada!*/
  97. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  98. {
  99. exit_val = MBEDTLS_EXIT_FAILURE;
  100. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  101. " ! Please run rsa_genkey first\n\n" );
  102. goto exit;
  103. }
  104. if(
  105. ( return_val = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
  106. ( return_val = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
  107. ( return_val = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
  108. ( return_val = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
  109. ( return_val = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
  110. ( return_val = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
  111. ( return_val = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
  112. ( return_val = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
  113. {
  114. exit_val = MBEDTLS_EXIT_FAILURE;
  115. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  116. return_val );
  117. fclose( f );
  118. goto exit;
  119. }
  120. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  121. fclose( f );
  122. if( strlen( argv[1] ) > 100 )
  123. {
  124. exit_val = MBEDTLS_EXIT_FAILURE;
  125. mbedtls_printf( " Input data larger than 100 characters.\n\n" );
  126. goto exit;
  127. }
  128. memcpy( input, argv[1], strlen( argv[1] ) );
  129. /*
  130. * Calculate the RSA encryption of the hash.
  131. */
  132. mbedtls_printf( "\n . Generating the RSA encrypted value (usando clave privada)" );
  133. fflush( stdout );
  134. return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
  135. &ctr_drbg, MBEDTLS_RSA_PRIVATE,
  136. strlen( argv[1] ), input, buf );
  137. if( return_val != 0 )
  138. {
  139. exit_val = MBEDTLS_EXIT_FAILURE;
  140. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
  141. return_val );
  142. goto exit;
  143. }
  144. /*
  145. * Write the signature into result-enc.txt
  146. */
  147. if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
  148. {
  149. exit_val = MBEDTLS_EXIT_FAILURE;
  150. mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
  151. goto exit;
  152. }
  153. for( i = 0; i < rsa.len; i++ )
  154. mbedtls_fprintf( f, "%02X%s", buf[i],
  155. ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
  156. fclose( f );
  157. mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
  158. exit:
  159. mbedtls_ctr_drbg_free( &ctr_drbg );
  160. mbedtls_entropy_free( &entropy );
  161. mbedtls_rsa_free( &rsa );
  162. #if defined(_WIN32)
  163. mbedtls_printf( " + Press Enter to exit this program.\n" );
  164. fflush( stdout ); getchar();
  165. #endif
  166. return( exit_val );
  167. }
  168. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
  169. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */