2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * The elliptic curve binary polynomial software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
28 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
31 int main(int argc, char *argv[])
33 puts("Elliptic curves are disabled.");
38 # include <openssl/crypto.h>
39 # include <openssl/bio.h>
40 # include <openssl/evp.h>
41 # include <openssl/bn.h>
42 # include <openssl/ec.h>
43 # ifndef OPENSSL_NO_ENGINE
44 # include <openssl/engine.h>
46 # include <openssl/err.h>
47 # include <openssl/rand.h>
49 static const char rnd_seed[] = "string to make the random number generator "
50 "think it has entropy";
52 /* declaration of the test functions */
53 int x9_62_tests(BIO *);
54 int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
55 int test_builtin(BIO *);
57 /* functions to change the RAND_METHOD */
58 int change_rand(void);
59 int restore_rand(void);
60 int fbytes(unsigned char *buf, int num);
62 static RAND_METHOD fake_rand;
63 static const RAND_METHOD *old_rand;
67 /* save old rand method */
68 if ((old_rand = RAND_get_rand_method()) == NULL)
71 fake_rand.seed = old_rand->seed;
72 fake_rand.cleanup = old_rand->cleanup;
73 fake_rand.add = old_rand->add;
74 fake_rand.status = old_rand->status;
75 /* use own random function */
76 fake_rand.bytes = fbytes;
77 fake_rand.pseudorand = old_rand->bytes;
78 /* set new RAND_METHOD */
79 if (!RAND_set_rand_method(&fake_rand))
84 int restore_rand(void)
86 if (!RAND_set_rand_method(old_rand))
92 static int fbytes_counter = 0, use_fake = 0;
93 static const char *numbers[8] = {
94 "651056770906015076056810763456358567190100156695615665659",
95 "6140507067065001063065065565667405560006161556565665656654",
96 "8763001015071075675010661307616710783570106710677817767166"
98 "7000000175690566466555057817571571075705015757757057795755"
100 "1275552191113212300012030439187146164646146646466749494799",
101 "1542725565216523985789236956265265265235675811949404040041",
102 "1456427555219115346513212300075341203043918714616464614664"
104 "1712787255652165239672857892369562652652652356758119494040"
108 int fbytes(unsigned char *buf, int num)
114 return old_rand->bytes(buf, num);
118 if (fbytes_counter >= 8)
123 if (!BN_dec2bn(&tmp, numbers[fbytes_counter])) {
128 if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
136 /* some tests from the X9.62 draft */
137 int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
140 const char message[] = "abc";
141 unsigned char digest[20];
142 unsigned int dgst_len = 0;
143 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
145 ECDSA_SIG *signature = NULL;
146 BIGNUM *r = NULL, *s = NULL;
147 BIGNUM *kinv = NULL, *rp = NULL;
148 BIGNUM *sig_r, *sig_s;
153 /* get the message digest */
154 if (!EVP_DigestInit(md_ctx, EVP_sha1())
155 || !EVP_DigestUpdate(md_ctx, (const void *)message, 3)
156 || !EVP_DigestFinal(md_ctx, digest, &dgst_len))
159 BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
161 if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
164 if (!EC_KEY_generate_key(key))
166 BIO_printf(out, ".");
167 (void)BIO_flush(out);
168 /* create the signature */
170 /* Use ECDSA_sign_setup to avoid use of ECDSA nonces */
171 if (!ECDSA_sign_setup(key, NULL, &kinv, &rp))
173 signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key);
174 if (signature == NULL)
176 BIO_printf(out, ".");
177 (void)BIO_flush(out);
178 /* compare the created signature with the expected signature */
179 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
181 if (!BN_dec2bn(&r, r_in) || !BN_dec2bn(&s, s_in))
183 ECDSA_SIG_get0(&sig_r, &sig_s, signature);
184 if (BN_cmp(sig_r, r) || BN_cmp(sig_s, s))
186 BIO_printf(out, ".");
187 (void)BIO_flush(out);
188 /* verify the signature */
189 if (ECDSA_do_verify(digest, 20, signature, key) != 1)
191 BIO_printf(out, ".");
192 (void)BIO_flush(out);
194 BIO_printf(out, " ok\n");
198 BIO_printf(out, " failed\n");
200 ECDSA_SIG_free(signature);
203 EVP_MD_CTX_free(md_ctx);
209 int x9_62_tests(BIO *out)
213 BIO_printf(out, "some tests from X9.62:\n");
215 /* set own rand method */
219 if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
220 "3342403536405981729393488334694600415596881826869351677613",
221 "5735822328888155254683894997897571951568553642892029982342"))
223 if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
224 "3086361431751678114926225473006680188549593787585317781474"
226 "3238135532097973577080787768312505059318910517550078427819"
229 # ifndef OPENSSL_NO_EC2M
230 if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
231 "87194383164871543355722284926904419997237591535066528048",
232 "308992691965804947361541664549085895292153777025772063598"))
234 if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
235 "2159633321041961198501834003903461262881815148684178964245"
237 "1970303740007316867383349976549972270528498040721988191026"
248 int test_builtin(BIO *out)
250 EC_builtin_curve *curves = NULL;
251 size_t crv_len = 0, n = 0;
252 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
254 ECDSA_SIG *ecdsa_sig = NULL;
255 unsigned char digest[20], wrong_digest[20];
256 unsigned char *signature = NULL;
257 const unsigned char *sig_ptr;
258 unsigned char *sig_ptr2;
259 unsigned char *raw_buf = NULL;
260 BIGNUM *sig_r, *sig_s;
261 unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
264 /* fill digest values with some random data */
265 if (RAND_bytes(digest, 20) <= 0 || RAND_bytes(wrong_digest, 20) <= 0) {
266 BIO_printf(out, "ERROR: unable to get random data\n");
271 * create and verify a ecdsa signature with every availble curve (with )
273 BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
274 "with some internal curves:\n");
276 /* get a list of all internal curves */
277 crv_len = EC_get_builtin_curves(NULL, 0);
278 curves = OPENSSL_malloc(sizeof(*curves) * crv_len);
279 if (curves == NULL) {
280 BIO_printf(out, "malloc error\n");
284 if (!EC_get_builtin_curves(curves, crv_len)) {
285 BIO_printf(out, "unable to get internal curves\n");
289 /* now create and verify a signature for every curve */
290 for (n = 0; n < crv_len; n++) {
291 unsigned char dirt, offset;
294 if (nid == NID_ipsec4 || nid == NID_X25519)
296 /* create new ecdsa key (== EC_KEY) */
297 if ((eckey = EC_KEY_new()) == NULL)
299 group = EC_GROUP_new_by_curve_name(nid);
302 if (EC_KEY_set_group(eckey, group) == 0)
304 EC_GROUP_free(group);
305 degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
312 BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
314 if (!EC_KEY_generate_key(eckey)) {
315 BIO_printf(out, " failed\n");
318 /* create second key */
319 if ((wrong_eckey = EC_KEY_new()) == NULL)
321 group = EC_GROUP_new_by_curve_name(nid);
324 if (EC_KEY_set_group(wrong_eckey, group) == 0)
326 EC_GROUP_free(group);
327 if (!EC_KEY_generate_key(wrong_eckey)) {
328 BIO_printf(out, " failed\n");
332 BIO_printf(out, ".");
333 (void)BIO_flush(out);
335 if (!EC_KEY_check_key(eckey)) {
336 BIO_printf(out, " failed\n");
339 BIO_printf(out, ".");
340 (void)BIO_flush(out);
341 /* create signature */
342 sig_len = ECDSA_size(eckey);
343 if ((signature = OPENSSL_malloc(sig_len)) == NULL)
345 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) {
346 BIO_printf(out, " failed\n");
349 BIO_printf(out, ".");
350 (void)BIO_flush(out);
351 /* verify signature */
352 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
353 BIO_printf(out, " failed\n");
356 BIO_printf(out, ".");
357 (void)BIO_flush(out);
358 /* verify signature with the wrong key */
359 if (ECDSA_verify(0, digest, 20, signature, sig_len, wrong_eckey) == 1) {
360 BIO_printf(out, " failed\n");
363 BIO_printf(out, ".");
364 (void)BIO_flush(out);
366 if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, eckey) == 1) {
367 BIO_printf(out, " failed\n");
370 BIO_printf(out, ".");
371 (void)BIO_flush(out);
373 if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, eckey) == 1) {
374 BIO_printf(out, " failed\n");
377 BIO_printf(out, ".");
378 (void)BIO_flush(out);
381 * Modify a single byte of the signature: to ensure we don't garble
382 * the ASN1 structure, we read the raw signature and modify a byte in
383 * one of the bignums directly.
386 if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL) {
387 BIO_printf(out, " failed\n");
391 ECDSA_SIG_get0(&sig_r, &sig_s, ecdsa_sig);
393 /* Store the two BIGNUMs in raw_buf. */
394 r_len = BN_num_bytes(sig_r);
395 s_len = BN_num_bytes(sig_s);
396 bn_len = (degree + 7) / 8;
397 if ((r_len > bn_len) || (s_len > bn_len)) {
398 BIO_printf(out, " failed\n");
401 buf_len = 2 * bn_len;
402 if ((raw_buf = OPENSSL_zalloc(buf_len)) == NULL)
404 BN_bn2bin(sig_r, raw_buf + bn_len - r_len);
405 BN_bn2bin(sig_s, raw_buf + buf_len - s_len);
407 /* Modify a single byte in the buffer. */
408 offset = raw_buf[10] % buf_len;
409 dirt = raw_buf[11] ? raw_buf[11] : 1;
410 raw_buf[offset] ^= dirt;
411 /* Now read the BIGNUMs back in from raw_buf. */
412 if ((BN_bin2bn(raw_buf, bn_len, sig_r) == NULL) ||
413 (BN_bin2bn(raw_buf + bn_len, bn_len, sig_s) == NULL))
416 sig_ptr2 = signature;
417 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
418 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1) {
419 BIO_printf(out, " failed\n");
423 * Sanity check: undo the modification and verify signature.
425 raw_buf[offset] ^= dirt;
426 if ((BN_bin2bn(raw_buf, bn_len, sig_r) == NULL) ||
427 (BN_bin2bn(raw_buf + bn_len, bn_len, sig_s) == NULL))
430 sig_ptr2 = signature;
431 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
432 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
433 BIO_printf(out, " failed\n");
436 BIO_printf(out, ".");
437 (void)BIO_flush(out);
439 BIO_printf(out, " ok\n");
441 /* clean bogus errors */
443 OPENSSL_free(signature);
447 EC_KEY_free(wrong_eckey);
449 ECDSA_SIG_free(ecdsa_sig);
451 OPENSSL_free(raw_buf);
458 EC_KEY_free(wrong_eckey);
459 ECDSA_SIG_free(ecdsa_sig);
460 OPENSSL_free(signature);
461 OPENSSL_free(raw_buf);
462 OPENSSL_free(curves);
473 out = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
475 p = getenv("OPENSSL_DEBUG_MEMORY");
476 if (p != NULL && strcmp(p, "on") == 0)
477 CRYPTO_set_mem_debug(1);
479 /* initialize the prng */
480 RAND_seed(rnd_seed, sizeof(rnd_seed));
483 if (!x9_62_tests(out))
485 if (!test_builtin(out))
491 BIO_printf(out, "\nECDSA test failed\n");
493 BIO_printf(out, "\nECDSA test passed\n");
495 ERR_print_errors(out);
497 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
498 if (CRYPTO_mem_leaks(out) <= 0)