security/nss/lib/freebl/ecl/ecl-priv.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef __ecl_priv_h_
     6 #define __ecl_priv_h_
     8 #include "ecl.h"
     9 #include "mpi.h"
    10 #include "mplogic.h"
    12 /* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */
    13 /* the following needs to go away... */
    14 #if defined(MP_USE_LONG_LONG_DIGIT) || defined(MP_USE_LONG_DIGIT)
    15 #define ECL_SIXTY_FOUR_BIT
    16 #else
    17 #define ECL_THIRTY_TWO_BIT
    18 #endif
    20 #define ECL_CURVE_DIGITS(curve_size_in_bits) \
    21 	(((curve_size_in_bits)+(sizeof(mp_digit)*8-1))/(sizeof(mp_digit)*8))
    22 #define ECL_BITS (sizeof(mp_digit)*8)
    23 #define ECL_MAX_FIELD_SIZE_DIGITS (80/sizeof(mp_digit))
    25 /* Gets the i'th bit in the binary representation of a. If i >= length(a), 
    26  * then return 0. (The above behaviour differs from mpl_get_bit, which
    27  * causes an error if i >= length(a).) */
    28 #define MP_GET_BIT(a, i) \
    29 	((i) >= mpl_significant_bits((a))) ? 0 : mpl_get_bit((a), (i))
    31 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
    32 #define MP_ADD_CARRY(a1, a2, s, cin, cout)   \
    33     { mp_word w; \
    34     w = ((mp_word)(cin)) + (a1) + (a2); \
    35     s = ACCUM(w); \
    36     cout = CARRYOUT(w); }
    38 #define MP_SUB_BORROW(a1, a2, s, bin, bout)   \
    39     { mp_word w; \
    40     w = ((mp_word)(a1)) - (a2) - (bin); \
    41     s = ACCUM(w); \
    42     bout = (w >> MP_DIGIT_BIT) & 1; }
    44 #else
    45 /* NOTE, 
    46  * cin and cout could be the same variable.
    47  * bin and bout could be the same variable.
    48  * a1 or a2 and s could be the same variable.
    49  * don't trash those outputs until their respective inputs have
    50  * been read. */
    51 #define MP_ADD_CARRY(a1, a2, s, cin, cout)   \
    52     { mp_digit tmp,sum; \
    53     tmp = (a1); \
    54     sum = tmp + (a2); \
    55     tmp = (sum < tmp);                     /* detect overflow */ \
    56     s = sum += (cin); \
    57     cout = tmp + (sum < (cin)); }
    59 #define MP_SUB_BORROW(a1, a2, s, bin, bout)   \
    60     { mp_digit tmp; \
    61     tmp = (a1); \
    62     s = tmp - (a2); \
    63     tmp = (s > tmp);                    /* detect borrow */ \
    64     if ((bin) && !s--) tmp++;	\
    65     bout = tmp; }
    66 #endif
    69 struct GFMethodStr;
    70 typedef struct GFMethodStr GFMethod;
    71 struct GFMethodStr {
    72 	/* Indicates whether the structure was constructed from dynamic memory 
    73 	 * or statically created. */
    74 	int constructed;
    75 	/* Irreducible that defines the field. For prime fields, this is the
    76 	 * prime p. For binary polynomial fields, this is the bitstring
    77 	 * representation of the irreducible polynomial. */
    78 	mp_int irr;
    79 	/* For prime fields, the value irr_arr[0] is the number of bits in the 
    80 	 * field. For binary polynomial fields, the irreducible polynomial
    81 	 * f(t) is represented as an array of unsigned int[], where f(t) is
    82 	 * of the form: f(t) = t^p[0] + t^p[1] + ... + t^p[4] where m = p[0]
    83 	 * > p[1] > ... > p[4] = 0. */
    84 	unsigned int irr_arr[5];
    85 	/* Field arithmetic methods. All methods (except field_enc and
    86 	 * field_dec) are assumed to take field-encoded parameters and return
    87 	 * field-encoded values. All methods (except field_enc and field_dec)
    88 	 * are required to be implemented. */
    89 	mp_err (*field_add) (const mp_int *a, const mp_int *b, mp_int *r,
    90 						 const GFMethod *meth);
    91 	mp_err (*field_neg) (const mp_int *a, mp_int *r, const GFMethod *meth);
    92 	mp_err (*field_sub) (const mp_int *a, const mp_int *b, mp_int *r,
    93 						 const GFMethod *meth);
    94 	mp_err (*field_mod) (const mp_int *a, mp_int *r, const GFMethod *meth);
    95 	mp_err (*field_mul) (const mp_int *a, const mp_int *b, mp_int *r,
    96 						 const GFMethod *meth);
    97 	mp_err (*field_sqr) (const mp_int *a, mp_int *r, const GFMethod *meth);
    98 	mp_err (*field_div) (const mp_int *a, const mp_int *b, mp_int *r,
    99 						 const GFMethod *meth);
   100 	mp_err (*field_enc) (const mp_int *a, mp_int *r, const GFMethod *meth);
   101 	mp_err (*field_dec) (const mp_int *a, mp_int *r, const GFMethod *meth);
   102 	/* Extra storage for implementation-specific data.  Any memory
   103 	 * allocated to these extra fields will be cleared by extra_free. */
   104 	void *extra1;
   105 	void *extra2;
   106 	void (*extra_free) (GFMethod *meth);
   107 };
   109 /* Construct generic GFMethods. */
   110 GFMethod *GFMethod_consGFp(const mp_int *irr);
   111 GFMethod *GFMethod_consGFp_mont(const mp_int *irr);
   112 GFMethod *GFMethod_consGF2m(const mp_int *irr,
   113 							const unsigned int irr_arr[5]);
   114 /* Free the memory allocated (if any) to a GFMethod object. */
   115 void GFMethod_free(GFMethod *meth);
   117 struct ECGroupStr {
   118 	/* Indicates whether the structure was constructed from dynamic memory 
   119 	 * or statically created. */
   120 	int constructed;
   121 	/* Field definition and arithmetic. */
   122 	GFMethod *meth;
   123 	/* Textual representation of curve name, if any. */
   124 	char *text;
   125 	/* Curve parameters, field-encoded. */
   126 	mp_int curvea, curveb;
   127 	/* x and y coordinates of the base point, field-encoded. */
   128 	mp_int genx, geny;
   129 	/* Order and cofactor of the base point. */
   130 	mp_int order;
   131 	int cofactor;
   132 	/* Point arithmetic methods. All methods are assumed to take
   133 	 * field-encoded parameters and return field-encoded values. All
   134 	 * methods (except base_point_mul and points_mul) are required to be
   135 	 * implemented. */
   136 	mp_err (*point_add) (const mp_int *px, const mp_int *py,
   137 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
   138 						 mp_int *ry, const ECGroup *group);
   139 	mp_err (*point_sub) (const mp_int *px, const mp_int *py,
   140 						 const mp_int *qx, const mp_int *qy, mp_int *rx,
   141 						 mp_int *ry, const ECGroup *group);
   142 	mp_err (*point_dbl) (const mp_int *px, const mp_int *py, mp_int *rx,
   143 						 mp_int *ry, const ECGroup *group);
   144 	mp_err (*point_mul) (const mp_int *n, const mp_int *px,
   145 						 const mp_int *py, mp_int *rx, mp_int *ry,
   146 						 const ECGroup *group);
   147 	mp_err (*base_point_mul) (const mp_int *n, mp_int *rx, mp_int *ry,
   148 							  const ECGroup *group);
   149 	mp_err (*points_mul) (const mp_int *k1, const mp_int *k2,
   150 						  const mp_int *px, const mp_int *py, mp_int *rx,
   151 						  mp_int *ry, const ECGroup *group);
   152 	mp_err (*validate_point) (const mp_int *px, const mp_int *py, const ECGroup *group);
   153 	/* Extra storage for implementation-specific data.  Any memory
   154 	 * allocated to these extra fields will be cleared by extra_free. */
   155 	void *extra1;
   156 	void *extra2;
   157 	void (*extra_free) (ECGroup *group);
   158 };
   160 /* Wrapper functions for generic prime field arithmetic. */
   161 mp_err ec_GFp_add(const mp_int *a, const mp_int *b, mp_int *r,
   162 				  const GFMethod *meth);
   163 mp_err ec_GFp_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
   164 mp_err ec_GFp_sub(const mp_int *a, const mp_int *b, mp_int *r,
   165 				  const GFMethod *meth);
   167 /* fixed length in-line adds. Count is in words */
   168 mp_err ec_GFp_add_3(const mp_int *a, const mp_int *b, mp_int *r,
   169 				  const GFMethod *meth);
   170 mp_err ec_GFp_add_4(const mp_int *a, const mp_int *b, mp_int *r,
   171 				  const GFMethod *meth);
   172 mp_err ec_GFp_add_5(const mp_int *a, const mp_int *b, mp_int *r,
   173 				  const GFMethod *meth);
   174 mp_err ec_GFp_add_6(const mp_int *a, const mp_int *b, mp_int *r,
   175 				  const GFMethod *meth);
   176 mp_err ec_GFp_sub_3(const mp_int *a, const mp_int *b, mp_int *r,
   177 				  const GFMethod *meth);
   178 mp_err ec_GFp_sub_4(const mp_int *a, const mp_int *b, mp_int *r,
   179 				  const GFMethod *meth);
   180 mp_err ec_GFp_sub_5(const mp_int *a, const mp_int *b, mp_int *r,
   181 				  const GFMethod *meth);
   182 mp_err ec_GFp_sub_6(const mp_int *a, const mp_int *b, mp_int *r,
   183 				  const GFMethod *meth);
   185 mp_err ec_GFp_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
   186 mp_err ec_GFp_mul(const mp_int *a, const mp_int *b, mp_int *r,
   187 				  const GFMethod *meth);
   188 mp_err ec_GFp_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
   189 mp_err ec_GFp_div(const mp_int *a, const mp_int *b, mp_int *r,
   190 				  const GFMethod *meth);
   191 /* Wrapper functions for generic binary polynomial field arithmetic. */
   192 mp_err ec_GF2m_add(const mp_int *a, const mp_int *b, mp_int *r,
   193 				   const GFMethod *meth);
   194 mp_err ec_GF2m_neg(const mp_int *a, mp_int *r, const GFMethod *meth);
   195 mp_err ec_GF2m_mod(const mp_int *a, mp_int *r, const GFMethod *meth);
   196 mp_err ec_GF2m_mul(const mp_int *a, const mp_int *b, mp_int *r,
   197 				   const GFMethod *meth);
   198 mp_err ec_GF2m_sqr(const mp_int *a, mp_int *r, const GFMethod *meth);
   199 mp_err ec_GF2m_div(const mp_int *a, const mp_int *b, mp_int *r,
   200 				   const GFMethod *meth);
   202 /* Montgomery prime field arithmetic. */
   203 mp_err ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r,
   204 					   const GFMethod *meth);
   205 mp_err ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
   206 mp_err ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r,
   207 					   const GFMethod *meth);
   208 mp_err ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
   209 mp_err ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth);
   210 void ec_GFp_extra_free_mont(GFMethod *meth);
   212 /* point multiplication */
   213 mp_err ec_pts_mul_basic(const mp_int *k1, const mp_int *k2,
   214 						const mp_int *px, const mp_int *py, mp_int *rx,
   215 						mp_int *ry, const ECGroup *group);
   216 mp_err ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2,
   217 						   const mp_int *px, const mp_int *py, mp_int *rx,
   218 						   mp_int *ry, const ECGroup *group);
   220 /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should
   221  * be an array of signed char's to output to, bitsize should be the number 
   222  * of bits of out, in is the original scalar, and w is the window size.
   223  * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A.
   224  * Menezes, "Software implementation of elliptic curve cryptography over
   225  * binary fields", Proc. CHES 2000. */
   226 mp_err ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in,
   227 					   int w);
   229 /* Optimized field arithmetic */
   230 mp_err ec_group_set_gfp192(ECGroup *group, ECCurveName);
   231 mp_err ec_group_set_gfp224(ECGroup *group, ECCurveName);
   232 mp_err ec_group_set_gfp256(ECGroup *group, ECCurveName);
   233 mp_err ec_group_set_gfp384(ECGroup *group, ECCurveName);
   234 mp_err ec_group_set_gfp521(ECGroup *group, ECCurveName);
   235 mp_err ec_group_set_gf2m163(ECGroup *group, ECCurveName name);
   236 mp_err ec_group_set_gf2m193(ECGroup *group, ECCurveName name);
   237 mp_err ec_group_set_gf2m233(ECGroup *group, ECCurveName name);
   239 /* Optimized point multiplication */
   240 mp_err ec_group_set_gfp256_32(ECGroup *group, ECCurveName name);
   242 /* Optimized floating-point arithmetic */
   243 #ifdef ECL_USE_FP
   244 mp_err ec_group_set_secp160r1_fp(ECGroup *group);
   245 mp_err ec_group_set_nistp192_fp(ECGroup *group);
   246 mp_err ec_group_set_nistp224_fp(ECGroup *group);
   247 #endif
   249 #endif							/* __ecl_priv_h_ */

mercurial