xref: /onnv-gate/usr/src/common/crypto/ecc/ecp_521.c (revision 5697:324be5104707)
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 #define ECP521_DIGITS ECL_CURVE_DIGITS(521)
56*5697Smcpowers 
57*5697Smcpowers /* Fast modular reduction for p521 = 2^521 - 1.  a can be r. Uses
58*5697Smcpowers  * algorithm 2.31 from Hankerson, Menezes, Vanstone. Guide to
59*5697Smcpowers  * Elliptic Curve Cryptography. */
60*5697Smcpowers mp_err
ec_GFp_nistp521_mod(const mp_int * a,mp_int * r,const GFMethod * meth)61*5697Smcpowers ec_GFp_nistp521_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
62*5697Smcpowers {
63*5697Smcpowers 	mp_err res = MP_OKAY;
64*5697Smcpowers 	int a_bits = mpl_significant_bits(a);
65*5697Smcpowers 	int i;
66*5697Smcpowers 
67*5697Smcpowers 	/* m1, m2 are statically-allocated mp_int of exactly the size we need */
68*5697Smcpowers 	mp_int m1;
69*5697Smcpowers 
70*5697Smcpowers 	mp_digit s1[ECP521_DIGITS] = { 0 };
71*5697Smcpowers 
72*5697Smcpowers 	MP_SIGN(&m1) = MP_ZPOS;
73*5697Smcpowers 	MP_ALLOC(&m1) = ECP521_DIGITS;
74*5697Smcpowers 	MP_USED(&m1) = ECP521_DIGITS;
75*5697Smcpowers 	MP_DIGITS(&m1) = s1;
76*5697Smcpowers 
77*5697Smcpowers 	if (a_bits < 521) {
78*5697Smcpowers 		if (a==r) return MP_OKAY;
79*5697Smcpowers 		return mp_copy(a, r);
80*5697Smcpowers 	}
81*5697Smcpowers 	/* for polynomials larger than twice the field size or polynomials
82*5697Smcpowers 	 * not using all words, use regular reduction */
83*5697Smcpowers 	if (a_bits > (521*2)) {
84*5697Smcpowers 		MP_CHECKOK(mp_mod(a, &meth->irr, r));
85*5697Smcpowers 	} else {
86*5697Smcpowers #define FIRST_DIGIT (ECP521_DIGITS-1)
87*5697Smcpowers 		for (i = FIRST_DIGIT; i < MP_USED(a)-1; i++) {
88*5697Smcpowers 			s1[i-FIRST_DIGIT] = (MP_DIGIT(a, i) >> 9)
89*5697Smcpowers 				| (MP_DIGIT(a, 1+i) << (MP_DIGIT_BIT-9));
90*5697Smcpowers 		}
91*5697Smcpowers 		s1[i-FIRST_DIGIT] = MP_DIGIT(a, i) >> 9;
92*5697Smcpowers 
93*5697Smcpowers 		if ( a != r ) {
94*5697Smcpowers 			MP_CHECKOK(s_mp_pad(r,ECP521_DIGITS));
95*5697Smcpowers 			for (i = 0; i < ECP521_DIGITS; i++) {
96*5697Smcpowers 				MP_DIGIT(r,i) = MP_DIGIT(a, i);
97*5697Smcpowers 			}
98*5697Smcpowers 		}
99*5697Smcpowers 		MP_USED(r) = ECP521_DIGITS;
100*5697Smcpowers 		MP_DIGIT(r,FIRST_DIGIT) &=  0x1FF;
101*5697Smcpowers 
102*5697Smcpowers 		MP_CHECKOK(s_mp_add(r, &m1));
103*5697Smcpowers 		if (MP_DIGIT(r, FIRST_DIGIT) & 0x200) {
104*5697Smcpowers 			MP_CHECKOK(s_mp_add_d(r,1));
105*5697Smcpowers 			MP_DIGIT(r,FIRST_DIGIT) &=  0x1FF;
106*5697Smcpowers 		}
107*5697Smcpowers 		s_mp_clamp(r);
108*5697Smcpowers 	}
109*5697Smcpowers 
110*5697Smcpowers   CLEANUP:
111*5697Smcpowers 	return res;
112*5697Smcpowers }
113*5697Smcpowers 
114*5697Smcpowers /* Compute the square of polynomial a, reduce modulo p521. Store the
115*5697Smcpowers  * result in r.  r could be a.  Uses optimized modular reduction for p521.
116*5697Smcpowers  */
117*5697Smcpowers mp_err
ec_GFp_nistp521_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)118*5697Smcpowers ec_GFp_nistp521_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
119*5697Smcpowers {
120*5697Smcpowers 	mp_err res = MP_OKAY;
121*5697Smcpowers 
122*5697Smcpowers 	MP_CHECKOK(mp_sqr(a, r));
123*5697Smcpowers 	MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
124*5697Smcpowers   CLEANUP:
125*5697Smcpowers 	return res;
126*5697Smcpowers }
127*5697Smcpowers 
128*5697Smcpowers /* Compute the product of two polynomials a and b, reduce modulo p521.
129*5697Smcpowers  * Store the result in r.  r could be a or b; a could be b.  Uses
130*5697Smcpowers  * optimized modular reduction for p521. */
131*5697Smcpowers mp_err
ec_GFp_nistp521_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)132*5697Smcpowers ec_GFp_nistp521_mul(const mp_int *a, const mp_int *b, mp_int *r,
133*5697Smcpowers 					const GFMethod *meth)
134*5697Smcpowers {
135*5697Smcpowers 	mp_err res = MP_OKAY;
136*5697Smcpowers 
137*5697Smcpowers 	MP_CHECKOK(mp_mul(a, b, r));
138*5697Smcpowers 	MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
139*5697Smcpowers   CLEANUP:
140*5697Smcpowers 	return res;
141*5697Smcpowers }
142*5697Smcpowers 
143*5697Smcpowers /* Divides two field elements. If a is NULL, then returns the inverse of
144*5697Smcpowers  * b. */
145*5697Smcpowers mp_err
ec_GFp_nistp521_div(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)146*5697Smcpowers ec_GFp_nistp521_div(const mp_int *a, const mp_int *b, mp_int *r,
147*5697Smcpowers 		   const GFMethod *meth)
148*5697Smcpowers {
149*5697Smcpowers 	mp_err res = MP_OKAY;
150*5697Smcpowers 	mp_int t;
151*5697Smcpowers 
152*5697Smcpowers 	/* If a is NULL, then return the inverse of b, otherwise return a/b. */
153*5697Smcpowers 	if (a == NULL) {
154*5697Smcpowers 		return mp_invmod(b, &meth->irr, r);
155*5697Smcpowers 	} else {
156*5697Smcpowers 		/* MPI doesn't support divmod, so we implement it using invmod and
157*5697Smcpowers 		 * mulmod. */
158*5697Smcpowers 		MP_CHECKOK(mp_init(&t, FLAG(b)));
159*5697Smcpowers 		MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
160*5697Smcpowers 		MP_CHECKOK(mp_mul(a, &t, r));
161*5697Smcpowers 		MP_CHECKOK(ec_GFp_nistp521_mod(r, r, meth));
162*5697Smcpowers 	  CLEANUP:
163*5697Smcpowers 		mp_clear(&t);
164*5697Smcpowers 		return res;
165*5697Smcpowers 	}
166*5697Smcpowers }
167*5697Smcpowers 
168*5697Smcpowers /* Wire in fast field arithmetic and precomputation of base point for
169*5697Smcpowers  * named curves. */
170*5697Smcpowers mp_err
ec_group_set_gfp521(ECGroup * group,ECCurveName name)171*5697Smcpowers ec_group_set_gfp521(ECGroup *group, ECCurveName name)
172*5697Smcpowers {
173*5697Smcpowers 	if (name == ECCurve_NIST_P521) {
174*5697Smcpowers 		group->meth->field_mod = &ec_GFp_nistp521_mod;
175*5697Smcpowers 		group->meth->field_mul = &ec_GFp_nistp521_mul;
176*5697Smcpowers 		group->meth->field_sqr = &ec_GFp_nistp521_sqr;
177*5697Smcpowers 		group->meth->field_div = &ec_GFp_nistp521_div;
178*5697Smcpowers 	}
179*5697Smcpowers 	return MP_OKAY;
180*5697Smcpowers }
181