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