Debian Codegraph @

external/boringssl/crypto/ec/ec_montgomery.c

raw
1 /* Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * [email protected].
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * ([email protected]). This product includes software written by Tim
52 * Hudson ([email protected]).
53 *
54 */
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68 #include <openssl/ec.h>
69
70 #include <openssl/bn.h>
71 #include <openssl/err.h>
72 #include <openssl/mem.h>
73
74 #include "internal.h"
75
76
77 int ec_GFp_mont_group_init(EC_GROUP *group) {
78 int ok;
79
80 ok = ec_GFp_simple_group_init(group);
81 group->mont = NULL;
82 return ok;
83 }
84
85 void ec_GFp_mont_group_finish(EC_GROUP *group) {
86 BN_MONT_CTX_free(group->mont);
87 group->mont = NULL;
88 ec_GFp_simple_group_finish(group);
89 }
90
91 int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
92 BN_MONT_CTX_free(dest->mont);
93 dest->mont = NULL;
94
95 if (!ec_GFp_simple_group_copy(dest, src)) {
96 return 0;
97 }
98
99 if (src->mont != NULL) {
100 dest->mont = BN_MONT_CTX_new();
101 if (dest->mont == NULL) {
102 return 0;
103 }
104 if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
105 goto err;
106 }
107 }
108
109 return 1;
110
111 err:
112 BN_MONT_CTX_free(dest->mont);
113 dest->mont = NULL;
114 return 0;
115 }
116
117 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
118 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
119 BN_CTX *new_ctx = NULL;
120 BN_MONT_CTX *mont = NULL;
121 int ret = 0;
122
123 BN_MONT_CTX_free(group->mont);
124 group->mont = NULL;
125
126 if (ctx == NULL) {
127 ctx = new_ctx = BN_CTX_new();
128 if (ctx == NULL) {
129 return 0;
130 }
131 }
132
133 mont = BN_MONT_CTX_new();
134 if (mont == NULL) {
135 goto err;
136 }
137 if (!BN_MONT_CTX_set(mont, p, ctx)) {
138 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
139 goto err;
140 }
141
142 group->mont = mont;
143 mont = NULL;
144
145 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
146
147 if (!ret) {
148 BN_MONT_CTX_free(group->mont);
149 group->mont = NULL;
150 }
151
152 err:
153 BN_CTX_free(new_ctx);
154 BN_MONT_CTX_free(mont);
155 return ret;
156 }
157
158 int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
159 const BIGNUM *b, BN_CTX *ctx) {
160 if (group->mont == NULL) {
161 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
162 return 0;
163 }
164
165 return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
166 }
167
168 int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
169 BN_CTX *ctx) {
170 if (group->mont == NULL) {
171 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
172 return 0;
173 }
174
175 return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
176 }
177
178 int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
179 BN_CTX *ctx) {
180 if (group->mont == NULL) {
181 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
182 return 0;
183 }
184
185 return BN_to_montgomery(r, a, group->mont, ctx);
186 }
187
188 int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
189 BN_CTX *ctx) {
190 if (group->mont == NULL) {
191 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
192 return 0;
193 }
194
195 return BN_from_montgomery(r, a, group->mont, ctx);
196 }
197
198 static int ec_GFp_mont_check_pub_key_order(const EC_GROUP *group,
199 const EC_POINT* pub_key,
200 BN_CTX *ctx) {
201 EC_POINT *point = EC_POINT_new(group);
202 int ret = 0;
203
204 if (point == NULL ||
205 !ec_wNAF_mul(group, point, NULL, pub_key, EC_GROUP_get0_order(group),
206 ctx) ||
207 !EC_POINT_is_at_infinity(group, point)) {
208 goto err;
209 }
210
211 ret = 1;
212
213 err:
214 EC_POINT_free(point);
215 return ret;
216 }
217
218 static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
219 const EC_POINT *point,
220 BIGNUM *x, BIGNUM *y,
221 BN_CTX *ctx) {
222 if (EC_POINT_is_at_infinity(group, point)) {
223 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
224 return 0;
225 }
226
227 BN_CTX *new_ctx = NULL;
228 if (ctx == NULL) {
229 ctx = new_ctx = BN_CTX_new();
230 if (ctx == NULL) {
231 return 0;
232 }
233 }
234
235 int ret = 0;
236
237 BN_CTX_start(ctx);
238
239 if (BN_cmp(&point->Z, &group->one) == 0) {
240 /* |point| is already affine. */
241 if (x != NULL && !BN_from_montgomery(x, &point->X, group->mont, ctx)) {
242 goto err;
243 }
244 if (y != NULL && !BN_from_montgomery(y, &point->Y, group->mont, ctx)) {
245 goto err;
246 }
247 } else {
248 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
249
250 BIGNUM *Z_1 = BN_CTX_get(ctx);
251 BIGNUM *Z_2 = BN_CTX_get(ctx);
252 BIGNUM *Z_3 = BN_CTX_get(ctx);
253 if (Z_1 == NULL ||
254 Z_2 == NULL ||
255 Z_3 == NULL) {
256 goto err;
257 }
258
259 /* The straightforward way to calculate the inverse of a Montgomery-encoded
260 * value where the result is Montgomery-encoded is:
261 *
262 * |BN_from_montgomery| + |BN_mod_inverse| + |BN_to_montgomery|.
263 *
264 * This is equivalent, but more efficient, because |BN_from_montgomery|
265 * is more efficient (at least in theory) than |BN_to_montgomery|, since it
266 * doesn't have to do the multiplication before the reduction. */
267 if (!BN_from_montgomery(Z_1, &point->Z, group->mont, ctx) ||
268 !BN_from_montgomery(Z_1, Z_1, group->mont, ctx) ||
269 !BN_mod_inverse(Z_1, Z_1, &group->field, ctx)) {
270 goto err;
271 }
272
273 if (!BN_mod_mul_montgomery(Z_2, Z_1, Z_1, group->mont, ctx)) {
274 goto err;
275 }
276
277 /* Instead of using |BN_from_montgomery| to convert the |x| coordinate
278 * and then calling |BN_from_montgomery| again to convert the |y|
279 * coordinate below, convert the common factor |Z_2| once now, saving one
280 * reduction. */
281 if (!BN_from_montgomery(Z_2, Z_2, group->mont, ctx)) {
282 goto err;
283 }
284
285 if (x != NULL) {
286 if (!BN_mod_mul_montgomery(x, &point->X, Z_2, group->mont, ctx)) {
287 goto err;
288 }
289 }
290
291 if (y != NULL) {
292 if (!BN_mod_mul_montgomery(Z_3, Z_2, Z_1, group->mont, ctx) ||
293 !BN_mod_mul_montgomery(y, &point->Y, Z_3, group->mont, ctx)) {
294 goto err;
295 }
296 }
297 }
298
299 ret = 1;
300
301 err:
302 BN_CTX_end(ctx);
303 BN_CTX_free(new_ctx);
304 return ret;
305 }
306
307 const EC_METHOD *EC_GFp_mont_method(void) {
308 static const EC_METHOD ret = {
309 ec_GFp_mont_group_init,
310 ec_GFp_mont_group_finish,
311 ec_GFp_mont_group_copy,
312 ec_GFp_mont_group_set_curve,
313 ec_GFp_mont_point_get_affine_coordinates,
314 ec_wNAF_mul /* XXX: Not constant time. */,
315 ec_GFp_mont_check_pub_key_order,
316 ec_GFp_mont_field_mul,
317 ec_GFp_mont_field_sqr,
318 ec_GFp_mont_field_encode,
319 ec_GFp_mont_field_decode,
320 };
321
322 return &ret;
323 }