pk_encrypt.c 5.0 KB

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