ecdsatest.c 12.9 KB
Newer Older
1
/*
M
Matt Caswell 已提交
2
 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
B
Bodo Möller 已提交
4
 *
R
Rich Salz 已提交
5 6 7 8
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
B
Bodo Möller 已提交
9
 */
R
Rich Salz 已提交
10

B
Bodo Möller 已提交
11 12 13 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

15
#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
16
# include "testutil.h"
17

P
Pauli 已提交
18
#ifndef OPENSSL_NO_EC
B
Bodo Möller 已提交
19

20 21 22 23
# include <openssl/crypto.h>
# include <openssl/bio.h>
# include <openssl/evp.h>
# include <openssl/bn.h>
D
Dr. Stephen Henson 已提交
24
# include <openssl/ec.h>
25 26 27 28 29
# ifndef OPENSSL_NO_ENGINE
#  include <openssl/engine.h>
# endif
# include <openssl/err.h>
# include <openssl/rand.h>
30

31
/* functions to change the RAND_METHOD */
R
Rich Salz 已提交
32
static int fbytes(unsigned char *buf, int num);
33

34 35
static RAND_METHOD fake_rand;
static const RAND_METHOD *old_rand;
36

R
Rich Salz 已提交
37
static int change_rand(void)
38 39
{
    /* save old rand method */
R
Rich Salz 已提交
40
    if (!TEST_ptr(old_rand = RAND_get_rand_method()))
41 42
        return 0;

R
Rich Salz 已提交
43
    fake_rand = *old_rand;
44 45 46
    /* use own random function */
    fake_rand.bytes = fbytes;
    /* set new RAND_METHOD */
R
Rich Salz 已提交
47
    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
48 49 50
        return 0;
    return 1;
}
51

R
Rich Salz 已提交
52
static int restore_rand(void)
53
{
R
Rich Salz 已提交
54
    if (!TEST_true(RAND_set_rand_method(old_rand)))
55
        return 0;
R
Rich Salz 已提交
56
    return 1;
57
}
B
Bodo Möller 已提交
58

59
static int fbytes_counter = 0, use_fake = 0;
60
static const char *numbers[8] = {
61 62 63 64 65 66 67 68 69 70 71 72 73
    "651056770906015076056810763456358567190100156695615665659",
    "6140507067065001063065065565667405560006161556565665656654",
    "8763001015071075675010661307616710783570106710677817767166"
        "71676178726717",
    "7000000175690566466555057817571571075705015757757057795755"
        "55657156756655",
    "1275552191113212300012030439187146164646146646466749494799",
    "1542725565216523985789236956265265265235675811949404040041",
    "1456427555219115346513212300075341203043918714616464614664"
        "64667494947990",
    "1712787255652165239672857892369562652652652356758119494040"
        "40041670216363"
};
74

R
Rich Salz 已提交
75
static int fbytes(unsigned char *buf, int num)
76
{
R
Rich Salz 已提交
77
    int ret = 0;
78 79 80 81 82 83 84 85 86
    BIGNUM *tmp = NULL;

    if (use_fake == 0)
        return old_rand->bytes(buf, num);

    use_fake = 0;

    if (fbytes_counter >= 8)
        return 0;
R
Rich Salz 已提交
87
    if (!TEST_ptr(tmp = BN_new()))
88
        return 0;
R
Rich Salz 已提交
89
    if (!TEST_true(BN_dec2bn(&tmp, numbers[fbytes_counter]))) {
90 91 92 93
        BN_free(tmp);
        return 0;
    }
    fbytes_counter++;
R
Rich Salz 已提交
94 95
    if (TEST_int_eq(BN_num_bytes(tmp), num)
            && TEST_true(BN_bn2bin(tmp, buf)))
96 97 98 99
        ret = 1;
    BN_free(tmp);
    return ret;
}
100 101

