Coverage Report

Created: 2024-11-21 07:03

/src/openssl/crypto/ec/ecp_nist.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the Apache License 2.0 (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
/*
12
 * ECDSA low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <limits.h>
18
19
#include <openssl/err.h>
20
#include <openssl/obj_mac.h>
21
#include "ec_local.h"
22
23
const EC_METHOD *EC_GFp_nist_method(void)
24
521
{
25
521
    static const EC_METHOD ret = {
26
521
        EC_FLAGS_DEFAULT_OCT,
27
521
        NID_X9_62_prime_field,
28
521
        ossl_ec_GFp_simple_group_init,
29
521
        ossl_ec_GFp_simple_group_finish,
30
521
        ossl_ec_GFp_simple_group_clear_finish,
31
521
        ossl_ec_GFp_nist_group_copy,
32
521
        ossl_ec_GFp_nist_group_set_curve,
33
521
        ossl_ec_GFp_simple_group_get_curve,
34
521
        ossl_ec_GFp_simple_group_get_degree,
35
521
        ossl_ec_group_simple_order_bits,
36
521
        ossl_ec_GFp_simple_group_check_discriminant,
37
521
        ossl_ec_GFp_simple_point_init,
38
521
        ossl_ec_GFp_simple_point_finish,
39
521
        ossl_ec_GFp_simple_point_clear_finish,
40
521
        ossl_ec_GFp_simple_point_copy,
41
521
        ossl_ec_GFp_simple_point_set_to_infinity,
42
521
        ossl_ec_GFp_simple_point_set_affine_coordinates,
43
521
        ossl_ec_GFp_simple_point_get_affine_coordinates,
44
521
        0, 0, 0,
45
521
        ossl_ec_GFp_simple_add,
46
521
        ossl_ec_GFp_simple_dbl,
47
521
        ossl_ec_GFp_simple_invert,
48
521
        ossl_ec_GFp_simple_is_at_infinity,
49
521
        ossl_ec_GFp_simple_is_on_curve,
50
521
        ossl_ec_GFp_simple_cmp,
51
521
        ossl_ec_GFp_simple_make_affine,
52
521
        ossl_ec_GFp_simple_points_make_affine,
53
521
        0 /* mul */ ,
54
521
        0 /* precompute_mult */ ,
55
521
        0 /* have_precompute_mult */ ,
56
521
        ossl_ec_GFp_nist_field_mul,
57
521
        ossl_ec_GFp_nist_field_sqr,
58
521
        0 /* field_div */ ,
59
521
        ossl_ec_GFp_simple_field_inv,
60
521
        0 /* field_encode */ ,
61
521
        0 /* field_decode */ ,
62
521
        0,                      /* field_set_to_one */
63
521
        ossl_ec_key_simple_priv2oct,
64
521
        ossl_ec_key_simple_oct2priv,
65
521
        0, /* set private */
66
521
        ossl_ec_key_simple_generate_key,
67
521
        ossl_ec_key_simple_check_key,
68
521
        ossl_ec_key_simple_generate_public_key,
69
521
        0, /* keycopy */
70
521
        0, /* keyfinish */
71
521
        ossl_ecdh_simple_compute_key,
72
521
        ossl_ecdsa_simple_sign_setup,
73
521
        ossl_ecdsa_simple_sign_sig,
74
521
        ossl_ecdsa_simple_verify_sig,
75
521
        0, /* field_inverse_mod_ord */
76
521
        ossl_ec_GFp_simple_blind_coordinates,
77
521
        ossl_ec_GFp_simple_ladder_pre,
78
521
        ossl_ec_GFp_simple_ladder_step,
79
521
        ossl_ec_GFp_simple_ladder_post
80
521
    };
81
82
521
    return &ret;
83
521
}
84
85
int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
86
477
{
87
477
    dest->field_mod_func = src->field_mod_func;
88
89
477
    return ossl_ec_GFp_simple_group_copy(dest, src);
90
477
}
91
92
int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
93
                                     const BIGNUM *a, const BIGNUM *b,
94
                                     BN_CTX *ctx)
95
521
{
96
521
    int ret = 0;
97
521
    BN_CTX *new_ctx = NULL;
98
99
521
    if (ctx == NULL)
100
0
        if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
101
0
            return 0;
102
103
521
    BN_CTX_start(ctx);
104
105
521
    if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
106
59
        group->field_mod_func = BN_nist_mod_192;
107
462
    else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
108
87
        group->field_mod_func = BN_nist_mod_224;
109
375
    else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
110
161
        group->field_mod_func = BN_nist_mod_256;
111
214
    else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
112
124
        group->field_mod_func = BN_nist_mod_384;
113
90
    else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
114
90
        group->field_mod_func = BN_nist_mod_521;
115
0
    else {
116
0
        ERR_raise(ERR_LIB_EC, EC_R_NOT_A_NIST_PRIME);
117
0
        goto err;
118
0
    }
119
120
521
    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
121
122
521
 err:
123
521
    BN_CTX_end(ctx);
124
521
    BN_CTX_free(new_ctx);
125
521
    return ret;
126
521
}
127
128
int ossl_ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
129
                               const BIGNUM *b, BN_CTX *ctx)
130
1.37M
{
131
1.37M
    int ret = 0;
132
1.37M
    BN_CTX *ctx_new = NULL;
133
134
1.37M
    if (!group || !r || !a || !b) {
135
0
        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
136
0
        goto err;
137
0
    }
138
1.37M
    if (!ctx)
139
0
        if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
140
0
            goto err;
141
142
1.37M
    if (!BN_mul(r, a, b, ctx))
143
0
        goto err;
144
1.37M
    if (!group->field_mod_func(r, r, group->field, ctx))
145
0
        goto err;
146
147
1.37M
    ret = 1;
148
1.37M
 err:
149
1.37M
    BN_CTX_free(ctx_new);
150
1.37M
    return ret;
151
1.37M
}
152
153
int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
154
                               BN_CTX *ctx)
155
737k
{
156
737k
    int ret = 0;
157
737k
    BN_CTX *ctx_new = NULL;
158
159
737k
    if (!group || !r || !a) {
160
0
        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
161
0
        goto err;
162
0
    }
163
737k
    if (!ctx)
164
0
        if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
165
0
            goto err;
166
167
737k
    if (!BN_sqr(r, a, ctx))
168
0
        goto err;
169
737k
    if (!group->field_mod_func(r, r, group->field, ctx))
170
0
        goto err;
171
172
737k
    ret = 1;
173
737k
 err:
174
737k
    BN_CTX_free(ctx_new);
175
737k
    return ret;
176
737k
}