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