]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecp_nist.c
Deprecate the ECDSA and EV_KEY_METHOD functions.
[thirdparty/openssl.git] / crypto / ec / ecp_nist.c
CommitLineData
5c6bf031 1/*
fd38836b 2 * Copyright 2001-2018 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,
42 ec_GFp_simple_set_Jprojective_coordinates_GFp,
43 ec_GFp_simple_get_Jprojective_coordinates_GFp,
44 ec_GFp_simple_point_set_affine_coordinates,
45 ec_GFp_simple_point_get_affine_coordinates,
46 0, 0, 0,
47 ec_GFp_simple_add,
48 ec_GFp_simple_dbl,
49 ec_GFp_simple_invert,
50 ec_GFp_simple_is_at_infinity,
51 ec_GFp_simple_is_on_curve,
52 ec_GFp_simple_cmp,
53 ec_GFp_simple_make_affine,
54 ec_GFp_simple_points_make_affine,
55 0 /* mul */ ,
56 0 /* precompute_mult */ ,
57 0 /* have_precompute_mult */ ,
58 ec_GFp_nist_field_mul,
59 ec_GFp_nist_field_sqr,
60 0 /* field_div */ ,
e0033efc 61 ec_GFp_simple_field_inv,
0f113f3e
MC
62 0 /* field_encode */ ,
63 0 /* field_decode */ ,
9ff9bccc
DSH
64 0, /* field_set_to_one */
65 ec_key_simple_priv2oct,
66 ec_key_simple_oct2priv,
67 0, /* set private */
68 ec_key_simple_generate_key,
69 ec_key_simple_check_key,
70 ec_key_simple_generate_public_key,
71 0, /* keycopy */
72 0, /* keyfinish */
f667820c 73 ecdh_simple_compute_key,
9bf682f6
PS
74 ecdsa_simple_sign_setup,
75 ecdsa_simple_sign_sig,
76 ecdsa_simple_verify_sig,
f667820c 77 0, /* field_inverse_mod_ord */
37124360 78 ec_GFp_simple_blind_coordinates,
9d91530d
BB
79 ec_GFp_simple_ladder_pre,
80 ec_GFp_simple_ladder_step,
81 ec_GFp_simple_ladder_post
0f113f3e
MC
82 };
83
84 return &ret;
85}
60428dbf 86
e2c9c91b 87int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
0f113f3e
MC
88{
89 dest->field_mod_func = src->field_mod_func;
e2c9c91b 90
0f113f3e
MC
91 return ec_GFp_simple_group_copy(dest, src);
92}
5c6bf031
BM
93
94int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
0f113f3e
MC
95 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
96{
97 int ret = 0;
98 BN_CTX *new_ctx = NULL;
99
100 if (ctx == NULL)
a9612d6c 101 if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
102 return 0;
103
104 BN_CTX_start(ctx);
105
106 if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0)
107 group->field_mod_func = BN_nist_mod_192;
108 else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0)
109 group->field_mod_func = BN_nist_mod_224;
110 else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0)
111 group->field_mod_func = BN_nist_mod_256;
112 else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0)
113 group->field_mod_func = BN_nist_mod_384;
114 else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0)
115 group->field_mod_func = BN_nist_mod_521;
116 else {
117 ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
118 goto err;
119 }
120
121 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
5c6bf031
BM
122
123 err:
0f113f3e 124 BN_CTX_end(ctx);
23a1d5e9 125 BN_CTX_free(new_ctx);
0f113f3e
MC
126 return ret;
127}
a2dbcf36 128
5c6bf031 129int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
0f113f3e
MC
130 const BIGNUM *b, BN_CTX *ctx)
131{
132 int ret = 0;
133 BN_CTX *ctx_new = NULL;
134
135 if (!group || !r || !a || !b) {
136 ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
137 goto err;
138 }
139 if (!ctx)
a9612d6c 140 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
141 goto err;
142
143 if (!BN_mul(r, a, b, ctx))
144 goto err;
145 if (!group->field_mod_func(r, r, group->field, ctx))
146 goto err;
147
148 ret = 1;
149 err:
23a1d5e9 150 BN_CTX_free(ctx_new);
0f113f3e
MC
151 return ret;
152}
a2dbcf36 153
5c6bf031 154int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
0f113f3e
MC
155 BN_CTX *ctx)
156{
157 int ret = 0;
158 BN_CTX *ctx_new = NULL;
159
160 if (!group || !r || !a) {
161 ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
162 goto err;
163 }
164 if (!ctx)
a9612d6c 165 if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
0f113f3e
MC
166 goto err;
167
168 if (!BN_sqr(r, a, ctx))
169 goto err;
170 if (!group->field_mod_func(r, r, group->field, ctx))
171 goto err;
172
173 ret = 1;
174 err:
23a1d5e9 175 BN_CTX_free(ctx_new);
0f113f3e
MC
176 return ret;
177}