xref: /onnv-gate/usr/src/common/crypto/ecc/ec_naf.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  *   Stephen Fung <fungstep@hotmail.com>, 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 #include "ecl-priv.h"
48*5697Smcpowers 
49*5697Smcpowers /* Returns 2^e as an integer. This is meant to be used for small powers of
50*5697Smcpowers  * two. */
51*5697Smcpowers int
ec_twoTo(int e)52*5697Smcpowers ec_twoTo(int e)
53*5697Smcpowers {
54*5697Smcpowers 	int a = 1;
55*5697Smcpowers 	int i;
56*5697Smcpowers 
57*5697Smcpowers 	for (i = 0; i < e; i++) {
58*5697Smcpowers 		a *= 2;
59*5697Smcpowers 	}
60*5697Smcpowers 	return a;
61*5697Smcpowers }
62*5697Smcpowers 
63*5697Smcpowers /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
64*5697Smcpowers  * be an array of signed char's to output to, bitsize should be the number
65*5697Smcpowers  * of bits of out, in is the original scalar, and w is the window size.
66*5697Smcpowers  * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
67*5697Smcpowers  * Menezes, "Software implementation of elliptic curve cryptography over
68*5697Smcpowers  * binary fields", Proc. CHES 2000. */
69*5697Smcpowers mp_err
ec_compute_wNAF(signed char * out,int bitsize,const mp_int * in,int w)70*5697Smcpowers ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, int w)
71*5697Smcpowers {
72*5697Smcpowers 	mp_int k;
73*5697Smcpowers 	mp_err res = MP_OKAY;
74*5697Smcpowers 	int i, twowm1, mask;
75*5697Smcpowers 
76*5697Smcpowers 	twowm1 = ec_twoTo(w - 1);
77*5697Smcpowers 	mask = 2 * twowm1 - 1;
78*5697Smcpowers 
79*5697Smcpowers 	MP_DIGITS(&k) = 0;
80*5697Smcpowers 	MP_CHECKOK(mp_init_copy(&k, in));
81*5697Smcpowers 
82*5697Smcpowers 	i = 0;
83*5697Smcpowers 	/* Compute wNAF form */
84*5697Smcpowers 	while (mp_cmp_z(&k) > 0) {
85*5697Smcpowers 		if (mp_isodd(&k)) {
86*5697Smcpowers 			out[i] = MP_DIGIT(&k, 0) & mask;
87*5697Smcpowers 			if (out[i] >= twowm1)
88*5697Smcpowers 				out[i] -= 2 * twowm1;
89*5697Smcpowers 
90*5697Smcpowers 			/* Subtract off out[i].  Note mp_sub_d only works with
91*5697Smcpowers 			 * unsigned digits */
92*5697Smcpowers 			if (out[i] >= 0) {
93*5697Smcpowers 				mp_sub_d(&k, out[i], &k);
94*5697Smcpowers 			} else {
95*5697Smcpowers 				mp_add_d(&k, -(out[i]), &k);
96*5697Smcpowers 			}
97*5697Smcpowers 		} else {
98*5697Smcpowers 			out[i] = 0;
99*5697Smcpowers 		}
100*5697Smcpowers 		mp_div_2(&k, &k);
101*5697Smcpowers 		i++;
102*5697Smcpowers 	}
103*5697Smcpowers 	/* Zero out the remaining elements of the out array. */
104*5697Smcpowers 	for (; i < bitsize + 1; i++) {
105*5697Smcpowers 		out[i] = 0;
106*5697Smcpowers 	}
107*5697Smcpowers   CLEANUP:
108*5697Smcpowers 	mp_clear(&k);
109*5697Smcpowers 	return res;
110*5697Smcpowers 
111*5697Smcpowers }
112