xref: /onnv-gate/usr/src/common/crypto/ecc/ecp_mont.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.
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>, Sun Microsystems Laboratories
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 /* Uses Montgomery reduction for field arithmetic.  See mpi/mpmontg.c for
48*5697Smcpowers  * code implementation. */
49*5697Smcpowers 
50*5697Smcpowers #include "mpi.h"
51*5697Smcpowers #include "mplogic.h"
52*5697Smcpowers #include "mpi-priv.h"
53*5697Smcpowers #include "ecl-priv.h"
54*5697Smcpowers #include "ecp.h"
55*5697Smcpowers #ifndef _KERNEL
56*5697Smcpowers #include <stdlib.h>
57*5697Smcpowers #include <stdio.h>
58*5697Smcpowers #endif
59*5697Smcpowers 
60*5697Smcpowers /* Construct a generic GFMethod for arithmetic over prime fields with
61*5697Smcpowers  * irreducible irr. */
62*5697Smcpowers GFMethod *
GFMethod_consGFp_mont(const mp_int * irr)63*5697Smcpowers GFMethod_consGFp_mont(const mp_int *irr)
64*5697Smcpowers {
65*5697Smcpowers 	mp_err res = MP_OKAY;
66*5697Smcpowers 	int i;
67*5697Smcpowers 	GFMethod *meth = NULL;
68*5697Smcpowers 	mp_mont_modulus *mmm;
69*5697Smcpowers 
70*5697Smcpowers 	meth = GFMethod_consGFp(irr);
71*5697Smcpowers 	if (meth == NULL)
72*5697Smcpowers 		return NULL;
73*5697Smcpowers 
74*5697Smcpowers #ifdef _KERNEL
75*5697Smcpowers 	mmm = (mp_mont_modulus *) kmem_alloc(sizeof(mp_mont_modulus),
76*5697Smcpowers 	    FLAG(irr));
77*5697Smcpowers #else
78*5697Smcpowers 	mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus));
79*5697Smcpowers #endif
80*5697Smcpowers 	if (mmm == NULL) {
81*5697Smcpowers 		res = MP_MEM;
82*5697Smcpowers 		goto CLEANUP;
83*5697Smcpowers 	}
84*5697Smcpowers 
85*5697Smcpowers 	meth->field_mul = &ec_GFp_mul_mont;
86*5697Smcpowers 	meth->field_sqr = &ec_GFp_sqr_mont;
87*5697Smcpowers 	meth->field_div = &ec_GFp_div_mont;
88*5697Smcpowers 	meth->field_enc = &ec_GFp_enc_mont;
89*5697Smcpowers 	meth->field_dec = &ec_GFp_dec_mont;
90*5697Smcpowers 	meth->extra1 = mmm;
91*5697Smcpowers 	meth->extra2 = NULL;
92*5697Smcpowers 	meth->extra_free = &ec_GFp_extra_free_mont;
93*5697Smcpowers 
94*5697Smcpowers 	mmm->N = meth->irr;
95*5697Smcpowers 	i = mpl_significant_bits(&meth->irr);
96*5697Smcpowers 	i += MP_DIGIT_BIT - 1;
97*5697Smcpowers 	mmm->b = i - i % MP_DIGIT_BIT;
98*5697Smcpowers 	mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0));
99*5697Smcpowers 
100*5697Smcpowers   CLEANUP:
101*5697Smcpowers 	if (res != MP_OKAY) {
102*5697Smcpowers 		GFMethod_free(meth);
103*5697Smcpowers 		return NULL;
104*5697Smcpowers 	}
105*5697Smcpowers 	return meth;
106*5697Smcpowers }
107*5697Smcpowers 
108*5697Smcpowers /* Wrapper functions for generic prime field arithmetic. */
109*5697Smcpowers 
110*5697Smcpowers /* Field multiplication using Montgomery reduction. */
111*5697Smcpowers mp_err
ec_GFp_mul_mont(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)112*5697Smcpowers ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
113*5697Smcpowers 				const GFMethod *meth)
114*5697Smcpowers {
115*5697Smcpowers 	mp_err res = MP_OKAY;
116*5697Smcpowers 
117*5697Smcpowers #ifdef MP_MONT_USE_MP_MUL
118*5697Smcpowers 	/* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont
119*5697Smcpowers 	 * is not implemented and we have to use mp_mul and s_mp_redc directly
120*5697Smcpowers 	 */
121*5697Smcpowers 	MP_CHECKOK(mp_mul(a, b, r));
122*5697Smcpowers 	MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
123*5697Smcpowers #else
124*5697Smcpowers 	mp_int s;
125*5697Smcpowers 
126*5697Smcpowers 	MP_DIGITS(&s) = 0;
127*5697Smcpowers 	/* s_mp_mul_mont doesn't allow source and destination to be the same */
128*5697Smcpowers 	if ((a == r) || (b == r)) {
129*5697Smcpowers 		MP_CHECKOK(mp_init(&s, FLAG(a)));
130*5697Smcpowers 		MP_CHECKOK(s_mp_mul_mont
131*5697Smcpowers 				   (a, b, &s, (mp_mont_modulus *) meth->extra1));
132*5697Smcpowers 		MP_CHECKOK(mp_copy(&s, r));
133*5697Smcpowers 		mp_clear(&s);
134*5697Smcpowers 	} else {
135*5697Smcpowers 		return s_mp_mul_mont(a, b, r, (mp_mont_modulus *) meth->extra1);
136*5697Smcpowers 	}
137*5697Smcpowers #endif
138*5697Smcpowers   CLEANUP:
139*5697Smcpowers 	return res;
140*5697Smcpowers }
141*5697Smcpowers 
142*5697Smcpowers /* Field squaring using Montgomery reduction. */
143*5697Smcpowers mp_err
ec_GFp_sqr_mont(const mp_int * a,mp_int * r,const GFMethod * meth)144*5697Smcpowers ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
145*5697Smcpowers {
146*5697Smcpowers 	return ec_GFp_mul_mont(a, a, r, meth);
147*5697Smcpowers }
148*5697Smcpowers 
149*5697Smcpowers /* Field division using Montgomery reduction. */
150*5697Smcpowers mp_err
ec_GFp_div_mont(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)151*5697Smcpowers ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
152*5697Smcpowers 				const GFMethod *meth)
153*5697Smcpowers {
154*5697Smcpowers 	mp_err res = MP_OKAY;
155*5697Smcpowers 
156*5697Smcpowers 	/* if A=aZ represents a encoded in montgomery coordinates with Z and #
157*5697Smcpowers 	 * and \ respectively represent multiplication and division in
158*5697Smcpowers 	 * montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv =
159*5697Smcpowers 	 * (1/b)Z = (1/B)(Z^2) where B # Binv = Z */
160*5697Smcpowers 	MP_CHECKOK(ec_GFp_div(a, b, r, meth));
161*5697Smcpowers 	MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
162*5697Smcpowers 	if (a == NULL) {
163*5697Smcpowers 		MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
164*5697Smcpowers 	}
165*5697Smcpowers   CLEANUP:
166*5697Smcpowers 	return res;
167*5697Smcpowers }
168*5697Smcpowers 
169*5697Smcpowers /* Encode a field element in Montgomery form. See s_mp_to_mont in
170*5697Smcpowers  * mpi/mpmontg.c */
171*5697Smcpowers mp_err
ec_GFp_enc_mont(const mp_int * a,mp_int * r,const GFMethod * meth)172*5697Smcpowers ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
173*5697Smcpowers {
174*5697Smcpowers 	mp_mont_modulus *mmm;
175*5697Smcpowers 	mp_err res = MP_OKAY;
176*5697Smcpowers 
177*5697Smcpowers 	mmm = (mp_mont_modulus *) meth->extra1;
178*5697Smcpowers 	MP_CHECKOK(mpl_lsh(a, r, mmm->b));
179*5697Smcpowers 	MP_CHECKOK(mp_mod(r, &mmm->N, r));
180*5697Smcpowers   CLEANUP:
181*5697Smcpowers 	return res;
182*5697Smcpowers }
183*5697Smcpowers 
184*5697Smcpowers /* Decode a field element from Montgomery form. */
185*5697Smcpowers mp_err
ec_GFp_dec_mont(const mp_int * a,mp_int * r,const GFMethod * meth)186*5697Smcpowers ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
187*5697Smcpowers {
188*5697Smcpowers 	mp_err res = MP_OKAY;
189*5697Smcpowers 
190*5697Smcpowers 	if (a != r) {
191*5697Smcpowers 		MP_CHECKOK(mp_copy(a, r));
192*5697Smcpowers 	}
193*5697Smcpowers 	MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
194*5697Smcpowers   CLEANUP:
195*5697Smcpowers 	return res;
196*5697Smcpowers }
197*5697Smcpowers 
198*5697Smcpowers /* Free the memory allocated to the extra fields of Montgomery GFMethod
199*5697Smcpowers  * object. */
200*5697Smcpowers void
ec_GFp_extra_free_mont(GFMethod * meth)201*5697Smcpowers ec_GFp_extra_free_mont(GFMethod *meth)
202*5697Smcpowers {
203*5697Smcpowers 	if (meth->extra1 != NULL) {
204*5697Smcpowers #ifdef _KERNEL
205*5697Smcpowers 		kmem_free(meth->extra1, sizeof(mp_mont_modulus));
206*5697Smcpowers #else
207*5697Smcpowers 		free(meth->extra1);
208*5697Smcpowers #endif
209*5697Smcpowers 		meth->extra1 = NULL;
210*5697Smcpowers 	}
211*5697Smcpowers }
212