]>
Commit | Line | Data |
---|---|---|
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 | 23 | const 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, | |
32ab57cb SL |
28 | ossl_ec_GFp_simple_group_init, |
29 | ossl_ec_GFp_simple_group_finish, | |
30 | ossl_ec_GFp_simple_group_clear_finish, | |
31 | ossl_ec_GFp_nist_group_copy, | |
32 | ossl_ec_GFp_nist_group_set_curve, | |
33 | ossl_ec_GFp_simple_group_get_curve, | |
34 | ossl_ec_GFp_simple_group_get_degree, | |
35 | ossl_ec_group_simple_order_bits, | |
36 | ossl_ec_GFp_simple_group_check_discriminant, | |
37 | ossl_ec_GFp_simple_point_init, | |
38 | ossl_ec_GFp_simple_point_finish, | |
39 | ossl_ec_GFp_simple_point_clear_finish, | |
40 | ossl_ec_GFp_simple_point_copy, | |
41 | ossl_ec_GFp_simple_point_set_to_infinity, | |
42 | ossl_ec_GFp_simple_point_set_affine_coordinates, | |
43 | ossl_ec_GFp_simple_point_get_affine_coordinates, | |
0f113f3e | 44 | 0, 0, 0, |
32ab57cb SL |
45 | ossl_ec_GFp_simple_add, |
46 | ossl_ec_GFp_simple_dbl, | |
47 | ossl_ec_GFp_simple_invert, | |
48 | ossl_ec_GFp_simple_is_at_infinity, | |
49 | ossl_ec_GFp_simple_is_on_curve, | |
50 | ossl_ec_GFp_simple_cmp, | |
51 | ossl_ec_GFp_simple_make_affine, | |
52 | ossl_ec_GFp_simple_points_make_affine, | |
0f113f3e MC |
53 | 0 /* mul */ , |
54 | 0 /* precompute_mult */ , | |
55 | 0 /* have_precompute_mult */ , | |
32ab57cb SL |
56 | ossl_ec_GFp_nist_field_mul, |
57 | ossl_ec_GFp_nist_field_sqr, | |
0f113f3e | 58 | 0 /* field_div */ , |
32ab57cb | 59 | ossl_ec_GFp_simple_field_inv, |
0f113f3e MC |
60 | 0 /* field_encode */ , |
61 | 0 /* field_decode */ , | |
9ff9bccc | 62 | 0, /* field_set_to_one */ |
32ab57cb SL |
63 | ossl_ec_key_simple_priv2oct, |
64 | ossl_ec_key_simple_oct2priv, | |
9ff9bccc | 65 | 0, /* set private */ |
32ab57cb SL |
66 | ossl_ec_key_simple_generate_key, |
67 | ossl_ec_key_simple_check_key, | |
68 | ossl_ec_key_simple_generate_public_key, | |
9ff9bccc DSH |
69 | 0, /* keycopy */ |
70 | 0, /* keyfinish */ | |
32ab57cb SL |
71 | ossl_ecdh_simple_compute_key, |
72 | ossl_ecdsa_simple_sign_setup, | |
73 | ossl_ecdsa_simple_sign_sig, | |
74 | ossl_ecdsa_simple_verify_sig, | |
f667820c | 75 | 0, /* field_inverse_mod_ord */ |
32ab57cb SL |
76 | ossl_ec_GFp_simple_blind_coordinates, |
77 | ossl_ec_GFp_simple_ladder_pre, | |
78 | ossl_ec_GFp_simple_ladder_step, | |
79 | ossl_ec_GFp_simple_ladder_post | |
0f113f3e MC |
80 | }; |
81 | ||
82 | return &ret; | |
83 | } | |
60428dbf | 84 | |
32ab57cb | 85 | int ossl_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 | |
32ab57cb | 89 | return ossl_ec_GFp_simple_group_copy(dest, src); |
0f113f3e | 90 | } |
5c6bf031 | 91 | |
32ab57cb SL |
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) | |
0f113f3e MC |
95 | { |
96 | int ret = 0; | |
97 | BN_CTX *new_ctx = NULL; | |
98 | ||
99 | if (ctx == NULL) | |
a9612d6c | 100 | if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
101 | return 0; |
102 | ||
103 | BN_CTX_start(ctx); | |
104 | ||
105 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) | |
106 | group->field_mod_func = BN_nist_mod_192; | |
107 | else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0) | |
108 | group->field_mod_func = BN_nist_mod_224; | |
109 | else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0) | |
110 | group->field_mod_func = BN_nist_mod_256; | |
111 | else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0) | |
112 | group->field_mod_func = BN_nist_mod_384; | |
113 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) | |
114 | group->field_mod_func = BN_nist_mod_521; | |
115 | else { | |
9311d0c4 | 116 | ERR_raise(ERR_LIB_EC, EC_R_NOT_A_NIST_PRIME); |
0f113f3e MC |
117 | goto err; |
118 | } | |
119 | ||
32ab57cb | 120 | ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx); |
5c6bf031 BM |
121 | |
122 | err: | |
0f113f3e | 123 | BN_CTX_end(ctx); |
23a1d5e9 | 124 | BN_CTX_free(new_ctx); |
0f113f3e MC |
125 | return ret; |
126 | } | |
a2dbcf36 | 127 | |
32ab57cb SL |
128 | int ossl_ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
129 | const BIGNUM *b, BN_CTX *ctx) | |
0f113f3e MC |
130 | { |
131 | int ret = 0; | |
132 | BN_CTX *ctx_new = NULL; | |
133 | ||
134 | if (!group || !r || !a || !b) { | |
9311d0c4 | 135 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
136 | goto err; |
137 | } | |
138 | if (!ctx) | |
a9612d6c | 139 | if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
140 | goto err; |
141 | ||
142 | if (!BN_mul(r, a, b, ctx)) | |
143 | goto err; | |
144 | if (!group->field_mod_func(r, r, group->field, ctx)) | |
145 | goto err; | |
146 | ||
147 | ret = 1; | |
148 | err: | |
23a1d5e9 | 149 | BN_CTX_free(ctx_new); |
0f113f3e MC |
150 | return ret; |
151 | } | |
a2dbcf36 | 152 | |
32ab57cb SL |
153 | int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
154 | BN_CTX *ctx) | |
0f113f3e MC |
155 | { |
156 | int ret = 0; | |
157 | BN_CTX *ctx_new = NULL; | |
158 | ||
159 | if (!group || !r || !a) { | |
9311d0c4 | 160 | ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
161 | goto err; |
162 | } | |
163 | if (!ctx) | |
a9612d6c | 164 | if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL) |
0f113f3e MC |
165 | goto err; |
166 | ||
167 | if (!BN_sqr(r, a, ctx)) | |
168 | goto err; | |
169 | if (!group->field_mod_func(r, r, group->field, ctx)) | |
170 | goto err; | |
171 | ||
172 | ret = 1; | |
173 | err: | |
23a1d5e9 | 174 | BN_CTX_free(ctx_new); |
0f113f3e MC |
175 | return ret; |
176 | } |