rsa_sign_pss.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * RSASSA-PSS/SHA-256 signature creation program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_snprintf snprintf
  57. #define mbedtls_printf printf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  63. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  64. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  65. !defined(MBEDTLS_CTR_DRBG_C)
  66. int main( void )
  67. {
  68. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  69. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  70. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  71. "MBEDTLS_CTR_DRBG_C not defined.\n");
  72. mbedtls_exit( 0 );
  73. }
  74. #else
  75. #include "mbedtls/entropy.h"
  76. #include "mbedtls/ctr_drbg.h"
  77. #include "mbedtls/md.h"
  78. #include "mbedtls/rsa.h"
  79. #include "mbedtls/x509.h"
  80. #include <stdio.h>
  81. #include <string.h>
  82. int main( int argc, char *argv[] )
  83. {
  84. FILE *f;
  85. int ret = 1;
  86. int exit_code = MBEDTLS_EXIT_FAILURE;
  87. mbedtls_pk_context pk;
  88. mbedtls_entropy_context entropy;
  89. mbedtls_ctr_drbg_context ctr_drbg;
  90. unsigned char hash[32];
  91. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  92. char filename[512];
  93. const char *pers = "rsa_sign_pss";
  94. size_t olen = 0;
  95. mbedtls_entropy_init( &entropy );
  96. mbedtls_pk_init( &pk );
  97. mbedtls_ctr_drbg_init( &ctr_drbg );
  98. if( argc != 3 )
  99. {
  100. mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
  101. #if defined(_WIN32)
  102. mbedtls_printf( "\n" );
  103. #endif
  104. goto exit;
  105. }
  106. mbedtls_printf( "\n . Seeding the random number generator..." );
  107. fflush( stdout );
  108. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  109. (const unsigned char *) pers,
  110. strlen( pers ) ) ) != 0 )
  111. {
  112. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  113. goto exit;
  114. }
  115. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  116. fflush( stdout );
  117. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  118. {
  119. mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  120. mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
  121. goto exit;
  122. }
  123. if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
  124. {
  125. mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
  126. goto exit;
  127. }
  128. mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
  129. /*
  130. * Compute the SHA-256 hash of the input file,
  131. * then calculate the RSA signature of the hash.
  132. */
  133. mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
  134. fflush( stdout );
  135. if( ( ret = mbedtls_md_file(
  136. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  137. argv[2], hash ) ) != 0 )
  138. {
  139. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  140. goto exit;
  141. }
  142. if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
  143. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  144. {
  145. mbedtls_printf( " failed\n ! mbedtls_pk_sign returned %d\n\n", ret );
  146. goto exit;
  147. }
  148. /*
  149. * Write the signature into <filename>.sig
  150. */
  151. mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
  152. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  153. {
  154. mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
  155. goto exit;
  156. }
  157. if( fwrite( buf, 1, olen, f ) != olen )
  158. {
  159. mbedtls_printf( "failed\n ! fwrite failed\n\n" );
  160. fclose( f );
  161. goto exit;
  162. }
  163. fclose( f );
  164. mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
  165. exit_code = MBEDTLS_EXIT_SUCCESS;
  166. exit:
  167. mbedtls_pk_free( &pk );
  168. mbedtls_ctr_drbg_free( &ctr_drbg );
  169. mbedtls_entropy_free( &entropy );
  170. #if defined(_WIN32)
  171. mbedtls_printf( " + Press Enter to exit this program.\n" );
  172. fflush( stdout ); getchar();
  173. #endif
  174. mbedtls_exit( exit_code );
  175. }
  176. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  177. MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  178. MBEDTLS_CTR_DRBG_C */