rsa_sign.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * RSA/SHA-256 signature creation 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_snprintf snprintf
  32. #define mbedtls_exit exit
  33. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  34. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  35. #endif /* MBEDTLS_PLATFORM_C */
  36. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  37. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
  38. !defined(MBEDTLS_FS_IO)
  39. int main( void )
  40. {
  41. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  42. "MBEDTLS_MD_C and/or "
  43. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
  44. mbedtls_exit( 0 );
  45. }
  46. #else
  47. #include "mbedtls/rsa.h"
  48. #include "mbedtls/md.h"
  49. #include <stdio.h>
  50. #include <string.h>
  51. int main( int argc, char *argv[] )
  52. {
  53. FILE *f;
  54. int ret = 1;
  55. int exit_code = MBEDTLS_EXIT_FAILURE;
  56. size_t i;
  57. mbedtls_rsa_context rsa;
  58. unsigned char hash[32];
  59. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  60. char filename[512];
  61. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  62. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  63. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  64. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  65. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  66. if( argc != 2 )
  67. {
  68. mbedtls_printf( "usage: rsa_sign <filename>\n" );
  69. #if defined(_WIN32)
  70. mbedtls_printf( "\n" );
  71. #endif
  72. goto exit;
  73. }
  74. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  75. fflush( stdout );
  76. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  77. {
  78. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  79. " ! Please run rsa_genkey first\n\n" );
  80. goto exit;
  81. }
  82. if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
  83. ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
  84. ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
  85. ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
  86. ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
  87. ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
  88. ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
  89. ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
  90. {
  91. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
  92. fclose( f );
  93. goto exit;
  94. }
  95. fclose( f );
  96. if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
  97. {
  98. mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
  99. ret );
  100. goto exit;
  101. }
  102. if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
  103. {
  104. mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
  105. ret );
  106. goto exit;
  107. }
  108. mbedtls_printf( "\n . Checking the private key" );
  109. fflush( stdout );
  110. if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
  111. {
  112. mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n", (unsigned int) -ret );
  113. goto exit;
  114. }
  115. /*
  116. * Compute the SHA-256 hash of the input file,
  117. * then calculate the RSA signature of the hash.
  118. */
  119. mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
  120. fflush( stdout );
  121. if( ( ret = mbedtls_md_file(
  122. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  123. argv[1], hash ) ) != 0 )
  124. {
  125. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
  126. goto exit;
  127. }
  128. if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
  129. 20, hash, buf ) ) != 0 )
  130. {
  131. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", (unsigned int) -ret );
  132. goto exit;
  133. }
  134. /*
  135. * Write the signature into <filename>.sig
  136. */
  137. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
  138. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  139. {
  140. mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
  141. goto exit;
  142. }
  143. for( i = 0; i < rsa.len; i++ )
  144. mbedtls_fprintf( f, "%02X%s", buf[i],
  145. ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
  146. fclose( f );
  147. mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
  148. exit_code = MBEDTLS_EXIT_SUCCESS;
  149. exit:
  150. mbedtls_rsa_free( &rsa );
  151. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  152. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  153. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  154. #if defined(_WIN32)
  155. mbedtls_printf( " + Press Enter to exit this program.\n" );
  156. fflush( stdout ); getchar();
  157. #endif
  158. mbedtls_exit( exit_code );
  159. }
  160. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  161. MBEDTLS_FS_IO */