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