rsa_encrypt.c 5.4 KB

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