ecdsa.c 6.9 KB

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