/* some tests from the X9.62 draft */
R
Rich Salz 已提交
102
static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
103 104 105 106 107
{
    int ret = 0;
    const char message[] = "abc";
    unsigned char digest[20];
    unsigned int dgst_len = 0;
R
Rich Salz 已提交
108
    EVP_MD_CTX *md_ctx;
109 110 111 112
    EC_KEY *key = NULL;
    ECDSA_SIG *signature = NULL;
    BIGNUM *r = NULL, *s = NULL;
    BIGNUM *kinv = NULL, *rp = NULL;
113
    const BIGNUM *sig_r, *sig_s;
114

R
Rich Salz 已提交
115
    if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
116 117
        goto x962_int_err;

118
    /* get the message digest */
R
Rich Salz 已提交
119 120 121
    if (!TEST_true(EVP_DigestInit(md_ctx, EVP_sha1()))
        || !TEST_true(EVP_DigestUpdate(md_ctx, (const void *)message, 3))
        || !TEST_true(EVP_DigestFinal(md_ctx, digest, &dgst_len)))
122 123
        goto x962_int_err;

R
Rich Salz 已提交
124 125
    TEST_info("testing %s", OBJ_nid2sn(nid));

126
    /* create the key */
R
Rich Salz 已提交
127
    if (!TEST_ptr(key = EC_KEY_new_by_curve_name(nid)))
128 129
        goto x962_int_err;
    use_fake = 1;
R
Rich Salz 已提交
130
    if (!TEST_true(EC_KEY_generate_key(key)))
131
        goto x962_int_err;
R
Rich Salz 已提交
132

133 134 135
    /* create the signature */
    use_fake = 1;
    /* Use ECDSA_sign_setup to avoid use of ECDSA nonces */
R
Rich Salz 已提交
136
    if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)))
137
        goto x962_int_err;
R
Rich Salz 已提交
138
    if (!TEST_ptr(signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key)))
139
        goto x962_int_err;
R
Rich Salz 已提交
140

141
    /* compare the created signature with the expected signature */
R
Rich Salz 已提交
142
    if (!TEST_ptr(r = BN_new()) || !TEST_ptr(s = BN_new()))
143
        goto x962_int_err;
R
Rich Salz 已提交
144
    if (!TEST_true(BN_dec2bn(&r, r_in)) || !TEST_true(BN_dec2bn(&s, s_in)))
145
        goto x962_int_err;
146
    ECDSA_SIG_get0(signature, &sig_r, &sig_s);
147 148
    if (!TEST_BN_eq(sig_r, r)
            || !TEST_BN_eq(sig_s, s))
149
        goto x962_int_err;
R
Rich Salz 已提交
150

151
    /* verify the signature */
R
Rich Salz 已提交
152
    if (!TEST_int_eq(ECDSA_do_verify(digest, 20, signature, key), 1))
153 154 155
        goto x962_int_err;

    ret = 1;
R
Rich Salz 已提交
156

157
 x962_int_err:
R
Rich Salz 已提交
158
    EC_KEY_free(key);
R
Rich Salz 已提交
159
    ECDSA_SIG_free(signature);
R
Rich Salz 已提交
160 161
    BN_free(r);
    BN_free(s);
162
    EVP_MD_CTX_free(md_ctx);
R
Rich Salz 已提交
163 164
    BN_clear_free(kinv);
    BN_clear_free(rp);
165 166
    return ret;
}
B
Bodo Möller 已提交
167

168
static int x9_62_tests(void)
169 170 171 172 173 174 175
{
    int ret = 0;

    /* set own rand method */
    if (!change_rand())
        goto x962_err;

R
Rich Salz 已提交
176 177 178
    if (!TEST_true(x9_62_test_internal(NID_X9_62_prime192v1,
                 "3342403536405981729393488334694600415596881826869351677613",
                 "5735822328888155254683894997897571951568553642892029982342")))
179
        goto x962_err;
R
Rich Salz 已提交
180 181
    if (!TEST_true(x9_62_test_internal(NID_X9_62_prime239v1,
                 "3086361431751678114926225473006680188549593787585317781474"
182
                             "62058306432176",
R
Rich Salz 已提交
183 184
                 "3238135532097973577080787768312505059318910517550078427819"
                             "78505179448783")))
185
        goto x962_err;
R
Rich Salz 已提交
186

187
# ifndef OPENSSL_NO_EC2M
R
Rich Salz 已提交
188 189 190
    if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb191v1,
                 "87194383164871543355722284926904419997237591535066528048",
                 "308992691965804947361541664549085895292153777025772063598")))
191
        goto x962_err;
R
Rich Salz 已提交
192 193
    if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb239v1,
                 "2159633321041961198501834003903461262881815148684178964245"
194
                             "5876922391552",
R
Rich Salz 已提交
195 196
                 "1970303740007316867383349976549972270528498040721988191026"
                             "49413465737174")))
197 198 199
        goto x962_err;
# endif
    ret = 1;
R
Rich Salz 已提交
200

201
 x962_err:
R
Rich Salz 已提交
202
    if (!TEST_true(restore_rand()))
203 204 205
        ret = 0;
    return ret;
}
206

