blob: 3bcf315b3b47f7de64d43aba047ede83c74becc5 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_printf printf
28# define mbedtls_exit exit
29# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
30# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010031#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ENTROPY_C) && \
34 defined(MBEDTLS_CTR_DRBG_C)
35# include "mbedtls/entropy.h"
36# include "mbedtls/ctr_drbg.h"
37# include "mbedtls/ecdsa.h"
38# include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020039
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020042
43/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020044 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020045 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020046#define VERBOSE
47
48/*
49 * Uncomment to force use of a specific curve
50 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020052
53#if !defined(ECPARAMS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054# define ECPARAMS mbedtls_ecp_curve_list()->MBEDTLS_PRIVATE(grp_id)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020055#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
58 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059int main(void)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
63 mbedtls_exit(0);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020064}
65#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020066# if defined(VERBOSE)
67static void dump_buf(const char *title, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020068{
69 size_t i;
70
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 mbedtls_printf("%s", title);
72 for (i = 0; i < len; i++)
73 mbedtls_printf("%c%c", "0123456789ABCDEF"[buf[i] / 16],
74 "0123456789ABCDEF"[buf[i] % 16]);
75 mbedtls_printf("\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020076}
77
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020079{
80 unsigned char buf[300];
81 size_t len;
82
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083 if (mbedtls_ecp_point_write_binary(
84 &key->MBEDTLS_PRIVATE(grp), &key->MBEDTLS_PRIVATE(Q),
85 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof(buf)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020087 return;
88 }
89
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090 dump_buf(title, buf, len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020091}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092# else
93# define dump_buf(a, b, c)
94# define dump_pubkey(a, b)
95# endif
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020096
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097int main(int argc, char *argv[])
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020098{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010099 int ret = 1;
100 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_ecdsa_context ctx_sign, ctx_verify;
102 mbedtls_entropy_context entropy;
103 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000104 unsigned char message[100];
105 unsigned char hash[32];
106 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200107 size_t sig_len;
108 const char *pers = "ecdsa";
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 ((void)argv);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200110
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111 mbedtls_ecdsa_init(&ctx_sign);
112 mbedtls_ecdsa_init(&ctx_verify);
113 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 memset(sig, 0, sizeof(sig));
116 memset(message, 0x25, sizeof(message));
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200117
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118 if (argc != 1) {
119 mbedtls_printf("usage: ecdsa\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121# if defined(_WIN32)
122 mbedtls_printf("\n");
123# endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200124
125 goto exit;
126 }
127
128 /*
129 * Generate a key pair for signing
130 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 mbedtls_printf("\n . Seeding the random number generator...");
132 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 mbedtls_entropy_init(&entropy);
135 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
136 (const unsigned char *)pers,
137 strlen(pers))) != 0) {
138 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200139 goto exit;
140 }
141
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142 mbedtls_printf(" ok\n . Generating key pair...");
143 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200144
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145 if ((ret = mbedtls_ecdsa_genkey(&ctx_sign, ECPARAMS,
146 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
147 mbedtls_printf(" failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200148 goto exit;
149 }
150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 mbedtls_printf(" ok (key size: %d bits)\n",
152 (int)ctx_sign.MBEDTLS_PRIVATE(grp).pbits);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 dump_pubkey(" + Public key: ", &ctx_sign);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200155
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200156 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000157 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200158 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 mbedtls_printf(" . Computing message hash...");
160 fflush(stdout);
Janos Follath0a5154b2017-03-10 11:31:41 +0000161
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162 if ((ret = mbedtls_sha256(message, sizeof(message), hash, 0)) != 0) {
163 mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100164 goto exit;
165 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000166
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167 mbedtls_printf(" ok\n");
Janos Follath0a5154b2017-03-10 11:31:41 +0000168
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169 dump_buf(" + Hash: ", hash, sizeof(hash));
Janos Follath0a5154b2017-03-10 11:31:41 +0000170
171 /*
172 * Sign message hash
173 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200174 mbedtls_printf(" . Signing message hash...");
175 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 if ((ret = mbedtls_ecdsa_write_signature(
178 &ctx_sign, MBEDTLS_MD_SHA256, hash, sizeof(hash), sig, sizeof(sig),
179 &sig_len, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
180 mbedtls_printf(
181 " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200182 goto exit;
183 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200184 mbedtls_printf(" ok (signature length = %u)\n", (unsigned int)sig_len);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 dump_buf(" + Signature: ", sig, sig_len);
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200187
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200188 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200189 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200190 *
191 * We could use the same context for verification and signatures, but we
192 * chose to use a new one in order to make it clear that the verifying
193 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200194 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195 mbedtls_printf(" . Preparing verification context...");
196 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200197
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198 if ((ret = mbedtls_ecp_group_copy(&ctx_verify.MBEDTLS_PRIVATE(grp),
199 &ctx_sign.MBEDTLS_PRIVATE(grp))) != 0) {
200 mbedtls_printf(" failed\n ! mbedtls_ecp_group_copy returned %d\n",
201 ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200202 goto exit;
203 }
204
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 if ((ret = mbedtls_ecp_copy(&ctx_verify.MBEDTLS_PRIVATE(Q),
206 &ctx_sign.MBEDTLS_PRIVATE(Q))) != 0) {
207 mbedtls_printf(" failed\n ! mbedtls_ecp_copy returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200208 goto exit;
209 }
210
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200211 /*
212 * Verify signature
213 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214 mbedtls_printf(" ok\n . Verifying signature...");
215 fflush(stdout);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200216
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 if ((ret = mbedtls_ecdsa_read_signature(&ctx_verify, hash, sizeof(hash),
218 sig, sig_len)) != 0) {
219 mbedtls_printf(
220 " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221 goto exit;
222 }
223
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200225
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100226 exit_code = MBEDTLS_EXIT_SUCCESS;
227
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200228exit:
229
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230# if defined(_WIN32)
231 mbedtls_printf(" + Press Enter to exit this program.\n");
232 fflush(stdout);
233 getchar();
234# endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 mbedtls_ecdsa_free(&ctx_verify);
237 mbedtls_ecdsa_free(&ctx_sign);
238 mbedtls_ctr_drbg_free(&ctr_drbg);
239 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200240
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200242}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200243#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && \
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200244 ECPARAMS */