blob: a4988b0b4850541e6e6152c7c8fa2e5402ccb931 [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_ECDSA_C) && \
15 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/entropy.h"
17#include "mbedtls/ctr_drbg.h"
18#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000019#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020020
21#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000022#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020023
24/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020025 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020026 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020027#define VERBOSE
28
29/*
30 * Uncomment to force use of a specific curve
31 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020033
34#if !defined(ECPARAMS)
Gilles Peskine5b8618b2021-09-28 12:34:53 +020035#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020036#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
39 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int main(void)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010043 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
44 mbedtls_exit(0);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020045}
46#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020047#if defined(VERBOSE)
Gilles Peskine449bd832023-01-11 14:50:10 +010048static void dump_buf(const char *title, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020049{
50 size_t i;
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_printf("%s", title);
53 for (i = 0; i < len; i++) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Gilles Peskine449bd832023-01-11 14:50:10 +010055 "0123456789ABCDEF" [buf[i] % 16]);
56 }
57 mbedtls_printf("\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020058}
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020061{
62 unsigned char buf[300];
63 size_t len;
64
Gilles Peskine52cc2a62023-06-22 22:32:05 +020065 if (mbedtls_ecp_write_public_key(key, MBEDTLS_ECP_PF_UNCOMPRESSED,
66 &len, buf, sizeof(buf)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020068 return;
69 }
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 dump_buf(title, buf, len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020072}
73#else
Gilles Peskine449bd832023-01-11 14:50:10 +010074#define dump_buf(a, b, c)
75#define dump_pubkey(a, b)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020076#endif
77
Simon Butcher63cb97e2018-12-06 17:43:31 +000078
Gilles Peskine449bd832023-01-11 14:50:10 +010079int main(int argc, char *argv[])
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020080{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010081 int ret = 1;
82 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_ecdsa_context ctx_sign, ctx_verify;
Gilles Peskine52cc2a62023-06-22 22:32:05 +020084 mbedtls_ecp_point Q;
85 mbedtls_ecp_point_init(&Q);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010099 memset(sig, 0, sizeof(sig));
100 memset(message, 0x25, sizeof(message));
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_printf("\n . Seeding the random number generator...");
116 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_printf(" ok\n . Generating key pair...");
127 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine52cc2a62023-06-22 22:32:05 +0200135 mbedtls_ecp_group_id grp_id = mbedtls_ecp_keypair_get_group_id(&ctx_sign);
136 const mbedtls_ecp_curve_info *curve_info =
137 mbedtls_ecp_curve_info_from_grp_id(grp_id);
138 mbedtls_printf(" ok (key size: %d bits)\n", (int) curve_info->bit_size);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 dump_pubkey(" + Public key: ", &ctx_sign);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200141
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200142 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000143 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_printf(" . Computing message hash...");
146 fflush(stdout);
Janos Follath0a5154b2017-03-10 11:31:41 +0000147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if ((ret = mbedtls_sha256(message, sizeof(message), hash, 0)) != 0) {
149 mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100150 goto exit;
151 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_printf(" ok\n");
Janos Follath0a5154b2017-03-10 11:31:41 +0000154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 dump_buf(" + Hash: ", hash, sizeof(hash));
Janos Follath0a5154b2017-03-10 11:31:41 +0000156
157 /*
158 * Sign message hash
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_printf(" . Signing message hash...");
161 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if ((ret = mbedtls_ecdsa_write_signature(&ctx_sign, MBEDTLS_MD_SHA256,
164 hash, sizeof(hash),
165 sig, sizeof(sig), &sig_len,
166 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
167 mbedtls_printf(" failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200168 goto exit;
169 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_printf(" ok (signature length = %u)\n", (unsigned int) sig_len);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 dump_buf(" + Signature: ", sig, sig_len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200173
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200174 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200175 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200176 *
177 * We could use the same context for verification and signatures, but we
178 * chose to use a new one in order to make it clear that the verifying
179 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_printf(" . Preparing verification context...");
182 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200183
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200184 if ((ret = mbedtls_ecp_export(&ctx_sign, NULL, NULL, &Q)) != 0) {
185 mbedtls_printf(" failed\n ! mbedtls_ecp_export returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200186 goto exit;
187 }
188
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200189 if ((ret = mbedtls_ecp_set_public_key(grp_id, &ctx_verify, &Q)) != 0) {
190 mbedtls_printf(" failed\n ! mbedtls_ecp_set_public_key returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 goto exit;
192 }
193
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200194 /*
195 * Verify signature
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_printf(" ok\n . Verifying signature...");
198 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((ret = mbedtls_ecdsa_read_signature(&ctx_verify,
201 hash, sizeof(hash),
202 sig, sig_len)) != 0) {
203 mbedtls_printf(" failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200204 goto exit;
205 }
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200208
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100209 exit_code = MBEDTLS_EXIT_SUCCESS;
210
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200211exit:
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_ecdsa_free(&ctx_verify);
214 mbedtls_ecdsa_free(&ctx_sign);
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200215 mbedtls_ecp_point_free(&Q);
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_ctr_drbg_free(&ctr_drbg);
217 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200222 ECPARAMS */