R
Rich Salz 已提交
207
static int test_builtin(void)
208 209 210 211 212
{
    EC_builtin_curve *curves = NULL;
    size_t crv_len = 0, n = 0;
    EC_KEY *eckey = NULL, *wrong_eckey = NULL;
    EC_GROUP *group;
213
    ECDSA_SIG *ecdsa_sig = NULL, *modified_sig = NULL;
214 215 216 217 218
    unsigned char digest[20], wrong_digest[20];
    unsigned char *signature = NULL;
    const unsigned char *sig_ptr;
    unsigned char *sig_ptr2;
    unsigned char *raw_buf = NULL;
219 220 221
    const BIGNUM *sig_r, *sig_s;
    BIGNUM *modified_r = NULL, *modified_s = NULL;
    BIGNUM *unmodified_r = NULL, *unmodified_s = NULL;
222 223 224 225
    unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
    int nid, ret = 0;

    /* fill digest values with some random data */
R
Rich Salz 已提交
226 227
    if (!TEST_true(RAND_bytes(digest, 20))
            || !TEST_true(RAND_bytes(wrong_digest, 20)))
228 229
        goto builtin_err;

R
Rich Salz 已提交
230
    /* create and verify a ecdsa signature with every available curve */
231 232
    /* get a list of all internal curves */
    crv_len = EC_get_builtin_curves(NULL, 0);
R
Rich Salz 已提交
233 234
    if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
            || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
235 236 237 238 239 240 241
        goto builtin_err;

    /* now create and verify a signature for every curve */
    for (n = 0; n < crv_len; n++) {
        unsigned char dirt, offset;

        nid = curves[n].nid;
M
Matt Caswell 已提交
242
        if (nid == NID_ipsec4)
243 244
            continue;
        /* create new ecdsa key (== EC_KEY) */
R
Rich Salz 已提交
245 246 247
        if (!TEST_ptr(eckey = EC_KEY_new())
                || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
                || !TEST_true(EC_KEY_set_group(eckey, group)))
248 249 250
            goto builtin_err;
        EC_GROUP_free(group);
        degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
R
Rich Salz 已提交
251
        if (degree < 160) {
252 253 254 255 256
            /* drop the curve */
            EC_KEY_free(eckey);
            eckey = NULL;
            continue;
        }
R
Rich Salz 已提交
257 258
        TEST_info("testing %s", OBJ_nid2sn(nid));

259
        /* create key */
R
Rich Salz 已提交
260
        if (!TEST_true(EC_KEY_generate_key(eckey)))
261 262
            goto builtin_err;
        /* create second key */
R
Rich Salz 已提交
263 264 265
        if (!TEST_ptr(wrong_eckey = EC_KEY_new())
                || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
                || !TEST_true(EC_KEY_set_group(wrong_eckey, group)))
266 267
            goto builtin_err;
        EC_GROUP_free(group);
R
Rich Salz 已提交
268
        if (!TEST_true(EC_KEY_generate_key(wrong_eckey)))
269 270 271
            goto builtin_err;

        /* check key */
R
Rich Salz 已提交
272
        if (!TEST_true(EC_KEY_check_key(eckey)))
273
            goto builtin_err;
R
Rich Salz 已提交
274

275 276
        /* create signature */
        sig_len = ECDSA_size(eckey);
R
Rich Salz 已提交
277 278 279
        if (!TEST_ptr(signature = OPENSSL_malloc(sig_len))
                || !TEST_true(ECDSA_sign(0, digest, 20, signature, &sig_len,
                                         eckey)))
280
            goto builtin_err;
R
Rich Salz 已提交
281

282
        /* verify signature */
R
Rich Salz 已提交
283 284
        if (!TEST_int_eq(ECDSA_verify(0, digest, 20, signature, sig_len,
                                      eckey), 1))
285
            goto builtin_err;
R
Rich Salz 已提交
286

287
        /* verify signature with the wrong key */
R
Rich Salz 已提交
288 289
        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature, sig_len,
                                      wrong_eckey), 1))
290
            goto builtin_err;
R
Rich Salz 已提交
291

292
        /* wrong digest */
R
Rich Salz 已提交
293 294
        if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, 20, signature,
                                      sig_len, eckey), 1))
295
            goto builtin_err;
R
Rich Salz 已提交
296

297
        /* wrong length */
R
Rich Salz 已提交
298 299
        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature,
                                      sig_len - 1, eckey), 1))
300 301 302 303 304 305 306 307
            goto builtin_err;

        /*
         * Modify a single byte of the signature: to ensure we don't garble
         * the ASN1 structure, we read the raw signature and modify a byte in
         * one of the bignums directly.
         */
        sig_ptr = signature;
