ecdsa.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Example ECDSA 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_printf printf
  31. #endif
  32. #if defined(MBEDTLS_ECDSA_C) && \
  33. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
  34. #include "mbedtls/entropy.h"
  35. #include "mbedtls/ctr_drbg.h"
  36. #include "mbedtls/ecdsa.h"
  37. #include <string.h>
  38. #endif
  39. /*
  40. * Uncomment to show key and signature details
  41. */
  42. #define VERBOSE
  43. /*
  44. * Uncomment to force use of a specific curve
  45. */
  46. #define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
  47. #if !defined(ECPARAMS)
  48. #define ECPARAMS mbedtls_ecp_curve_list()->grp_id
  49. #endif
  50. #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
  51. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
  52. int main( void )
  53. {
  54. mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
  55. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
  56. return( 0 );
  57. }
  58. #else
  59. #if defined(VERBOSE)
  60. static void dump_buf( const char *title, unsigned char *buf, size_t len )
  61. {
  62. size_t i;
  63. mbedtls_printf( "%s", title );
  64. for( i = 0; i < len; i++ )
  65. mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
  66. "0123456789ABCDEF" [buf[i] % 16] );
  67. mbedtls_printf( "\n" );
  68. }
  69. static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
  70. {
  71. unsigned char buf[300];
  72. size_t len;
  73. if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
  74. MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
  75. {
  76. mbedtls_printf("internal error\n");
  77. return;
  78. }
  79. dump_buf( title, buf, len );
  80. }
  81. #else
  82. #define dump_buf( a, b, c )
  83. #define dump_pubkey( a, b )
  84. #endif
  85. int main( int argc, char *argv[] )
  86. {
  87. int ret;
  88. mbedtls_ecdsa_context ctx_sign, ctx_verify;
  89. mbedtls_entropy_context entropy;
  90. mbedtls_ctr_drbg_context ctr_drbg;
  91. unsigned char hash[] = "This should be the hash of a message.";
  92. unsigned char sig[512];
  93. size_t sig_len;
  94. const char *pers = "ecdsa";
  95. ((void) argv);
  96. mbedtls_ecdsa_init( &ctx_sign );
  97. mbedtls_ecdsa_init( &ctx_verify );
  98. mbedtls_ctr_drbg_init( &ctr_drbg );
  99. memset(sig, 0, sizeof( sig ) );
  100. ret = 1;
  101. if( argc != 1 )
  102. {
  103. mbedtls_printf( "usage: ecdsa\n" );
  104. #if defined(_WIN32)
  105. mbedtls_printf( "\n" );
  106. #endif
  107. goto exit;
  108. }
  109. /*
  110. * Generate a key pair for signing
  111. */
  112. mbedtls_printf( "\n . Seeding the random number generator..." );
  113. fflush( stdout );
  114. mbedtls_entropy_init( &entropy );
  115. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  116. (const unsigned char *) pers,
  117. strlen( pers ) ) ) != 0 )
  118. {
  119. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  120. goto exit;
  121. }
  122. mbedtls_printf( " ok\n . Generating key pair..." );
  123. fflush( stdout );
  124. if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
  125. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  126. {
  127. mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
  128. goto exit;
  129. }
  130. mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
  131. dump_pubkey( " + Public key: ", &ctx_sign );
  132. /*
  133. * Sign some message hash
  134. */
  135. mbedtls_printf( " . Signing message..." );
  136. fflush( stdout );
  137. if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
  138. hash, sizeof( hash ),
  139. sig, &sig_len,
  140. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  141. {
  142. mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
  143. goto exit;
  144. }
  145. mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
  146. dump_buf( " + Hash: ", hash, sizeof hash );
  147. dump_buf( " + Signature: ", sig, sig_len );
  148. /*
  149. * Transfer public information to verifying context
  150. *
  151. * We could use the same context for verification and signatures, but we
  152. * chose to use a new one in order to make it clear that the verifying
  153. * context only needs the public key (Q), and not the private key (d).
  154. */
  155. mbedtls_printf( " . Preparing verification context..." );
  156. fflush( stdout );
  157. if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
  158. {
  159. mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
  160. goto exit;
  161. }
  162. if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
  163. {
  164. mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
  165. goto exit;
  166. }
  167. ret = 0;
  168. /*
  169. * Verify signature
  170. */
  171. mbedtls_printf( " ok\n . Verifying signature..." );
  172. fflush( stdout );
  173. if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
  174. hash, sizeof( hash ),
  175. sig, sig_len ) ) != 0 )
  176. {
  177. mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
  178. goto exit;
  179. }
  180. mbedtls_printf( " ok\n" );
  181. exit:
  182. #if defined(_WIN32)
  183. mbedtls_printf( " + Press Enter to exit this program.\n" );
  184. fflush( stdout ); getchar();
  185. #endif
  186. mbedtls_ecdsa_free( &ctx_verify );
  187. mbedtls_ecdsa_free( &ctx_sign );
  188. mbedtls_ctr_drbg_free( &ctr_drbg );
  189. mbedtls_entropy_free( &entropy );
  190. return( ret );
  191. }
  192. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
  193. ECPARAMS */