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