rsa_encrypt.c 5.4 KB

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