rsa_sign.c 4.8 KB

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