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.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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 * openssl-core@openssl.org.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
68 #include <openssl/ec.h>
70 #include <openssl/bn.h>
71 #include <openssl/err.h>
72 #include <openssl/mem.h>
74 #include "../bn/internal.h"
75 #include "../delocate.h"
79 int ec_GFp_mont_group_init(EC_GROUP *group) {
82 ok = ec_GFp_simple_group_init(group);
87 void ec_GFp_mont_group_finish(EC_GROUP *group) {
88 BN_MONT_CTX_free(group->mont);
90 ec_GFp_simple_group_finish(group);
93 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
94 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
95 BN_CTX *new_ctx = NULL;
98 BN_MONT_CTX_free(group->mont);
102 ctx = new_ctx = BN_CTX_new();
108 group->mont = BN_MONT_CTX_new_for_modulus(p, ctx);
109 if (group->mont == NULL) {
110 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
114 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
117 BN_MONT_CTX_free(group->mont);
122 BN_CTX_free(new_ctx);
126 int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
127 const BIGNUM *b, BN_CTX *ctx) {
128 if (group->mont == NULL) {
129 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
133 return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
136 int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
138 if (group->mont == NULL) {
139 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
143 return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
146 int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
148 if (group->mont == NULL) {
149 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
153 return BN_to_montgomery(r, a, group->mont, ctx);
156 int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
158 if (group->mont == NULL) {
159 OPENSSL_PUT_ERROR(EC, EC_R_NOT_INITIALIZED);
163 return BN_from_montgomery(r, a, group->mont, ctx);
166 static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
167 const EC_POINT *point,
168 BIGNUM *x, BIGNUM *y,
170 if (EC_POINT_is_at_infinity(group, point)) {
171 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
175 BN_CTX *new_ctx = NULL;
177 ctx = new_ctx = BN_CTX_new();
187 if (BN_cmp(&point->Z, &group->one) == 0) {
188 // |point| is already affine.
189 if (x != NULL && !BN_from_montgomery(x, &point->X, group->mont, ctx)) {
192 if (y != NULL && !BN_from_montgomery(y, &point->Y, group->mont, ctx)) {
196 // transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3)
198 BIGNUM *Z_1 = BN_CTX_get(ctx);
199 BIGNUM *Z_2 = BN_CTX_get(ctx);
200 BIGNUM *Z_3 = BN_CTX_get(ctx);
207 // The straightforward way to calculate the inverse of a Montgomery-encoded
208 // value where the result is Montgomery-encoded is:
210 // |BN_from_montgomery| + invert + |BN_to_montgomery|.
212 // This is equivalent, but more efficient, because |BN_from_montgomery|
213 // is more efficient (at least in theory) than |BN_to_montgomery|, since it
214 // doesn't have to do the multiplication before the reduction.
216 // Use Fermat's Little Theorem instead of |BN_mod_inverse_odd| since this
217 // inversion may be done as the final step of private key operations.
218 // Unfortunately, this is suboptimal for ECDSA verification.
219 if (!BN_from_montgomery(Z_1, &point->Z, group->mont, ctx) ||
220 !BN_from_montgomery(Z_1, Z_1, group->mont, ctx) ||
221 !bn_mod_inverse_prime(Z_1, Z_1, &group->field, ctx, group->mont)) {
225 if (!BN_mod_mul_montgomery(Z_2, Z_1, Z_1, group->mont, ctx)) {
229 // Instead of using |BN_from_montgomery| to convert the |x| coordinate
230 // and then calling |BN_from_montgomery| again to convert the |y|
231 // coordinate below, convert the common factor |Z_2| once now, saving one
233 if (!BN_from_montgomery(Z_2, Z_2, group->mont, ctx)) {
238 if (!BN_mod_mul_montgomery(x, &point->X, Z_2, group->mont, ctx)) {
244 if (!BN_mod_mul_montgomery(Z_3, Z_2, Z_1, group->mont, ctx) ||
245 !BN_mod_mul_montgomery(y, &point->Y, Z_3, group->mont, ctx)) {
255 BN_CTX_free(new_ctx);
259 DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_mont_method) {
260 out->group_init = ec_GFp_mont_group_init;
261 out->group_finish = ec_GFp_mont_group_finish;
262 out->group_set_curve = ec_GFp_mont_group_set_curve;
263 out->point_get_affine_coordinates = ec_GFp_mont_point_get_affine_coordinates;
264 out->mul = ec_wNAF_mul /* XXX: Not constant time. */;
265 out->mul_public = ec_wNAF_mul;
266 out->field_mul = ec_GFp_mont_field_mul;
267 out->field_sqr = ec_GFp_mont_field_sqr;
268 out->field_encode = ec_GFp_mont_field_encode;
269 out->field_decode = ec_GFp_mont_field_decode;