rsa_encrypt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * RSA simple data encryption program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_fprintf fprintf
  57. #define mbedtls_printf printf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
  63. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
  64. defined(MBEDTLS_CTR_DRBG_C)
  65. #include "mbedtls/rsa.h"
  66. #include "mbedtls/entropy.h"
  67. #include "mbedtls/ctr_drbg.h"
  68. #include <string.h>
  69. #endif
  70. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  71. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
  72. !defined(MBEDTLS_CTR_DRBG_C)
  73. int main( void )
  74. {
  75. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  76. "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
  77. "MBEDTLS_CTR_DRBG_C not defined.\n");
  78. mbedtls_exit( 0 );
  79. }
  80. #else
  81. int main( int argc, char *argv[] )
  82. {
  83. FILE *f;
  84. int ret = 1;
  85. int exit_code = MBEDTLS_EXIT_FAILURE;
  86. size_t i;
  87. mbedtls_rsa_context rsa;
  88. mbedtls_entropy_context entropy;
  89. mbedtls_ctr_drbg_context ctr_drbg;
  90. unsigned char input[1024];
  91. unsigned char buf[512];
  92. const char *pers = "rsa_encrypt";
  93. mbedtls_mpi N, E;
  94. if( argc != 2 )
  95. {
  96. mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
  97. #if defined(_WIN32)
  98. mbedtls_printf( "\n" );
  99. #endif
  100. mbedtls_exit( exit_code );
  101. }
  102. mbedtls_printf( "\n . Seeding the random number generator..." );
  103. fflush( stdout );
  104. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  105. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  106. mbedtls_ctr_drbg_init( &ctr_drbg );
  107. mbedtls_entropy_init( &entropy );
  108. ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  109. &entropy, (const unsigned char *) pers,
  110. strlen( pers ) );
  111. if( ret != 0 )
  112. {
  113. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  114. ret );
  115. goto exit;
  116. }
  117. mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
  118. fflush( stdout );
  119. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  120. {
  121. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
  122. " ! Please run rsa_genkey first\n\n" );
  123. goto exit;
  124. }
  125. if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
  126. ( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
  127. {
  128. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  129. ret );
  130. fclose( f );
  131. goto exit;
  132. }
  133. fclose( f );
  134. if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
  135. {
  136. mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
  137. ret );
  138. goto exit;
  139. }
  140. if( strlen( argv[1] ) > 100 )
  141. {
  142. mbedtls_printf( " Input data larger than 100 characters.\n\n" );
  143. goto exit;
  144. }
  145. memcpy( input, argv[1], strlen( argv[1] ) );
  146. /*
  147. * Calculate the RSA encryption of the hash.
  148. */
  149. mbedtls_printf( "\n . Generating the RSA encrypted value" );
  150. fflush( stdout );
  151. ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
  152. &ctr_drbg, MBEDTLS_RSA_PUBLIC,
  153. strlen( argv[1] ), input, buf );
  154. if( ret != 0 )
  155. {
  156. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
  157. ret );
  158. goto exit;
  159. }
  160. /*
  161. * Write the signature into result-enc.txt
  162. */
  163. if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
  164. {
  165. mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
  166. goto exit;
  167. }
  168. for( i = 0; i < rsa.len; i++ )
  169. mbedtls_fprintf( f, "%02X%s", buf[i],
  170. ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
  171. fclose( f );
  172. mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
  173. exit_code = MBEDTLS_EXIT_SUCCESS;
  174. exit:
  175. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  176. mbedtls_ctr_drbg_free( &ctr_drbg );
  177. mbedtls_entropy_free( &entropy );
  178. mbedtls_rsa_free( &rsa );
  179. #if defined(_WIN32)
  180. mbedtls_printf( " + Press Enter to exit this program.\n" );
  181. fflush( stdout ); getchar();
  182. #endif
  183. mbedtls_exit( exit_code );
  184. }
  185. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
  186. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */