rsa_sign.c 5.7 KB

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