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 NSS_DISABLE_ECC michael@0: michael@0: #ifdef FREEBL_NO_DEPEND michael@0: #include "stubs.h" michael@0: #endif michael@0: michael@0: #include "blapi.h" michael@0: #include "secoid.h" michael@0: #include "secitem.h" michael@0: #include "secerr.h" michael@0: #include "ec.h" michael@0: #include "ecl-curve.h" michael@0: michael@0: #define CHECK_OK(func) if (func == NULL) goto cleanup michael@0: #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup michael@0: michael@0: /* michael@0: * Initializes a SECItem from a hexadecimal string michael@0: * michael@0: * Warning: This function ignores leading 00's, so any leading 00's michael@0: * in the hexadecimal string must be optional. michael@0: */ michael@0: static SECItem * michael@0: hexString2SECItem(PLArenaPool *arena, SECItem *item, const char *str) michael@0: { michael@0: int i = 0; michael@0: int byteval = 0; michael@0: int tmp = PORT_Strlen(str); michael@0: michael@0: if ((tmp % 2) != 0) return NULL; michael@0: michael@0: /* skip leading 00's unless the hex string is "00" */ michael@0: while ((tmp > 2) && (str[0] == '0') && (str[1] == '0')) { michael@0: str += 2; michael@0: tmp -= 2; michael@0: } michael@0: michael@0: item->data = (unsigned char *) PORT_ArenaAlloc(arena, tmp/2); michael@0: if (item->data == NULL) return NULL; michael@0: item->len = tmp/2; michael@0: michael@0: while (str[i]) { michael@0: if ((str[i] >= '0') && (str[i] <= '9')) michael@0: tmp = str[i] - '0'; michael@0: else if ((str[i] >= 'a') && (str[i] <= 'f')) michael@0: tmp = str[i] - 'a' + 10; michael@0: else if ((str[i] >= 'A') && (str[i] <= 'F')) michael@0: tmp = str[i] - 'A' + 10; michael@0: else michael@0: return NULL; michael@0: michael@0: byteval = byteval * 16 + tmp; michael@0: if ((i % 2) != 0) { michael@0: item->data[i/2] = byteval; michael@0: byteval = 0; michael@0: } michael@0: i++; michael@0: } michael@0: michael@0: return item; michael@0: } michael@0: michael@0: /* Copy all of the fields from srcParams into dstParams michael@0: */ michael@0: SECStatus michael@0: EC_CopyParams(PLArenaPool *arena, ECParams *dstParams, michael@0: const ECParams *srcParams) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: michael@0: dstParams->arena = arena; michael@0: dstParams->type = srcParams->type; michael@0: dstParams->fieldID.size = srcParams->fieldID.size; michael@0: dstParams->fieldID.type = srcParams->fieldID.type; michael@0: if (srcParams->fieldID.type == ec_field_GFp) { michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->fieldID.u.prime, michael@0: &srcParams->fieldID.u.prime)); michael@0: } else { michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->fieldID.u.poly, michael@0: &srcParams->fieldID.u.poly)); michael@0: } michael@0: dstParams->fieldID.k1 = srcParams->fieldID.k1; michael@0: dstParams->fieldID.k2 = srcParams->fieldID.k2; michael@0: dstParams->fieldID.k3 = srcParams->fieldID.k3; michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.a, michael@0: &srcParams->curve.a)); michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.b, michael@0: &srcParams->curve.b)); michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.seed, michael@0: &srcParams->curve.seed)); michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->base, michael@0: &srcParams->base)); michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->order, michael@0: &srcParams->order)); michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->DEREncoding, michael@0: &srcParams->DEREncoding)); michael@0: dstParams->name = srcParams->name; michael@0: CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curveOID, michael@0: &srcParams->curveOID)); michael@0: dstParams->cofactor = srcParams->cofactor; michael@0: michael@0: return SECSuccess; michael@0: michael@0: cleanup: michael@0: return SECFailure; michael@0: } michael@0: michael@0: static SECStatus michael@0: gf_populate_params(ECCurveName name, ECFieldType field_type, ECParams *params) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: const ECCurveParams *curveParams; michael@0: /* 2 ['0'+'4'] + MAX_ECKEY_LEN * 2 [x,y] * 2 [hex string] + 1 ['\0'] */ michael@0: char genenc[3 + 2 * 2 * MAX_ECKEY_LEN]; michael@0: michael@0: if ((name < ECCurve_noName) || (name > ECCurve_pastLastCurve)) goto cleanup; michael@0: params->name = name; michael@0: curveParams = ecCurve_map[params->name]; michael@0: CHECK_OK(curveParams); michael@0: params->fieldID.size = curveParams->size; michael@0: params->fieldID.type = field_type; michael@0: if (field_type == ec_field_GFp) { michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->fieldID.u.prime, michael@0: curveParams->irr)); michael@0: } else { michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->fieldID.u.poly, michael@0: curveParams->irr)); michael@0: } michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->curve.a, michael@0: curveParams->curvea)); michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->curve.b, michael@0: curveParams->curveb)); michael@0: genenc[0] = '0'; michael@0: genenc[1] = '4'; michael@0: genenc[2] = '\0'; michael@0: strcat(genenc, curveParams->genx); michael@0: strcat(genenc, curveParams->geny); michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->base, genenc)); michael@0: CHECK_OK(hexString2SECItem(params->arena, ¶ms->order, michael@0: curveParams->order)); michael@0: params->cofactor = curveParams->cofactor; michael@0: michael@0: rv = SECSuccess; michael@0: michael@0: cleanup: michael@0: return rv; michael@0: } michael@0: michael@0: SECStatus michael@0: EC_FillParams(PLArenaPool *arena, const SECItem *encodedParams, michael@0: ECParams *params) michael@0: { michael@0: SECStatus rv = SECFailure; michael@0: SECOidTag tag; michael@0: SECItem oid = { siBuffer, NULL, 0}; michael@0: michael@0: #if EC_DEBUG michael@0: int i; michael@0: michael@0: printf("Encoded params in EC_DecodeParams: "); michael@0: for (i = 0; i < encodedParams->len; i++) { michael@0: printf("%02x:", encodedParams->data[i]); michael@0: } michael@0: printf("\n"); michael@0: #endif michael@0: michael@0: if ((encodedParams->len != ANSI_X962_CURVE_OID_TOTAL_LEN) && michael@0: (encodedParams->len != SECG_CURVE_OID_TOTAL_LEN)) { michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); michael@0: return SECFailure; michael@0: }; michael@0: michael@0: oid.len = encodedParams->len - 2; michael@0: oid.data = encodedParams->data + 2; michael@0: if ((encodedParams->data[0] != SEC_ASN1_OBJECT_ID) || michael@0: ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN)) { michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); michael@0: return SECFailure; michael@0: } michael@0: michael@0: params->arena = arena; michael@0: params->cofactor = 0; michael@0: params->type = ec_params_named; michael@0: params->name = ECCurve_noName; michael@0: michael@0: /* For named curves, fill out curveOID */ michael@0: params->curveOID.len = oid.len; michael@0: params->curveOID.data = (unsigned char *) PORT_ArenaAlloc(arena, oid.len); michael@0: if (params->curveOID.data == NULL) goto cleanup; michael@0: memcpy(params->curveOID.data, oid.data, oid.len); michael@0: michael@0: #if EC_DEBUG michael@0: printf("Curve: %s\n", SECOID_FindOIDTagDescription(tag)); michael@0: #endif michael@0: michael@0: switch (tag) { michael@0: michael@0: /* Binary curves */ michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V1: michael@0: /* Populate params for c2pnb163v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB163V1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V2: michael@0: /* Populate params for c2pnb163v2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB163V2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB163V3: michael@0: /* Populate params for c2pnb163v3 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB163V3, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB176V1: michael@0: /* Populate params for c2pnb176v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB176V1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V1: michael@0: /* Populate params for c2tnb191v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB191V1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V2: michael@0: /* Populate params for c2tnb191v2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB191V2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB191V3: michael@0: /* Populate params for c2tnb191v3 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB191V3, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB208W1: michael@0: /* Populate params for c2pnb208w1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB208W1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V1: michael@0: /* Populate params for c2tnb239v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB239V1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V2: michael@0: /* Populate params for c2tnb239v2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB239V2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB239V3: michael@0: /* Populate params for c2tnb239v3 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB239V3, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB272W1: michael@0: /* Populate params for c2pnb272w1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB272W1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB304W1: michael@0: /* Populate params for c2pnb304w1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB304W1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB359V1: michael@0: /* Populate params for c2tnb359v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB359V1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2PNB368W1: michael@0: /* Populate params for c2pnb368w1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_PNB368W1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_C2TNB431R1: michael@0: /* Populate params for c2tnb431r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_CHAR2_TNB431R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT113R1: michael@0: /* Populate params for sect113r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_113R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT113R2: michael@0: /* Populate params for sect113r2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_113R2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT131R1: michael@0: /* Populate params for sect131r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_131R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT131R2: michael@0: /* Populate params for sect131r2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_131R2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT163K1: michael@0: /* Populate params for sect163k1 michael@0: * (the NIST K-163 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_163K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT163R1: michael@0: /* Populate params for sect163r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_163R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT163R2: michael@0: /* Populate params for sect163r2 michael@0: * (the NIST B-163 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_163R2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT193R1: michael@0: /* Populate params for sect193r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_193R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT193R2: michael@0: /* Populate params for sect193r2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_193R2, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT233K1: michael@0: /* Populate params for sect233k1 michael@0: * (the NIST K-233 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_233K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT233R1: michael@0: /* Populate params for sect233r1 michael@0: * (the NIST B-233 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_233R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT239K1: michael@0: /* Populate params for sect239k1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_239K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT283K1: michael@0: /* Populate params for sect283k1 michael@0: * (the NIST K-283 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_283K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT283R1: michael@0: /* Populate params for sect283r1 michael@0: * (the NIST B-283 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_283R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT409K1: michael@0: /* Populate params for sect409k1 michael@0: * (the NIST K-409 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_409K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT409R1: michael@0: /* Populate params for sect409r1 michael@0: * (the NIST B-409 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_409R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT571K1: michael@0: /* Populate params for sect571k1 michael@0: * (the NIST K-571 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_571K1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECT571R1: michael@0: /* Populate params for sect571r1 michael@0: * (the NIST B-571 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_CHAR2_571R1, ec_field_GF2m, michael@0: params) ); michael@0: break; michael@0: michael@0: /* Prime curves */ michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V1: michael@0: /* Populate params for prime192v1 aka secp192r1 michael@0: * (the NIST P-192 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_192V1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V2: michael@0: /* Populate params for prime192v2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_192V2, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME192V3: michael@0: /* Populate params for prime192v3 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_192V3, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V1: michael@0: /* Populate params for prime239v1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_239V1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V2: michael@0: /* Populate params for prime239v2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_239V2, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME239V3: michael@0: /* Populate params for prime239v3 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_239V3, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_ANSIX962_EC_PRIME256V1: michael@0: /* Populate params for prime256v1 aka secp256r1 michael@0: * (the NIST P-256 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_X9_62_PRIME_256V1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP112R1: michael@0: /* Populate params for secp112r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_112R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP112R2: michael@0: /* Populate params for secp112r2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_112R2, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP128R1: michael@0: /* Populate params for secp128r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_128R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP128R2: michael@0: /* Populate params for secp128r2 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_128R2, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP160K1: michael@0: /* Populate params for secp160k1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_160K1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP160R1: michael@0: /* Populate params for secp160r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_160R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP160R2: michael@0: /* Populate params for secp160r1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_160R2, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP192K1: michael@0: /* Populate params for secp192k1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_192K1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP224K1: michael@0: /* Populate params for secp224k1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_224K1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP224R1: michael@0: /* Populate params for secp224r1 michael@0: * (the NIST P-224 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_224R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP256K1: michael@0: /* Populate params for secp256k1 */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_256K1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP384R1: michael@0: /* Populate params for secp384r1 michael@0: * (the NIST P-384 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_384R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: case SEC_OID_SECG_EC_SECP521R1: michael@0: /* Populate params for secp521r1 michael@0: * (the NIST P-521 curve) michael@0: */ michael@0: CHECK_SEC_OK( gf_populate_params(ECCurve_SECG_PRIME_521R1, ec_field_GFp, michael@0: params) ); michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: }; michael@0: michael@0: cleanup: michael@0: if (!params->cofactor) { michael@0: PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); michael@0: #if EC_DEBUG michael@0: printf("Unrecognized curve, returning NULL params\n"); michael@0: #endif michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: SECStatus michael@0: EC_DecodeParams(const SECItem *encodedParams, ECParams **ecparams) michael@0: { michael@0: PLArenaPool *arena; michael@0: ECParams *params; michael@0: SECStatus rv = SECFailure; michael@0: michael@0: /* Initialize an arena for the ECParams structure */ michael@0: if (!(arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE))) michael@0: return SECFailure; michael@0: michael@0: params = (ECParams *)PORT_ArenaZAlloc(arena, sizeof(ECParams)); michael@0: if (!params) { michael@0: PORT_FreeArena(arena, PR_TRUE); michael@0: return SECFailure; michael@0: } michael@0: michael@0: /* Copy the encoded params */ michael@0: SECITEM_AllocItem(arena, &(params->DEREncoding), michael@0: encodedParams->len); michael@0: memcpy(params->DEREncoding.data, encodedParams->data, encodedParams->len); michael@0: michael@0: /* Fill out the rest of the ECParams structure based on michael@0: * the encoded params michael@0: */ michael@0: rv = EC_FillParams(arena, encodedParams, params); michael@0: if (rv == SECFailure) { michael@0: PORT_FreeArena(arena, PR_TRUE); michael@0: return SECFailure; michael@0: } else { michael@0: *ecparams = params;; michael@0: return SECSuccess; michael@0: } michael@0: } michael@0: michael@0: #endif /* NSS_DISABLE_ECC */