ecdsa.c 7.0 KB

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