pk_sign.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Public key-based 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_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  63. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_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_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  69. "MBEDTLS_SHA256_C and/or MBEDTLS_MD_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/error.h"
  76. #include "mbedtls/entropy.h"
  77. #include "mbedtls/ctr_drbg.h"
  78. #include "mbedtls/md.h"
  79. #include "mbedtls/pk.h"
  80. #include <stdio.h>
  81. #include <string.h>
  82. /*
  83. * For the currently used signature algorithms the buffer to store any signature
  84. * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
  85. */
  86. #if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
  87. #define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
  88. #else
  89. #define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
  90. #endif
  91. int main( int argc, char *argv[] )
  92. {
  93. FILE *f;
  94. int ret = 1;
  95. int exit_code = MBEDTLS_EXIT_FAILURE;
  96. mbedtls_pk_context pk;
  97. mbedtls_entropy_context entropy;
  98. mbedtls_ctr_drbg_context ctr_drbg;
  99. unsigned char hash[32];
  100. unsigned char buf[SIGNATURE_MAX_SIZE];
  101. char filename[512];
  102. const char *pers = "mbedtls_pk_sign";
  103. size_t olen = 0;
  104. mbedtls_entropy_init( &entropy );
  105. mbedtls_ctr_drbg_init( &ctr_drbg );
  106. mbedtls_pk_init( &pk );
  107. if( argc != 3 )
  108. {
  109. mbedtls_printf( "usage: mbedtls_pk_sign <key_file> <filename>\n" );
  110. #if defined(_WIN32)
  111. mbedtls_printf( "\n" );
  112. #endif
  113. goto exit;
  114. }
  115. mbedtls_printf( "\n . Seeding the random number generator..." );
  116. fflush( stdout );
  117. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  118. (const unsigned char *) pers,
  119. strlen( pers ) ) ) != 0 )
  120. {
  121. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
  122. goto exit;
  123. }
  124. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  125. fflush( stdout );
  126. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  127. {
  128. mbedtls_printf( " failed\n ! Could not parse '%s'\n", argv[1] );
  129. goto exit;
  130. }
  131. /*
  132. * Compute the SHA-256 hash of the input file,
  133. * then calculate the signature of the hash.
  134. */
  135. mbedtls_printf( "\n . Generating the SHA-256 signature" );
  136. fflush( stdout );
  137. if( ( ret = mbedtls_md_file(
  138. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  139. argv[2], hash ) ) != 0 )
  140. {
  141. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  142. goto exit;
  143. }
  144. if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
  145. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  146. {
  147. mbedtls_printf( " failed\n ! mbedtls_pk_sign returned -0x%04x\n", -ret );
  148. goto exit;
  149. }
  150. /*
  151. * Write the signature into <filename>.sig
  152. */
  153. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
  154. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  155. {
  156. mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
  157. goto exit;
  158. }
  159. if( fwrite( buf, 1, olen, f ) != olen )
  160. {
  161. mbedtls_printf( "failed\n ! fwrite failed\n\n" );
  162. fclose( f );
  163. goto exit;
  164. }
  165. fclose( f );
  166. mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
  167. exit_code = MBEDTLS_EXIT_SUCCESS;
  168. exit:
  169. mbedtls_pk_free( &pk );
  170. mbedtls_ctr_drbg_free( &ctr_drbg );
  171. mbedtls_entropy_free( &entropy );
  172. #if defined(MBEDTLS_ERROR_C)
  173. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  174. {
  175. mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
  176. mbedtls_printf( " ! Last error was: %s\n", buf );
  177. }
  178. #endif
  179. #if defined(_WIN32)
  180. mbedtls_printf( " + Press Enter to exit this program.\n" );
  181. fflush( stdout ); getchar();
  182. #endif
  183. mbedtls_exit( exit_code );
  184. }
  185. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
  186. MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  187. MBEDTLS_CTR_DRBG_C */