ecdsa.c 7.0 KB

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