pk_encrypt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_PK_PARSE_C) && \
  63. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
  64. defined(MBEDTLS_CTR_DRBG_C)
  65. #include "mbedtls/error.h"
  66. #include "mbedtls/pk.h"
  67. #include "mbedtls/entropy.h"
  68. #include "mbedtls/ctr_drbg.h"
  69. #include <stdio.h>
  70. #include <string.h>
  71. #endif
  72. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
  73. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
  74. !defined(MBEDTLS_CTR_DRBG_C)
  75. int main( void )
  76. {
  77. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
  78. "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
  79. "MBEDTLS_CTR_DRBG_C not defined.\n");
  80. mbedtls_exit( 0 );
  81. }
  82. #else
  83. int main( int argc, char *argv[] )
  84. {
  85. FILE *f;
  86. int ret = 1;
  87. int exit_code = MBEDTLS_EXIT_FAILURE;
  88. size_t i, olen = 0;
  89. mbedtls_pk_context pk;
  90. mbedtls_entropy_context entropy;
  91. mbedtls_ctr_drbg_context ctr_drbg;
  92. unsigned char input[1024];
  93. unsigned char buf[512];
  94. const char *pers = "mbedtls_pk_encrypt";
  95. mbedtls_ctr_drbg_init( &ctr_drbg );
  96. mbedtls_entropy_init( &entropy );
  97. mbedtls_pk_init( &pk );
  98. if( argc != 3 )
  99. {
  100. mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
  101. #if defined(_WIN32)
  102. mbedtls_printf( "\n" );
  103. #endif
  104. goto exit;
  105. }
  106. mbedtls_printf( "\n . Seeding the random number generator..." );
  107. fflush( stdout );
  108. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  109. &entropy, (const unsigned char *) pers,
  110. strlen( pers ) ) ) != 0 )
  111. {
  112. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  113. -ret );
  114. goto exit;
  115. }
  116. mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
  117. fflush( stdout );
  118. if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  119. {
  120. mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
  121. goto exit;
  122. }
  123. if( strlen( argv[2] ) > 100 )
  124. {
  125. mbedtls_printf( " Input data larger than 100 characters.\n\n" );
  126. goto exit;
  127. }
  128. memcpy( input, argv[2], strlen( argv[2] ) );
  129. /*
  130. * Calculate the RSA encryption of the hash.
  131. */
  132. mbedtls_printf( "\n . Generating the encrypted value" );
  133. fflush( stdout );
  134. if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
  135. buf, &olen, sizeof(buf),
  136. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  137. {
  138. mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
  139. -ret );
  140. goto exit;
  141. }
  142. /*
  143. * Write the signature into result-enc.txt
  144. */
  145. if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
  146. {
  147. mbedtls_printf( " failed\n ! Could not create %s\n\n",
  148. "result-enc.txt" );
  149. ret = 1;
  150. goto exit;
  151. }
  152. for( i = 0; i < olen; i++ )
  153. {
  154. mbedtls_fprintf( f, "%02X%s", buf[i],
  155. ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
  156. }
  157. fclose( f );
  158. mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
  159. exit_code = MBEDTLS_EXIT_SUCCESS;
  160. exit:
  161. mbedtls_pk_free( &pk );
  162. mbedtls_entropy_free( &entropy );
  163. mbedtls_ctr_drbg_free( &ctr_drbg );
  164. #if defined(MBEDTLS_ERROR_C)
  165. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  166. {
  167. mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
  168. mbedtls_printf( " ! Last error was: %s\n", buf );
  169. }
  170. #endif
  171. #if defined(_WIN32)
  172. mbedtls_printf( " + Press Enter to exit this program.\n" );
  173. fflush( stdout ); getchar();
  174. #endif
  175. mbedtls_exit( exit_code );
  176. }
  177. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
  178. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */