pk_encrypt.c 4.7 KB

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