pk_sign.c 5.4 KB

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