xref: /openbsd-src/regress/lib/libcrypto/ecdsa/ecdsatest.c (revision 821da3608162c23c1dae71622ad0ce408a892e16)
1 /* crypto/ecdsa/ecdsatest.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the OpenSSL open source
65  * license provided above.
66  *
67  * The elliptic curve binary polynomial software is originally written by
68  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69  *
70  */
71 
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 
76 #include <openssl/crypto.h>
77 #include <openssl/bio.h>
78 #include <openssl/evp.h>
79 #include <openssl/bn.h>
80 #include <openssl/ecdsa.h>
81 #ifndef OPENSSL_NO_ENGINE
82 #include <openssl/engine.h>
83 #endif
84 #include <openssl/err.h>
85 #include <openssl/rand.h>
86 
87 /* declaration of the test functions */
88 int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
89 int test_builtin(BIO *);
90 
91 /* some tests from the X9.62 draft */
92 int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
93 	{
94 	int	ret = 0;
95 	const char message[] = "abc";
96 	unsigned char digest[20];
97 	unsigned int  dgst_len = 0;
98 	EVP_MD_CTX md_ctx;
99 	EC_KEY    *key = NULL;
100 	ECDSA_SIG *signature = NULL;
101 	BIGNUM    *r = NULL, *s = NULL;
102 
103 	EVP_MD_CTX_init(&md_ctx);
104 	/* get the message digest */
105 	EVP_DigestInit(&md_ctx, EVP_ecdsa());
106 	EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
107 	EVP_DigestFinal(&md_ctx, digest, &dgst_len);
108 
109 	BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
110 	/* create the key */
111 	if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
112 		goto x962_int_err;
113 	if (!EC_KEY_generate_key(key))
114 		goto x962_int_err;
115 	BIO_printf(out, ".");
116 	(void)BIO_flush(out);
117 	/* create the signature */
118 	signature = ECDSA_do_sign(digest, 20, key);
119 	if (signature == NULL)
120 		goto x962_int_err;
121 	BIO_printf(out, ".");
122 	(void)BIO_flush(out);
123 	/* compare the created signature with the expected signature */
124 	if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
125 		goto x962_int_err;
126 	if (!BN_dec2bn(&r, r_in) ||
127 	    !BN_dec2bn(&s, s_in))
128 		goto x962_int_err;
129 	if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
130 		goto x962_int_err;
131 	BIO_printf(out, ".");
132 	(void)BIO_flush(out);
133 	/* verify the signature */
134 	if (ECDSA_do_verify(digest, 20, signature, key) != 1)
135 		goto x962_int_err;
136 	BIO_printf(out, ".");
137 	(void)BIO_flush(out);
138 
139 	BIO_printf(out, " ok\n");
140 	ret = 1;
141 x962_int_err:
142 	if (!ret)
143 		BIO_printf(out, " failed\n");
144 	if (key)
145 		EC_KEY_free(key);
146 	if (signature)
147 		ECDSA_SIG_free(signature);
148 	if (r)
149 		BN_free(r);
150 	if (s)
151 		BN_free(s);
152 	EVP_MD_CTX_cleanup(&md_ctx);
153 	return ret;
154 	}
155 
156 int test_builtin(BIO *out)
157 	{
158 	EC_builtin_curve *curves = NULL;
159 	size_t		crv_len = 0, n = 0;
160 	EC_KEY		*eckey = NULL, *wrong_eckey = NULL;
161 	EC_GROUP	*group;
162 	ECDSA_SIG	*ecdsa_sig = NULL;
163 	unsigned char	digest[20], wrong_digest[20];
164 	unsigned char	*signature = NULL;
165 	const unsigned char	*sig_ptr;
166 	unsigned char	*sig_ptr2;
167 	unsigned char	*raw_buf = NULL;
168 	unsigned int	sig_len, degree, r_len, s_len, bn_len, buf_len;
169 	int		nid, ret =  0;
170 
171 	/* fill digest values with some random data */
172 	if (!RAND_pseudo_bytes(digest, 20) ||
173 	    !RAND_pseudo_bytes(wrong_digest, 20))
174 		{
175 		BIO_printf(out, "ERROR: unable to get random data\n");
176 		goto builtin_err;
177 		}
178 
179 	/* create and verify a ecdsa signature with every availble curve
180 	 * (with ) */
181 	BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
182 		"with some internal curves:\n");
183 
184 	/* get a list of all internal curves */
185 	crv_len = EC_get_builtin_curves(NULL, 0);
186 
187 	curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
188 
189 	if (curves == NULL)
190 		{
191 		BIO_printf(out, "malloc error\n");
192 		goto builtin_err;
193 		}
194 
195 	if (!EC_get_builtin_curves(curves, crv_len))
196 		{
197 		BIO_printf(out, "unable to get internal curves\n");
198 		goto builtin_err;
199 		}
200 
201 	/* now create and verify a signature for every curve */
202 	for (n = 0; n < crv_len; n++)
203 		{
204 		unsigned char dirt, offset;
205 
206 		nid = curves[n].nid;
207 		if (nid == NID_ipsec4)
208 			continue;
209 		/* create new ecdsa key (== EC_KEY) */
210 		if ((eckey = EC_KEY_new()) == NULL)
211 			goto builtin_err;
212 		group = EC_GROUP_new_by_curve_name(nid);
213 		if (group == NULL)
214 			goto builtin_err;
215 		if (EC_KEY_set_group(eckey, group) == 0)
216 			goto builtin_err;
217 		EC_GROUP_free(group);
218 		degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
219 		if (degree < 160)
220 			/* drop the curve */
221 			{
222 			EC_KEY_free(eckey);
223 			eckey = NULL;
224 			continue;
225 			}
226 		BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
227 		/* create key */
228 		if (!EC_KEY_generate_key(eckey))
229 			{
230 			BIO_printf(out, " failed\n");
231 			goto builtin_err;
232 			}
233 		/* create second key */
234 		if ((wrong_eckey = EC_KEY_new()) == NULL)
235 			goto builtin_err;
236 		group = EC_GROUP_new_by_curve_name(nid);
237 		if (group == NULL)
238 			goto builtin_err;
239 		if (EC_KEY_set_group(wrong_eckey, group) == 0)
240 			goto builtin_err;
241 		EC_GROUP_free(group);
242 		if (!EC_KEY_generate_key(wrong_eckey))
243 			{
244 			BIO_printf(out, " failed\n");
245 			goto builtin_err;
246 			}
247 
248 		BIO_printf(out, ".");
249 		(void)BIO_flush(out);
250 		/* check key */
251 		if (!EC_KEY_check_key(eckey))
252 			{
253 			BIO_printf(out, " failed\n");
254 			goto builtin_err;
255 			}
256 		BIO_printf(out, ".");
257 		(void)BIO_flush(out);
258 		/* create signature */
259 		sig_len = ECDSA_size(eckey);
260 		if ((signature = OPENSSL_malloc(sig_len)) == NULL)
261 			goto builtin_err;
262                 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
263 			{
264 			BIO_printf(out, " failed\n");
265 			goto builtin_err;
266 			}
267 		BIO_printf(out, ".");
268 		(void)BIO_flush(out);
269 		/* verify signature */
270 		if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
271 			{
272 			BIO_printf(out, " failed\n");
273 			goto builtin_err;
274 			}
275 		BIO_printf(out, ".");
276 		(void)BIO_flush(out);
277 		/* verify signature with the wrong key */
278 		if (ECDSA_verify(0, digest, 20, signature, sig_len,
279 			wrong_eckey) == 1)
280 			{
281 			BIO_printf(out, " failed\n");
282 			goto builtin_err;
283 			}
284 		BIO_printf(out, ".");
285 		(void)BIO_flush(out);
286 		/* wrong digest */
287 		if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
288 			eckey) == 1)
289 			{
290 			BIO_printf(out, " failed\n");
291 			goto builtin_err;
292 			}
293 		BIO_printf(out, ".");
294 		(void)BIO_flush(out);
295 		/* wrong length */
296 		if (ECDSA_verify(0, digest, 20, signature, sig_len - 1,
297 			eckey) == 1)
298 			{
299 			BIO_printf(out, " failed\n");
300 			goto builtin_err;
301 			}
302 		BIO_printf(out, ".");
303 		(void)BIO_flush(out);
304 
305 		/* Modify a single byte of the signature: to ensure we don't
306 		 * garble the ASN1 structure, we read the raw signature and
307 		 * modify a byte in one of the bignums directly. */
308 		sig_ptr = signature;
309 		if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL)
310 			{
311 			BIO_printf(out, " failed\n");
312 			goto builtin_err;
313 			}
314 
315 		/* Store the two BIGNUMs in raw_buf. */
316 		r_len = BN_num_bytes(ecdsa_sig->r);
317 		s_len = BN_num_bytes(ecdsa_sig->s);
318 		bn_len = (degree + 7) / 8;
319 		if ((r_len > bn_len) || (s_len > bn_len))
320 			{
321 			BIO_printf(out, " failed\n");
322 			goto builtin_err;
323 			}
324 		buf_len = 2 * bn_len;
325 		if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
326 			goto builtin_err;
327 		/* Pad the bignums with leading zeroes. */
328 		memset(raw_buf, 0, buf_len);
329 		BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
330 		BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
331 
332 		/* Modify a single byte in the buffer. */
333 		offset = raw_buf[10] % buf_len;
334 		dirt   = raw_buf[11] ? raw_buf[11] : 1;
335 		raw_buf[offset] ^= dirt;
336 		/* Now read the BIGNUMs back in from raw_buf. */
337 		if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
338 			(BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
339 			goto builtin_err;
340 
341 		sig_ptr2 = signature;
342 		sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
343 		if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
344 			{
345 			BIO_printf(out, " failed\n");
346 			goto builtin_err;
347 			}
348 		/* Sanity check: undo the modification and verify signature. */
349 		raw_buf[offset] ^= dirt;
350 		if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
351 			(BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
352 			goto builtin_err;
353 
354 		sig_ptr2 = signature;
355 		sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
356 		if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
357 			{
358 			BIO_printf(out, " failed\n");
359 			goto builtin_err;
360 			}
361 		BIO_printf(out, ".");
362 		(void)BIO_flush(out);
363 
364 		BIO_printf(out, " ok\n");
365 		/* cleanup */
366 		/* clean bogus errors */
367 		ERR_clear_error();
368 		OPENSSL_free(signature);
369 		signature = NULL;
370 		EC_KEY_free(eckey);
371 		eckey = NULL;
372 		EC_KEY_free(wrong_eckey);
373 		wrong_eckey = NULL;
374 		ECDSA_SIG_free(ecdsa_sig);
375 		ecdsa_sig = NULL;
376 		OPENSSL_free(raw_buf);
377 		raw_buf = NULL;
378 		}
379 
380 	ret = 1;
381 builtin_err:
382 	if (eckey)
383 		EC_KEY_free(eckey);
384 	if (wrong_eckey)
385 		EC_KEY_free(wrong_eckey);
386 	if (ecdsa_sig)
387 		ECDSA_SIG_free(ecdsa_sig);
388 	if (signature)
389 		OPENSSL_free(signature);
390 	if (raw_buf)
391 		OPENSSL_free(raw_buf);
392 	if (curves)
393 		OPENSSL_free(curves);
394 
395 	return ret;
396 	}
397 
398 int main(void)
399 	{
400 	int 	ret = 1;
401 	BIO	*out;
402 
403 	out = BIO_new_fp(stdout, BIO_NOCLOSE);
404 
405 	/* enable memory leak checking unless explicitly disabled */
406 	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
407 		(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
408 		{
409 		CRYPTO_malloc_debug_init();
410 		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
411 		}
412 	else
413 		{
414 		/* OPENSSL_DEBUG_MEMORY=off */
415 		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
416 		}
417 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
418 
419 	ERR_load_crypto_strings();
420 
421 	/* the tests */
422 	if (!test_builtin(out)) goto err;
423 
424 	ret = 0;
425 err:
426 	if (ret)
427 		BIO_printf(out, "\nECDSA test failed\n");
428 	else
429 		BIO_printf(out, "\nECDSA test passed\n");
430 	if (ret)
431 		ERR_print_errors(out);
432 	CRYPTO_cleanup_all_ex_data();
433 	ERR_remove_thread_state(NULL);
434 	ERR_free_strings();
435 	CRYPTO_mem_leaks(out);
436 	if (out != NULL)
437 		BIO_free(out);
438 	return ret;
439 	}
440