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.

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

mercurial