summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/ecdsa/ecdsatest.c
diff options
context:
space:
mode:
authormiod <>2014-04-15 18:01:18 +0000
committermiod <>2014-04-15 18:01:18 +0000
commitf385533fd495dad9242f2eded4a9017f658ee678 (patch)
tree861cdfe8fca12990e76c301d0853c2ab45705858 /src/regress/lib/libcrypto/ecdsa/ecdsatest.c
parentfea4fc3d16f04ff054803f9276895492961ab5e2 (diff)
downloadopenbsd-f385533fd495dad9242f2eded4a9017f658ee678.tar.gz
openbsd-f385533fd495dad9242f2eded4a9017f658ee678.tar.bz2
openbsd-f385533fd495dad9242f2eded4a9017f658ee678.zip
Import the OpenSSL libcrypto tests in a form suitable for our rergress
infrastructure. The following tests have not been imported, for their code lacks a licence: asn1, rsa, sha256, sha512, wp.
Diffstat (limited to 'src/regress/lib/libcrypto/ecdsa/ecdsatest.c')
-rw-r--r--src/regress/lib/libcrypto/ecdsa/ecdsatest.c555
1 files changed, 555 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/ecdsa/ecdsatest.c b/src/regress/lib/libcrypto/ecdsa/ecdsatest.c
new file mode 100644
index 0000000000..8dd04e0fd3
--- /dev/null
+++ b/src/regress/lib/libcrypto/ecdsa/ecdsatest.c
@@ -0,0 +1,555 @@
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 */
88int x9_62_tests(BIO *);
89int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
90int test_builtin(BIO *);
91
92/* functions to change the RAND_METHOD */
93int change_rand(void);
94int restore_rand(void);
95int fbytes(unsigned char *buf, int num);
96
97RAND_METHOD fake_rand;
98const RAND_METHOD *old_rand;
99
100int change_rand(void)
101 {
102 /* save old rand method */
103 if ((old_rand = RAND_get_rand_method()) == NULL)
104 return 0;
105
106 fake_rand.seed = old_rand->seed;
107 fake_rand.cleanup = old_rand->cleanup;
108 fake_rand.add = old_rand->add;
109 fake_rand.status = old_rand->status;
110 /* use own random function */
111 fake_rand.bytes = fbytes;
112 fake_rand.pseudorand = old_rand->bytes;
113 /* set new RAND_METHOD */
114 if (!RAND_set_rand_method(&fake_rand))
115 return 0;
116 return 1;
117 }
118
119int restore_rand(void)
120 {
121 if (!RAND_set_rand_method(old_rand))
122 return 0;
123 else
124 return 1;
125 }
126
127static int fbytes_counter = 0;
128static const char *numbers[8] = {
129 "651056770906015076056810763456358567190100156695615665659",
130 "6140507067065001063065065565667405560006161556565665656654",
131 "8763001015071075675010661307616710783570106710677817767166"
132 "71676178726717",
133 "7000000175690566466555057817571571075705015757757057795755"
134 "55657156756655",
135 "1275552191113212300012030439187146164646146646466749494799",
136 "1542725565216523985789236956265265265235675811949404040041",
137 "1456427555219115346513212300075341203043918714616464614664"
138 "64667494947990",
139 "1712787255652165239672857892369562652652652356758119494040"
140 "40041670216363"};
141
142int fbytes(unsigned char *buf, int num)
143 {
144 int ret;
145 BIGNUM *tmp = NULL;
146
147 if (fbytes_counter >= 8)
148 return 0;
149 tmp = BN_new();
150 if (!tmp)
151 return 0;
152 if (!BN_dec2bn(&tmp, numbers[fbytes_counter]))
153 {
154 BN_free(tmp);
155 return 0;
156 }
157 fbytes_counter ++;
158 if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
159 ret = 0;
160 else
161 ret = 1;
162 if (tmp)
163 BN_free(tmp);
164 return ret;
165 }
166
167/* some tests from the X9.62 draft */
168int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
169 {
170 int ret = 0;
171 const char message[] = "abc";
172 unsigned char digest[20];
173 unsigned int dgst_len = 0;
174 EVP_MD_CTX md_ctx;
175 EC_KEY *key = NULL;
176 ECDSA_SIG *signature = NULL;
177 BIGNUM *r = NULL, *s = NULL;
178
179 EVP_MD_CTX_init(&md_ctx);
180 /* get the message digest */
181 EVP_DigestInit(&md_ctx, EVP_ecdsa());
182 EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
183 EVP_DigestFinal(&md_ctx, digest, &dgst_len);
184
185 BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
186 /* create the key */
187 if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
188 goto x962_int_err;
189 if (!EC_KEY_generate_key(key))
190 goto x962_int_err;
191 BIO_printf(out, ".");
192 (void)BIO_flush(out);
193 /* create the signature */
194 signature = ECDSA_do_sign(digest, 20, key);
195 if (signature == NULL)
196 goto x962_int_err;
197 BIO_printf(out, ".");
198 (void)BIO_flush(out);
199 /* compare the created signature with the expected signature */
200 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
201 goto x962_int_err;
202 if (!BN_dec2bn(&r, r_in) ||
203 !BN_dec2bn(&s, s_in))
204 goto x962_int_err;
205 if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
206 goto x962_int_err;
207 BIO_printf(out, ".");
208 (void)BIO_flush(out);
209 /* verify the signature */
210 if (ECDSA_do_verify(digest, 20, signature, key) != 1)
211 goto x962_int_err;
212 BIO_printf(out, ".");
213 (void)BIO_flush(out);
214
215 BIO_printf(out, " ok\n");
216 ret = 1;
217x962_int_err:
218 if (!ret)
219 BIO_printf(out, " failed\n");
220 if (key)
221 EC_KEY_free(key);
222 if (signature)
223 ECDSA_SIG_free(signature);
224 if (r)
225 BN_free(r);
226 if (s)
227 BN_free(s);
228 EVP_MD_CTX_cleanup(&md_ctx);
229 return ret;
230 }
231
232int x9_62_tests(BIO *out)
233 {
234 int ret = 0;
235
236 BIO_printf(out, "some tests from X9.62:\n");
237
238 /* set own rand method */
239 if (!change_rand())
240 goto x962_err;
241
242 if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
243 "3342403536405981729393488334694600415596881826869351677613",
244 "5735822328888155254683894997897571951568553642892029982342"))
245 goto x962_err;
246 if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
247 "3086361431751678114926225473006680188549593787585317781474"
248 "62058306432176",
249 "3238135532097973577080787768312505059318910517550078427819"
250 "78505179448783"))
251 goto x962_err;
252#ifndef OPENSSL_NO_EC2M
253 if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
254 "87194383164871543355722284926904419997237591535066528048",
255 "308992691965804947361541664549085895292153777025772063598"))
256 goto x962_err;
257 if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
258 "2159633321041961198501834003903461262881815148684178964245"
259 "5876922391552",
260 "1970303740007316867383349976549972270528498040721988191026"
261 "49413465737174"))
262 goto x962_err;
263#endif
264 ret = 1;
265x962_err:
266 if (!restore_rand())
267 ret = 0;
268 return ret;
269 }
270
271int test_builtin(BIO *out)
272 {
273 EC_builtin_curve *curves = NULL;
274 size_t crv_len = 0, n = 0;
275 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
276 EC_GROUP *group;
277 ECDSA_SIG *ecdsa_sig = NULL;
278 unsigned char digest[20], wrong_digest[20];
279 unsigned char *signature = NULL;
280 const unsigned char *sig_ptr;
281 unsigned char *sig_ptr2;
282 unsigned char *raw_buf = NULL;
283 unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
284 int nid, ret = 0;
285
286 /* fill digest values with some random data */
287 if (!RAND_pseudo_bytes(digest, 20) ||
288 !RAND_pseudo_bytes(wrong_digest, 20))
289 {
290 BIO_printf(out, "ERROR: unable to get random data\n");
291 goto builtin_err;
292 }
293
294 /* create and verify a ecdsa signature with every availble curve
295 * (with ) */
296 BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
297 "with some internal curves:\n");
298
299 /* get a list of all internal curves */
300 crv_len = EC_get_builtin_curves(NULL, 0);
301
302 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
303
304 if (curves == NULL)
305 {
306 BIO_printf(out, "malloc error\n");
307 goto builtin_err;
308 }
309
310 if (!EC_get_builtin_curves(curves, crv_len))
311 {
312 BIO_printf(out, "unable to get internal curves\n");
313 goto builtin_err;
314 }
315
316 /* now create and verify a signature for every curve */
317 for (n = 0; n < crv_len; n++)
318 {
319 unsigned char dirt, offset;
320
321 nid = curves[n].nid;
322 if (nid == NID_ipsec4)
323 continue;
324 /* create new ecdsa key (== EC_KEY) */
325 if ((eckey = EC_KEY_new()) == NULL)
326 goto builtin_err;
327 group = EC_GROUP_new_by_curve_name(nid);
328 if (group == NULL)
329 goto builtin_err;
330 if (EC_KEY_set_group(eckey, group) == 0)
331 goto builtin_err;
332 EC_GROUP_free(group);
333 degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
334 if (degree < 160)
335 /* drop the curve */
336 {
337 EC_KEY_free(eckey);
338 eckey = NULL;
339 continue;
340 }
341 BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
342 /* create key */
343 if (!EC_KEY_generate_key(eckey))
344 {
345 BIO_printf(out, " failed\n");
346 goto builtin_err;
347 }
348 /* create second key */
349 if ((wrong_eckey = EC_KEY_new()) == NULL)
350 goto builtin_err;
351 group = EC_GROUP_new_by_curve_name(nid);
352 if (group == NULL)
353 goto builtin_err;
354 if (EC_KEY_set_group(wrong_eckey, group) == 0)
355 goto builtin_err;
356 EC_GROUP_free(group);
357 if (!EC_KEY_generate_key(wrong_eckey))
358 {
359 BIO_printf(out, " failed\n");
360 goto builtin_err;
361 }
362
363 BIO_printf(out, ".");
364 (void)BIO_flush(out);
365 /* check key */
366 if (!EC_KEY_check_key(eckey))
367 {
368 BIO_printf(out, " failed\n");
369 goto builtin_err;
370 }
371 BIO_printf(out, ".");
372 (void)BIO_flush(out);
373 /* create signature */
374 sig_len = ECDSA_size(eckey);
375 if ((signature = OPENSSL_malloc(sig_len)) == NULL)
376 goto builtin_err;
377 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
378 {
379 BIO_printf(out, " failed\n");
380 goto builtin_err;
381 }
382 BIO_printf(out, ".");
383 (void)BIO_flush(out);
384 /* verify signature */
385 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
386 {
387 BIO_printf(out, " failed\n");
388 goto builtin_err;
389 }
390 BIO_printf(out, ".");
391 (void)BIO_flush(out);
392 /* verify signature with the wrong key */
393 if (ECDSA_verify(0, digest, 20, signature, sig_len,
394 wrong_eckey) == 1)
395 {
396 BIO_printf(out, " failed\n");
397 goto builtin_err;
398 }
399 BIO_printf(out, ".");
400 (void)BIO_flush(out);
401 /* wrong digest */
402 if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
403 eckey) == 1)
404 {
405 BIO_printf(out, " failed\n");
406 goto builtin_err;
407 }
408 BIO_printf(out, ".");
409 (void)BIO_flush(out);
410 /* wrong length */
411 if (ECDSA_verify(0, digest, 20, signature, sig_len - 1,
412 eckey) == 1)
413 {
414 BIO_printf(out, " failed\n");
415 goto builtin_err;
416 }
417 BIO_printf(out, ".");
418 (void)BIO_flush(out);
419
420 /* Modify a single byte of the signature: to ensure we don't
421 * garble the ASN1 structure, we read the raw signature and
422 * modify a byte in one of the bignums directly. */
423 sig_ptr = signature;
424 if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL)
425 {
426 BIO_printf(out, " failed\n");
427 goto builtin_err;
428 }
429
430 /* Store the two BIGNUMs in raw_buf. */
431 r_len = BN_num_bytes(ecdsa_sig->r);
432 s_len = BN_num_bytes(ecdsa_sig->s);
433 bn_len = (degree + 7) / 8;
434 if ((r_len > bn_len) || (s_len > bn_len))
435 {
436 BIO_printf(out, " failed\n");
437 goto builtin_err;
438 }
439 buf_len = 2 * bn_len;
440 if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
441 goto builtin_err;
442 /* Pad the bignums with leading zeroes. */
443 memset(raw_buf, 0, buf_len);
444 BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
445 BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
446
447 /* Modify a single byte in the buffer. */
448 offset = raw_buf[10] % buf_len;
449 dirt = raw_buf[11] ? raw_buf[11] : 1;
450 raw_buf[offset] ^= dirt;
451 /* Now read the BIGNUMs back in from raw_buf. */
452 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
453 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
454 goto builtin_err;
455
456 sig_ptr2 = signature;
457 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
458 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
459 {
460 BIO_printf(out, " failed\n");
461 goto builtin_err;
462 }
463 /* Sanity check: undo the modification and verify signature. */
464 raw_buf[offset] ^= dirt;
465 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
466 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
467 goto builtin_err;
468
469 sig_ptr2 = signature;
470 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
471 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
472 {
473 BIO_printf(out, " failed\n");
474 goto builtin_err;
475 }
476 BIO_printf(out, ".");
477 (void)BIO_flush(out);
478
479 BIO_printf(out, " ok\n");
480 /* cleanup */
481 /* clean bogus errors */
482 ERR_clear_error();
483 OPENSSL_free(signature);
484 signature = NULL;
485 EC_KEY_free(eckey);
486 eckey = NULL;
487 EC_KEY_free(wrong_eckey);
488 wrong_eckey = NULL;
489 ECDSA_SIG_free(ecdsa_sig);
490 ecdsa_sig = NULL;
491 OPENSSL_free(raw_buf);
492 raw_buf = NULL;
493 }
494
495 ret = 1;
496builtin_err:
497 if (eckey)
498 EC_KEY_free(eckey);
499 if (wrong_eckey)
500 EC_KEY_free(wrong_eckey);
501 if (ecdsa_sig)
502 ECDSA_SIG_free(ecdsa_sig);
503 if (signature)
504 OPENSSL_free(signature);
505 if (raw_buf)
506 OPENSSL_free(raw_buf);
507 if (curves)
508 OPENSSL_free(curves);
509
510 return ret;
511 }
512
513int main(void)
514 {
515 int ret = 1;
516 BIO *out;
517
518 out = BIO_new_fp(stdout, BIO_NOCLOSE);
519
520 /* enable memory leak checking unless explicitly disabled */
521 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
522 (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
523 {
524 CRYPTO_malloc_debug_init();
525 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
526 }
527 else
528 {
529 /* OPENSSL_DEBUG_MEMORY=off */
530 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
531 }
532 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
533
534 ERR_load_crypto_strings();
535
536 /* the tests */
537 if (!x9_62_tests(out)) goto err;
538 if (!test_builtin(out)) goto err;
539
540 ret = 0;
541err:
542 if (ret)
543 BIO_printf(out, "\nECDSA test failed\n");
544 else
545 BIO_printf(out, "\nECDSA test passed\n");
546 if (ret)
547 ERR_print_errors(out);
548 CRYPTO_cleanup_all_ex_data();
549 ERR_remove_thread_state(NULL);
550 ERR_free_strings();
551 CRYPTO_mem_leaks(out);
552 if (out != NULL)
553 BIO_free(out);
554 return ret;
555 }