rsa_encrypt.c 5.3 KB

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