blob: afd6fb31a4d4348c04659ac75326770e99e6acac [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_ECDSA_C) && \
13 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/entropy.h"
15#include "mbedtls/ctr_drbg.h"
16#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000017#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020018
19#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000020#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020021
22/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020023 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020024 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020025#define VERBOSE
26
27/*
28 * Uncomment to force use of a specific curve
29 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020031
32#if !defined(ECPARAMS)
Gilles Peskine5b8618b2021-09-28 12:34:53 +020033#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020034#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
37 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010038int main(void)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010041 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
42 mbedtls_exit(0);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020043}
44#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020045#if defined(VERBOSE)
Gilles Peskine449bd832023-01-11 14:50:10 +010046static void dump_buf(const char *title, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020047{
48 size_t i;
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_printf("%s", title);
51 for (i = 0; i < len; i++) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Gilles Peskine449bd832023-01-11 14:50:10 +010053 "0123456789ABCDEF" [buf[i] % 16]);
54 }
55 mbedtls_printf("\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020056}
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020059{
60 unsigned char buf[300];
61 size_t len;
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (mbedtls_ecp_point_write_binary(&key->MBEDTLS_PRIVATE(grp), &key->MBEDTLS_PRIVATE(Q),
Dave Rodgman6dd757a2023-02-02 12:40:50 +000064 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof(buf)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020066 return;
67 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 dump_buf(title, buf, len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020070}
71#else
Gilles Peskine449bd832023-01-11 14:50:10 +010072#define dump_buf(a, b, c)
73#define dump_pubkey(a, b)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020074#endif
75
Simon Butcher63cb97e2018-12-06 17:43:31 +000076
Gilles Peskine449bd832023-01-11 14:50:10 +010077int main(int argc, char *argv[])
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020078{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010079 int ret = 1;
80 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_ecdsa_context ctx_sign, ctx_verify;
82 mbedtls_entropy_context entropy;
83 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +000084 unsigned char message[100];
85 unsigned char hash[32];
86 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020087 size_t sig_len;
88 const char *pers = "ecdsa";
89 ((void) argv);
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_ecdsa_init(&ctx_sign);
92 mbedtls_ecdsa_init(&ctx_verify);
93 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 memset(sig, 0, sizeof(sig));
96 memset(message, 0x25, sizeof(message));
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (argc != 1) {
99 mbedtls_printf("usage: ecdsa\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200100
101#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_printf("\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200103#endif
104
105 goto exit;
106 }
107
108 /*
109 * Generate a key pair for signing
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_printf("\n . Seeding the random number generator...");
112 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_entropy_init(&entropy);
115 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
116 (const unsigned char *) pers,
117 strlen(pers))) != 0) {
118 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf(" ok\n . Generating key pair...");
123 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if ((ret = mbedtls_ecdsa_genkey(&ctx_sign, ECPARAMS,
126 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
127 mbedtls_printf(" failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf(" ok (key size: %d bits)\n", (int) ctx_sign.MBEDTLS_PRIVATE(grp).pbits);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 dump_pubkey(" + Public key: ", &ctx_sign);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200134
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200135 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000136 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200137 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_printf(" . Computing message hash...");
139 fflush(stdout);
Janos Follath0a5154b2017-03-10 11:31:41 +0000140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if ((ret = mbedtls_sha256(message, sizeof(message), hash, 0)) != 0) {
142 mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100143 goto exit;
144 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_printf(" ok\n");
Janos Follath0a5154b2017-03-10 11:31:41 +0000147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 dump_buf(" + Hash: ", hash, sizeof(hash));
Janos Follath0a5154b2017-03-10 11:31:41 +0000149
150 /*
151 * Sign message hash
152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_printf(" . Signing message hash...");
154 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if ((ret = mbedtls_ecdsa_write_signature(&ctx_sign, MBEDTLS_MD_SHA256,
157 hash, sizeof(hash),
158 sig, sizeof(sig), &sig_len,
159 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
160 mbedtls_printf(" failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200161 goto exit;
162 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_printf(" ok (signature length = %u)\n", (unsigned int) sig_len);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 dump_buf(" + Signature: ", sig, sig_len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200166
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200167 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200168 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200169 *
170 * We could use the same context for verification and signatures, but we
171 * chose to use a new one in order to make it clear that the verifying
172 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_printf(" . Preparing verification context...");
175 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if ((ret =
178 mbedtls_ecp_group_copy(&ctx_verify.MBEDTLS_PRIVATE(grp),
179 &ctx_sign.MBEDTLS_PRIVATE(grp))) != 0) {
180 mbedtls_printf(" failed\n ! mbedtls_ecp_group_copy returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200181 goto exit;
182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if ((ret =
185 mbedtls_ecp_copy(&ctx_verify.MBEDTLS_PRIVATE(Q), &ctx_sign.MBEDTLS_PRIVATE(Q))) != 0) {
186 mbedtls_printf(" failed\n ! mbedtls_ecp_copy returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200187 goto exit;
188 }
189
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200190 /*
191 * Verify signature
192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_printf(" ok\n . Verifying signature...");
194 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_ecdsa_read_signature(&ctx_verify,
197 hash, sizeof(hash),
198 sig, sig_len)) != 0) {
199 mbedtls_printf(" failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200200 goto exit;
201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200204
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100205 exit_code = MBEDTLS_EXIT_SUCCESS;
206
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200207exit:
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_ecdsa_free(&ctx_verify);
210 mbedtls_ecdsa_free(&ctx_sign);
211 mbedtls_ctr_drbg_free(&ctr_drbg);
212 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 ECPARAMS */