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