]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/ecdsatest.c
Update the test framework so that the need for test_main is removed. Everything
[thirdparty/openssl.git] / test / ecdsatest.c
CommitLineData
2b32b281 1/*
ad887416 2 * Copyright 2002-2017 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4d94ae00 4 *
440e5d80
RS
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
4d94ae00 9 */
440e5d80 10
4d94ae00
BM
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
4d94ae00 14
10bf4fc2 15#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
1297ef99 16# include "testutil.h"
3b6aa36c 17
a69de3f2 18#ifndef OPENSSL_NO_EC
4d94ae00 19
0f113f3e
MC
20# include <openssl/crypto.h>
21# include <openssl/bio.h>
22# include <openssl/evp.h>
23# include <openssl/bn.h>
fb29bb59 24# include <openssl/ec.h>
0f113f3e
MC
25# ifndef OPENSSL_NO_ENGINE
26# include <openssl/engine.h>
27# endif
28# include <openssl/err.h>
29# include <openssl/rand.h>
690ecff7 30
f367ac2b
RS
31static const char rnd_seed[] =
32 "string to make the random number generator think it has randomness";
2b32b281 33
2b32b281
BM
34
35/* functions to change the RAND_METHOD */
b66411f6 36static int fbytes(unsigned char *buf, int num);
2b32b281 37
df2ee0e2
BL
38static RAND_METHOD fake_rand;
39static const RAND_METHOD *old_rand;
2b32b281 40
b66411f6 41static int change_rand(void)
0f113f3e
MC
42{
43 /* save old rand method */
b66411f6 44 if (!TEST_ptr(old_rand = RAND_get_rand_method()))
0f113f3e
MC
45 return 0;
46
b66411f6 47 fake_rand = *old_rand;
0f113f3e
MC
48 /* use own random function */
49 fake_rand.bytes = fbytes;
0f113f3e 50 /* set new RAND_METHOD */
b66411f6 51 if (!TEST_true(RAND_set_rand_method(&fake_rand)))
0f113f3e
MC
52 return 0;
53 return 1;
54}
2b32b281 55
b66411f6 56static int restore_rand(void)
0f113f3e 57{
b66411f6 58 if (!TEST_true(RAND_set_rand_method(old_rand)))
0f113f3e 59 return 0;
b66411f6 60 return 1;
0f113f3e 61}
4d94ae00 62
d80399a3 63static int fbytes_counter = 0, use_fake = 0;
2b32b281 64static const char *numbers[8] = {
0f113f3e
MC
65 "651056770906015076056810763456358567190100156695615665659",
66 "6140507067065001063065065565667405560006161556565665656654",
67 "8763001015071075675010661307616710783570106710677817767166"
68 "71676178726717",
69 "7000000175690566466555057817571571075705015757757057795755"
70 "55657156756655",
71 "1275552191113212300012030439187146164646146646466749494799",
72 "1542725565216523985789236956265265265235675811949404040041",
73 "1456427555219115346513212300075341203043918714616464614664"
74 "64667494947990",
75 "1712787255652165239672857892369562652652652356758119494040"
76 "40041670216363"
77};
2b32b281 78
b66411f6 79static int fbytes(unsigned char *buf, int num)
0f113f3e 80{
b66411f6 81 int ret = 0;
0f113f3e
MC
82 BIGNUM *tmp = NULL;
83
84 if (use_fake == 0)
85 return old_rand->bytes(buf, num);
86
87 use_fake = 0;
88
89 if (fbytes_counter >= 8)
90 return 0;
b66411f6 91 if (!TEST_ptr(tmp = BN_new()))
0f113f3e 92 return 0;
b66411f6 93 if (!TEST_true(BN_dec2bn(&tmp, numbers[fbytes_counter]))) {
0f113f3e
MC
94 BN_free(tmp);
95 return 0;
96 }
97 fbytes_counter++;
b66411f6
RS
98 if (TEST_int_eq(BN_num_bytes(tmp), num)
99 && TEST_true(BN_bn2bin(tmp, buf)))
0f113f3e
MC
100 ret = 1;
101 BN_free(tmp);
102 return ret;
103}
2b32b281
BM
104
105/* some tests from the X9.62 draft */
b66411f6 106static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
0f113f3e
MC
107{
108 int ret = 0;
109 const char message[] = "abc";
110 unsigned char digest[20];
111 unsigned int dgst_len = 0;
b66411f6 112 EVP_MD_CTX *md_ctx;
0f113f3e
MC
113 EC_KEY *key = NULL;
114 ECDSA_SIG *signature = NULL;
115 BIGNUM *r = NULL, *s = NULL;
116 BIGNUM *kinv = NULL, *rp = NULL;
9267c11b 117 const BIGNUM *sig_r, *sig_s;
0f113f3e 118
b66411f6 119 if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
6e59a892
RL
120 goto x962_int_err;
121
0f113f3e 122 /* get the message digest */
b66411f6
RS
123 if (!TEST_true(EVP_DigestInit(md_ctx, EVP_sha1()))
124 || !TEST_true(EVP_DigestUpdate(md_ctx, (const void *)message, 3))
125 || !TEST_true(EVP_DigestFinal(md_ctx, digest, &dgst_len)))
0f113f3e
MC
126 goto x962_int_err;
127
b66411f6
RS
128 TEST_info("testing %s", OBJ_nid2sn(nid));
129
0f113f3e 130 /* create the key */
b66411f6 131 if (!TEST_ptr(key = EC_KEY_new_by_curve_name(nid)))
0f113f3e
MC
132 goto x962_int_err;
133 use_fake = 1;
b66411f6 134 if (!TEST_true(EC_KEY_generate_key(key)))
0f113f3e 135 goto x962_int_err;
b66411f6 136
0f113f3e
MC
137 /* create the signature */
138 use_fake = 1;
139 /* Use ECDSA_sign_setup to avoid use of ECDSA nonces */
b66411f6 140 if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)))
0f113f3e 141 goto x962_int_err;
b66411f6 142 if (!TEST_ptr(signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key)))
0f113f3e 143 goto x962_int_err;
b66411f6 144
0f113f3e 145 /* compare the created signature with the expected signature */
b66411f6 146 if (!TEST_ptr(r = BN_new()) || !TEST_ptr(s = BN_new()))
0f113f3e 147 goto x962_int_err;
b66411f6 148 if (!TEST_true(BN_dec2bn(&r, r_in)) || !TEST_true(BN_dec2bn(&s, s_in)))
0f113f3e 149 goto x962_int_err;
9267c11b 150 ECDSA_SIG_get0(signature, &sig_r, &sig_s);
dc352c19
P
151 if (!TEST_BN_eq(sig_r, r)
152 || !TEST_BN_eq(sig_s, s))
0f113f3e 153 goto x962_int_err;
b66411f6 154
0f113f3e 155 /* verify the signature */
b66411f6 156 if (!TEST_int_eq(ECDSA_do_verify(digest, 20, signature, key), 1))
0f113f3e 157 goto x962_int_err;
0f113f3e 158
0f113f3e 159 ret = 1;
b66411f6 160
0f113f3e 161 x962_int_err:
8fdc3734 162 EC_KEY_free(key);
25aaa98a 163 ECDSA_SIG_free(signature);
23a1d5e9
RS
164 BN_free(r);
165 BN_free(s);
bfb0641f 166 EVP_MD_CTX_free(md_ctx);
23a1d5e9
RS
167 BN_clear_free(kinv);
168 BN_clear_free(rp);
0f113f3e
MC
169 return ret;
170}
4d94ae00 171
b66411f6 172static int x9_62_tests()
0f113f3e
MC
173{
174 int ret = 0;
175
0f113f3e
MC
176 /* set own rand method */
177 if (!change_rand())
178 goto x962_err;
179
b66411f6
RS
180 if (!TEST_true(x9_62_test_internal(NID_X9_62_prime192v1,
181 "3342403536405981729393488334694600415596881826869351677613",
182 "5735822328888155254683894997897571951568553642892029982342")))
0f113f3e 183 goto x962_err;
b66411f6
RS
184 if (!TEST_true(x9_62_test_internal(NID_X9_62_prime239v1,
185 "3086361431751678114926225473006680188549593787585317781474"
0f113f3e 186 "62058306432176",
b66411f6
RS
187 "3238135532097973577080787768312505059318910517550078427819"
188 "78505179448783")))
0f113f3e 189 goto x962_err;
b66411f6 190
0f113f3e 191# ifndef OPENSSL_NO_EC2M
b66411f6
RS
192 if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb191v1,
193 "87194383164871543355722284926904419997237591535066528048",
194 "308992691965804947361541664549085895292153777025772063598")))
0f113f3e 195 goto x962_err;
b66411f6
RS
196 if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb239v1,
197 "2159633321041961198501834003903461262881815148684178964245"
0f113f3e 198 "5876922391552",
b66411f6
RS
199 "1970303740007316867383349976549972270528498040721988191026"
200 "49413465737174")))
0f113f3e
MC
201 goto x962_err;
202# endif
203 ret = 1;
b66411f6 204
0f113f3e 205 x962_err:
b66411f6 206 if (!TEST_true(restore_rand()))
0f113f3e
MC
207 ret = 0;
208 return ret;
209}
2b32b281 210
b66411f6 211static int test_builtin(void)
0f113f3e
MC
212{
213 EC_builtin_curve *curves = NULL;
214 size_t crv_len = 0, n = 0;
215 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
216 EC_GROUP *group;
9267c11b 217 ECDSA_SIG *ecdsa_sig = NULL, *modified_sig = NULL;
0f113f3e
MC
218 unsigned char digest[20], wrong_digest[20];
219 unsigned char *signature = NULL;
220 const unsigned char *sig_ptr;
221 unsigned char *sig_ptr2;
222 unsigned char *raw_buf = NULL;
9267c11b
EK
223 const BIGNUM *sig_r, *sig_s;
224 BIGNUM *modified_r = NULL, *modified_s = NULL;
225 BIGNUM *unmodified_r = NULL, *unmodified_s = NULL;
0f113f3e
MC
226 unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
227 int nid, ret = 0;
228
229 /* fill digest values with some random data */
b66411f6
RS
230 if (!TEST_true(RAND_bytes(digest, 20))
231 || !TEST_true(RAND_bytes(wrong_digest, 20)))
0f113f3e 232 goto builtin_err;
0f113f3e 233
b66411f6 234 /* create and verify a ecdsa signature with every available curve */
0f113f3e
MC
235 /* get a list of all internal curves */
236 crv_len = EC_get_builtin_curves(NULL, 0);
b66411f6
RS
237 if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
238 || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
0f113f3e 239 goto builtin_err;
0f113f3e
MC
240
241 /* now create and verify a signature for every curve */
242 for (n = 0; n < crv_len; n++) {
243 unsigned char dirt, offset;
244
245 nid = curves[n].nid;
4a5bbc4e 246 if (nid == NID_ipsec4 || nid == NID_X25519)
0f113f3e
MC
247 continue;
248 /* create new ecdsa key (== EC_KEY) */
b66411f6
RS
249 if (!TEST_ptr(eckey = EC_KEY_new())
250 || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
251 || !TEST_true(EC_KEY_set_group(eckey, group)))
0f113f3e
MC
252 goto builtin_err;
253 EC_GROUP_free(group);
254 degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
8fdc3734 255 if (degree < 160) {
0f113f3e 256 /* drop the curve */
0f113f3e
MC
257 EC_KEY_free(eckey);
258 eckey = NULL;
259 continue;
260 }
b66411f6
RS
261 TEST_info("testing %s", OBJ_nid2sn(nid));
262
0f113f3e 263 /* create key */
b66411f6 264 if (!TEST_true(EC_KEY_generate_key(eckey)))
0f113f3e 265 goto builtin_err;
0f113f3e 266 /* create second key */
b66411f6
RS
267 if (!TEST_ptr(wrong_eckey = EC_KEY_new())
268 || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
269 || !TEST_true(EC_KEY_set_group(wrong_eckey, group)))
0f113f3e
MC
270 goto builtin_err;
271 EC_GROUP_free(group);
b66411f6 272 if (!TEST_true(EC_KEY_generate_key(wrong_eckey)))
0f113f3e 273 goto builtin_err;
0f113f3e 274
0f113f3e 275 /* check key */
b66411f6 276 if (!TEST_true(EC_KEY_check_key(eckey)))
0f113f3e 277 goto builtin_err;
b66411f6 278
0f113f3e
MC
279 /* create signature */
280 sig_len = ECDSA_size(eckey);
b66411f6
RS
281 if (!TEST_ptr(signature = OPENSSL_malloc(sig_len))
282 || !TEST_true(ECDSA_sign(0, digest, 20, signature, &sig_len,
283 eckey)))
0f113f3e 284 goto builtin_err;
b66411f6 285
0f113f3e 286 /* verify signature */
b66411f6
RS
287 if (!TEST_int_eq(ECDSA_verify(0, digest, 20, signature, sig_len,
288 eckey), 1))
0f113f3e 289 goto builtin_err;
b66411f6 290
0f113f3e 291 /* verify signature with the wrong key */
b66411f6
RS
292 if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature, sig_len,
293 wrong_eckey), 1))
0f113f3e 294 goto builtin_err;
b66411f6 295
0f113f3e 296 /* wrong digest */
b66411f6
RS
297 if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, 20, signature,
298 sig_len, eckey), 1))
0f113f3e 299 goto builtin_err;
b66411f6 300
0f113f3e 301 /* wrong length */
b66411f6
RS
302 if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature,
303 sig_len - 1, eckey), 1))
0f113f3e 304 goto builtin_err;
0f113f3e
MC
305
306 /*
307 * Modify a single byte of the signature: to ensure we don't garble
308 * the ASN1 structure, we read the raw signature and modify a byte in
309 * one of the bignums directly.
310 */
311 sig_ptr = signature;
b66411f6 312 if (!TEST_ptr(ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)))
0f113f3e 313 goto builtin_err;
0f113f3e 314
9267c11b 315 ECDSA_SIG_get0(ecdsa_sig, &sig_r, &sig_s);
cf70b8f5 316
0f113f3e 317 /* Store the two BIGNUMs in raw_buf. */
cf70b8f5
DSH
318 r_len = BN_num_bytes(sig_r);
319 s_len = BN_num_bytes(sig_s);
0f113f3e 320 bn_len = (degree + 7) / 8;
b66411f6
RS
321 if (!TEST_false(r_len > bn_len)
322 || !TEST_false(s_len > bn_len))
0f113f3e 323 goto builtin_err;
0f113f3e 324 buf_len = 2 * bn_len;
b66411f6 325 if (!TEST_ptr(raw_buf = OPENSSL_zalloc(buf_len)))
0f113f3e 326 goto builtin_err;
cf70b8f5
DSH
327 BN_bn2bin(sig_r, raw_buf + bn_len - r_len);
328 BN_bn2bin(sig_s, raw_buf + buf_len - s_len);
0f113f3e
MC
329
330 /* Modify a single byte in the buffer. */
331 offset = raw_buf[10] % buf_len;
332 dirt = raw_buf[11] ? raw_buf[11] : 1;
333 raw_buf[offset] ^= dirt;
b66411f6 334
0f113f3e 335 /* Now read the BIGNUMs back in from raw_buf. */
b66411f6 336 if (!TEST_ptr(modified_sig = ECDSA_SIG_new()))
0f113f3e 337 goto builtin_err;
b66411f6
RS
338 if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))
339 || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,
340 bn_len, NULL))
341 || !TEST_true(ECDSA_SIG_set0(modified_sig,
342 modified_r, modified_s))) {
9267c11b
EK
343 BN_free(modified_r);
344 BN_free(modified_s);
345 goto builtin_err;
346 }
0f113f3e 347 sig_ptr2 = signature;
9267c11b 348 sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
b66411f6 349 if (!TEST_false(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
0f113f3e 350 goto builtin_err;
b66411f6
RS
351
352 /* Sanity check: undo the modification and verify signature. */
0f113f3e 353 raw_buf[offset] ^= dirt;
b66411f6
RS
354 if (!TEST_ptr(unmodified_r = BN_bin2bn(raw_buf, bn_len, NULL))
355 || !TEST_ptr(unmodified_s = BN_bin2bn(raw_buf + bn_len,
356 bn_len, NULL))
357 || !TEST_true(ECDSA_SIG_set0(modified_sig, unmodified_r,
358 unmodified_s))) {
9267c11b
EK
359 BN_free(unmodified_r);
360 BN_free(unmodified_s);
0f113f3e 361 goto builtin_err;
9267c11b 362 }
0f113f3e
MC
363
364 sig_ptr2 = signature;
9267c11b 365 sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
b66411f6 366 if (!TEST_true(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
0f113f3e 367 goto builtin_err;
0f113f3e 368
0f113f3e 369 /* cleanup */
0f113f3e
MC
370 ERR_clear_error();
371 OPENSSL_free(signature);
372 signature = NULL;
373 EC_KEY_free(eckey);
374 eckey = NULL;
375 EC_KEY_free(wrong_eckey);
376 wrong_eckey = NULL;
377 ECDSA_SIG_free(ecdsa_sig);
378 ecdsa_sig = NULL;
9267c11b
EK
379 ECDSA_SIG_free(modified_sig);
380 modified_sig = NULL;
0f113f3e
MC
381 OPENSSL_free(raw_buf);
382 raw_buf = NULL;
383 }
384
385 ret = 1;
386 builtin_err:
8fdc3734
RS
387 EC_KEY_free(eckey);
388 EC_KEY_free(wrong_eckey);
25aaa98a 389 ECDSA_SIG_free(ecdsa_sig);
9267c11b 390 ECDSA_SIG_free(modified_sig);
b548a1f1
RS
391 OPENSSL_free(signature);
392 OPENSSL_free(raw_buf);
393 OPENSSL_free(curves);
0f113f3e
MC
394
395 return ret;
396}
1297ef99 397#endif
4d94ae00 398
ad887416 399int setup_tests(void)
0f113f3e 400{
a69de3f2
P
401#ifdef OPENSSL_NO_EC
402 TEST_note("Elliptic curves are disabled.");
403#else
0f113f3e
MC
404 /* initialize the prng */
405 RAND_seed(rnd_seed, sizeof(rnd_seed));
b66411f6
RS
406 ADD_TEST(x9_62_tests);
407 ADD_TEST(test_builtin);
a69de3f2 408#endif
ad887416 409 return 1;
0f113f3e 410}