ecdsa.c 6.6 KB

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