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