pk_encrypt.c 5.1 KB

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