blob: 24d79fca8e79054dcc78a7b52b061497185006ec [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_ECDSA_C) && \
17 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
20#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000021#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020022
23#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000024#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020025
26/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020027 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020028 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020029#define VERBOSE
30
31/*
32 * Uncomment to force use of a specific curve
33 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020035
36#if !defined(ECPARAMS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020038#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042int main(void)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
46 mbedtls_exit(0);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020047}
48#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020049#if defined(VERBOSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050static void dump_buf(const char *title, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020051{
52 size_t i;
53
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 mbedtls_printf("%s", title);
55 for (i = 0; i < len; i++) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 "0123456789ABCDEF" [buf[i] % 16]);
58 }
59 mbedtls_printf("\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020060}
61
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020063{
64 unsigned char buf[300];
65 size_t len;
66
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if (mbedtls_ecp_point_write_binary(&key->grp, &key->Q,
Dave Rodgman18688702023-02-02 12:40:50 +000068 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof(buf)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020070 return;
71 }
72
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 dump_buf(title, buf, len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020074}
75#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define dump_buf(a, b, c)
77#define dump_pubkey(a, b)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020078#endif
79
Simon Butcher63cb97e2018-12-06 17:43:31 +000080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081int main(int argc, char *argv[])
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020082{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010083 int ret = 1;
84 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ecdsa_context ctx_sign, ctx_verify;
86 mbedtls_entropy_context entropy;
87 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +000088 unsigned char message[100];
89 unsigned char hash[32];
90 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020091 size_t sig_len;
92 const char *pers = "ecdsa";
93 ((void) argv);
94
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 mbedtls_ecdsa_init(&ctx_sign);
96 mbedtls_ecdsa_init(&ctx_verify);
97 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 memset(sig, 0, sizeof(sig));
100 memset(message, 0x25, sizeof(message));
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if (argc != 1) {
103 mbedtls_printf("usage: ecdsa\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200104
105#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_printf("\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200107#endif
108
109 goto exit;
110 }
111
112 /*
113 * Generate a key pair for signing
114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_printf("\n . Seeding the random number generator...");
116 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_entropy_init(&entropy);
119 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
120 (const unsigned char *) pers,
121 strlen(pers))) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200123 goto exit;
124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_printf(" ok\n . Generating key pair...");
127 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if ((ret = mbedtls_ecdsa_genkey(&ctx_sign, ECPARAMS,
130 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
131 mbedtls_printf(" failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200132 goto exit;
133 }
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_printf(" ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 dump_pubkey(" + Public key: ", &ctx_sign);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200138
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200139 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000140 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 mbedtls_printf(" . Computing message hash...");
143 fflush(stdout);
Janos Follath0a5154b2017-03-10 11:31:41 +0000144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if ((ret = mbedtls_sha256_ret(message, sizeof(message), hash, 0)) != 0) {
146 mbedtls_printf(" failed\n ! mbedtls_sha256_ret returned %d\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100147 goto exit;
148 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_printf(" ok\n");
Janos Follath0a5154b2017-03-10 11:31:41 +0000151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 dump_buf(" + Hash: ", hash, sizeof(hash));
Janos Follath0a5154b2017-03-10 11:31:41 +0000153
154 /*
155 * Sign message hash
156 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 mbedtls_printf(" . Signing message hash...");
158 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if ((ret = mbedtls_ecdsa_write_signature(&ctx_sign, MBEDTLS_MD_SHA256,
161 hash, sizeof(hash),
162 sig, &sig_len,
163 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
164 mbedtls_printf(" failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200165 goto exit;
166 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 mbedtls_printf(" ok (signature length = %u)\n", (unsigned int) sig_len);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 dump_buf(" + Signature: ", sig, sig_len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200170
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200171 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200172 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200173 *
174 * We could use the same context for verification and signatures, but we
175 * chose to use a new one in order to make it clear that the verifying
176 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_printf(" . Preparing verification context...");
179 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if ((ret = mbedtls_ecp_group_copy(&ctx_verify.grp, &ctx_sign.grp)) != 0) {
182 mbedtls_printf(" failed\n ! mbedtls_ecp_group_copy returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200183 goto exit;
184 }
185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if ((ret = mbedtls_ecp_copy(&ctx_verify.Q, &ctx_sign.Q)) != 0) {
187 mbedtls_printf(" failed\n ! mbedtls_ecp_copy returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200188 goto exit;
189 }
190
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 /*
192 * Verify signature
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_printf(" ok\n . Verifying signature...");
195 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 if ((ret = mbedtls_ecdsa_read_signature(&ctx_verify,
198 hash, sizeof(hash),
199 sig, sig_len)) != 0) {
200 mbedtls_printf(" failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200201 goto exit;
202 }
203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200205
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100206 exit_code = MBEDTLS_EXIT_SUCCESS;
207
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200208exit:
209
210#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 mbedtls_printf(" + Press Enter to exit this program.\n");
212 fflush(stdout); getchar();
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200213#endif
214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 mbedtls_ecdsa_free(&ctx_verify);
216 mbedtls_ecdsa_free(&ctx_sign);
217 mbedtls_ctr_drbg_free(&ctr_drbg);
218 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200223 ECPARAMS */