rsa_sign_pss.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * RSASSA-PSS/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_snprintf snprintf
  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_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  38. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  39. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  40. !defined(MBEDTLS_CTR_DRBG_C)
  41. int main( void )
  42. {
  43. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  44. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  45. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  46. "MBEDTLS_CTR_DRBG_C not defined.\n");
  47. return( 0 );
  48. }
  49. #else
  50. #include "mbedtls/entropy.h"
  51. #include "mbedtls/ctr_drbg.h"
  52. #include "mbedtls/md.h"
  53. #include "mbedtls/rsa.h"
  54. #include "mbedtls/x509.h"
  55. #include <stdio.h>
  56. #include <string.h>
  57. int main( int argc, char *argv[] )
  58. {
  59. FILE *f;
  60. int ret = 1;
  61. int exit_code = MBEDTLS_EXIT_FAILURE;
  62. mbedtls_pk_context pk;
  63. mbedtls_entropy_context entropy;
  64. mbedtls_ctr_drbg_context ctr_drbg;
  65. unsigned char hash[32];
  66. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  67. char filename[512];
  68. const char *pers = "rsa_sign_pss";
  69. size_t olen = 0;
  70. mbedtls_entropy_init( &entropy );
  71. mbedtls_pk_init( &pk );
  72. mbedtls_ctr_drbg_init( &ctr_drbg );
  73. if( argc != 3 )
  74. {
  75. mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\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, &entropy,
  84. (const unsigned char *) pers,
  85. strlen( pers ) ) ) != 0 )
  86. {
  87. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  88. goto exit;
  89. }
  90. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  91. fflush( stdout );
  92. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  93. {
  94. mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  95. mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
  96. goto exit;
  97. }
  98. if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
  99. {
  100. mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
  101. goto exit;
  102. }
  103. mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
  104. /*
  105. * Compute the SHA-256 hash of the input file,
  106. * then calculate the RSA signature of the hash.
  107. */
  108. mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
  109. fflush( stdout );
  110. if( ( ret = mbedtls_md_file(
  111. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  112. argv[2], hash ) ) != 0 )
  113. {
  114. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  115. goto exit;
  116. }
  117. if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
  118. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  119. {
  120. mbedtls_printf( " failed\n ! mbedtls_pk_sign returned %d\n\n", ret );
  121. goto exit;
  122. }
  123. /*
  124. * Write the signature into <filename>.sig
  125. */
  126. mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
  127. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  128. {
  129. mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
  130. goto exit;
  131. }
  132. if( fwrite( buf, 1, olen, f ) != olen )
  133. {
  134. mbedtls_printf( "failed\n ! fwrite failed\n\n" );
  135. fclose( f );
  136. goto exit;
  137. }
  138. fclose( f );
  139. mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
  140. exit_code = MBEDTLS_EXIT_SUCCESS;
  141. exit:
  142. mbedtls_pk_free( &pk );
  143. mbedtls_ctr_drbg_free( &ctr_drbg );
  144. mbedtls_entropy_free( &entropy );
  145. #if defined(_WIN32)
  146. mbedtls_printf( " + Press Enter to exit this program.\n" );
  147. fflush( stdout ); getchar();
  148. #endif
  149. return( exit_code );
  150. }
  151. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  152. MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  153. MBEDTLS_CTR_DRBG_C */