pk_sign.c 4.8 KB

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