ecdsa_sm2.c 6.8 KB

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