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