1 /*
2  *  Example ECDSA program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include "mbedtls/build_info.h"
9 
10 #include "mbedtls/platform.h"
11 
12 #if defined(MBEDTLS_ECDSA_C) && \
13     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
14 #include "mbedtls/entropy.h"
15 #include "mbedtls/ctr_drbg.h"
16 #include "mbedtls/ecdsa.h"
17 #include "mbedtls/sha256.h"
18 
19 #include <string.h>
20 #endif
21 
22 /*
23  * Uncomment to show key and signature details
24  */
25 #define VERBOSE
26 
27 /*
28  * Uncomment to force use of a specific curve
29  */
30 #define ECPARAMS    MBEDTLS_ECP_DP_SECP192R1
31 
32 #if !defined(ECPARAMS)
33 #define ECPARAMS    mbedtls_ecp_curve_list()->grp_id
34 #endif
35 
36 #if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
37     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)38 int main(void)
39 {
40     mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
41                    "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
42     mbedtls_exit(0);
43 }
44 #else
45 #if defined(VERBOSE)
dump_buf(const char * title,unsigned char * buf,size_t len)46 static void dump_buf(const char *title, unsigned char *buf, size_t len)
47 {
48     size_t i;
49 
50     mbedtls_printf("%s", title);
51     for (i = 0; i < len; i++) {
52         mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
53                        "0123456789ABCDEF" [buf[i] % 16]);
54     }
55     mbedtls_printf("\n");
56 }
57 
dump_pubkey(const char * title,mbedtls_ecdsa_context * key)58 static void dump_pubkey(const char *title, mbedtls_ecdsa_context *key)
59 {
60     unsigned char buf[300];
61     size_t len;
62 
63     if (mbedtls_ecp_point_write_binary(&key->MBEDTLS_PRIVATE(grp), &key->MBEDTLS_PRIVATE(Q),
64                                        MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof(buf)) != 0) {
65         mbedtls_printf("internal error\n");
66         return;
67     }
68 
69     dump_buf(title, buf, len);
70 }
71 #else
72 #define dump_buf(a, b, c)
73 #define dump_pubkey(a, b)
74 #endif
75 
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79     int ret = 1;
80     int exit_code = MBEDTLS_EXIT_FAILURE;
81     mbedtls_ecdsa_context ctx_sign, ctx_verify;
82     mbedtls_entropy_context entropy;
83     mbedtls_ctr_drbg_context ctr_drbg;
84     unsigned char message[100];
85     unsigned char hash[32];
86     unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
87     size_t sig_len;
88     const char *pers = "ecdsa";
89     ((void) argv);
90 
91     mbedtls_ecdsa_init(&ctx_sign);
92     mbedtls_ecdsa_init(&ctx_verify);
93     mbedtls_ctr_drbg_init(&ctr_drbg);
94 
95     memset(sig, 0, sizeof(sig));
96     memset(message, 0x25, sizeof(message));
97 
98     if (argc != 1) {
99         mbedtls_printf("usage: ecdsa\n");
100 
101 #if defined(_WIN32)
102         mbedtls_printf("\n");
103 #endif
104 
105         goto exit;
106     }
107 
108     /*
109      * Generate a key pair for signing
110      */
111     mbedtls_printf("\n  . Seeding the random number generator...");
112     fflush(stdout);
113 
114     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);
119         goto exit;
120     }
121 
122     mbedtls_printf(" ok\n  . Generating key pair...");
123     fflush(stdout);
124 
125     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);
128         goto exit;
129     }
130 
131     mbedtls_printf(" ok (key size: %d bits)\n", (int) ctx_sign.MBEDTLS_PRIVATE(grp).pbits);
132 
133     dump_pubkey("  + Public key: ", &ctx_sign);
134 
135     /*
136      * Compute message hash
137      */
138     mbedtls_printf("  . Computing message hash...");
139     fflush(stdout);
140 
141     if ((ret = mbedtls_sha256(message, sizeof(message), hash, 0)) != 0) {
142         mbedtls_printf(" failed\n  ! mbedtls_sha256 returned %d\n", ret);
143         goto exit;
144     }
145 
146     mbedtls_printf(" ok\n");
147 
148     dump_buf("  + Hash: ", hash, sizeof(hash));
149 
150     /*
151      * Sign message hash
152      */
153     mbedtls_printf("  . Signing message hash...");
154     fflush(stdout);
155 
156     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);
161         goto exit;
162     }
163     mbedtls_printf(" ok (signature length = %u)\n", (unsigned int) sig_len);
164 
165     dump_buf("  + Signature: ", sig, sig_len);
166 
167     /*
168      * Transfer public information to verifying context
169      *
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).
173      */
174     mbedtls_printf("  . Preparing verification context...");
175     fflush(stdout);
176 
177     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);
181         goto exit;
182     }
183 
184     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);
187         goto exit;
188     }
189 
190     /*
191      * Verify signature
192      */
193     mbedtls_printf(" ok\n  . Verifying signature...");
194     fflush(stdout);
195 
196     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);
200         goto exit;
201     }
202 
203     mbedtls_printf(" ok\n");
204 
205     exit_code = MBEDTLS_EXIT_SUCCESS;
206 
207 exit:
208 
209     mbedtls_ecdsa_free(&ctx_verify);
210     mbedtls_ecdsa_free(&ctx_sign);
211     mbedtls_ctr_drbg_free(&ctr_drbg);
212     mbedtls_entropy_free(&entropy);
213 
214     mbedtls_exit(exit_code);
215 }
216 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
217           ECPARAMS */
218