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