1*c40a6cd7SToomas Soome /*
2f9fbec18Smcpowers * ***** BEGIN LICENSE BLOCK *****
3f9fbec18Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4f9fbec18Smcpowers *
5f9fbec18Smcpowers * The contents of this file are subject to the Mozilla Public License Version
6f9fbec18Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
7f9fbec18Smcpowers * the License. You may obtain a copy of the License at
8f9fbec18Smcpowers * http://www.mozilla.org/MPL/
9f9fbec18Smcpowers *
10f9fbec18Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
11f9fbec18Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12f9fbec18Smcpowers * for the specific language governing rights and limitations under the
13f9fbec18Smcpowers * License.
14f9fbec18Smcpowers *
15f9fbec18Smcpowers * The Original Code is the elliptic curve math library for prime field curves.
16f9fbec18Smcpowers *
17f9fbec18Smcpowers * The Initial Developer of the Original Code is
18f9fbec18Smcpowers * Sun Microsystems, Inc.
19f9fbec18Smcpowers * Portions created by the Initial Developer are Copyright (C) 2003
20f9fbec18Smcpowers * the Initial Developer. All Rights Reserved.
21f9fbec18Smcpowers *
22f9fbec18Smcpowers * Contributor(s):
23f9fbec18Smcpowers * Douglas Stebila <douglas@stebila.ca>
24f9fbec18Smcpowers *
25f9fbec18Smcpowers * Alternatively, the contents of this file may be used under the terms of
26f9fbec18Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27f9fbec18Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28f9fbec18Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29f9fbec18Smcpowers * of those above. If you wish to allow use of your version of this file only
30f9fbec18Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31f9fbec18Smcpowers * use your version of this file under the terms of the MPL, indicate your
32f9fbec18Smcpowers * decision by deleting the provisions above and replace them with the notice
33f9fbec18Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34f9fbec18Smcpowers * the provisions above, a recipient may use your version of this file under
35f9fbec18Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36f9fbec18Smcpowers *
37f9fbec18Smcpowers * ***** END LICENSE BLOCK ***** */
38f9fbec18Smcpowers /*
39f9fbec18Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40f9fbec18Smcpowers * Use is subject to license terms.
41f9fbec18Smcpowers *
42f9fbec18Smcpowers * Sun elects to use this software under the MPL license.
43f9fbec18Smcpowers */
44f9fbec18Smcpowers
45f9fbec18Smcpowers #include "ecp.h"
46f9fbec18Smcpowers #include "mpi.h"
47f9fbec18Smcpowers #include "mplogic.h"
48f9fbec18Smcpowers #include "mpi-priv.h"
49f9fbec18Smcpowers #ifndef _KERNEL
50f9fbec18Smcpowers #include <stdlib.h>
51f9fbec18Smcpowers #endif
52f9fbec18Smcpowers
53f9fbec18Smcpowers #define ECP521_DIGITS ECL_CURVE_DIGITS(521)
54f9fbec18Smcpowers
55f9fbec18Smcpowers /* Fast modular reduction for p521 = 2^521 - 1. a can be r. Uses
56*c40a6cd7SToomas Soome * algorithm 2.31 from Hankerson, Menezes, Vanstone. Guide to
57f9fbec18Smcpowers * Elliptic Curve Cryptography. */
58f9fbec18Smcpowers mp_err
ec_GFp_nistp521_mod(const mp_int * a,mp_int * r,const GFMethod * meth)59f9fbec18Smcpowers ec_GFp_nistp521_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60f9fbec18Smcpowers {
61f9fbec18Smcpowers mp_err res = MP_OKAY;
62f9fbec18Smcpowers int a_bits = mpl_significant_bits(a);
63f9fbec18Smcpowers int i;
64f9fbec18Smcpowers
65f9fbec18Smcpowers /* m1, m2 are statically-allocated mp_int of exactly the size we need */
66f9fbec18Smcpowers mp_int m1;
67f9fbec18Smcpowers
68f9fbec18Smcpowers mp_digit s1[ECP521_DIGITS] = { 0 };
69f9fbec18Smcpowers
70f9fbec18Smcpowers MP_SIGN(&m1) = MP_ZPOS;
71f9fbec18Smcpowers MP_ALLOC(&m1) = ECP521_DIGITS;
72f9fbec18Smcpowers MP_USED(&m1) = ECP521_DIGITS;
73f9fbec18Smcpowers MP_DIGITS(&m1) = s1;
74f9fbec18Smcpowers
75f9fbec18Smcpowers if (a_bits < 521) {
76f9fbec18Smcpowers if (a==r) return MP_OKAY;
77f9fbec18Smcpowers return mp_copy(a, r);
78f9fbec18Smcpowers }
79*c40a6cd7SToomas Soome /* for polynomials larger than twice the field size or polynomials
80f9fbec18Smcpowers * not using all words, use regular reduction */
81f9fbec18Smcpowers if (a_bits > (521*2)) {
82f9fbec18Smcpowers MP_CHECKOK(mp_mod(a, &meth->irr, r));
83f9fbec18Smcpowers } else {
84f9fbec18Smcpowers #define FIRST_DIGIT (ECP521_DIGITS-1)
85f9fbec18Smcpowers for (i = FIRST_DIGIT; i < MP_USED(a)-1; i++) {
86*c40a6cd7SToomas Soome s1[i-FIRST_DIGIT] = (MP_DIGIT(a, i) >> 9)
87f9fbec18Smcpowers | (MP_DIGIT(a, 1+i) << (MP_DIGIT_BIT-9));
88f9fbec18Smcpowers }
89f9fbec18Smcpowers s1[i-FIRST_DIGIT] = MP_DIGIT(a, i) >> 9;
90f9fbec18Smcpowers
91f9fbec18Smcpowers if ( a != r ) {
92f9fbec18Smcpowers MP_CHECKOK(s_mp_pad(r,ECP521_DIGITS));
93f9fbec18Smcpowers for (i = 0; i < ECP521_DIGITS; i++) {
94f9fbec18Smcpowers MP_DIGIT(r,i) = MP_DIGIT(a, i);
95f9fbec18Smcpowers }
96f9fbec18Smcpowers }
97f9fbec18Smcpowers MP_USED(r) = ECP521_DIGITS;
98f9fbec18Smcpowers MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
99f9fbec18Smcpowers
100f9fbec18Smcpowers MP_CHECKOK(s_mp_add(r, &m1));
101f9fbec18Smcpowers if (MP_DIGIT(r, FIRST_DIGIT) & 0x200) {
102f9fbec18Smcpowers MP_CHECKOK(s_mp_add_d(r,1));
103f9fbec18Smcpowers MP_DIGIT(r,FIRST_DIGIT) &= 0x1FF;
104f9fbec18Smcpowers }
105f9fbec18Smcpowers s_mp_clamp(r);
106f9fbec18Smcpowers }
107f9fbec18Smcpowers
108f9fbec18Smcpowers CLEANUP:
109f9fbec18Smcpowers return res;
110f9fbec18Smcpowers }
111f9fbec18Smcpowers
112f9fbec18Smcpowers /* Compute the square of polynomial a, reduce modulo p521. Store the
113*c40a6cd7SToomas Soome * result in r. r could be a. Uses optimized modular reduction for p521.
114f9fbec18Smcpowers */
115f9fbec18Smcpowers mp_err
ec_GFp_nistp521_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)116f9fbec18Smcpowers ec_GFp_nistp521_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
117f9fbec18Smcpowers {
118f9fbec18Smcpowers mp_err res = MP_OKAY;
119f9fbec18Smcpowers
120f9fbec18Smcpowers MP_CHECKOK(mp_sqr(a, r));
121f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
122f9fbec18Smcpowers CLEANUP:
123f9fbec18Smcpowers return res;
124f9fbec18Smcpowers }
125f9fbec18Smcpowers
126f9fbec18Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p521.
127f9fbec18Smcpowers * Store the result in r. r could be a or b; a could be b. Uses
128f9fbec18Smcpowers * optimized modular reduction for p521. */
129f9fbec18Smcpowers mp_err
ec_GFp_nistp521_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)130f9fbec18Smcpowers ec_GFp_nistp521_mul(const mp_int *a, const mp_int *b, mp_int *r,
131f9fbec18Smcpowers const GFMethod *meth)
132f9fbec18Smcpowers {
133f9fbec18Smcpowers mp_err res = MP_OKAY;
134f9fbec18Smcpowers
135f9fbec18Smcpowers MP_CHECKOK(mp_mul(a, b, r));
136f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
137f9fbec18Smcpowers CLEANUP:
138f9fbec18Smcpowers return res;
139f9fbec18Smcpowers }
140f9fbec18Smcpowers
141f9fbec18Smcpowers /* Divides two field elements. If a is NULL, then returns the inverse of
142f9fbec18Smcpowers * b. */
143f9fbec18Smcpowers mp_err
ec_GFp_nistp521_div(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)144f9fbec18Smcpowers ec_GFp_nistp521_div(const mp_int *a, const mp_int *b, mp_int *r,
145f9fbec18Smcpowers const GFMethod *meth)
146f9fbec18Smcpowers {
147f9fbec18Smcpowers mp_err res = MP_OKAY;
148f9fbec18Smcpowers mp_int t;
149f9fbec18Smcpowers
150f9fbec18Smcpowers /* If a is NULL, then return the inverse of b, otherwise return a/b. */
151f9fbec18Smcpowers if (a == NULL) {
152f9fbec18Smcpowers return mp_invmod(b, &meth->irr, r);
153f9fbec18Smcpowers } else {
154*c40a6cd7SToomas Soome /* MPI doesn't support divmod, so we implement it using invmod and
155f9fbec18Smcpowers * mulmod. */
156f9fbec18Smcpowers MP_CHECKOK(mp_init(&t, FLAG(b)));
157f9fbec18Smcpowers MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
158f9fbec18Smcpowers MP_CHECKOK(mp_mul(a, &t, r));
159f9fbec18Smcpowers MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
160f9fbec18Smcpowers CLEANUP:
161f9fbec18Smcpowers mp_clear(&t);
162f9fbec18Smcpowers return res;
163f9fbec18Smcpowers }
164f9fbec18Smcpowers }
165f9fbec18Smcpowers
166f9fbec18Smcpowers /* Wire in fast field arithmetic and precomputation of base point for
167f9fbec18Smcpowers * named curves. */
168f9fbec18Smcpowers mp_err
ec_group_set_gfp521(ECGroup * group,ECCurveName name)169f9fbec18Smcpowers ec_group_set_gfp521(ECGroup *group, ECCurveName name)
170f9fbec18Smcpowers {
171f9fbec18Smcpowers if (name == ECCurve_NIST_P521) {
172f9fbec18Smcpowers group->meth->field_mod = &ec_GFp_nistp521_mod;
173f9fbec18Smcpowers group->meth->field_mul = &ec_GFp_nistp521_mul;
174f9fbec18Smcpowers group->meth->field_sqr = &ec_GFp_nistp521_sqr;
175f9fbec18Smcpowers group->meth->field_div = &ec_GFp_nistp521_div;
176f9fbec18Smcpowers }
177f9fbec18Smcpowers return MP_OKAY;
178f9fbec18Smcpowers }
179