]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecp_nist.c
Update copyright year
[thirdparty/openssl.git] / crypto / ec / ecp_nist.c
CommitLineData
5c6bf031 1/*
33388b44 2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
38e3c581 4 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
38e3c581 9 */
4f22f405 10
579422c8
P
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
37942fab
NL
17#include <limits.h>
18
5c6bf031
BM
19#include <openssl/err.h>
20#include <openssl/obj_mac.h>
706457b7 21#include "ec_local.h"
0657bf9c 22
0657bf9c 23const EC_METHOD *EC_GFp_nist_method(void)
0f113f3e
MC
24{
25 static const EC_METHOD ret = {
26 EC_FLAGS_DEFAULT_OCT,
27 NID_X9_62_prime_field,
28 ec_GFp_simple_group_init,
29 ec_GFp_simple_group_finish,
30 ec_GFp_simple_group_clear_finish,
31 ec_GFp_nist_group_copy,
32 ec_GFp_nist_group_set_curve,
33 ec_GFp_simple_group_get_curve,
34 ec_GFp_simple_group_get_degree,
9ff9bccc 35 ec_group_simple_order_bits,
0f113f3e
MC
36 ec_GFp_simple_group_check_discriminant,
37 ec_GFp_simple_point_init,
38 ec_GFp_simple_point_finish,
39 ec_GFp_simple_point_clear_finish,
40 ec_GFp_simple_point_copy,
41 ec_GFp_simple_point_set_to_infinity,
0f113f3e
MC
42 ec_GFp_simple_point_set_affine_coordinates,
43 ec_GFp_simple_point_get_affine_coordinates,
44 0, 0, 0,
45 ec_GFp_simple_add,
46 ec_GFp_simple_dbl,
47 ec_GFp_simple_invert,
48 ec_GFp_simple_is_at_infinity,
49 ec_GFp_simple_is_on_curve,
50 ec_GFp_simple_cmp,
51 ec_GFp_simple_make_affine,
52 ec_GFp_simple_points_make_affine,
53 0 /* mul */ ,
54 0 /* precompute_mult */ ,
55 0 /* have_precompute_mult */ ,
56 ec_GFp_nist_field_mul,
57 ec_GFp_nist_field_sqr,
58 0 /* field_div */ ,
e0033efc 59 ec_GFp_simple_field_inv,
0f113f3e
MC
60 0 /* field_encode */ ,
61 0 /* field_decode */ ,
9ff9bccc
DSH
62 0, /* field_set_to_one */
63 ec_key_simple_priv2oct,
64 ec_key_simple_oct2priv,
65 0, /* set private */
66 ec_key_simple_generate_key,
67 ec_key_simple_check_key,
68 ec_key_simple_generate_public_key,
69 0, /* keycopy */
70 0, /* keyfinish */
f667820c 71 ecdh_simple_compute_key,
9bf682f6
PS
72 ecdsa_simple_sign_setup,
73 ecdsa_simple_sign_sig,
74 ecdsa_simple_verify_sig,
f667820c 75 0, /* field_inverse_mod_ord */
37124360 76 ec_GFp_simple_blind_coordinates,
9d91530d
BB
77 ec_GFp_simple_ladder_pre,
78 ec_GFp_simple_ladder_step,
79 ec_GFp_simple_ladder_post
0f113f3e
MC
80 };
81
82 return &ret;
83}
60428dbf 84
e2c9c91b 85int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
0f113f3e
MC
86{
87 dest->field_mod_func = src->field_mod_func;
e2c9c91b 88
0f113f3e
MC
89 return ec_GFp_simple_group_copy(dest, src);
90}
5c6bf031
BM
91
92int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
0f113f3e
MC
93 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
94{
95 int ret = 0;
96 BN_CTX *new_ctx = NULL;
97
98 if (ctx == NULL)
a9612d6c 99 if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
100 return 0;
101
102 BN_CTX_start(ctx);
103
104 if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
105 group->field_mod_func = BN_nist_mod_192;
106 else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
107 group->field_mod_func = BN_nist_mod_224;
108 else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
109 group->field_mod_func = BN_nist_mod_256;
110 else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
111 group->field_mod_func = BN_nist_mod_384;
112 else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
113 group->field_mod_func = BN_nist_mod_521;
114 else {
115 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
116 goto err;
117 }
118
119 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
5c6bf031
BM
120
121 err:
0f113f3e 122 BN_CTX_end(ctx);
23a1d5e9 123 BN_CTX_free(new_ctx);
0f113f3e
MC
124 return ret;
125}
a2dbcf36 126
5c6bf031 127int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
0f113f3e
MC
128 const BIGNUM *b, BN_CTX *ctx)
129{
130 int ret = 0;
131 BN_CTX *ctx_new = NULL;
132
133 if (!group || !r || !a || !b) {
134 ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
135 goto err;
136 }
137 if (!ctx)
a9612d6c 138 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
139 goto err;
140
141 if (!BN_mul(r, a, b, ctx))
142 goto err;
143 if (!group->field_mod_func(r, r, group->field, ctx))
144 goto err;
145
146 ret = 1;
147 err:
23a1d5e9 148 BN_CTX_free(ctx_new);
0f113f3e
MC
149 return ret;
150}
a2dbcf36 151
5c6bf031 152int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
0f113f3e
MC
153 BN_CTX *ctx)
154{
155 int ret = 0;
156 BN_CTX *ctx_new = NULL;
157
158 if (!group || !r || !a) {
159 ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
160 goto err;
161 }
162 if (!ctx)
a9612d6c 163 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
164 goto err;
165
166 if (!BN_sqr(r, a, ctx))
167 goto err;
168 if (!group->field_mod_func(r, r, group->field, ctx))
169 goto err;
170
171 ret = 1;
172 err:
23a1d5e9 173 BN_CTX_free(ctx_new);
0f113f3e
MC
174 return ret;
175}