security/nss/lib/freebl/ecl/ecl.c

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 #include "mpi.h"
michael@0 6 #include "mplogic.h"
michael@0 7 #include "ecl.h"
michael@0 8 #include "ecl-priv.h"
michael@0 9 #include "ec2.h"
michael@0 10 #include "ecp.h"
michael@0 11 #include <stdlib.h>
michael@0 12 #include <string.h>
michael@0 13
michael@0 14 /* Allocate memory for a new ECGroup object. */
michael@0 15 ECGroup *
michael@0 16 ECGroup_new()
michael@0 17 {
michael@0 18 mp_err res = MP_OKAY;
michael@0 19 ECGroup *group;
michael@0 20 group = (ECGroup *) malloc(sizeof(ECGroup));
michael@0 21 if (group == NULL)
michael@0 22 return NULL;
michael@0 23 group->constructed = MP_YES;
michael@0 24 group->meth = NULL;
michael@0 25 group->text = NULL;
michael@0 26 MP_DIGITS(&group->curvea) = 0;
michael@0 27 MP_DIGITS(&group->curveb) = 0;
michael@0 28 MP_DIGITS(&group->genx) = 0;
michael@0 29 MP_DIGITS(&group->geny) = 0;
michael@0 30 MP_DIGITS(&group->order) = 0;
michael@0 31 group->base_point_mul = NULL;
michael@0 32 group->points_mul = NULL;
michael@0 33 group->validate_point = NULL;
michael@0 34 group->extra1 = NULL;
michael@0 35 group->extra2 = NULL;
michael@0 36 group->extra_free = NULL;
michael@0 37 MP_CHECKOK(mp_init(&group->curvea));
michael@0 38 MP_CHECKOK(mp_init(&group->curveb));
michael@0 39 MP_CHECKOK(mp_init(&group->genx));
michael@0 40 MP_CHECKOK(mp_init(&group->geny));
michael@0 41 MP_CHECKOK(mp_init(&group->order));
michael@0 42
michael@0 43 CLEANUP:
michael@0 44 if (res != MP_OKAY) {
michael@0 45 ECGroup_free(group);
michael@0 46 return NULL;
michael@0 47 }
michael@0 48 return group;
michael@0 49 }
michael@0 50
michael@0 51 /* Construct a generic ECGroup for elliptic curves over prime fields. */
michael@0 52 ECGroup *
michael@0 53 ECGroup_consGFp(const mp_int *irr, const mp_int *curvea,
michael@0 54 const mp_int *curveb, const mp_int *genx,
michael@0 55 const mp_int *geny, const mp_int *order, int cofactor)
michael@0 56 {
michael@0 57 mp_err res = MP_OKAY;
michael@0 58 ECGroup *group = NULL;
michael@0 59
michael@0 60 group = ECGroup_new();
michael@0 61 if (group == NULL)
michael@0 62 return NULL;
michael@0 63
michael@0 64 group->meth = GFMethod_consGFp(irr);
michael@0 65 if (group->meth == NULL) {
michael@0 66 res = MP_MEM;
michael@0 67 goto CLEANUP;
michael@0 68 }
michael@0 69 MP_CHECKOK(mp_copy(curvea, &group->curvea));
michael@0 70 MP_CHECKOK(mp_copy(curveb, &group->curveb));
michael@0 71 MP_CHECKOK(mp_copy(genx, &group->genx));
michael@0 72 MP_CHECKOK(mp_copy(geny, &group->geny));
michael@0 73 MP_CHECKOK(mp_copy(order, &group->order));
michael@0 74 group->cofactor = cofactor;
michael@0 75 group->point_add = &ec_GFp_pt_add_aff;
michael@0 76 group->point_sub = &ec_GFp_pt_sub_aff;
michael@0 77 group->point_dbl = &ec_GFp_pt_dbl_aff;
michael@0 78 group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
michael@0 79 group->base_point_mul = NULL;
michael@0 80 group->points_mul = &ec_GFp_pts_mul_jac;
michael@0 81 group->validate_point = &ec_GFp_validate_point;
michael@0 82
michael@0 83 CLEANUP:
michael@0 84 if (res != MP_OKAY) {
michael@0 85 ECGroup_free(group);
michael@0 86 return NULL;
michael@0 87 }
michael@0 88 return group;
michael@0 89 }
michael@0 90
michael@0 91 /* Construct a generic ECGroup for elliptic curves over prime fields with
michael@0 92 * field arithmetic implemented in Montgomery coordinates. */
michael@0 93 ECGroup *
michael@0 94 ECGroup_consGFp_mont(const mp_int *irr, const mp_int *curvea,
michael@0 95 const mp_int *curveb, const mp_int *genx,
michael@0 96 const mp_int *geny, const mp_int *order, int cofactor)
michael@0 97 {
michael@0 98 mp_err res = MP_OKAY;
michael@0 99 ECGroup *group = NULL;
michael@0 100
michael@0 101 group = ECGroup_new();
michael@0 102 if (group == NULL)
michael@0 103 return NULL;
michael@0 104
michael@0 105 group->meth = GFMethod_consGFp_mont(irr);
michael@0 106 if (group->meth == NULL) {
michael@0 107 res = MP_MEM;
michael@0 108 goto CLEANUP;
michael@0 109 }
michael@0 110 MP_CHECKOK(group->meth->
michael@0 111 field_enc(curvea, &group->curvea, group->meth));
michael@0 112 MP_CHECKOK(group->meth->
michael@0 113 field_enc(curveb, &group->curveb, group->meth));
michael@0 114 MP_CHECKOK(group->meth->field_enc(genx, &group->genx, group->meth));
michael@0 115 MP_CHECKOK(group->meth->field_enc(geny, &group->geny, group->meth));
michael@0 116 MP_CHECKOK(mp_copy(order, &group->order));
michael@0 117 group->cofactor = cofactor;
michael@0 118 group->point_add = &ec_GFp_pt_add_aff;
michael@0 119 group->point_sub = &ec_GFp_pt_sub_aff;
michael@0 120 group->point_dbl = &ec_GFp_pt_dbl_aff;
michael@0 121 group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
michael@0 122 group->base_point_mul = NULL;
michael@0 123 group->points_mul = &ec_GFp_pts_mul_jac;
michael@0 124 group->validate_point = &ec_GFp_validate_point;
michael@0 125
michael@0 126 CLEANUP:
michael@0 127 if (res != MP_OKAY) {
michael@0 128 ECGroup_free(group);
michael@0 129 return NULL;
michael@0 130 }
michael@0 131 return group;
michael@0 132 }
michael@0 133
michael@0 134 #ifdef NSS_ECC_MORE_THAN_SUITE_B
michael@0 135 /* Construct a generic ECGroup for elliptic curves over binary polynomial
michael@0 136 * fields. */
michael@0 137 ECGroup *
michael@0 138 ECGroup_consGF2m(const mp_int *irr, const unsigned int irr_arr[5],
michael@0 139 const mp_int *curvea, const mp_int *curveb,
michael@0 140 const mp_int *genx, const mp_int *geny,
michael@0 141 const mp_int *order, int cofactor)
michael@0 142 {
michael@0 143 mp_err res = MP_OKAY;
michael@0 144 ECGroup *group = NULL;
michael@0 145
michael@0 146 group = ECGroup_new();
michael@0 147 if (group == NULL)
michael@0 148 return NULL;
michael@0 149
michael@0 150 group->meth = GFMethod_consGF2m(irr, irr_arr);
michael@0 151 if (group->meth == NULL) {
michael@0 152 res = MP_MEM;
michael@0 153 goto CLEANUP;
michael@0 154 }
michael@0 155 MP_CHECKOK(mp_copy(curvea, &group->curvea));
michael@0 156 MP_CHECKOK(mp_copy(curveb, &group->curveb));
michael@0 157 MP_CHECKOK(mp_copy(genx, &group->genx));
michael@0 158 MP_CHECKOK(mp_copy(geny, &group->geny));
michael@0 159 MP_CHECKOK(mp_copy(order, &group->order));
michael@0 160 group->cofactor = cofactor;
michael@0 161 group->point_add = &ec_GF2m_pt_add_aff;
michael@0 162 group->point_sub = &ec_GF2m_pt_sub_aff;
michael@0 163 group->point_dbl = &ec_GF2m_pt_dbl_aff;
michael@0 164 group->point_mul = &ec_GF2m_pt_mul_mont;
michael@0 165 group->base_point_mul = NULL;
michael@0 166 group->points_mul = &ec_pts_mul_basic;
michael@0 167 group->validate_point = &ec_GF2m_validate_point;
michael@0 168
michael@0 169 CLEANUP:
michael@0 170 if (res != MP_OKAY) {
michael@0 171 ECGroup_free(group);
michael@0 172 return NULL;
michael@0 173 }
michael@0 174 return group;
michael@0 175 }
michael@0 176 #endif
michael@0 177
michael@0 178 /* Construct ECGroup from hex parameters and name, if any. Called by
michael@0 179 * ECGroup_fromHex and ECGroup_fromName. */
michael@0 180 ECGroup *
michael@0 181 ecgroup_fromNameAndHex(const ECCurveName name,
michael@0 182 const ECCurveParams * params)
michael@0 183 {
michael@0 184 mp_int irr, curvea, curveb, genx, geny, order;
michael@0 185 int bits;
michael@0 186 ECGroup *group = NULL;
michael@0 187 mp_err res = MP_OKAY;
michael@0 188
michael@0 189 /* initialize values */
michael@0 190 MP_DIGITS(&irr) = 0;
michael@0 191 MP_DIGITS(&curvea) = 0;
michael@0 192 MP_DIGITS(&curveb) = 0;
michael@0 193 MP_DIGITS(&genx) = 0;
michael@0 194 MP_DIGITS(&geny) = 0;
michael@0 195 MP_DIGITS(&order) = 0;
michael@0 196 MP_CHECKOK(mp_init(&irr));
michael@0 197 MP_CHECKOK(mp_init(&curvea));
michael@0 198 MP_CHECKOK(mp_init(&curveb));
michael@0 199 MP_CHECKOK(mp_init(&genx));
michael@0 200 MP_CHECKOK(mp_init(&geny));
michael@0 201 MP_CHECKOK(mp_init(&order));
michael@0 202 MP_CHECKOK(mp_read_radix(&irr, params->irr, 16));
michael@0 203 MP_CHECKOK(mp_read_radix(&curvea, params->curvea, 16));
michael@0 204 MP_CHECKOK(mp_read_radix(&curveb, params->curveb, 16));
michael@0 205 MP_CHECKOK(mp_read_radix(&genx, params->genx, 16));
michael@0 206 MP_CHECKOK(mp_read_radix(&geny, params->geny, 16));
michael@0 207 MP_CHECKOK(mp_read_radix(&order, params->order, 16));
michael@0 208
michael@0 209 /* determine number of bits */
michael@0 210 bits = mpl_significant_bits(&irr) - 1;
michael@0 211 if (bits < MP_OKAY) {
michael@0 212 res = bits;
michael@0 213 goto CLEANUP;
michael@0 214 }
michael@0 215
michael@0 216 /* determine which optimizations (if any) to use */
michael@0 217 if (params->field == ECField_GFp) {
michael@0 218 switch (name) {
michael@0 219 #ifdef NSS_ECC_MORE_THAN_SUITE_B
michael@0 220 #ifdef ECL_USE_FP
michael@0 221 case ECCurve_SECG_PRIME_160R1:
michael@0 222 group =
michael@0 223 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 224 &order, params->cofactor);
michael@0 225 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 226 MP_CHECKOK(ec_group_set_secp160r1_fp(group));
michael@0 227 break;
michael@0 228 #endif
michael@0 229 case ECCurve_SECG_PRIME_192R1:
michael@0 230 #ifdef ECL_USE_FP
michael@0 231 group =
michael@0 232 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 233 &order, params->cofactor);
michael@0 234 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 235 MP_CHECKOK(ec_group_set_nistp192_fp(group));
michael@0 236 #else
michael@0 237 group =
michael@0 238 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 239 &order, params->cofactor);
michael@0 240 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 241 MP_CHECKOK(ec_group_set_gfp192(group, name));
michael@0 242 #endif
michael@0 243 break;
michael@0 244 case ECCurve_SECG_PRIME_224R1:
michael@0 245 #ifdef ECL_USE_FP
michael@0 246 group =
michael@0 247 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 248 &order, params->cofactor);
michael@0 249 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 250 MP_CHECKOK(ec_group_set_nistp224_fp(group));
michael@0 251 #else
michael@0 252 group =
michael@0 253 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 254 &order, params->cofactor);
michael@0 255 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 256 MP_CHECKOK(ec_group_set_gfp224(group, name));
michael@0 257 #endif
michael@0 258 break;
michael@0 259 #endif /* NSS_ECC_MORE_THAN_SUITE_B */
michael@0 260 case ECCurve_SECG_PRIME_256R1:
michael@0 261 group =
michael@0 262 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 263 &order, params->cofactor);
michael@0 264 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 265 MP_CHECKOK(ec_group_set_gfp256(group, name));
michael@0 266 MP_CHECKOK(ec_group_set_gfp256_32(group, name));
michael@0 267 break;
michael@0 268 case ECCurve_SECG_PRIME_521R1:
michael@0 269 group =
michael@0 270 ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
michael@0 271 &order, params->cofactor);
michael@0 272 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 273 MP_CHECKOK(ec_group_set_gfp521(group, name));
michael@0 274 break;
michael@0 275 default:
michael@0 276 /* use generic arithmetic */
michael@0 277 group =
michael@0 278 ECGroup_consGFp_mont(&irr, &curvea, &curveb, &genx, &geny,
michael@0 279 &order, params->cofactor);
michael@0 280 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 281 }
michael@0 282 #ifdef NSS_ECC_MORE_THAN_SUITE_B
michael@0 283 } else if (params->field == ECField_GF2m) {
michael@0 284 group = ECGroup_consGF2m(&irr, NULL, &curvea, &curveb, &genx, &geny, &order, params->cofactor);
michael@0 285 if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
michael@0 286 if ((name == ECCurve_NIST_K163) ||
michael@0 287 (name == ECCurve_NIST_B163) ||
michael@0 288 (name == ECCurve_SECG_CHAR2_163R1)) {
michael@0 289 MP_CHECKOK(ec_group_set_gf2m163(group, name));
michael@0 290 } else if ((name == ECCurve_SECG_CHAR2_193R1) ||
michael@0 291 (name == ECCurve_SECG_CHAR2_193R2)) {
michael@0 292 MP_CHECKOK(ec_group_set_gf2m193(group, name));
michael@0 293 } else if ((name == ECCurve_NIST_K233) ||
michael@0 294 (name == ECCurve_NIST_B233)) {
michael@0 295 MP_CHECKOK(ec_group_set_gf2m233(group, name));
michael@0 296 }
michael@0 297 #endif
michael@0 298 } else {
michael@0 299 res = MP_UNDEF;
michael@0 300 goto CLEANUP;
michael@0 301 }
michael@0 302
michael@0 303 /* set name, if any */
michael@0 304 if ((group != NULL) && (params->text != NULL)) {
michael@0 305 group->text = strdup(params->text);
michael@0 306 if (group->text == NULL) {
michael@0 307 res = MP_MEM;
michael@0 308 }
michael@0 309 }
michael@0 310
michael@0 311 CLEANUP:
michael@0 312 mp_clear(&irr);
michael@0 313 mp_clear(&curvea);
michael@0 314 mp_clear(&curveb);
michael@0 315 mp_clear(&genx);
michael@0 316 mp_clear(&geny);
michael@0 317 mp_clear(&order);
michael@0 318 if (res != MP_OKAY) {
michael@0 319 ECGroup_free(group);
michael@0 320 return NULL;
michael@0 321 }
michael@0 322 return group;
michael@0 323 }
michael@0 324
michael@0 325 /* Construct ECGroup from hexadecimal representations of parameters. */
michael@0 326 ECGroup *
michael@0 327 ECGroup_fromHex(const ECCurveParams * params)
michael@0 328 {
michael@0 329 return ecgroup_fromNameAndHex(ECCurve_noName, params);
michael@0 330 }
michael@0 331
michael@0 332 /* Construct ECGroup from named parameters. */
michael@0 333 ECGroup *
michael@0 334 ECGroup_fromName(const ECCurveName name)
michael@0 335 {
michael@0 336 ECGroup *group = NULL;
michael@0 337 ECCurveParams *params = NULL;
michael@0 338 mp_err res = MP_OKAY;
michael@0 339
michael@0 340 params = EC_GetNamedCurveParams(name);
michael@0 341 if (params == NULL) {
michael@0 342 res = MP_UNDEF;
michael@0 343 goto CLEANUP;
michael@0 344 }
michael@0 345
michael@0 346 /* construct actual group */
michael@0 347 group = ecgroup_fromNameAndHex(name, params);
michael@0 348 if (group == NULL) {
michael@0 349 res = MP_UNDEF;
michael@0 350 goto CLEANUP;
michael@0 351 }
michael@0 352
michael@0 353 CLEANUP:
michael@0 354 EC_FreeCurveParams(params);
michael@0 355 if (res != MP_OKAY) {
michael@0 356 ECGroup_free(group);
michael@0 357 return NULL;
michael@0 358 }
michael@0 359 return group;
michael@0 360 }
michael@0 361
michael@0 362 /* Validates an EC public key as described in Section 5.2.2 of X9.62. */
michael@0 363 mp_err ECPoint_validate(const ECGroup *group, const mp_int *px, const
michael@0 364 mp_int *py)
michael@0 365 {
michael@0 366 /* 1: Verify that publicValue is not the point at infinity */
michael@0 367 /* 2: Verify that the coordinates of publicValue are elements
michael@0 368 * of the field.
michael@0 369 */
michael@0 370 /* 3: Verify that publicValue is on the curve. */
michael@0 371 /* 4: Verify that the order of the curve times the publicValue
michael@0 372 * is the point at infinity.
michael@0 373 */
michael@0 374 return group->validate_point(px, py, group);
michael@0 375 }
michael@0 376
michael@0 377 /* Free the memory allocated (if any) to an ECGroup object. */
michael@0 378 void
michael@0 379 ECGroup_free(ECGroup *group)
michael@0 380 {
michael@0 381 if (group == NULL)
michael@0 382 return;
michael@0 383 GFMethod_free(group->meth);
michael@0 384 if (group->constructed == MP_NO)
michael@0 385 return;
michael@0 386 mp_clear(&group->curvea);
michael@0 387 mp_clear(&group->curveb);
michael@0 388 mp_clear(&group->genx);
michael@0 389 mp_clear(&group->geny);
michael@0 390 mp_clear(&group->order);
michael@0 391 if (group->text != NULL)
michael@0 392 free(group->text);
michael@0 393 if (group->extra_free != NULL)
michael@0 394 group->extra_free(group);
michael@0 395 free(group);
michael@0 396 }

mercurial