1*5697Smcpowers /*
2*5697Smcpowers * ***** BEGIN LICENSE BLOCK *****
3*5697Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4*5697Smcpowers *
5*5697Smcpowers * The contents of this file are subject to the Mozilla Public License Version
6*5697Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
7*5697Smcpowers * the License. You may obtain a copy of the License at
8*5697Smcpowers * http://www.mozilla.org/MPL/
9*5697Smcpowers *
10*5697Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
11*5697Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12*5697Smcpowers * for the specific language governing rights and limitations under the
13*5697Smcpowers * License.
14*5697Smcpowers *
15*5697Smcpowers * The Original Code is the elliptic curve math library for prime field curves.
16*5697Smcpowers *
17*5697Smcpowers * The Initial Developer of the Original Code is
18*5697Smcpowers * Sun Microsystems, Inc.
19*5697Smcpowers * Portions created by the Initial Developer are Copyright (C) 2003
20*5697Smcpowers * the Initial Developer. All Rights Reserved.
21*5697Smcpowers *
22*5697Smcpowers * Contributor(s):
23*5697Smcpowers * Douglas Stebila <douglas@stebila.ca>
24*5697Smcpowers *
25*5697Smcpowers * Alternatively, the contents of this file may be used under the terms of
26*5697Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27*5697Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28*5697Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29*5697Smcpowers * of those above. If you wish to allow use of your version of this file only
30*5697Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31*5697Smcpowers * use your version of this file under the terms of the MPL, indicate your
32*5697Smcpowers * decision by deleting the provisions above and replace them with the notice
33*5697Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34*5697Smcpowers * the provisions above, a recipient may use your version of this file under
35*5697Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36*5697Smcpowers *
37*5697Smcpowers * ***** END LICENSE BLOCK ***** */
38*5697Smcpowers /*
39*5697Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40*5697Smcpowers * Use is subject to license terms.
41*5697Smcpowers *
42*5697Smcpowers * Sun elects to use this software under the MPL license.
43*5697Smcpowers */
44*5697Smcpowers
45*5697Smcpowers #pragma ident "%Z%%M% %I% %E% SMI"
46*5697Smcpowers
47*5697Smcpowers #include "ecp.h"
48*5697Smcpowers #include "mpi.h"
49*5697Smcpowers #include "mplogic.h"
50*5697Smcpowers #include "mpi-priv.h"
51*5697Smcpowers #ifndef _KERNEL
52*5697Smcpowers #include <stdlib.h>
53*5697Smcpowers #endif
54*5697Smcpowers
55*5697Smcpowers /* Fast modular reduction for p384 = 2^384 - 2^128 - 2^96 + 2^32 - 1. a can be r.
56*5697Smcpowers * Uses algorithm 2.30 from Hankerson, Menezes, Vanstone. Guide to
57*5697Smcpowers * Elliptic Curve Cryptography. */
58*5697Smcpowers mp_err
ec_GFp_nistp384_mod(const mp_int * a,mp_int * r,const GFMethod * meth)59*5697Smcpowers ec_GFp_nistp384_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60*5697Smcpowers {
61*5697Smcpowers mp_err res = MP_OKAY;
62*5697Smcpowers int a_bits = mpl_significant_bits(a);
63*5697Smcpowers int i;
64*5697Smcpowers
65*5697Smcpowers /* m1, m2 are statically-allocated mp_int of exactly the size we need */
66*5697Smcpowers mp_int m[10];
67*5697Smcpowers
68*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
69*5697Smcpowers mp_digit s[10][12];
70*5697Smcpowers for (i = 0; i < 10; i++) {
71*5697Smcpowers MP_SIGN(&m[i]) = MP_ZPOS;
72*5697Smcpowers MP_ALLOC(&m[i]) = 12;
73*5697Smcpowers MP_USED(&m[i]) = 12;
74*5697Smcpowers MP_DIGITS(&m[i]) = s[i];
75*5697Smcpowers }
76*5697Smcpowers #else
77*5697Smcpowers mp_digit s[10][6];
78*5697Smcpowers for (i = 0; i < 10; i++) {
79*5697Smcpowers MP_SIGN(&m[i]) = MP_ZPOS;
80*5697Smcpowers MP_ALLOC(&m[i]) = 6;
81*5697Smcpowers MP_USED(&m[i]) = 6;
82*5697Smcpowers MP_DIGITS(&m[i]) = s[i];
83*5697Smcpowers }
84*5697Smcpowers #endif
85*5697Smcpowers
86*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
87*5697Smcpowers /* for polynomials larger than twice the field size or polynomials
88*5697Smcpowers * not using all words, use regular reduction */
89*5697Smcpowers if ((a_bits > 768) || (a_bits <= 736)) {
90*5697Smcpowers MP_CHECKOK(mp_mod(a, &meth->irr, r));
91*5697Smcpowers } else {
92*5697Smcpowers for (i = 0; i < 12; i++) {
93*5697Smcpowers s[0][i] = MP_DIGIT(a, i);
94*5697Smcpowers }
95*5697Smcpowers s[1][0] = 0;
96*5697Smcpowers s[1][1] = 0;
97*5697Smcpowers s[1][2] = 0;
98*5697Smcpowers s[1][3] = 0;
99*5697Smcpowers s[1][4] = MP_DIGIT(a, 21);
100*5697Smcpowers s[1][5] = MP_DIGIT(a, 22);
101*5697Smcpowers s[1][6] = MP_DIGIT(a, 23);
102*5697Smcpowers s[1][7] = 0;
103*5697Smcpowers s[1][8] = 0;
104*5697Smcpowers s[1][9] = 0;
105*5697Smcpowers s[1][10] = 0;
106*5697Smcpowers s[1][11] = 0;
107*5697Smcpowers for (i = 0; i < 12; i++) {
108*5697Smcpowers s[2][i] = MP_DIGIT(a, i+12);
109*5697Smcpowers }
110*5697Smcpowers s[3][0] = MP_DIGIT(a, 21);
111*5697Smcpowers s[3][1] = MP_DIGIT(a, 22);
112*5697Smcpowers s[3][2] = MP_DIGIT(a, 23);
113*5697Smcpowers for (i = 3; i < 12; i++) {
114*5697Smcpowers s[3][i] = MP_DIGIT(a, i+9);
115*5697Smcpowers }
116*5697Smcpowers s[4][0] = 0;
117*5697Smcpowers s[4][1] = MP_DIGIT(a, 23);
118*5697Smcpowers s[4][2] = 0;
119*5697Smcpowers s[4][3] = MP_DIGIT(a, 20);
120*5697Smcpowers for (i = 4; i < 12; i++) {
121*5697Smcpowers s[4][i] = MP_DIGIT(a, i+8);
122*5697Smcpowers }
123*5697Smcpowers s[5][0] = 0;
124*5697Smcpowers s[5][1] = 0;
125*5697Smcpowers s[5][2] = 0;
126*5697Smcpowers s[5][3] = 0;
127*5697Smcpowers s[5][4] = MP_DIGIT(a, 20);
128*5697Smcpowers s[5][5] = MP_DIGIT(a, 21);
129*5697Smcpowers s[5][6] = MP_DIGIT(a, 22);
130*5697Smcpowers s[5][7] = MP_DIGIT(a, 23);
131*5697Smcpowers s[5][8] = 0;
132*5697Smcpowers s[5][9] = 0;
133*5697Smcpowers s[5][10] = 0;
134*5697Smcpowers s[5][11] = 0;
135*5697Smcpowers s[6][0] = MP_DIGIT(a, 20);
136*5697Smcpowers s[6][1] = 0;
137*5697Smcpowers s[6][2] = 0;
138*5697Smcpowers s[6][3] = MP_DIGIT(a, 21);
139*5697Smcpowers s[6][4] = MP_DIGIT(a, 22);
140*5697Smcpowers s[6][5] = MP_DIGIT(a, 23);
141*5697Smcpowers s[6][6] = 0;
142*5697Smcpowers s[6][7] = 0;
143*5697Smcpowers s[6][8] = 0;
144*5697Smcpowers s[6][9] = 0;
145*5697Smcpowers s[6][10] = 0;
146*5697Smcpowers s[6][11] = 0;
147*5697Smcpowers s[7][0] = MP_DIGIT(a, 23);
148*5697Smcpowers for (i = 1; i < 12; i++) {
149*5697Smcpowers s[7][i] = MP_DIGIT(a, i+11);
150*5697Smcpowers }
151*5697Smcpowers s[8][0] = 0;
152*5697Smcpowers s[8][1] = MP_DIGIT(a, 20);
153*5697Smcpowers s[8][2] = MP_DIGIT(a, 21);
154*5697Smcpowers s[8][3] = MP_DIGIT(a, 22);
155*5697Smcpowers s[8][4] = MP_DIGIT(a, 23);
156*5697Smcpowers s[8][5] = 0;
157*5697Smcpowers s[8][6] = 0;
158*5697Smcpowers s[8][7] = 0;
159*5697Smcpowers s[8][8] = 0;
160*5697Smcpowers s[8][9] = 0;
161*5697Smcpowers s[8][10] = 0;
162*5697Smcpowers s[8][11] = 0;
163*5697Smcpowers s[9][0] = 0;
164*5697Smcpowers s[9][1] = 0;
165*5697Smcpowers s[9][2] = 0;
166*5697Smcpowers s[9][3] = MP_DIGIT(a, 23);
167*5697Smcpowers s[9][4] = MP_DIGIT(a, 23);
168*5697Smcpowers s[9][5] = 0;
169*5697Smcpowers s[9][6] = 0;
170*5697Smcpowers s[9][7] = 0;
171*5697Smcpowers s[9][8] = 0;
172*5697Smcpowers s[9][9] = 0;
173*5697Smcpowers s[9][10] = 0;
174*5697Smcpowers s[9][11] = 0;
175*5697Smcpowers
176*5697Smcpowers MP_CHECKOK(mp_add(&m[0], &m[1], r));
177*5697Smcpowers MP_CHECKOK(mp_add(r, &m[1], r));
178*5697Smcpowers MP_CHECKOK(mp_add(r, &m[2], r));
179*5697Smcpowers MP_CHECKOK(mp_add(r, &m[3], r));
180*5697Smcpowers MP_CHECKOK(mp_add(r, &m[4], r));
181*5697Smcpowers MP_CHECKOK(mp_add(r, &m[5], r));
182*5697Smcpowers MP_CHECKOK(mp_add(r, &m[6], r));
183*5697Smcpowers MP_CHECKOK(mp_sub(r, &m[7], r));
184*5697Smcpowers MP_CHECKOK(mp_sub(r, &m[8], r));
185*5697Smcpowers MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
186*5697Smcpowers s_mp_clamp(r);
187*5697Smcpowers }
188*5697Smcpowers #else
189*5697Smcpowers /* for polynomials larger than twice the field size or polynomials
190*5697Smcpowers * not using all words, use regular reduction */
191*5697Smcpowers if ((a_bits > 768) || (a_bits <= 736)) {
192*5697Smcpowers MP_CHECKOK(mp_mod(a, &meth->irr, r));
193*5697Smcpowers } else {
194*5697Smcpowers for (i = 0; i < 6; i++) {
195*5697Smcpowers s[0][i] = MP_DIGIT(a, i);
196*5697Smcpowers }
197*5697Smcpowers s[1][0] = 0;
198*5697Smcpowers s[1][1] = 0;
199*5697Smcpowers s[1][2] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
200*5697Smcpowers s[1][3] = MP_DIGIT(a, 11) >> 32;
201*5697Smcpowers s[1][4] = 0;
202*5697Smcpowers s[1][5] = 0;
203*5697Smcpowers for (i = 0; i < 6; i++) {
204*5697Smcpowers s[2][i] = MP_DIGIT(a, i+6);
205*5697Smcpowers }
206*5697Smcpowers s[3][0] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
207*5697Smcpowers s[3][1] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
208*5697Smcpowers for (i = 2; i < 6; i++) {
209*5697Smcpowers s[3][i] = (MP_DIGIT(a, i+4) >> 32) | (MP_DIGIT(a, i+5) << 32);
210*5697Smcpowers }
211*5697Smcpowers s[4][0] = (MP_DIGIT(a, 11) >> 32) << 32;
212*5697Smcpowers s[4][1] = MP_DIGIT(a, 10) << 32;
213*5697Smcpowers for (i = 2; i < 6; i++) {
214*5697Smcpowers s[4][i] = MP_DIGIT(a, i+4);
215*5697Smcpowers }
216*5697Smcpowers s[5][0] = 0;
217*5697Smcpowers s[5][1] = 0;
218*5697Smcpowers s[5][2] = MP_DIGIT(a, 10);
219*5697Smcpowers s[5][3] = MP_DIGIT(a, 11);
220*5697Smcpowers s[5][4] = 0;
221*5697Smcpowers s[5][5] = 0;
222*5697Smcpowers s[6][0] = (MP_DIGIT(a, 10) << 32) >> 32;
223*5697Smcpowers s[6][1] = (MP_DIGIT(a, 10) >> 32) << 32;
224*5697Smcpowers s[6][2] = MP_DIGIT(a, 11);
225*5697Smcpowers s[6][3] = 0;
226*5697Smcpowers s[6][4] = 0;
227*5697Smcpowers s[6][5] = 0;
228*5697Smcpowers s[7][0] = (MP_DIGIT(a, 11) >> 32) | (MP_DIGIT(a, 6) << 32);
229*5697Smcpowers for (i = 1; i < 6; i++) {
230*5697Smcpowers s[7][i] = (MP_DIGIT(a, i+5) >> 32) | (MP_DIGIT(a, i+6) << 32);
231*5697Smcpowers }
232*5697Smcpowers s[8][0] = MP_DIGIT(a, 10) << 32;
233*5697Smcpowers s[8][1] = (MP_DIGIT(a, 10) >> 32) | (MP_DIGIT(a, 11) << 32);
234*5697Smcpowers s[8][2] = MP_DIGIT(a, 11) >> 32;
235*5697Smcpowers s[8][3] = 0;
236*5697Smcpowers s[8][4] = 0;
237*5697Smcpowers s[8][5] = 0;
238*5697Smcpowers s[9][0] = 0;
239*5697Smcpowers s[9][1] = (MP_DIGIT(a, 11) >> 32) << 32;
240*5697Smcpowers s[9][2] = MP_DIGIT(a, 11) >> 32;
241*5697Smcpowers s[9][3] = 0;
242*5697Smcpowers s[9][4] = 0;
243*5697Smcpowers s[9][5] = 0;
244*5697Smcpowers
245*5697Smcpowers MP_CHECKOK(mp_add(&m[0], &m[1], r));
246*5697Smcpowers MP_CHECKOK(mp_add(r, &m[1], r));
247*5697Smcpowers MP_CHECKOK(mp_add(r, &m[2], r));
248*5697Smcpowers MP_CHECKOK(mp_add(r, &m[3], r));
249*5697Smcpowers MP_CHECKOK(mp_add(r, &m[4], r));
250*5697Smcpowers MP_CHECKOK(mp_add(r, &m[5], r));
251*5697Smcpowers MP_CHECKOK(mp_add(r, &m[6], r));
252*5697Smcpowers MP_CHECKOK(mp_sub(r, &m[7], r));
253*5697Smcpowers MP_CHECKOK(mp_sub(r, &m[8], r));
254*5697Smcpowers MP_CHECKOK(mp_submod(r, &m[9], &meth->irr, r));
255*5697Smcpowers s_mp_clamp(r);
256*5697Smcpowers }
257*5697Smcpowers #endif
258*5697Smcpowers
259*5697Smcpowers CLEANUP:
260*5697Smcpowers return res;
261*5697Smcpowers }
262*5697Smcpowers
263*5697Smcpowers /* Compute the square of polynomial a, reduce modulo p384. Store the
264*5697Smcpowers * result in r. r could be a. Uses optimized modular reduction for p384.
265*5697Smcpowers */
266*5697Smcpowers mp_err
ec_GFp_nistp384_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)267*5697Smcpowers ec_GFp_nistp384_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
268*5697Smcpowers {
269*5697Smcpowers mp_err res = MP_OKAY;
270*5697Smcpowers
271*5697Smcpowers MP_CHECKOK(mp_sqr(a, r));
272*5697Smcpowers MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
273*5697Smcpowers CLEANUP:
274*5697Smcpowers return res;
275*5697Smcpowers }
276*5697Smcpowers
277*5697Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p384.
278*5697Smcpowers * Store the result in r. r could be a or b; a could be b. Uses
279*5697Smcpowers * optimized modular reduction for p384. */
280*5697Smcpowers mp_err
ec_GFp_nistp384_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)281*5697Smcpowers ec_GFp_nistp384_mul(const mp_int *a, const mp_int *b, mp_int *r,
282*5697Smcpowers const GFMethod *meth)
283*5697Smcpowers {
284*5697Smcpowers mp_err res = MP_OKAY;
285*5697Smcpowers
286*5697Smcpowers MP_CHECKOK(mp_mul(a, b, r));
287*5697Smcpowers MP_CHECKOK(ec_GFp_nistp384_mod(r, r, meth));
288*5697Smcpowers CLEANUP:
289*5697Smcpowers return res;
290*5697Smcpowers }
291*5697Smcpowers
292*5697Smcpowers /* Wire in fast field arithmetic and precomputation of base point for
293*5697Smcpowers * named curves. */
294*5697Smcpowers mp_err
ec_group_set_gfp384(ECGroup * group,ECCurveName name)295*5697Smcpowers ec_group_set_gfp384(ECGroup *group, ECCurveName name)
296*5697Smcpowers {
297*5697Smcpowers if (name == ECCurve_NIST_P384) {
298*5697Smcpowers group->meth->field_mod = &ec_GFp_nistp384_mod;
299*5697Smcpowers group->meth->field_mul = &ec_GFp_nistp384_mul;
300*5697Smcpowers group->meth->field_sqr = &ec_GFp_nistp384_sqr;
301*5697Smcpowers }
302*5697Smcpowers return MP_OKAY;
303*5697Smcpowers }
304