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 p256 = 2^256 - 2^224 + 2^192+ 2^96 - 1. a can be r.
56*5697Smcpowers * Uses algorithm 2.29 from Hankerson, Menezes, Vanstone. Guide to
57*5697Smcpowers * Elliptic Curve Cryptography. */
58*5697Smcpowers mp_err
ec_GFp_nistp256_mod(const mp_int * a,mp_int * r,const GFMethod * meth)59*5697Smcpowers ec_GFp_nistp256_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60*5697Smcpowers {
61*5697Smcpowers mp_err res = MP_OKAY;
62*5697Smcpowers mp_size a_used = MP_USED(a);
63*5697Smcpowers int a_bits = mpl_significant_bits(a);
64*5697Smcpowers mp_digit carry;
65*5697Smcpowers
66*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
67*5697Smcpowers mp_digit a8=0, a9=0, a10=0, a11=0, a12=0, a13=0, a14=0, a15=0;
68*5697Smcpowers mp_digit r0, r1, r2, r3, r4, r5, r6, r7;
69*5697Smcpowers int r8; /* must be a signed value ! */
70*5697Smcpowers #else
71*5697Smcpowers mp_digit a4=0, a5=0, a6=0, a7=0;
72*5697Smcpowers mp_digit a4h, a4l, a5h, a5l, a6h, a6l, a7h, a7l;
73*5697Smcpowers mp_digit r0, r1, r2, r3;
74*5697Smcpowers int r4; /* must be a signed value ! */
75*5697Smcpowers #endif
76*5697Smcpowers /* for polynomials larger than twice the field size
77*5697Smcpowers * use regular reduction */
78*5697Smcpowers if (a_bits < 256) {
79*5697Smcpowers if (a == r) return MP_OKAY;
80*5697Smcpowers return mp_copy(a,r);
81*5697Smcpowers }
82*5697Smcpowers if (a_bits > 512) {
83*5697Smcpowers MP_CHECKOK(mp_mod(a, &meth->irr, r));
84*5697Smcpowers } else {
85*5697Smcpowers
86*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
87*5697Smcpowers switch (a_used) {
88*5697Smcpowers case 16:
89*5697Smcpowers a15 = MP_DIGIT(a,15);
90*5697Smcpowers case 15:
91*5697Smcpowers a14 = MP_DIGIT(a,14);
92*5697Smcpowers case 14:
93*5697Smcpowers a13 = MP_DIGIT(a,13);
94*5697Smcpowers case 13:
95*5697Smcpowers a12 = MP_DIGIT(a,12);
96*5697Smcpowers case 12:
97*5697Smcpowers a11 = MP_DIGIT(a,11);
98*5697Smcpowers case 11:
99*5697Smcpowers a10 = MP_DIGIT(a,10);
100*5697Smcpowers case 10:
101*5697Smcpowers a9 = MP_DIGIT(a,9);
102*5697Smcpowers case 9:
103*5697Smcpowers a8 = MP_DIGIT(a,8);
104*5697Smcpowers }
105*5697Smcpowers
106*5697Smcpowers r0 = MP_DIGIT(a,0);
107*5697Smcpowers r1 = MP_DIGIT(a,1);
108*5697Smcpowers r2 = MP_DIGIT(a,2);
109*5697Smcpowers r3 = MP_DIGIT(a,3);
110*5697Smcpowers r4 = MP_DIGIT(a,4);
111*5697Smcpowers r5 = MP_DIGIT(a,5);
112*5697Smcpowers r6 = MP_DIGIT(a,6);
113*5697Smcpowers r7 = MP_DIGIT(a,7);
114*5697Smcpowers
115*5697Smcpowers /* sum 1 */
116*5697Smcpowers MP_ADD_CARRY(r3, a11, r3, 0, carry);
117*5697Smcpowers MP_ADD_CARRY(r4, a12, r4, carry, carry);
118*5697Smcpowers MP_ADD_CARRY(r5, a13, r5, carry, carry);
119*5697Smcpowers MP_ADD_CARRY(r6, a14, r6, carry, carry);
120*5697Smcpowers MP_ADD_CARRY(r7, a15, r7, carry, carry);
121*5697Smcpowers r8 = carry;
122*5697Smcpowers MP_ADD_CARRY(r3, a11, r3, 0, carry);
123*5697Smcpowers MP_ADD_CARRY(r4, a12, r4, carry, carry);
124*5697Smcpowers MP_ADD_CARRY(r5, a13, r5, carry, carry);
125*5697Smcpowers MP_ADD_CARRY(r6, a14, r6, carry, carry);
126*5697Smcpowers MP_ADD_CARRY(r7, a15, r7, carry, carry);
127*5697Smcpowers r8 += carry;
128*5697Smcpowers /* sum 2 */
129*5697Smcpowers MP_ADD_CARRY(r3, a12, r3, 0, carry);
130*5697Smcpowers MP_ADD_CARRY(r4, a13, r4, carry, carry);
131*5697Smcpowers MP_ADD_CARRY(r5, a14, r5, carry, carry);
132*5697Smcpowers MP_ADD_CARRY(r6, a15, r6, carry, carry);
133*5697Smcpowers MP_ADD_CARRY(r7, 0, r7, carry, carry);
134*5697Smcpowers r8 += carry;
135*5697Smcpowers /* combine last bottom of sum 3 with second sum 2 */
136*5697Smcpowers MP_ADD_CARRY(r0, a8, r0, 0, carry);
137*5697Smcpowers MP_ADD_CARRY(r1, a9, r1, carry, carry);
138*5697Smcpowers MP_ADD_CARRY(r2, a10, r2, carry, carry);
139*5697Smcpowers MP_ADD_CARRY(r3, a12, r3, carry, carry);
140*5697Smcpowers MP_ADD_CARRY(r4, a13, r4, carry, carry);
141*5697Smcpowers MP_ADD_CARRY(r5, a14, r5, carry, carry);
142*5697Smcpowers MP_ADD_CARRY(r6, a15, r6, carry, carry);
143*5697Smcpowers MP_ADD_CARRY(r7, a15, r7, carry, carry); /* from sum 3 */
144*5697Smcpowers r8 += carry;
145*5697Smcpowers /* sum 3 (rest of it)*/
146*5697Smcpowers MP_ADD_CARRY(r6, a14, r6, 0, carry);
147*5697Smcpowers MP_ADD_CARRY(r7, 0, r7, carry, carry);
148*5697Smcpowers r8 += carry;
149*5697Smcpowers /* sum 4 (rest of it)*/
150*5697Smcpowers MP_ADD_CARRY(r0, a9, r0, 0, carry);
151*5697Smcpowers MP_ADD_CARRY(r1, a10, r1, carry, carry);
152*5697Smcpowers MP_ADD_CARRY(r2, a11, r2, carry, carry);
153*5697Smcpowers MP_ADD_CARRY(r3, a13, r3, carry, carry);
154*5697Smcpowers MP_ADD_CARRY(r4, a14, r4, carry, carry);
155*5697Smcpowers MP_ADD_CARRY(r5, a15, r5, carry, carry);
156*5697Smcpowers MP_ADD_CARRY(r6, a13, r6, carry, carry);
157*5697Smcpowers MP_ADD_CARRY(r7, a8, r7, carry, carry);
158*5697Smcpowers r8 += carry;
159*5697Smcpowers /* diff 5 */
160*5697Smcpowers MP_SUB_BORROW(r0, a11, r0, 0, carry);
161*5697Smcpowers MP_SUB_BORROW(r1, a12, r1, carry, carry);
162*5697Smcpowers MP_SUB_BORROW(r2, a13, r2, carry, carry);
163*5697Smcpowers MP_SUB_BORROW(r3, 0, r3, carry, carry);
164*5697Smcpowers MP_SUB_BORROW(r4, 0, r4, carry, carry);
165*5697Smcpowers MP_SUB_BORROW(r5, 0, r5, carry, carry);
166*5697Smcpowers MP_SUB_BORROW(r6, a8, r6, carry, carry);
167*5697Smcpowers MP_SUB_BORROW(r7, a10, r7, carry, carry);
168*5697Smcpowers r8 -= carry;
169*5697Smcpowers /* diff 6 */
170*5697Smcpowers MP_SUB_BORROW(r0, a12, r0, 0, carry);
171*5697Smcpowers MP_SUB_BORROW(r1, a13, r1, carry, carry);
172*5697Smcpowers MP_SUB_BORROW(r2, a14, r2, carry, carry);
173*5697Smcpowers MP_SUB_BORROW(r3, a15, r3, carry, carry);
174*5697Smcpowers MP_SUB_BORROW(r4, 0, r4, carry, carry);
175*5697Smcpowers MP_SUB_BORROW(r5, 0, r5, carry, carry);
176*5697Smcpowers MP_SUB_BORROW(r6, a9, r6, carry, carry);
177*5697Smcpowers MP_SUB_BORROW(r7, a11, r7, carry, carry);
178*5697Smcpowers r8 -= carry;
179*5697Smcpowers /* diff 7 */
180*5697Smcpowers MP_SUB_BORROW(r0, a13, r0, 0, carry);
181*5697Smcpowers MP_SUB_BORROW(r1, a14, r1, carry, carry);
182*5697Smcpowers MP_SUB_BORROW(r2, a15, r2, carry, carry);
183*5697Smcpowers MP_SUB_BORROW(r3, a8, r3, carry, carry);
184*5697Smcpowers MP_SUB_BORROW(r4, a9, r4, carry, carry);
185*5697Smcpowers MP_SUB_BORROW(r5, a10, r5, carry, carry);
186*5697Smcpowers MP_SUB_BORROW(r6, 0, r6, carry, carry);
187*5697Smcpowers MP_SUB_BORROW(r7, a12, r7, carry, carry);
188*5697Smcpowers r8 -= carry;
189*5697Smcpowers /* diff 8 */
190*5697Smcpowers MP_SUB_BORROW(r0, a14, r0, 0, carry);
191*5697Smcpowers MP_SUB_BORROW(r1, a15, r1, carry, carry);
192*5697Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry);
193*5697Smcpowers MP_SUB_BORROW(r3, a9, r3, carry, carry);
194*5697Smcpowers MP_SUB_BORROW(r4, a10, r4, carry, carry);
195*5697Smcpowers MP_SUB_BORROW(r5, a11, r5, carry, carry);
196*5697Smcpowers MP_SUB_BORROW(r6, 0, r6, carry, carry);
197*5697Smcpowers MP_SUB_BORROW(r7, a13, r7, carry, carry);
198*5697Smcpowers r8 -= carry;
199*5697Smcpowers
200*5697Smcpowers /* reduce the overflows */
201*5697Smcpowers while (r8 > 0) {
202*5697Smcpowers mp_digit r8_d = r8;
203*5697Smcpowers MP_ADD_CARRY(r0, r8_d, r0, 0, carry);
204*5697Smcpowers MP_ADD_CARRY(r1, 0, r1, carry, carry);
205*5697Smcpowers MP_ADD_CARRY(r2, 0, r2, carry, carry);
206*5697Smcpowers MP_ADD_CARRY(r3, -r8_d, r3, carry, carry);
207*5697Smcpowers MP_ADD_CARRY(r4, MP_DIGIT_MAX, r4, carry, carry);
208*5697Smcpowers MP_ADD_CARRY(r5, MP_DIGIT_MAX, r5, carry, carry);
209*5697Smcpowers MP_ADD_CARRY(r6, -(r8_d+1), r6, carry, carry);
210*5697Smcpowers MP_ADD_CARRY(r7, (r8_d-1), r7, carry, carry);
211*5697Smcpowers r8 = carry;
212*5697Smcpowers }
213*5697Smcpowers
214*5697Smcpowers /* reduce the underflows */
215*5697Smcpowers while (r8 < 0) {
216*5697Smcpowers mp_digit r8_d = -r8;
217*5697Smcpowers MP_SUB_BORROW(r0, r8_d, r0, 0, carry);
218*5697Smcpowers MP_SUB_BORROW(r1, 0, r1, carry, carry);
219*5697Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry);
220*5697Smcpowers MP_SUB_BORROW(r3, -r8_d, r3, carry, carry);
221*5697Smcpowers MP_SUB_BORROW(r4, MP_DIGIT_MAX, r4, carry, carry);
222*5697Smcpowers MP_SUB_BORROW(r5, MP_DIGIT_MAX, r5, carry, carry);
223*5697Smcpowers MP_SUB_BORROW(r6, -(r8_d+1), r6, carry, carry);
224*5697Smcpowers MP_SUB_BORROW(r7, (r8_d-1), r7, carry, carry);
225*5697Smcpowers r8 = -carry;
226*5697Smcpowers }
227*5697Smcpowers if (a != r) {
228*5697Smcpowers MP_CHECKOK(s_mp_pad(r,8));
229*5697Smcpowers }
230*5697Smcpowers MP_SIGN(r) = MP_ZPOS;
231*5697Smcpowers MP_USED(r) = 8;
232*5697Smcpowers
233*5697Smcpowers MP_DIGIT(r,7) = r7;
234*5697Smcpowers MP_DIGIT(r,6) = r6;
235*5697Smcpowers MP_DIGIT(r,5) = r5;
236*5697Smcpowers MP_DIGIT(r,4) = r4;
237*5697Smcpowers MP_DIGIT(r,3) = r3;
238*5697Smcpowers MP_DIGIT(r,2) = r2;
239*5697Smcpowers MP_DIGIT(r,1) = r1;
240*5697Smcpowers MP_DIGIT(r,0) = r0;
241*5697Smcpowers
242*5697Smcpowers /* final reduction if necessary */
243*5697Smcpowers if ((r7 == MP_DIGIT_MAX) &&
244*5697Smcpowers ((r6 > 1) || ((r6 == 1) &&
245*5697Smcpowers (r5 || r4 || r3 ||
246*5697Smcpowers ((r2 == MP_DIGIT_MAX) && (r1 == MP_DIGIT_MAX)
247*5697Smcpowers && (r0 == MP_DIGIT_MAX)))))) {
248*5697Smcpowers MP_CHECKOK(mp_sub(r, &meth->irr, r));
249*5697Smcpowers }
250*5697Smcpowers #ifdef notdef
251*5697Smcpowers
252*5697Smcpowers
253*5697Smcpowers /* smooth the negatives */
254*5697Smcpowers while (MP_SIGN(r) != MP_ZPOS) {
255*5697Smcpowers MP_CHECKOK(mp_add(r, &meth->irr, r));
256*5697Smcpowers }
257*5697Smcpowers while (MP_USED(r) > 8) {
258*5697Smcpowers MP_CHECKOK(mp_sub(r, &meth->irr, r));
259*5697Smcpowers }
260*5697Smcpowers
261*5697Smcpowers /* final reduction if necessary */
262*5697Smcpowers if (MP_DIGIT(r,7) >= MP_DIGIT(&meth->irr,7)) {
263*5697Smcpowers if (mp_cmp(r,&meth->irr) != MP_LT) {
264*5697Smcpowers MP_CHECKOK(mp_sub(r, &meth->irr, r));
265*5697Smcpowers }
266*5697Smcpowers }
267*5697Smcpowers #endif
268*5697Smcpowers s_mp_clamp(r);
269*5697Smcpowers #else
270*5697Smcpowers switch (a_used) {
271*5697Smcpowers case 8:
272*5697Smcpowers a7 = MP_DIGIT(a,7);
273*5697Smcpowers case 7:
274*5697Smcpowers a6 = MP_DIGIT(a,6);
275*5697Smcpowers case 6:
276*5697Smcpowers a5 = MP_DIGIT(a,5);
277*5697Smcpowers case 5:
278*5697Smcpowers a4 = MP_DIGIT(a,4);
279*5697Smcpowers }
280*5697Smcpowers a7l = a7 << 32;
281*5697Smcpowers a7h = a7 >> 32;
282*5697Smcpowers a6l = a6 << 32;
283*5697Smcpowers a6h = a6 >> 32;
284*5697Smcpowers a5l = a5 << 32;
285*5697Smcpowers a5h = a5 >> 32;
286*5697Smcpowers a4l = a4 << 32;
287*5697Smcpowers a4h = a4 >> 32;
288*5697Smcpowers r3 = MP_DIGIT(a,3);
289*5697Smcpowers r2 = MP_DIGIT(a,2);
290*5697Smcpowers r1 = MP_DIGIT(a,1);
291*5697Smcpowers r0 = MP_DIGIT(a,0);
292*5697Smcpowers
293*5697Smcpowers /* sum 1 */
294*5697Smcpowers MP_ADD_CARRY(r1, a5h << 32, r1, 0, carry);
295*5697Smcpowers MP_ADD_CARRY(r2, a6, r2, carry, carry);
296*5697Smcpowers MP_ADD_CARRY(r3, a7, r3, carry, carry);
297*5697Smcpowers r4 = carry;
298*5697Smcpowers MP_ADD_CARRY(r1, a5h << 32, r1, 0, carry);
299*5697Smcpowers MP_ADD_CARRY(r2, a6, r2, carry, carry);
300*5697Smcpowers MP_ADD_CARRY(r3, a7, r3, carry, carry);
301*5697Smcpowers r4 += carry;
302*5697Smcpowers /* sum 2 */
303*5697Smcpowers MP_ADD_CARRY(r1, a6l, r1, 0, carry);
304*5697Smcpowers MP_ADD_CARRY(r2, a6h | a7l, r2, carry, carry);
305*5697Smcpowers MP_ADD_CARRY(r3, a7h, r3, carry, carry);
306*5697Smcpowers r4 += carry;
307*5697Smcpowers MP_ADD_CARRY(r1, a6l, r1, 0, carry);
308*5697Smcpowers MP_ADD_CARRY(r2, a6h | a7l, r2, carry, carry);
309*5697Smcpowers MP_ADD_CARRY(r3, a7h, r3, carry, carry);
310*5697Smcpowers r4 += carry;
311*5697Smcpowers
312*5697Smcpowers /* sum 3 */
313*5697Smcpowers MP_ADD_CARRY(r0, a4, r0, 0, carry);
314*5697Smcpowers MP_ADD_CARRY(r1, a5l >> 32, r1, carry, carry);
315*5697Smcpowers MP_ADD_CARRY(r2, 0, r2, carry, carry);
316*5697Smcpowers MP_ADD_CARRY(r3, a7, r3, carry, carry);
317*5697Smcpowers r4 += carry;
318*5697Smcpowers /* sum 4 */
319*5697Smcpowers MP_ADD_CARRY(r0, a4h | a5l, r0, 0, carry);
320*5697Smcpowers MP_ADD_CARRY(r1, a5h|(a6h<<32), r1, carry, carry);
321*5697Smcpowers MP_ADD_CARRY(r2, a7, r2, carry, carry);
322*5697Smcpowers MP_ADD_CARRY(r3, a6h | a4l, r3, carry, carry);
323*5697Smcpowers r4 += carry;
324*5697Smcpowers /* diff 5 */
325*5697Smcpowers MP_SUB_BORROW(r0, a5h | a6l, r0, 0, carry);
326*5697Smcpowers MP_SUB_BORROW(r1, a6h, r1, carry, carry);
327*5697Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry);
328*5697Smcpowers MP_SUB_BORROW(r3, (a4l>>32)|a5l,r3, carry, carry);
329*5697Smcpowers r4 -= carry;
330*5697Smcpowers /* diff 6 */
331*5697Smcpowers MP_SUB_BORROW(r0, a6, r0, 0, carry);
332*5697Smcpowers MP_SUB_BORROW(r1, a7, r1, carry, carry);
333*5697Smcpowers MP_SUB_BORROW(r2, 0, r2, carry, carry);
334*5697Smcpowers MP_SUB_BORROW(r3, a4h|(a5h<<32),r3, carry, carry);
335*5697Smcpowers r4 -= carry;
336*5697Smcpowers /* diff 7 */
337*5697Smcpowers MP_SUB_BORROW(r0, a6h|a7l, r0, 0, carry);
338*5697Smcpowers MP_SUB_BORROW(r1, a7h|a4l, r1, carry, carry);
339*5697Smcpowers MP_SUB_BORROW(r2, a4h|a5l, r2, carry, carry);
340*5697Smcpowers MP_SUB_BORROW(r3, a6l, r3, carry, carry);
341*5697Smcpowers r4 -= carry;
342*5697Smcpowers /* diff 8 */
343*5697Smcpowers MP_SUB_BORROW(r0, a7, r0, 0, carry);
344*5697Smcpowers MP_SUB_BORROW(r1, a4h<<32, r1, carry, carry);
345*5697Smcpowers MP_SUB_BORROW(r2, a5, r2, carry, carry);
346*5697Smcpowers MP_SUB_BORROW(r3, a6h<<32, r3, carry, carry);
347*5697Smcpowers r4 -= carry;
348*5697Smcpowers
349*5697Smcpowers /* reduce the overflows */
350*5697Smcpowers while (r4 > 0) {
351*5697Smcpowers mp_digit r4_long = r4;
352*5697Smcpowers mp_digit r4l = (r4_long << 32);
353*5697Smcpowers MP_ADD_CARRY(r0, r4_long, r0, 0, carry);
354*5697Smcpowers MP_ADD_CARRY(r1, -r4l, r1, carry, carry);
355*5697Smcpowers MP_ADD_CARRY(r2, MP_DIGIT_MAX, r2, carry, carry);
356*5697Smcpowers MP_ADD_CARRY(r3, r4l-r4_long-1,r3, carry, carry);
357*5697Smcpowers r4 = carry;
358*5697Smcpowers }
359*5697Smcpowers
360*5697Smcpowers /* reduce the underflows */
361*5697Smcpowers while (r4 < 0) {
362*5697Smcpowers mp_digit r4_long = -r4;
363*5697Smcpowers mp_digit r4l = (r4_long << 32);
364*5697Smcpowers MP_SUB_BORROW(r0, r4_long, r0, 0, carry);
365*5697Smcpowers MP_SUB_BORROW(r1, -r4l, r1, carry, carry);
366*5697Smcpowers MP_SUB_BORROW(r2, MP_DIGIT_MAX, r2, carry, carry);
367*5697Smcpowers MP_SUB_BORROW(r3, r4l-r4_long-1,r3, carry, carry);
368*5697Smcpowers r4 = -carry;
369*5697Smcpowers }
370*5697Smcpowers
371*5697Smcpowers if (a != r) {
372*5697Smcpowers MP_CHECKOK(s_mp_pad(r,4));
373*5697Smcpowers }
374*5697Smcpowers MP_SIGN(r) = MP_ZPOS;
375*5697Smcpowers MP_USED(r) = 4;
376*5697Smcpowers
377*5697Smcpowers MP_DIGIT(r,3) = r3;
378*5697Smcpowers MP_DIGIT(r,2) = r2;
379*5697Smcpowers MP_DIGIT(r,1) = r1;
380*5697Smcpowers MP_DIGIT(r,0) = r0;
381*5697Smcpowers
382*5697Smcpowers /* final reduction if necessary */
383*5697Smcpowers if ((r3 > 0xFFFFFFFF00000001ULL) ||
384*5697Smcpowers ((r3 == 0xFFFFFFFF00000001ULL) &&
385*5697Smcpowers (r2 || (r1 >> 32)||
386*5697Smcpowers (r1 == 0xFFFFFFFFULL && r0 == MP_DIGIT_MAX)))) {
387*5697Smcpowers /* very rare, just use mp_sub */
388*5697Smcpowers MP_CHECKOK(mp_sub(r, &meth->irr, r));
389*5697Smcpowers }
390*5697Smcpowers
391*5697Smcpowers s_mp_clamp(r);
392*5697Smcpowers #endif
393*5697Smcpowers }
394*5697Smcpowers
395*5697Smcpowers CLEANUP:
396*5697Smcpowers return res;
397*5697Smcpowers }
398*5697Smcpowers
399*5697Smcpowers /* Compute the square of polynomial a, reduce modulo p256. Store the
400*5697Smcpowers * result in r. r could be a. Uses optimized modular reduction for p256.
401*5697Smcpowers */
402*5697Smcpowers mp_err
ec_GFp_nistp256_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)403*5697Smcpowers ec_GFp_nistp256_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
404*5697Smcpowers {
405*5697Smcpowers mp_err res = MP_OKAY;
406*5697Smcpowers
407*5697Smcpowers MP_CHECKOK(mp_sqr(a, r));
408*5697Smcpowers MP_CHECKOK(ec_GFp_nistp256_mod(r, r, meth));
409*5697Smcpowers CLEANUP:
410*5697Smcpowers return res;
411*5697Smcpowers }
412*5697Smcpowers
413*5697Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p256.
414*5697Smcpowers * Store the result in r. r could be a or b; a could be b. Uses
415*5697Smcpowers * optimized modular reduction for p256. */
416*5697Smcpowers mp_err
ec_GFp_nistp256_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)417*5697Smcpowers ec_GFp_nistp256_mul(const mp_int *a, const mp_int *b, mp_int *r,
418*5697Smcpowers const GFMethod *meth)
419*5697Smcpowers {
420*5697Smcpowers mp_err res = MP_OKAY;
421*5697Smcpowers
422*5697Smcpowers MP_CHECKOK(mp_mul(a, b, r));
423*5697Smcpowers MP_CHECKOK(ec_GFp_nistp256_mod(r, r, meth));
424*5697Smcpowers CLEANUP:
425*5697Smcpowers return res;
426*5697Smcpowers }
427*5697Smcpowers
428*5697Smcpowers /* Wire in fast field arithmetic and precomputation of base point for
429*5697Smcpowers * named curves. */
430*5697Smcpowers mp_err
ec_group_set_gfp256(ECGroup * group,ECCurveName name)431*5697Smcpowers ec_group_set_gfp256(ECGroup *group, ECCurveName name)
432*5697Smcpowers {
433*5697Smcpowers if (name == ECCurve_NIST_P256) {
434*5697Smcpowers group->meth->field_mod = &ec_GFp_nistp256_mod;
435*5697Smcpowers group->meth->field_mul = &ec_GFp_nistp256_mul;
436*5697Smcpowers group->meth->field_sqr = &ec_GFp_nistp256_sqr;
437*5697Smcpowers }
438*5697Smcpowers return MP_OKAY;
439*5697Smcpowers }
440