xref: /titanic_44/usr/src/common/crypto/ecc/ecl-priv.h (revision 69b3e10436272b0970d58743de375d0dd61046ce)
1 /*
2  * ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the elliptic curve math library.
16  *
17  * The Initial Developer of the Original Code is
18  * Sun Microsystems, Inc.
19  * Portions created by the Initial Developer are Copyright (C) 2003
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Stephen Fung <fungstep@hotmail.com> and
24  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 /*
40  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
41  * Use is subject to license terms.
42  *
43  * Sun elects to use this software under the MPL license.
44  */
45 
46 #ifndef _ECL_PRIV_H
47 #define _ECL_PRIV_H
48 
49 #pragma ident	"%Z%%M%	%I%	%E% SMI"
50 
51 #include "ecl.h"
52 #include "mpi.h"
53 #include "mplogic.h"
54 
55 /* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */
56 /* the following needs to go away... */
57 #if defined(MP_USE_LONG_LONG_DIGIT) || defined(MP_USE_LONG_DIGIT)
58 #define ECL_SIXTY_FOUR_BIT
59 #else
60 #define ECL_THIRTY_TWO_BIT
61 #endif
62 
63 #define ECL_CURVE_DIGITS(curve_size_in_bits) \
64 	(((curve_size_in_bits)+(sizeof(mp_digit)*8-1))/(sizeof(mp_digit)*8))
65 #define ECL_BITS (sizeof(mp_digit)*8)
66 #define ECL_MAX_FIELD_SIZE_DIGITS (80/sizeof(mp_digit))
67 
68 /* Gets the i'th bit in the binary representation of a. If i >= length(a),
69  * then return 0. (The above behaviour differs from mpl_get_bit, which
70  * causes an error if i >= length(a).) */
71 #define MP_GET_BIT(a, i) \
72 	((i) >= mpl_significant_bits((a))) ? 0 : mpl_get_bit((a), (i))
73 
74 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
75 #define MP_ADD_CARRY(a1, a2, s, cin, cout)   \
76     { mp_word w; \
77     w = ((mp_word)(cin)) + (a1) + (a2); \
78     s = ACCUM(w); \
79     cout = CARRYOUT(w); }
80 
81 #define MP_SUB_BORROW(a1, a2, s, bin, bout)   \
82     { mp_word w; \
83     w = ((mp_word)(a1)) - (a2) - (bin); \
84     s = ACCUM(w); \
85     bout = (w >> MP_DIGIT_BIT) & 1; }
86 
87 #else
88 /* NOTE,
89  * cin and cout could be the same variable.
90  * bin and bout could be the same variable.
91  * a1 or a2 and s could be the same variable.
92  * don't trash those outputs until their respective inputs have
93  * been read. */
94 #define MP_ADD_CARRY(a1, a2, s, cin, cout)   \
95     { mp_digit tmp,sum; \
96     tmp = (a1); \
97     sum = tmp + (a2); \
98     tmp = (sum < tmp);                     /* detect overflow */ \
99     s = sum += (cin); \
100     cout = tmp + (sum < (cin)); }
101 
102 #define MP_SUB_BORROW(a1, a2, s, bin, bout)   \
103     { mp_digit tmp; \
104     tmp = (a1); \
105     s = tmp - (a2); \
106     tmp = (s > tmp);                    /* detect borrow */ \
107     if ((bin) && !s--) tmp++;	\
108     bout = tmp; }
109 #endif
110 
111 
112 struct GFMethodStr;
113 typedef struct GFMethodStr GFMethod;
114 struct GFMethodStr {
115 	/* Indicates whether the structure was constructed from dynamic memory
116 	 * or statically created. */
117 	int constructed;
118 	/* Irreducible that defines the field. For prime fields, this is the
119 	 * prime p. For binary polynomial fields, this is the bitstring
120 	 * representation of the irreducible polynomial. */
121 	mp_int irr;
122 	/* For prime fields, the value irr_arr[0] is the number of bits in the
123 	 * field. For binary polynomial fields, the irreducible polynomial
124 	 * f(t) is represented as an array of unsigned int[], where f(t) is
125 	 * of the form: f(t) = t^p[0] + t^p[1] + ... + t^p[4] where m = p[0]
126 	 * > p[1] > ... > p[4] = 0. */
127 	unsigned int irr_arr[5];
128 	/* Field arithmetic methods. All methods (except field_enc and
129 	 * field_dec) are assumed to take field-encoded parameters and return
130 	 * field-encoded values. All methods (except field_enc and field_dec)
131 	 * are required to be implemented. */
132 	mp_err (*field_add) (const mp_int *a, const mp_int *b, mp_int *r,
133 						 const GFMethod *meth);
134 	mp_err (*field_neg) (const mp_int *a, mp_int *r, const GFMethod *meth);
135 	mp_err (*field_sub) (const mp_int *a, const mp_int *b, mp_int *r,
136 						 const GFMethod *meth);
137 	mp_err (*field_mod) (const mp_int *a, mp_int *r, const GFMethod *meth);
138 	mp_err (*field_mul) (const mp_int *a, const mp_int *b, mp_int *r,
139 						 const GFMethod *meth);
140 	mp_err (*field_sqr) (const mp_int *a, mp_int *r, const GFMethod *meth);
141 	mp_err (*field_div) (const mp_int *a, const mp_int *b, mp_int *r,
142 						 const GFMethod *meth);
143 	mp_err (*field_enc) (const mp_int *a, mp_int *r, const GFMethod *meth);
144 	mp_err (*field_dec) (const mp_int *a, mp_int *r, const GFMethod *meth);
145 	/* Extra storage for implementation-specific data.  Any memory
146 	 * allocated to these extra fields will be cleared by extra_free. */
147 	void *extra1;
148 	void *extra2;
149 	void (*extra_free) (GFMethod *meth);
150 };
151 
152 /* Construct generic GFMethods. */
153 GFMethod *GFMethod_consGFp(const mp_int *irr);
154 GFMethod *GFMethod_consGFp_mont(const mp_int *irr);
155 GFMethod *GFMethod_consGF2m(const mp_int *irr,
156 							const unsigned int irr_arr[5]);
157 /* Free the memory allocated (if any) to a GFMethod object. */
158 void GFMethod_free(GFMethod *meth);
159 
160 struct ECGroupStr {
161 	/* Indicates whether the structure was constructed from dynamic memory
162 	 * or statically created. */
163 	int constructed;
164 	/* Field definition and arithmetic. */
165 	GFMethod *meth;
166 	/* Textual representation of curve name, if any. */
167 	char *text;
168 #ifdef _KERNEL
169 	int text_len;
170 #endif
171 	/* Curve parameters, field-encoded. */
172 	mp_int curvea, curveb;
173 	/* x and y coordinates of the base point, field-encoded. */
174 	mp_int genx, geny;
175 	/* Order and cofactor of the base point. */
176 	mp_int order;
177 	int cofactor;
178 	/* Point arithmetic methods. All methods are assumed to take
179 	 * field-encoded parameters and return field-encoded values. All
180 	 * methods (except base_point_mul and points_mul) are required to be
181 	 * implemented. */
182 	mp_err (*point_add) (const mp_int *px, const mp_int *py,
183 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
184 						 mp_int *ry, const ECGroup *group);
185 	mp_err (*point_sub) (const mp_int *px, const mp_int *py,
186 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
187 						 mp_int *ry, const ECGroup *group);
188 	mp_err (*point_dbl) (const mp_int *px, const mp_int *py, mp_int *rx,
189 						 mp_int *ry, const ECGroup *group);
190 	mp_err (*point_mul) (const mp_int *n, const mp_int *px,
191 						 const mp_int *py, mp_int *rx, mp_int *ry,
192 						 const ECGroup *group);
193 	mp_err (*base_point_mul) (const mp_int *n, mp_int *rx, mp_int *ry,
194 							  const ECGroup *group);
195 	mp_err (*points_mul) (const mp_int *k1, const mp_int *k2,
196 						  const mp_int *px, const mp_int *py, mp_int *rx,
197 						  mp_int *ry, const ECGroup *group);
198 	mp_err (*validate_point) (const mp_int *px, const mp_int *py, const ECGroup *group);
199 	/* Extra storage for implementation-specific data.  Any memory
200 	 * allocated to these extra fields will be cleared by extra_free. */
201 	void *extra1;
202 	void *extra2;
203 	void (*extra_free) (ECGroup *group);
204 };
205 
206 /* Wrapper functions for generic prime field arithmetic. */
207 mp_err ec_GFp_add(const mp_int *a, const mp_int *b, mp_int *r,
208 				  const GFMethod *meth);
209 mp_err ec_GFp_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
210 mp_err ec_GFp_sub(const mp_int *a, const mp_int *b, mp_int *r,
211 				  const GFMethod *meth);
212 
213 /* fixed length in-line adds. Count is in words */
214 mp_err ec_GFp_add_3(const mp_int *a, const mp_int *b, mp_int *r,
215 				  const GFMethod *meth);
216 mp_err ec_GFp_add_4(const mp_int *a, const mp_int *b, mp_int *r,
217 				  const GFMethod *meth);
218 mp_err ec_GFp_add_5(const mp_int *a, const mp_int *b, mp_int *r,
219 				  const GFMethod *meth);
220 mp_err ec_GFp_add_6(const mp_int *a, const mp_int *b, mp_int *r,
221 				  const GFMethod *meth);
222 mp_err ec_GFp_sub_3(const mp_int *a, const mp_int *b, mp_int *r,
223 				  const GFMethod *meth);
224 mp_err ec_GFp_sub_4(const mp_int *a, const mp_int *b, mp_int *r,
225 				  const GFMethod *meth);
226 mp_err ec_GFp_sub_5(const mp_int *a, const mp_int *b, mp_int *r,
227 				  const GFMethod *meth);
228 mp_err ec_GFp_sub_6(const mp_int *a, const mp_int *b, mp_int *r,
229 				  const GFMethod *meth);
230 
231 mp_err ec_GFp_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
232 mp_err ec_GFp_mul(const mp_int *a, const mp_int *b, mp_int *r,
233 				  const GFMethod *meth);
234 mp_err ec_GFp_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
235 mp_err ec_GFp_div(const mp_int *a, const mp_int *b, mp_int *r,
236 				  const GFMethod *meth);
237 /* Wrapper functions for generic binary polynomial field arithmetic. */
238 mp_err ec_GF2m_add(const mp_int *a, const mp_int *b, mp_int *r,
239 				   const GFMethod *meth);
240 mp_err ec_GF2m_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
241 mp_err ec_GF2m_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
242 mp_err ec_GF2m_mul(const mp_int *a, const mp_int *b, mp_int *r,
243 				   const GFMethod *meth);
244 mp_err ec_GF2m_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
245 mp_err ec_GF2m_div(const mp_int *a, const mp_int *b, mp_int *r,
246 				   const GFMethod *meth);
247 
248 /* Montgomery prime field arithmetic. */
249 mp_err ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
250 					   const GFMethod *meth);
251 mp_err ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
252 mp_err ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
253 					   const GFMethod *meth);
254 mp_err ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
255 mp_err ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
256 void ec_GFp_extra_free_mont(GFMethod *meth);
257 
258 /* point multiplication */
259 mp_err ec_pts_mul_basic(const mp_int *k1, const mp_int *k2,
260 						const mp_int *px, const mp_int *py, mp_int *rx,
261 						mp_int *ry, const ECGroup *group);
262 mp_err ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2,
263 						   const mp_int *px, const mp_int *py, mp_int *rx,
264 						   mp_int *ry, const ECGroup *group);
265 
266 /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
267  * be an array of signed char's to output to, bitsize should be the number
268  * of bits of out, in is the original scalar, and w is the window size.
269  * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
270  * Menezes, "Software implementation of elliptic curve cryptography over
271  * binary fields", Proc. CHES 2000. */
272 mp_err ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in,
273 					   int w);
274 
275 /* Optimized field arithmetic */
276 mp_err ec_group_set_gfp192(ECGroup *group, ECCurveName);
277 mp_err ec_group_set_gfp224(ECGroup *group, ECCurveName);
278 mp_err ec_group_set_gfp256(ECGroup *group, ECCurveName);
279 mp_err ec_group_set_gfp384(ECGroup *group, ECCurveName);
280 mp_err ec_group_set_gfp521(ECGroup *group, ECCurveName);
281 mp_err ec_group_set_gf2m163(ECGroup *group, ECCurveName name);
282 mp_err ec_group_set_gf2m193(ECGroup *group, ECCurveName name);
283 mp_err ec_group_set_gf2m233(ECGroup *group, ECCurveName name);
284 
285 /* Optimized floating-point arithmetic */
286 #ifdef ECL_USE_FP
287 mp_err ec_group_set_secp160r1_fp(ECGroup *group);
288 mp_err ec_group_set_nistp192_fp(ECGroup *group);
289 mp_err ec_group_set_nistp224_fp(ECGroup *group);
290 #endif
291 
292 #endif /* _ECL_PRIV_H */
293