pk_sign.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Public key-based signature creation program
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_snprintf snprintf
  30. #define mbedtls_printf printf
  31. #define mbedtls_exit exit
  32. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  33. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  34. #endif /* MBEDTLS_PLATFORM_C */
  35. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  36. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
  37. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  38. !defined(MBEDTLS_CTR_DRBG_C)
  39. int main( void )
  40. {
  41. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  42. "MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
  43. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  44. "MBEDTLS_CTR_DRBG_C not defined.\n");
  45. mbedtls_exit( 0 );
  46. }
  47. #else
  48. #include "mbedtls/error.h"
  49. #include "mbedtls/entropy.h"
  50. #include "mbedtls/ctr_drbg.h"
  51. #include "mbedtls/md.h"
  52. #include "mbedtls/pk.h"
  53. #include <stdio.h>
  54. #include <string.h>
  55. int main( int argc, char *argv[] )
  56. {
  57. FILE *f;
  58. int ret = 1;
  59. int exit_code = MBEDTLS_EXIT_FAILURE;
  60. mbedtls_pk_context pk;
  61. mbedtls_entropy_context entropy;
  62. mbedtls_ctr_drbg_context ctr_drbg;
  63. unsigned char hash[32];
  64. unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
  65. char filename[512];
  66. const char *pers = "mbedtls_pk_sign";
  67. size_t olen = 0;
  68. mbedtls_entropy_init( &entropy );
  69. mbedtls_ctr_drbg_init( &ctr_drbg );
  70. mbedtls_pk_init( &pk );
  71. if( argc != 3 )
  72. {
  73. mbedtls_printf( "usage: mbedtls_pk_sign <key_file> <filename>\n" );
  74. #if defined(_WIN32)
  75. mbedtls_printf( "\n" );
  76. #endif
  77. goto exit;
  78. }
  79. mbedtls_printf( "\n . Seeding the random number generator..." );
  80. fflush( stdout );
  81. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  82. (const unsigned char *) pers,
  83. strlen( pers ) ) ) != 0 )
  84. {
  85. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
  86. goto exit;
  87. }
  88. mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
  89. fflush( stdout );
  90. if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
  91. {
  92. mbedtls_printf( " failed\n ! Could not parse '%s'\n", argv[1] );
  93. goto exit;
  94. }
  95. /*
  96. * Compute the SHA-256 hash of the input file,
  97. * then calculate the signature of the hash.
  98. */
  99. mbedtls_printf( "\n . Generating the SHA-256 signature" );
  100. fflush( stdout );
  101. if( ( ret = mbedtls_md_file(
  102. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  103. argv[2], hash ) ) != 0 )
  104. {
  105. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  106. goto exit;
  107. }
  108. if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
  109. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  110. {
  111. mbedtls_printf( " failed\n ! mbedtls_pk_sign returned -0x%04x\n", (unsigned int) -ret );
  112. goto exit;
  113. }
  114. /*
  115. * Write the signature into <filename>.sig
  116. */
  117. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
  118. if( ( f = fopen( filename, "wb+" ) ) == NULL )
  119. {
  120. mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
  121. goto exit;
  122. }
  123. if( fwrite( buf, 1, olen, f ) != olen )
  124. {
  125. mbedtls_printf( "failed\n ! fwrite failed\n\n" );
  126. fclose( f );
  127. goto exit;
  128. }
  129. fclose( f );
  130. mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
  131. exit_code = MBEDTLS_EXIT_SUCCESS;
  132. exit:
  133. mbedtls_pk_free( &pk );
  134. mbedtls_ctr_drbg_free( &ctr_drbg );
  135. mbedtls_entropy_free( &entropy );
  136. #if defined(MBEDTLS_ERROR_C)
  137. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  138. {
  139. mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
  140. mbedtls_printf( " ! Last error was: %s\n", buf );
  141. }
  142. #endif
  143. #if defined(_WIN32)
  144. mbedtls_printf( " + Press Enter to exit this program.\n" );
  145. fflush( stdout ); getchar();
  146. #endif
  147. mbedtls_exit( exit_code );
  148. }
  149. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
  150. MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  151. MBEDTLS_CTR_DRBG_C */