1*21b7c6baStb /* $OpenBSD: ecdsatest.c,v 1.18 2023/11/19 13:11:06 tb Exp $ */
23c6bd008Smiod /*
33c6bd008Smiod * Written by Nils Larsch for the OpenSSL project.
43c6bd008Smiod */
53c6bd008Smiod /* ====================================================================
63c6bd008Smiod * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
73c6bd008Smiod *
83c6bd008Smiod * Redistribution and use in source and binary forms, with or without
93c6bd008Smiod * modification, are permitted provided that the following conditions
103c6bd008Smiod * are met:
113c6bd008Smiod *
123c6bd008Smiod * 1. Redistributions of source code must retain the above copyright
133c6bd008Smiod * notice, this list of conditions and the following disclaimer.
143c6bd008Smiod *
153c6bd008Smiod * 2. Redistributions in binary form must reproduce the above copyright
163c6bd008Smiod * notice, this list of conditions and the following disclaimer in
173c6bd008Smiod * the documentation and/or other materials provided with the
183c6bd008Smiod * distribution.
193c6bd008Smiod *
203c6bd008Smiod * 3. All advertising materials mentioning features or use of this
213c6bd008Smiod * software must display the following acknowledgment:
223c6bd008Smiod * "This product includes software developed by the OpenSSL Project
233c6bd008Smiod * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
243c6bd008Smiod *
253c6bd008Smiod * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
263c6bd008Smiod * endorse or promote products derived from this software without
273c6bd008Smiod * prior written permission. For written permission, please contact
283c6bd008Smiod * licensing@OpenSSL.org.
293c6bd008Smiod *
303c6bd008Smiod * 5. Products derived from this software may not be called "OpenSSL"
313c6bd008Smiod * nor may "OpenSSL" appear in their names without prior written
323c6bd008Smiod * permission of the OpenSSL Project.
333c6bd008Smiod *
343c6bd008Smiod * 6. Redistributions of any form whatsoever must retain the following
353c6bd008Smiod * acknowledgment:
363c6bd008Smiod * "This product includes software developed by the OpenSSL Project
373c6bd008Smiod * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
383c6bd008Smiod *
393c6bd008Smiod * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
403c6bd008Smiod * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
413c6bd008Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
423c6bd008Smiod * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
433c6bd008Smiod * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
443c6bd008Smiod * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
453c6bd008Smiod * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
463c6bd008Smiod * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
473c6bd008Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
483c6bd008Smiod * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
493c6bd008Smiod * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
503c6bd008Smiod * OF THE POSSIBILITY OF SUCH DAMAGE.
513c6bd008Smiod * ====================================================================
523c6bd008Smiod *
533c6bd008Smiod * This product includes cryptographic software written by Eric Young
543c6bd008Smiod * (eay@cryptsoft.com). This product includes software written by Tim
553c6bd008Smiod * Hudson (tjh@cryptsoft.com).
563c6bd008Smiod *
573c6bd008Smiod */
583c6bd008Smiod /* ====================================================================
593c6bd008Smiod * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
603c6bd008Smiod *
613c6bd008Smiod * Portions of the attached software ("Contribution") are developed by
623c6bd008Smiod * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
633c6bd008Smiod *
643c6bd008Smiod * The Contribution is licensed pursuant to the OpenSSL open source
653c6bd008Smiod * license provided above.
663c6bd008Smiod *
673c6bd008Smiod * The elliptic curve binary polynomial software is originally written by
683c6bd008Smiod * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
693c6bd008Smiod *
703c6bd008Smiod */
713c6bd008Smiod
723c6bd008Smiod #include <stdio.h>
733c6bd008Smiod #include <stdlib.h>
743c6bd008Smiod #include <string.h>
753c6bd008Smiod
763c6bd008Smiod #include <openssl/crypto.h>
773c6bd008Smiod #include <openssl/bio.h>
783c6bd008Smiod #include <openssl/evp.h>
793c6bd008Smiod #include <openssl/bn.h>
803c6bd008Smiod #include <openssl/ecdsa.h>
813c6bd008Smiod #include <openssl/err.h>
823c6bd008Smiod
83fdb0798eStb int test_builtin(void);
843c6bd008Smiod
852824dd00Stb int
test_builtin(void)86fdb0798eStb test_builtin(void)
873c6bd008Smiod {
88fdb0798eStb unsigned char digest[20], wrong_digest[20];
893c6bd008Smiod EC_builtin_curve *curves = NULL;
902824dd00Stb size_t num_curves = 0, n = 0;
913c6bd008Smiod EC_KEY *eckey = NULL, *wrong_eckey = NULL;
923c6bd008Smiod EC_GROUP *group;
933c6bd008Smiod ECDSA_SIG *ecdsa_sig = NULL;
9467c2f6e5Stb BIGNUM *r = NULL, *s = NULL;
953c6bd008Smiod unsigned char *signature = NULL;
963c6bd008Smiod const unsigned char *sig_ptr;
973c6bd008Smiod unsigned char *sig_ptr2;
983c6bd008Smiod unsigned char *raw_buf = NULL;
993c6bd008Smiod unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
100fdb0798eStb int nid;
101fdb0798eStb int failed = 1;
1023c6bd008Smiod
1033c6bd008Smiod /* fill digest values with some random data */
1047603f286Sjsing arc4random_buf(digest, 20);
1057603f286Sjsing arc4random_buf(wrong_digest, 20);
1063c6bd008Smiod
107efde5abdStb /* create and verify a ecdsa signature with every available curve */
108fdb0798eStb printf("\ntesting ECDSA_sign() and ECDSA_verify() "
1093c6bd008Smiod "with some internal curves:\n");
1103c6bd008Smiod
1113c6bd008Smiod /* get a list of all internal curves */
1122824dd00Stb num_curves = EC_get_builtin_curves(NULL, 0);
1133c6bd008Smiod
1142824dd00Stb curves = reallocarray(NULL, sizeof(EC_builtin_curve), num_curves);
115efde5abdStb if (curves == NULL) {
116fdb0798eStb printf("reallocarray error\n");
117fdb0798eStb goto err;
1183c6bd008Smiod }
1193c6bd008Smiod
1202824dd00Stb if (!EC_get_builtin_curves(curves, num_curves)) {
121fdb0798eStb printf("unable to get internal curves\n");
122fdb0798eStb goto err;
1233c6bd008Smiod }
1243c6bd008Smiod
1253c6bd008Smiod /* now create and verify a signature for every curve */
1262824dd00Stb for (n = 0; n < num_curves; n++) {
1273c6bd008Smiod unsigned char dirt, offset;
1283c6bd008Smiod
1293c6bd008Smiod nid = curves[n].nid;
1303c6bd008Smiod if (nid == NID_ipsec4)
1313c6bd008Smiod continue;
132fdb0798eStb
1333c6bd008Smiod if ((eckey = EC_KEY_new()) == NULL)
134fdb0798eStb goto err;
1353c6bd008Smiod group = EC_GROUP_new_by_curve_name(nid);
1363c6bd008Smiod if (group == NULL)
137fdb0798eStb goto err;
1383c6bd008Smiod if (EC_KEY_set_group(eckey, group) == 0)
139fdb0798eStb goto err;
140fdb0798eStb degree = EC_GROUP_get_degree(group);
1413c6bd008Smiod EC_GROUP_free(group);
142efde5abdStb if (degree < 160) {
1433c6bd008Smiod /* drop the curve */
1443c6bd008Smiod EC_KEY_free(eckey);
1453c6bd008Smiod eckey = NULL;
1463c6bd008Smiod continue;
1473c6bd008Smiod }
148fdb0798eStb printf("%s: ", OBJ_nid2sn(nid));
149fdb0798eStb
150efde5abdStb if (!EC_KEY_generate_key(eckey)) {
151fdb0798eStb goto err;
1523c6bd008Smiod }
1533c6bd008Smiod
15448185ea6Stb /* Exercise ECParameters_dup() and let ASAN test for leaks. */
155e20de6bdStb if ((wrong_eckey = ECParameters_dup(eckey)) == NULL)
156fdb0798eStb goto err;
157fdb0798eStb group = EC_GROUP_new_by_curve_name(nid);
158fdb0798eStb if (group == NULL)
159fdb0798eStb goto err;
160fdb0798eStb if (EC_KEY_set_group(wrong_eckey, group) == 0)
161fdb0798eStb goto err;
162fdb0798eStb EC_GROUP_free(group);
163fdb0798eStb if (!EC_KEY_generate_key(wrong_eckey))
164fdb0798eStb goto err;
165fdb0798eStb
166fdb0798eStb printf(".");
167fdb0798eStb fflush(stdout);
168fdb0798eStb
169fdb0798eStb if (!EC_KEY_check_key(eckey))
170fdb0798eStb goto err;
171fdb0798eStb
172fdb0798eStb printf(".");
173fdb0798eStb fflush(stdout);
174fdb0798eStb
175f430d62bStb if ((sig_len = ECDSA_size(eckey)) == 0)
176fdb0798eStb goto err;
1778f4b6089Sbeck if ((signature = malloc(sig_len)) == NULL)
178fdb0798eStb goto err;
179fdb0798eStb if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
180fdb0798eStb goto err;
181fdb0798eStb
182fdb0798eStb printf(".");
183fdb0798eStb fflush(stdout);
184fdb0798eStb
185fdb0798eStb if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
186fdb0798eStb goto err;
187fdb0798eStb
188fdb0798eStb printf(".");
189fdb0798eStb fflush(stdout);
190fdb0798eStb
1913c6bd008Smiod /* verify signature with the wrong key */
1923c6bd008Smiod if (ECDSA_verify(0, digest, 20, signature, sig_len,
193fdb0798eStb wrong_eckey) == 1)
194fdb0798eStb goto err;
195fdb0798eStb
196fdb0798eStb printf(".");
197fdb0798eStb fflush(stdout);
198fdb0798eStb
1993c6bd008Smiod if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
200fdb0798eStb eckey) == 1)
201fdb0798eStb goto err;
202fdb0798eStb
203fdb0798eStb printf(".");
204fdb0798eStb fflush(stdout);
205fdb0798eStb
2063c6bd008Smiod if (ECDSA_verify(0, digest, 20, signature, sig_len - 1,
207fdb0798eStb eckey) == 1)
208fdb0798eStb goto err;
209fdb0798eStb
210fdb0798eStb printf(".");
211fdb0798eStb fflush(stdout);
2123c6bd008Smiod
213efde5abdStb /*
214efde5abdStb * Modify a single byte of the signature: to ensure we don't
2153c6bd008Smiod * garble the ASN1 structure, we read the raw signature and
216efde5abdStb * modify a byte in one of the bignums directly.
217efde5abdStb */
2183c6bd008Smiod sig_ptr = signature;
219efde5abdStb if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr,
220fdb0798eStb sig_len)) == NULL)
221fdb0798eStb goto err;
2223c6bd008Smiod
2233c6bd008Smiod /* Store the two BIGNUMs in raw_buf. */
22467c2f6e5Stb r_len = BN_num_bytes(ECDSA_SIG_get0_r(ecdsa_sig));
22567c2f6e5Stb s_len = BN_num_bytes(ECDSA_SIG_get0_s(ecdsa_sig));
2263c6bd008Smiod bn_len = (degree + 7) / 8;
227fdb0798eStb if ((r_len > bn_len) || (s_len > bn_len))
228fdb0798eStb goto err;
229fdb0798eStb
2303c6bd008Smiod buf_len = 2 * bn_len;
2318f4b6089Sbeck if ((raw_buf = calloc(1, buf_len)) == NULL)
232fdb0798eStb goto err;
233fdb0798eStb BN_bn2bin(ECDSA_SIG_get0_r(ecdsa_sig),
234fdb0798eStb raw_buf + bn_len - r_len);
235fdb0798eStb BN_bn2bin(ECDSA_SIG_get0_s(ecdsa_sig),
236fdb0798eStb raw_buf + buf_len - s_len);
2373c6bd008Smiod
2383c6bd008Smiod /* Modify a single byte in the buffer. */
2393c6bd008Smiod offset = raw_buf[10] % buf_len;
2403c6bd008Smiod dirt = raw_buf[11] ? raw_buf[11] : 1;
2413c6bd008Smiod raw_buf[offset] ^= dirt;
2423c6bd008Smiod /* Now read the BIGNUMs back in from raw_buf. */
24367c2f6e5Stb if ((r = BN_bin2bn(raw_buf, bn_len, NULL)) == NULL ||
24467c2f6e5Stb (s = BN_bin2bn(raw_buf + bn_len, bn_len, NULL)) == NULL)
245fdb0798eStb goto err;
24667c2f6e5Stb if (!ECDSA_SIG_set0(ecdsa_sig, r, s))
247fdb0798eStb goto err;
24867c2f6e5Stb r = NULL;
24967c2f6e5Stb s = NULL;
2503c6bd008Smiod
251f430d62bStb if ((sig_len = i2d_ECDSA_SIG(ecdsa_sig, NULL)) <= 0)
252fdb0798eStb goto err;
253f430d62bStb free(signature);
254f430d62bStb if ((signature = calloc(1, sig_len)) == NULL)
255fdb0798eStb goto err;
256f430d62bStb
2573c6bd008Smiod sig_ptr2 = signature;
258d179e808Stb if ((sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2)) <= 0)
259fdb0798eStb goto err;
260fdb0798eStb if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
261fdb0798eStb goto err;
262fdb0798eStb
2633c6bd008Smiod /* Sanity check: undo the modification and verify signature. */
2643c6bd008Smiod raw_buf[offset] ^= dirt;
26567c2f6e5Stb if ((r = BN_bin2bn(raw_buf, bn_len, NULL)) == NULL ||
26667c2f6e5Stb (s = BN_bin2bn(raw_buf + bn_len, bn_len, NULL)) == NULL)
267fdb0798eStb goto err;
26867c2f6e5Stb if (!ECDSA_SIG_set0(ecdsa_sig, r, s))
269fdb0798eStb goto err;
27067c2f6e5Stb r = NULL;
27167c2f6e5Stb s = NULL;
2723c6bd008Smiod
273f430d62bStb if ((sig_len = i2d_ECDSA_SIG(ecdsa_sig, NULL)) <= 0)
274fdb0798eStb goto err;
275f430d62bStb free(signature);
276f430d62bStb if ((signature = calloc(1, sig_len)) == NULL)
277fdb0798eStb goto err;
278f430d62bStb
2793c6bd008Smiod sig_ptr2 = signature;
280d179e808Stb if ((sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2)) <= 0)
281fdb0798eStb goto err;
282efde5abdStb if (ECDSA_verify(0, digest, 20, signature, sig_len,
283fdb0798eStb eckey) != 1)
284fdb0798eStb goto err;
2853c6bd008Smiod
286fdb0798eStb printf(".");
287fdb0798eStb fflush(stdout);
288fdb0798eStb
289fdb0798eStb printf(" ok\n");
290fdb0798eStb
2913c6bd008Smiod ERR_clear_error();
2928f4b6089Sbeck free(signature);
2933c6bd008Smiod signature = NULL;
2943c6bd008Smiod EC_KEY_free(eckey);
2953c6bd008Smiod eckey = NULL;
2963c6bd008Smiod EC_KEY_free(wrong_eckey);
2973c6bd008Smiod wrong_eckey = NULL;
2983c6bd008Smiod ECDSA_SIG_free(ecdsa_sig);
2993c6bd008Smiod ecdsa_sig = NULL;
3008f4b6089Sbeck free(raw_buf);
3013c6bd008Smiod raw_buf = NULL;
3023c6bd008Smiod }
3033c6bd008Smiod
304fdb0798eStb failed = 0;
305fdb0798eStb
306fdb0798eStb err:
307fdb0798eStb if (failed)
308fdb0798eStb printf(" failed\n");
309fdb0798eStb
31067c2f6e5Stb BN_free(r);
31167c2f6e5Stb BN_free(s);
3123c6bd008Smiod EC_KEY_free(eckey);
3133c6bd008Smiod EC_KEY_free(wrong_eckey);
3143c6bd008Smiod ECDSA_SIG_free(ecdsa_sig);
3158f4b6089Sbeck free(signature);
3168f4b6089Sbeck free(raw_buf);
3178f4b6089Sbeck free(curves);
3183c6bd008Smiod
319fdb0798eStb return failed;
3203c6bd008Smiod }
3213c6bd008Smiod
3222824dd00Stb int
main(void)3232824dd00Stb main(void)
3243c6bd008Smiod {
325fdb0798eStb int failed = 1;
3263c6bd008Smiod
3273c6bd008Smiod /* the tests */
328fdb0798eStb if (test_builtin())
329efde5abdStb goto err;
3303c6bd008Smiod
331fdb0798eStb printf("\nECDSA test passed\n");
332fdb0798eStb failed = 0;
333fdb0798eStb
3343c6bd008Smiod err:
335fdb0798eStb if (failed) {
336fdb0798eStb printf("\nECDSA test failed\n");
337fdb0798eStb ERR_print_errors_fp(stdout);
338fdb0798eStb }
339fdb0798eStb
3403c6bd008Smiod CRYPTO_cleanup_all_ex_data();
3413c6bd008Smiod ERR_remove_thread_state(NULL);
3423c6bd008Smiod ERR_free_strings();
343fdb0798eStb
344fdb0798eStb return failed;
3453c6bd008Smiod }
346