R
Rich Salz 已提交
308
        if (!TEST_ptr(ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)))
309 310
            goto builtin_err;

311
        ECDSA_SIG_get0(ecdsa_sig, &sig_r, &sig_s);
312

313
        /* Store the two BIGNUMs in raw_buf. */
314 315
        r_len = BN_num_bytes(sig_r);
        s_len = BN_num_bytes(sig_s);
316
        bn_len = (degree + 7) / 8;
R
Rich Salz 已提交
317 318
        if (!TEST_false(r_len > bn_len)
                || !TEST_false(s_len > bn_len))
319 320
            goto builtin_err;
        buf_len = 2 * bn_len;
R
Rich Salz 已提交
321
        if (!TEST_ptr(raw_buf = OPENSSL_zalloc(buf_len)))
322
            goto builtin_err;
323 324
        BN_bn2bin(sig_r, raw_buf + bn_len - r_len);
        BN_bn2bin(sig_s, raw_buf + buf_len - s_len);
325 326 327 328 329

        /* Modify a single byte in the buffer. */
        offset = raw_buf[10] % buf_len;
        dirt = raw_buf[11] ? raw_buf[11] : 1;
        raw_buf[offset] ^= dirt;
R
Rich Salz 已提交
330

331
        /* Now read the BIGNUMs back in from raw_buf. */
R
Rich Salz 已提交
332
        if (!TEST_ptr(modified_sig = ECDSA_SIG_new()))
333
            goto builtin_err;
R
Rich Salz 已提交
334 335 336 337 338
        if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))
                || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,
                                                    bn_len, NULL))
                || !TEST_true(ECDSA_SIG_set0(modified_sig,
                                             modified_r, modified_s))) {
339 340 341 342
            BN_free(modified_r);
            BN_free(modified_s);
            goto builtin_err;
        }
343
        sig_ptr2 = signature;
344
        sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
R
Rich Salz 已提交
345
        if (!TEST_false(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
346
            goto builtin_err;
R
Rich Salz 已提交
347 348

        /* Sanity check: undo the modification and verify signature. */
349
        raw_buf[offset] ^= dirt;
R
Rich Salz 已提交
350 351 352 353 354
        if (!TEST_ptr(unmodified_r = BN_bin2bn(raw_buf, bn_len, NULL))
                || !TEST_ptr(unmodified_s = BN_bin2bn(raw_buf + bn_len,
                                                       bn_len, NULL))
                || !TEST_true(ECDSA_SIG_set0(modified_sig, unmodified_r,
                                             unmodified_s))) {
355 356
            BN_free(unmodified_r);
            BN_free(unmodified_s);
357
            goto builtin_err;
358
        }
359 360

        sig_ptr2 = signature;
361
        sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
R
Rich Salz 已提交
362
        if (!TEST_true(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
363 364 365 366 367 368 369 370 371 372 373 374
            goto builtin_err;

        /* cleanup */
        ERR_clear_error();
        OPENSSL_free(signature);
        signature = NULL;
        EC_KEY_free(eckey);
        eckey = NULL;
        EC_KEY_free(wrong_eckey);
        wrong_eckey = NULL;
        ECDSA_SIG_free(ecdsa_sig);
        ecdsa_sig = NULL;
375 376
        ECDSA_SIG_free(modified_sig);
        modified_sig = NULL;
377 378 379 380 381 382
        OPENSSL_free(raw_buf);
        raw_buf = NULL;
    }

    ret = 1;
 builtin_err:
R
Rich Salz 已提交
383 384
    EC_KEY_free(eckey);
    EC_KEY_free(wrong_eckey);
R
Rich Salz 已提交
385
    ECDSA_SIG_free(ecdsa_sig);
386
    ECDSA_SIG_free(modified_sig);
R
Rich Salz 已提交
387 388 389
    OPENSSL_free(signature);
    OPENSSL_free(raw_buf);
    OPENSSL_free(curves);
390 391 392

    return ret;
}
393
#endif
B
Bodo Möller 已提交
394

395
int setup_tests(void)
396
{
P
Pauli 已提交
397 398 399
#ifdef OPENSSL_NO_EC
    TEST_note("Elliptic curves are disabled.");
#else
R
Rich Salz 已提交
400 401
    ADD_TEST(x9_62_tests);
    ADD_TEST(test_builtin);
P
Pauli 已提交
402
#endif
403
    return 1;
404
}