andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef __ecl_priv_h_ andre@0: #define __ecl_priv_h_ andre@0: andre@0: #include "ecl.h" andre@0: #include "mpi.h" andre@0: #include "mplogic.h" andre@0: andre@0: /* MAX_FIELD_SIZE_DIGITS is the maximum size of field element supported */ andre@0: /* the following needs to go away... */ andre@0: #if defined(MP_USE_LONG_LONG_DIGIT) || defined(MP_USE_LONG_DIGIT) andre@0: #define ECL_SIXTY_FOUR_BIT andre@0: #else andre@0: #define ECL_THIRTY_TWO_BIT andre@0: #endif andre@0: andre@0: #define ECL_CURVE_DIGITS(curve_size_in_bits) \ andre@0: (((curve_size_in_bits)+(sizeof(mp_digit)*8-1))/(sizeof(mp_digit)*8)) andre@0: #define ECL_BITS (sizeof(mp_digit)*8) andre@0: #define ECL_MAX_FIELD_SIZE_DIGITS (80/sizeof(mp_digit)) andre@0: andre@0: /* Gets the i'th bit in the binary representation of a. If i >= length(a), andre@0: * then return 0. (The above behaviour differs from mpl_get_bit, which andre@0: * causes an error if i >= length(a).) */ andre@0: #define MP_GET_BIT(a, i) \ andre@0: ((i) >= mpl_significant_bits((a))) ? 0 : mpl_get_bit((a), (i)) andre@0: andre@0: #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD) andre@0: #define MP_ADD_CARRY(a1, a2, s, cin, cout) \ andre@0: { mp_word w; \ andre@0: w = ((mp_word)(cin)) + (a1) + (a2); \ andre@0: s = ACCUM(w); \ andre@0: cout = CARRYOUT(w); } andre@0: andre@0: #define MP_SUB_BORROW(a1, a2, s, bin, bout) \ andre@0: { mp_word w; \ andre@0: w = ((mp_word)(a1)) - (a2) - (bin); \ andre@0: s = ACCUM(w); \ andre@0: bout = (w >> MP_DIGIT_BIT) & 1; } andre@0: andre@0: #else andre@0: /* NOTE, andre@0: * cin and cout could be the same variable. andre@0: * bin and bout could be the same variable. andre@0: * a1 or a2 and s could be the same variable. andre@0: * don't trash those outputs until their respective inputs have andre@0: * been read. */ andre@0: #define MP_ADD_CARRY(a1, a2, s, cin, cout) \ andre@0: { mp_digit tmp,sum; \ andre@0: tmp = (a1); \ andre@0: sum = tmp + (a2); \ andre@0: tmp = (sum < tmp); /* detect overflow */ \ andre@0: s = sum += (cin); \ andre@0: cout = tmp + (sum < (cin)); } andre@0: andre@0: #define MP_SUB_BORROW(a1, a2, s, bin, bout) \ andre@0: { mp_digit tmp; \ andre@0: tmp = (a1); \ andre@0: s = tmp - (a2); \ andre@0: tmp = (s > tmp); /* detect borrow */ \ andre@0: if ((bin) && !s--) tmp++; \ andre@0: bout = tmp; } andre@0: #endif andre@0: andre@0: andre@0: struct GFMethodStr; andre@0: typedef struct GFMethodStr GFMethod; andre@0: struct GFMethodStr { andre@0: /* Indicates whether the structure was constructed from dynamic memory andre@0: * or statically created. */ andre@0: int constructed; andre@0: /* Irreducible that defines the field. For prime fields, this is the andre@0: * prime p. For binary polynomial fields, this is the bitstring andre@0: * representation of the irreducible polynomial. */ andre@0: mp_int irr; andre@0: /* For prime fields, the value irr_arr[0] is the number of bits in the andre@0: * field. For binary polynomial fields, the irreducible polynomial andre@0: * f(t) is represented as an array of unsigned int[], where f(t) is andre@0: * of the form: f(t) = t^p[0] + t^p[1] + ... + t^p[4] where m = p[0] andre@0: * > p[1] > ... > p[4] = 0. */ andre@0: unsigned int irr_arr[5]; andre@0: /* Field arithmetic methods. All methods (except field_enc and andre@0: * field_dec) are assumed to take field-encoded parameters and return andre@0: * field-encoded values. All methods (except field_enc and field_dec) andre@0: * are required to be implemented. */ andre@0: mp_err (*field_add) (const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err (*field_neg) (const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err (*field_sub) (const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err (*field_mod) (const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err (*field_mul) (const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err (*field_sqr) (const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err (*field_div) (const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err (*field_enc) (const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err (*field_dec) (const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: /* Extra storage for implementation-specific data. Any memory andre@0: * allocated to these extra fields will be cleared by extra_free. */ andre@0: void *extra1; andre@0: void *extra2; andre@0: void (*extra_free) (GFMethod *meth); andre@0: }; andre@0: andre@0: /* Construct generic GFMethods. */ andre@0: GFMethod *GFMethod_consGFp(const mp_int *irr); andre@0: GFMethod *GFMethod_consGFp_mont(const mp_int *irr); andre@0: GFMethod *GFMethod_consGF2m(const mp_int *irr, andre@0: const unsigned int irr_arr[5]); andre@0: /* Free the memory allocated (if any) to a GFMethod object. */ andre@0: void GFMethod_free(GFMethod *meth); andre@0: andre@0: struct ECGroupStr { andre@0: /* Indicates whether the structure was constructed from dynamic memory andre@0: * or statically created. */ andre@0: int constructed; andre@0: /* Field definition and arithmetic. */ andre@0: GFMethod *meth; andre@0: /* Textual representation of curve name, if any. */ andre@0: char *text; andre@0: /* Curve parameters, field-encoded. */ andre@0: mp_int curvea, curveb; andre@0: /* x and y coordinates of the base point, field-encoded. */ andre@0: mp_int genx, geny; andre@0: /* Order and cofactor of the base point. */ andre@0: mp_int order; andre@0: int cofactor; andre@0: /* Point arithmetic methods. All methods are assumed to take andre@0: * field-encoded parameters and return field-encoded values. All andre@0: * methods (except base_point_mul and points_mul) are required to be andre@0: * implemented. */ andre@0: mp_err (*point_add) (const mp_int *px, const mp_int *py, andre@0: const mp_int *qx, const mp_int *qy, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: mp_err (*point_sub) (const mp_int *px, const mp_int *py, andre@0: const mp_int *qx, const mp_int *qy, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: mp_err (*point_dbl) (const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: mp_err (*point_mul) (const mp_int *n, const mp_int *px, andre@0: const mp_int *py, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group); andre@0: mp_err (*base_point_mul) (const mp_int *n, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group); andre@0: mp_err (*points_mul) (const mp_int *k1, const mp_int *k2, andre@0: const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: mp_err (*validate_point) (const mp_int *px, const mp_int *py, const ECGroup *group); andre@0: /* Extra storage for implementation-specific data. Any memory andre@0: * allocated to these extra fields will be cleared by extra_free. */ andre@0: void *extra1; andre@0: void *extra2; andre@0: void (*extra_free) (ECGroup *group); andre@0: }; andre@0: andre@0: /* Wrapper functions for generic prime field arithmetic. */ andre@0: mp_err ec_GFp_add(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_neg(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GFp_sub(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: andre@0: /* fixed length in-line adds. Count is in words */ andre@0: mp_err ec_GFp_add_3(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_add_4(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_add_5(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_add_6(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sub_3(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sub_4(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sub_5(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sub_6(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: andre@0: mp_err ec_GFp_mod(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GFp_mul(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sqr(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GFp_div(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: /* Wrapper functions for generic binary polynomial field arithmetic. */ andre@0: mp_err ec_GF2m_add(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GF2m_neg(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GF2m_mod(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GF2m_mul(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GF2m_sqr(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GF2m_div(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: andre@0: /* Montgomery prime field arithmetic. */ andre@0: mp_err ec_GFp_mul_mont(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_sqr_mont(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GFp_div_mont(const mp_int *a, const mp_int *b, mp_int *r, andre@0: const GFMethod *meth); andre@0: mp_err ec_GFp_enc_mont(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: mp_err ec_GFp_dec_mont(const mp_int *a, mp_int *r, const GFMethod *meth); andre@0: void ec_GFp_extra_free_mont(GFMethod *meth); andre@0: andre@0: /* point multiplication */ andre@0: mp_err ec_pts_mul_basic(const mp_int *k1, const mp_int *k2, andre@0: const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: mp_err ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2, andre@0: const mp_int *px, const mp_int *py, mp_int *rx, andre@0: mp_int *ry, const ECGroup *group); andre@0: andre@0: /* Computes the windowed non-adjacent-form (NAF) of a scalar. Out should andre@0: * be an array of signed char's to output to, bitsize should be the number andre@0: * of bits of out, in is the original scalar, and w is the window size. andre@0: * NAF is discussed in the paper: D. Hankerson, J. Hernandez and A. andre@0: * Menezes, "Software implementation of elliptic curve cryptography over andre@0: * binary fields", Proc. CHES 2000. */ andre@0: mp_err ec_compute_wNAF(signed char *out, int bitsize, const mp_int *in, andre@0: int w); andre@0: andre@0: /* Optimized field arithmetic */ andre@0: mp_err ec_group_set_gfp192(ECGroup *group, ECCurveName); andre@0: mp_err ec_group_set_gfp224(ECGroup *group, ECCurveName); andre@0: mp_err ec_group_set_gfp256(ECGroup *group, ECCurveName); andre@0: mp_err ec_group_set_gfp384(ECGroup *group, ECCurveName); andre@0: mp_err ec_group_set_gfp521(ECGroup *group, ECCurveName); andre@0: mp_err ec_group_set_gf2m163(ECGroup *group, ECCurveName name); andre@0: mp_err ec_group_set_gf2m193(ECGroup *group, ECCurveName name); andre@0: mp_err ec_group_set_gf2m233(ECGroup *group, ECCurveName name); andre@0: andre@0: /* Optimized point multiplication */ andre@0: mp_err ec_group_set_gfp256_32(ECGroup *group, ECCurveName name); andre@0: andre@0: /* Optimized floating-point arithmetic */ andre@0: #ifdef ECL_USE_FP andre@0: mp_err ec_group_set_secp160r1_fp(ECGroup *group); andre@0: mp_err ec_group_set_nistp192_fp(ECGroup *group); andre@0: mp_err ec_group_set_nistp224_fp(ECGroup *group); andre@0: #endif andre@0: andre@0: #endif /* __ecl_priv_h_ */