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