xref: /titanic_52/usr/src/common/crypto/ecc/ecp_mont.c (revision f9fbec18f5b458b560ecf45d3db8e8bd56bf6942)
1*f9fbec18Smcpowers /*
2*f9fbec18Smcpowers  * ***** BEGIN LICENSE BLOCK *****
3*f9fbec18Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4*f9fbec18Smcpowers  *
5*f9fbec18Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
6*f9fbec18Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
7*f9fbec18Smcpowers  * the License. You may obtain a copy of the License at
8*f9fbec18Smcpowers  * http://www.mozilla.org/MPL/
9*f9fbec18Smcpowers  *
10*f9fbec18Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
11*f9fbec18Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12*f9fbec18Smcpowers  * for the specific language governing rights and limitations under the
13*f9fbec18Smcpowers  * License.
14*f9fbec18Smcpowers  *
15*f9fbec18Smcpowers  * The Original Code is the elliptic curve math library.
16*f9fbec18Smcpowers  *
17*f9fbec18Smcpowers  * The Initial Developer of the Original Code is
18*f9fbec18Smcpowers  * Sun Microsystems, Inc.
19*f9fbec18Smcpowers  * Portions created by the Initial Developer are Copyright (C) 2003
20*f9fbec18Smcpowers  * the Initial Developer. All Rights Reserved.
21*f9fbec18Smcpowers  *
22*f9fbec18Smcpowers  * Contributor(s):
23*f9fbec18Smcpowers  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24*f9fbec18Smcpowers  *
25*f9fbec18Smcpowers  * Alternatively, the contents of this file may be used under the terms of
26*f9fbec18Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
27*f9fbec18Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28*f9fbec18Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
29*f9fbec18Smcpowers  * of those above. If you wish to allow use of your version of this file only
30*f9fbec18Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
31*f9fbec18Smcpowers  * use your version of this file under the terms of the MPL, indicate your
32*f9fbec18Smcpowers  * decision by deleting the provisions above and replace them with the notice
33*f9fbec18Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
34*f9fbec18Smcpowers  * the provisions above, a recipient may use your version of this file under
35*f9fbec18Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
36*f9fbec18Smcpowers  *
37*f9fbec18Smcpowers  * ***** END LICENSE BLOCK ***** */
38*f9fbec18Smcpowers /*
39*f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
40*f9fbec18Smcpowers  * Use is subject to license terms.
41*f9fbec18Smcpowers  *
42*f9fbec18Smcpowers  * Sun elects to use this software under the MPL license.
43*f9fbec18Smcpowers  */
44*f9fbec18Smcpowers 
45*f9fbec18Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
46*f9fbec18Smcpowers 
47*f9fbec18Smcpowers /* Uses Montgomery reduction for field arithmetic.  See mpi/mpmontg.c for
48*f9fbec18Smcpowers  * code implementation. */
49*f9fbec18Smcpowers 
50*f9fbec18Smcpowers #include "mpi.h"
51*f9fbec18Smcpowers #include "mplogic.h"
52*f9fbec18Smcpowers #include "mpi-priv.h"
53*f9fbec18Smcpowers #include "ecl-priv.h"
54*f9fbec18Smcpowers #include "ecp.h"
55*f9fbec18Smcpowers #ifndef _KERNEL
56*f9fbec18Smcpowers #include <stdlib.h>
57*f9fbec18Smcpowers #include <stdio.h>
58*f9fbec18Smcpowers #endif
59*f9fbec18Smcpowers 
60*f9fbec18Smcpowers /* Construct a generic GFMethod for arithmetic over prime fields with
61*f9fbec18Smcpowers  * irreducible irr. */
62*f9fbec18Smcpowers GFMethod *
63*f9fbec18Smcpowers GFMethod_consGFp_mont(const mp_int *irr)
64*f9fbec18Smcpowers {
65*f9fbec18Smcpowers 	mp_err res = MP_OKAY;
66*f9fbec18Smcpowers 	int i;
67*f9fbec18Smcpowers 	GFMethod *meth = NULL;
68*f9fbec18Smcpowers 	mp_mont_modulus *mmm;
69*f9fbec18Smcpowers 
70*f9fbec18Smcpowers 	meth = GFMethod_consGFp(irr);
71*f9fbec18Smcpowers 	if (meth == NULL)
72*f9fbec18Smcpowers 		return NULL;
73*f9fbec18Smcpowers 
74*f9fbec18Smcpowers #ifdef _KERNEL
75*f9fbec18Smcpowers 	mmm = (mp_mont_modulus *) kmem_alloc(sizeof(mp_mont_modulus),
76*f9fbec18Smcpowers 	    FLAG(irr));
77*f9fbec18Smcpowers #else
78*f9fbec18Smcpowers 	mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus));
79*f9fbec18Smcpowers #endif
80*f9fbec18Smcpowers 	if (mmm == NULL) {
81*f9fbec18Smcpowers 		res = MP_MEM;
82*f9fbec18Smcpowers 		goto CLEANUP;
83*f9fbec18Smcpowers 	}
84*f9fbec18Smcpowers 
85*f9fbec18Smcpowers 	meth->field_mul = &ec_GFp_mul_mont;
86*f9fbec18Smcpowers 	meth->field_sqr = &ec_GFp_sqr_mont;
87*f9fbec18Smcpowers 	meth->field_div = &ec_GFp_div_mont;
88*f9fbec18Smcpowers 	meth->field_enc = &ec_GFp_enc_mont;
89*f9fbec18Smcpowers 	meth->field_dec = &ec_GFp_dec_mont;
90*f9fbec18Smcpowers 	meth->extra1 = mmm;
91*f9fbec18Smcpowers 	meth->extra2 = NULL;
92*f9fbec18Smcpowers 	meth->extra_free = &ec_GFp_extra_free_mont;
93*f9fbec18Smcpowers 
94*f9fbec18Smcpowers 	mmm->N = meth->irr;
95*f9fbec18Smcpowers 	i = mpl_significant_bits(&meth->irr);
96*f9fbec18Smcpowers 	i += MP_DIGIT_BIT - 1;
97*f9fbec18Smcpowers 	mmm->b = i - i % MP_DIGIT_BIT;
98*f9fbec18Smcpowers 	mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0));
99*f9fbec18Smcpowers 
100*f9fbec18Smcpowers   CLEANUP:
101*f9fbec18Smcpowers 	if (res != MP_OKAY) {
102*f9fbec18Smcpowers 		GFMethod_free(meth);
103*f9fbec18Smcpowers 		return NULL;
104*f9fbec18Smcpowers 	}
105*f9fbec18Smcpowers 	return meth;
106*f9fbec18Smcpowers }
107*f9fbec18Smcpowers 
108*f9fbec18Smcpowers /* Wrapper functions for generic prime field arithmetic. */
109*f9fbec18Smcpowers 
110*f9fbec18Smcpowers /* Field multiplication using Montgomery reduction. */
111*f9fbec18Smcpowers mp_err
112*f9fbec18Smcpowers ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
113*f9fbec18Smcpowers 				const GFMethod *meth)
114*f9fbec18Smcpowers {
115*f9fbec18Smcpowers 	mp_err res = MP_OKAY;
116*f9fbec18Smcpowers 
117*f9fbec18Smcpowers #ifdef MP_MONT_USE_MP_MUL
118*f9fbec18Smcpowers 	/* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont
119*f9fbec18Smcpowers 	 * is not implemented and we have to use mp_mul and s_mp_redc directly
120*f9fbec18Smcpowers 	 */
121*f9fbec18Smcpowers 	MP_CHECKOK(mp_mul(a, b, r));
122*f9fbec18Smcpowers 	MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
123*f9fbec18Smcpowers #else
124*f9fbec18Smcpowers 	mp_int s;
125*f9fbec18Smcpowers 
126*f9fbec18Smcpowers 	MP_DIGITS(&s) = 0;
127*f9fbec18Smcpowers 	/* s_mp_mul_mont doesn't allow source and destination to be the same */
128*f9fbec18Smcpowers 	if ((a == r) || (b == r)) {
129*f9fbec18Smcpowers 		MP_CHECKOK(mp_init(&s, FLAG(a)));
130*f9fbec18Smcpowers 		MP_CHECKOK(s_mp_mul_mont
131*f9fbec18Smcpowers 				   (a, b, &s, (mp_mont_modulus *) meth->extra1));
132*f9fbec18Smcpowers 		MP_CHECKOK(mp_copy(&s, r));
133*f9fbec18Smcpowers 		mp_clear(&s);
134*f9fbec18Smcpowers 	} else {
135*f9fbec18Smcpowers 		return s_mp_mul_mont(a, b, r, (mp_mont_modulus *) meth->extra1);
136*f9fbec18Smcpowers 	}
137*f9fbec18Smcpowers #endif
138*f9fbec18Smcpowers   CLEANUP:
139*f9fbec18Smcpowers 	return res;
140*f9fbec18Smcpowers }
141*f9fbec18Smcpowers 
142*f9fbec18Smcpowers /* Field squaring using Montgomery reduction. */
143*f9fbec18Smcpowers mp_err
144*f9fbec18Smcpowers ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
145*f9fbec18Smcpowers {
146*f9fbec18Smcpowers 	return ec_GFp_mul_mont(a, a, r, meth);
147*f9fbec18Smcpowers }
148*f9fbec18Smcpowers 
149*f9fbec18Smcpowers /* Field division using Montgomery reduction. */
150*f9fbec18Smcpowers mp_err
151*f9fbec18Smcpowers ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
152*f9fbec18Smcpowers 				const GFMethod *meth)
153*f9fbec18Smcpowers {
154*f9fbec18Smcpowers 	mp_err res = MP_OKAY;
155*f9fbec18Smcpowers 
156*f9fbec18Smcpowers 	/* if A=aZ represents a encoded in montgomery coordinates with Z and #
157*f9fbec18Smcpowers 	 * and \ respectively represent multiplication and division in
158*f9fbec18Smcpowers 	 * montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv =
159*f9fbec18Smcpowers 	 * (1/b)Z = (1/B)(Z^2) where B # Binv = Z */
160*f9fbec18Smcpowers 	MP_CHECKOK(ec_GFp_div(a, b, r, meth));
161*f9fbec18Smcpowers 	MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
162*f9fbec18Smcpowers 	if (a == NULL) {
163*f9fbec18Smcpowers 		MP_CHECKOK(ec_GFp_enc_mont(r, r, meth));
164*f9fbec18Smcpowers 	}
165*f9fbec18Smcpowers   CLEANUP:
166*f9fbec18Smcpowers 	return res;
167*f9fbec18Smcpowers }
168*f9fbec18Smcpowers 
169*f9fbec18Smcpowers /* Encode a field element in Montgomery form. See s_mp_to_mont in
170*f9fbec18Smcpowers  * mpi/mpmontg.c */
171*f9fbec18Smcpowers mp_err
172*f9fbec18Smcpowers ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
173*f9fbec18Smcpowers {
174*f9fbec18Smcpowers 	mp_mont_modulus *mmm;
175*f9fbec18Smcpowers 	mp_err res = MP_OKAY;
176*f9fbec18Smcpowers 
177*f9fbec18Smcpowers 	mmm = (mp_mont_modulus *) meth->extra1;
178*f9fbec18Smcpowers 	MP_CHECKOK(mpl_lsh(a, r, mmm->b));
179*f9fbec18Smcpowers 	MP_CHECKOK(mp_mod(r, &mmm->N, r));
180*f9fbec18Smcpowers   CLEANUP:
181*f9fbec18Smcpowers 	return res;
182*f9fbec18Smcpowers }
183*f9fbec18Smcpowers 
184*f9fbec18Smcpowers /* Decode a field element from Montgomery form. */
185*f9fbec18Smcpowers mp_err
186*f9fbec18Smcpowers ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth)
187*f9fbec18Smcpowers {
188*f9fbec18Smcpowers 	mp_err res = MP_OKAY;
189*f9fbec18Smcpowers 
190*f9fbec18Smcpowers 	if (a != r) {
191*f9fbec18Smcpowers 		MP_CHECKOK(mp_copy(a, r));
192*f9fbec18Smcpowers 	}
193*f9fbec18Smcpowers 	MP_CHECKOK(s_mp_redc(r, (mp_mont_modulus *) meth->extra1));
194*f9fbec18Smcpowers   CLEANUP:
195*f9fbec18Smcpowers 	return res;
196*f9fbec18Smcpowers }
197*f9fbec18Smcpowers 
198*f9fbec18Smcpowers /* Free the memory allocated to the extra fields of Montgomery GFMethod
199*f9fbec18Smcpowers  * object. */
200*f9fbec18Smcpowers void
201*f9fbec18Smcpowers ec_GFp_extra_free_mont(GFMethod *meth)
202*f9fbec18Smcpowers {
203*f9fbec18Smcpowers 	if (meth->extra1 != NULL) {
204*f9fbec18Smcpowers #ifdef _KERNEL
205*f9fbec18Smcpowers 		kmem_free(meth->extra1, sizeof(mp_mont_modulus));
206*f9fbec18Smcpowers #else
207*f9fbec18Smcpowers 		free(meth->extra1);
208*f9fbec18Smcpowers #endif
209*f9fbec18Smcpowers 		meth->extra1 = NULL;
210*f9fbec18Smcpowers 	}
211*f9fbec18Smcpowers }
212