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