]>
Commit | Line | Data |
---|---|---|
38e3c581 | 1 | /* crypto/ec/ecp_nist.c */ |
5c6bf031 BM |
2 | /* |
3 | * Written by Nils Larsch for the OpenSSL project. | |
4 | */ | |
38e3c581 | 5 | /* ==================================================================== |
5c6bf031 | 6 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
38e3c581 BM |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * openssl-core@openssl.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
7793f30e BM |
58 | /* ==================================================================== |
59 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
60 | * Portions of this software developed by SUN MICROSYSTEMS, INC., | |
61 | * and contributed to the OpenSSL project. | |
62 | */ | |
38e3c581 | 63 | |
5c6bf031 BM |
64 | #include <openssl/err.h> |
65 | #include <openssl/obj_mac.h> | |
38e3c581 | 66 | #include "ec_lcl.h" |
0657bf9c | 67 | |
0657bf9c BM |
68 | const EC_METHOD *EC_GFp_nist_method(void) |
69 | { | |
58fc6229 | 70 | static const EC_METHOD ret = { |
5c6bf031 | 71 | NID_X9_62_prime_field, |
f1f25544 | 72 | ec_GFp_nist_group_init, |
2e0db076 BM |
73 | ec_GFp_nist_group_finish, |
74 | ec_GFp_nist_group_clear_finish, | |
60428dbf | 75 | ec_GFp_nist_group_copy, |
35b73a1f BM |
76 | ec_GFp_nist_group_set_curve, |
77 | ec_GFp_simple_group_get_curve, | |
7793f30e | 78 | ec_GFp_simple_group_get_degree, |
17d6bb81 | 79 | ec_GFp_simple_group_check_discriminant, |
58fc6229 BM |
80 | ec_GFp_simple_point_init, |
81 | ec_GFp_simple_point_finish, | |
82 | ec_GFp_simple_point_clear_finish, | |
83 | ec_GFp_simple_point_copy, | |
226cc7de | 84 | ec_GFp_simple_point_set_to_infinity, |
1d5bd6cf BM |
85 | ec_GFp_simple_set_Jprojective_coordinates_GFp, |
86 | ec_GFp_simple_get_Jprojective_coordinates_GFp, | |
35b73a1f BM |
87 | ec_GFp_simple_point_set_affine_coordinates, |
88 | ec_GFp_simple_point_get_affine_coordinates, | |
89 | ec_GFp_simple_set_compressed_coordinates, | |
58fc6229 BM |
90 | ec_GFp_simple_point2oct, |
91 | ec_GFp_simple_oct2point, | |
92 | ec_GFp_simple_add, | |
93 | ec_GFp_simple_dbl, | |
1d5bd6cf | 94 | ec_GFp_simple_invert, |
7793f30e BM |
95 | 0 /* mul */, |
96 | 0 /* precompute_mult */, | |
58fc6229 BM |
97 | ec_GFp_simple_is_at_infinity, |
98 | ec_GFp_simple_is_on_curve, | |
1d5bd6cf | 99 | ec_GFp_simple_cmp, |
58fc6229 | 100 | ec_GFp_simple_make_affine, |
48fe4d62 | 101 | ec_GFp_simple_points_make_affine, |
60428dbf | 102 | ec_GFp_nist_field_mul, |
58fc6229 | 103 | ec_GFp_nist_field_sqr, |
7793f30e | 104 | 0 /* field_div */, |
156e8557 | 105 | 0 /* field_encode */, |
48fe4d62 BM |
106 | 0 /* field_decode */, |
107 | 0 /* field_set_to_one */ }; | |
0657bf9c BM |
108 | |
109 | return &ret; | |
110 | } | |
60428dbf | 111 | |
5c6bf031 BM |
112 | #if BN_BITS2 == 64 && UINT_MAX != 4294967295UL && ULONG_MAX != 4294967295UL |
113 | #define NO_32_BIT_TYPE | |
114 | #endif | |
60428dbf BM |
115 | |
116 | int ec_GFp_nist_group_init(EC_GROUP *group) | |
117 | { | |
118 | int ok; | |
119 | ||
120 | ok = ec_GFp_simple_group_init(group); | |
48fe4d62 | 121 | group->field_data1 = NULL; |
60428dbf BM |
122 | return ok; |
123 | } | |
124 | ||
5c6bf031 BM |
125 | void ec_GFp_nist_group_finish(EC_GROUP *group) |
126 | { | |
127 | BN_free(&group->field); | |
128 | BN_free(&group->a); | |
129 | BN_free(&group->b); | |
130 | } | |
131 | ||
132 | ||
133 | void ec_GFp_nist_group_clear_finish(EC_GROUP *group) | |
134 | { | |
135 | BN_clear_free(&group->field); | |
136 | BN_clear_free(&group->a); | |
137 | BN_clear_free(&group->b); | |
138 | } | |
139 | ||
140 | ||
141 | int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p, | |
142 | const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | |
143 | { | |
144 | int ret = 0; | |
145 | BN_CTX *new_ctx = NULL; | |
146 | BIGNUM *tmp_bn; | |
147 | ||
148 | if (ctx == NULL) | |
149 | if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0; | |
150 | ||
151 | BN_CTX_start(ctx); | |
152 | if ((tmp_bn = BN_CTX_get(ctx)) == NULL) goto err; | |
153 | ||
154 | if (BN_ucmp(BN_get0_nist_prime_192(), p) == 0) | |
19b8d06a | 155 | group->field_mod_func = BN_nist_mod_192; |
5c6bf031 | 156 | else if (BN_ucmp(BN_get0_nist_prime_224(), p) == 0) |
62dd6f16 | 157 | { |
19b8d06a BM |
158 | #if !defined(NO_32_BIT_TYPE) || defined(OPENSSL_NO_ASM) |
159 | group->field_mod_func = BN_nist_mod_224; | |
5c6bf031 | 160 | #else |
19b8d06a | 161 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE_GFP, EC_R_NOT_A_SUPPORTED_NIST_PRIME); |
5c6bf031 BM |
162 | goto err; |
163 | #endif | |
62dd6f16 | 164 | } |
5c6bf031 | 165 | else if (BN_ucmp(BN_get0_nist_prime_256(), p) == 0) |
62dd6f16 | 166 | { |
19b8d06a BM |
167 | #if !defined(NO_32_BIT_TYPE) || defined(OPENSSL_NO_ASM) |
168 | group->field_mod_func = BN_nist_mod_256; | |
5c6bf031 | 169 | #else |
19b8d06a | 170 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE_GFP, EC_R_NOT_A_SUPPORTED_NIST_PRIME); |
5c6bf031 BM |
171 | goto err; |
172 | #endif | |
62dd6f16 | 173 | } |
5c6bf031 | 174 | else if (BN_ucmp(BN_get0_nist_prime_384(), p) == 0) |
62dd6f16 | 175 | { |
19b8d06a BM |
176 | #if !defined(NO_32_BIT_TYPE) || defined(OPENSSL_NO_ASM) |
177 | group->field_mod_func = BN_nist_mod_384; | |
5c6bf031 | 178 | #else |
19b8d06a | 179 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE_GFP, EC_R_NOT_A_SUPPORTED_NIST_PRIME); |
5c6bf031 BM |
180 | goto err; |
181 | #endif | |
62dd6f16 | 182 | } |
5c6bf031 | 183 | else if (BN_ucmp(BN_get0_nist_prime_521(), p) == 0) |
19b8d06a BM |
184 | /* this one works in the NO_32_BIT_TYPE case */ |
185 | group->field_mod_func = BN_nist_mod_521; | |
5c6bf031 BM |
186 | else |
187 | { | |
19b8d06a | 188 | ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE_GFP, EC_R_NOT_A_NIST_PRIME); |
5c6bf031 BM |
189 | goto err; |
190 | } | |
191 | ||
192 | /* group->field */ | |
193 | if (!BN_copy(&group->field, p)) goto err; | |
b53e44e5 | 194 | BN_set_sign(&group->field, 0); |
5c6bf031 BM |
195 | |
196 | /* group->a */ | |
19b8d06a | 197 | if (!group->field_mod_func(&group->a, a, p, ctx)) goto err; |
5c6bf031 BM |
198 | |
199 | /* group->b */ | |
19b8d06a | 200 | if (!group->field_mod_func(&group->b, b, p, ctx)) goto err; |
5c6bf031 BM |
201 | |
202 | /* group->a_is_minus3 */ | |
203 | if (!BN_add_word(tmp_bn, 3)) goto err; | |
204 | group->a_is_minus3 = (0 == BN_cmp(tmp_bn, &group->field)); | |
205 | ||
206 | ret = 1; | |
207 | ||
208 | err: | |
209 | BN_CTX_end(ctx); | |
210 | if (new_ctx != NULL) | |
211 | BN_CTX_free(new_ctx); | |
212 | return ret; | |
213 | } | |
214 | ||
215 | int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src) | |
216 | { | |
217 | if (dest == NULL || src == NULL) | |
218 | return 0; | |
219 | ||
220 | if (!BN_copy(&dest->field, &src->field)) | |
221 | return 0; | |
222 | if (!BN_copy(&dest->a, &src->a)) | |
223 | return 0; | |
224 | if (!BN_copy(&dest->b, &src->b)) | |
225 | return 0; | |
60428dbf | 226 | |
5c6bf031 | 227 | dest->curve_name = src->curve_name; |
60428dbf | 228 | |
5c6bf031 | 229 | dest->a_is_minus3 = src->a_is_minus3; |
60428dbf | 230 | |
5c6bf031 BM |
231 | dest->field_data1 = src->field_data1; |
232 | ||
233 | return 1; | |
234 | } | |
60428dbf | 235 | |
5c6bf031 BM |
236 | int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
237 | const BIGNUM *b, BN_CTX *ctx) | |
238 | { | |
239 | int ret=0; | |
240 | BN_CTX *ctx_new=NULL; | |
60428dbf | 241 | |
5c6bf031 BM |
242 | if (!group || !r || !a || !b) |
243 | { | |
244 | ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER); | |
245 | goto err; | |
246 | } | |
247 | if (!ctx) | |
248 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err; | |
60428dbf | 249 | |
5c6bf031 | 250 | if (!BN_mul(r, a, b, ctx)) goto err; |
19b8d06a | 251 | if (!group->field_mod_func(r, r, &group->field, ctx)) |
5c6bf031 | 252 | goto err; |
60428dbf | 253 | |
5c6bf031 BM |
254 | ret=1; |
255 | err: | |
256 | if (ctx_new) | |
257 | BN_CTX_free(ctx_new); | |
258 | return ret; | |
259 | } | |
60428dbf | 260 | |
5c6bf031 BM |
261 | int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, |
262 | BN_CTX *ctx) | |
263 | { | |
264 | int ret=0; | |
265 | BN_CTX *ctx_new=NULL; | |
60428dbf | 266 | |
5c6bf031 BM |
267 | if (!group || !r || !a) |
268 | { | |
269 | ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER); | |
270 | goto err; | |
271 | } | |
272 | if (!ctx) | |
273 | if ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err; | |
60428dbf | 274 | |
5c6bf031 | 275 | if (!BN_sqr(r, a, ctx)) goto err; |
19b8d06a | 276 | if (!group->field_mod_func(r, r, &group->field, ctx)) |
5c6bf031 | 277 | goto err; |
60428dbf | 278 | |
5c6bf031 BM |
279 | ret=1; |
280 | err: | |
281 | if (ctx_new) | |
282 | BN_CTX_free(ctx_new); | |
283 | return ret; | |
284 | } |