Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Example ECDSA program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 8 | #include "mbedtls/build_info.h" |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12 | #if defined(MBEDTLS_ECDSA_C) && \ |
| 13 | defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/entropy.h" |
| 15 | #include "mbedtls/ctr_drbg.h" |
| 16 | #include "mbedtls/ecdsa.h" |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 17 | #include "mbedtls/sha256.h" |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 18 | |
| 19 | #include <string.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 20 | #endif |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 21 | |
| 22 | /* |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 23 | * Uncomment to show key and signature details |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 24 | */ |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 25 | #define VERBOSE |
| 26 | |
| 27 | /* |
| 28 | * Uncomment to force use of a specific curve |
| 29 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #define ECPARAMS MBEDTLS_ECP_DP_SECP192R1 |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 31 | |
| 32 | #if !defined(ECPARAMS) |
Gilles Peskine | 5b8618b | 2021-09-28 12:34:53 +0200 | [diff] [blame] | 33 | #define ECPARAMS mbedtls_ecp_curve_list()->grp_id |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 34 | #endif |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \ |
| 37 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int main(void) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 39 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n"); |
| 42 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 43 | } |
| 44 | #else |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 45 | #if defined(VERBOSE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | static void dump_buf(const char *title, unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 47 | { |
| 48 | size_t i; |
| 49 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | mbedtls_printf("%s", title); |
| 51 | for (i = 0; i < len; i++) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | "0123456789ABCDEF" [buf[i] % 16]); |
| 54 | } |
| 55 | mbedtls_printf("\n"); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 59 | { |
| 60 | unsigned char buf[300]; |
| 61 | size_t len; |
| 62 | |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 63 | if (mbedtls_ecp_write_public_key(key, MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 64 | &len, buf, sizeof(buf)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | mbedtls_printf("internal error\n"); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | dump_buf(title, buf, len); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 70 | } |
| 71 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | #define dump_buf(a, b, c) |
| 73 | #define dump_pubkey(a, b) |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 74 | #endif |
| 75 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | int main(int argc, char *argv[]) |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 78 | { |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 79 | int ret = 1; |
| 80 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | mbedtls_ecdsa_context ctx_sign, ctx_verify; |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 82 | mbedtls_ecp_point Q; |
| 83 | mbedtls_ecp_point_init(&Q); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | mbedtls_entropy_context entropy; |
| 85 | mbedtls_ctr_drbg_context ctr_drbg; |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 86 | unsigned char message[100]; |
| 87 | unsigned char hash[32]; |
| 88 | unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 89 | size_t sig_len; |
| 90 | const char *pers = "ecdsa"; |
| 91 | ((void) argv); |
| 92 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | mbedtls_ecdsa_init(&ctx_sign); |
| 94 | mbedtls_ecdsa_init(&ctx_verify); |
| 95 | mbedtls_ctr_drbg_init(&ctr_drbg); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 96 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | memset(sig, 0, sizeof(sig)); |
| 98 | memset(message, 0x25, sizeof(message)); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | if (argc != 1) { |
| 101 | mbedtls_printf("usage: ecdsa\n"); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 102 | |
| 103 | #if defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_printf("\n"); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 105 | #endif |
| 106 | |
| 107 | goto exit; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Generate a key pair for signing |
| 112 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 114 | fflush(stdout); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | mbedtls_entropy_init(&entropy); |
| 117 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 118 | (const unsigned char *) pers, |
| 119 | strlen(pers))) != 0) { |
| 120 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 121 | goto exit; |
| 122 | } |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | mbedtls_printf(" ok\n . Generating key pair..."); |
| 125 | fflush(stdout); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if ((ret = mbedtls_ecdsa_genkey(&ctx_sign, ECPARAMS, |
| 128 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 129 | mbedtls_printf(" failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 130 | goto exit; |
| 131 | } |
| 132 | |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 133 | mbedtls_ecp_group_id grp_id = mbedtls_ecp_keypair_get_group_id(&ctx_sign); |
| 134 | const mbedtls_ecp_curve_info *curve_info = |
| 135 | mbedtls_ecp_curve_info_from_grp_id(grp_id); |
| 136 | mbedtls_printf(" ok (key size: %d bits)\n", (int) curve_info->bit_size); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | dump_pubkey(" + Public key: ", &ctx_sign); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 140 | /* |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 141 | * Compute message hash |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 142 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | mbedtls_printf(" . Computing message hash..."); |
| 144 | fflush(stdout); |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if ((ret = mbedtls_sha256(message, sizeof(message), hash, 0)) != 0) { |
| 147 | mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n", ret); |
Andres Amaya Garcia | 1ff60f4 | 2017-06-28 13:26:36 +0100 | [diff] [blame] | 148 | goto exit; |
| 149 | } |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | mbedtls_printf(" ok\n"); |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | dump_buf(" + Hash: ", hash, sizeof(hash)); |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * Sign message hash |
| 157 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | mbedtls_printf(" . Signing message hash..."); |
| 159 | fflush(stdout); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | if ((ret = mbedtls_ecdsa_write_signature(&ctx_sign, MBEDTLS_MD_SHA256, |
| 162 | hash, sizeof(hash), |
| 163 | sig, sizeof(sig), &sig_len, |
| 164 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 165 | mbedtls_printf(" failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 166 | goto exit; |
| 167 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | mbedtls_printf(" ok (signature length = %u)\n", (unsigned int) sig_len); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | dump_buf(" + Signature: ", sig, sig_len); |
Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 171 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 172 | /* |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 173 | * Transfer public information to verifying context |
Manuel Pégourié-Gonnard | 4e3e7c2 | 2014-07-08 13:05:49 +0200 | [diff] [blame] | 174 | * |
| 175 | * We could use the same context for verification and signatures, but we |
| 176 | * chose to use a new one in order to make it clear that the verifying |
| 177 | * context only needs the public key (Q), and not the private key (d). |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | mbedtls_printf(" . Preparing verification context..."); |
| 180 | fflush(stdout); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 181 | |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 182 | if ((ret = mbedtls_ecp_export(&ctx_sign, NULL, NULL, &Q)) != 0) { |
| 183 | mbedtls_printf(" failed\n ! mbedtls_ecp_export returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 184 | goto exit; |
| 185 | } |
| 186 | |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 187 | if ((ret = mbedtls_ecp_set_public_key(grp_id, &ctx_verify, &Q)) != 0) { |
| 188 | mbedtls_printf(" failed\n ! mbedtls_ecp_set_public_key returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 189 | goto exit; |
| 190 | } |
| 191 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Verify signature |
| 194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | mbedtls_printf(" ok\n . Verifying signature..."); |
| 196 | fflush(stdout); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | if ((ret = mbedtls_ecdsa_read_signature(&ctx_verify, |
| 199 | hash, sizeof(hash), |
| 200 | sig, sig_len)) != 0) { |
| 201 | mbedtls_printf(" failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 202 | goto exit; |
| 203 | } |
| 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 206 | |
Andres Amaya Garcia | 2602a1f | 2018-04-29 19:45:25 +0100 | [diff] [blame] | 207 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 208 | |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 209 | exit: |
| 210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | mbedtls_ecdsa_free(&ctx_verify); |
| 212 | mbedtls_ecdsa_free(&ctx_sign); |
Gilles Peskine | 52cc2a6 | 2023-06-22 22:32:05 +0200 | [diff] [blame] | 213 | mbedtls_ecp_point_free(&Q); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 215 | mbedtls_entropy_free(&entropy); |
Manuel Pégourié-Gonnard | bf3109f | 2013-08-14 21:36:01 +0200 | [diff] [blame] | 216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | mbedtls_exit(exit_code); |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 218 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 220 | ECPARAMS */ |