Back to index

lightning-sunbird  0.9+nobinonly
ecdecode.c
Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Elliptic Curve Cryptography library.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Dr Vipul Gupta <vipul.gupta@sun.com> and
00024  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifdef NSS_ENABLE_ECC
00041 
00042 #include "blapi.h"
00043 #include "secoid.h"
00044 #include "secitem.h"
00045 #include "secerr.h"
00046 #include "ec.h"
00047 #include "ecl-curve.h"
00048 
00049 #define CHECK_OK(func) if (func == NULL) goto cleanup
00050 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
00051 
00052 /*
00053  * Initializes a SECItem from a hexadecimal string
00054  *
00055  * Warning: This function ignores leading 00's, so any leading 00's
00056  * in the hexadecimal string must be optional.
00057  */
00058 static SECItem *
00059 hexString2SECItem(PRArenaPool *arena, SECItem *item, const char *str)
00060 {
00061     int i = 0;
00062     int byteval = 0;
00063     int tmp = PORT_Strlen(str);
00064 
00065     if ((tmp % 2) != 0) return NULL;
00066     
00067     /* skip leading 00's unless the hex string is "00" */
00068     while ((tmp > 2) && (str[0] == '0') && (str[1] == '0')) {
00069         str += 2;
00070         tmp -= 2;
00071     }
00072 
00073     item->data = (unsigned char *) PORT_ArenaAlloc(arena, tmp/2);
00074     if (item->data == NULL) return NULL;
00075     item->len = tmp/2;
00076 
00077     while (str[i]) {
00078         if ((str[i] >= '0') && (str[i] <= '9'))
00079            tmp = str[i] - '0';
00080        else if ((str[i] >= 'a') && (str[i] <= 'f'))
00081            tmp = str[i] - 'a' + 10;
00082        else if ((str[i] >= 'A') && (str[i] <= 'F'))
00083            tmp = str[i] - 'A' + 10;
00084        else
00085            return NULL;
00086 
00087        byteval = byteval * 16 + tmp;
00088        if ((i % 2) != 0) {
00089            item->data[i/2] = byteval;
00090            byteval = 0;
00091        }
00092        i++;
00093     }
00094 
00095     return item;
00096 }
00097 
00098 /* Copy all of the fields from srcParams into dstParams
00099  */
00100 SECStatus
00101 EC_CopyParams(PRArenaPool *arena, ECParams *dstParams,
00102              const ECParams *srcParams)
00103 {
00104     SECStatus rv = SECFailure;
00105 
00106     dstParams->arena = arena;
00107     dstParams->type = srcParams->type;
00108     dstParams->fieldID.size = srcParams->fieldID.size;
00109     dstParams->fieldID.type = srcParams->fieldID.type;
00110     if (srcParams->fieldID.type == ec_field_GFp) {
00111        CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->fieldID.u.prime,
00112            &srcParams->fieldID.u.prime));
00113     } else {
00114        CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->fieldID.u.poly,
00115            &srcParams->fieldID.u.poly));
00116     }
00117     dstParams->fieldID.k1 = srcParams->fieldID.k1;
00118     dstParams->fieldID.k2 = srcParams->fieldID.k2;
00119     dstParams->fieldID.k3 = srcParams->fieldID.k3;
00120     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.a,
00121        &srcParams->curve.a));
00122     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.b,
00123        &srcParams->curve.b));
00124     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curve.seed,
00125        &srcParams->curve.seed));
00126     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->base,
00127        &srcParams->base));
00128     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->order,
00129        &srcParams->order));
00130     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->DEREncoding,
00131        &srcParams->DEREncoding));
00132        dstParams->name = srcParams->name;
00133     CHECK_SEC_OK(SECITEM_CopyItem(arena, &dstParams->curveOID,
00134        &srcParams->curveOID));
00135     dstParams->cofactor = srcParams->cofactor;
00136 
00137     return SECSuccess;
00138 
00139 cleanup:
00140     return SECFailure;
00141 }
00142 
00143 SECStatus
00144 EC_FillParams(PRArenaPool *arena, const SECItem *encodedParams, 
00145     ECParams *params)
00146 {
00147     SECOidTag tag;
00148     SECItem oid = { siBuffer, NULL, 0};
00149     const ECCurveParams *curveParams;
00150     /* 2 ['0'+'4'] + MAX_ECKEY_LEN * 2 [x,y] * 2 [hex string] + 1 ['\0'] */
00151     char genenc[3 + 2 * 2 * MAX_ECKEY_LEN];
00152 
00153 #if EC_DEBUG
00154     int i;
00155 
00156     printf("Encoded params in EC_DecodeParams: ");
00157     for (i = 0; i < encodedParams->len; i++) {
00158            printf("%02x:", encodedParams->data[i]);
00159     }
00160     printf("\n");
00161 #endif
00162 
00163     if ((encodedParams->len != ANSI_X962_CURVE_OID_TOTAL_LEN) &&
00164        (encodedParams->len != SECG_CURVE_OID_TOTAL_LEN)) {
00165            PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE);
00166            return SECFailure;
00167     };
00168 
00169     oid.len = encodedParams->len - 2;
00170     oid.data = encodedParams->data + 2;
00171     if ((encodedParams->data[0] != SEC_ASN1_OBJECT_ID) ||
00172        ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN)) { 
00173            PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE);
00174            return SECFailure;
00175     }
00176 
00177     params->arena = arena;
00178     params->cofactor = 0;
00179     params->type = ec_params_named;
00180     params->name = ECCurve_noName;
00181 
00182     /* For named curves, fill out curveOID */
00183     params->curveOID.len = oid.len;
00184     params->curveOID.data = (unsigned char *) PORT_ArenaAlloc(arena, oid.len);
00185     if (params->curveOID.data == NULL) goto cleanup;
00186     memcpy(params->curveOID.data, oid.data, oid.len);
00187 
00188 #if EC_DEBUG
00189     printf("Curve: %s\n", SECOID_FindOIDTagDescription(tag));
00190 #endif
00191 
00192     switch (tag) {
00193 
00194 #define GF2M_POPULATE \
00195        if ((params->name < ECCurve_noName) || \
00196            (params->name > ECCurve_pastLastCurve)) goto cleanup; \
00197        CHECK_OK(curveParams); \
00198        params->fieldID.size = curveParams->size; \
00199        params->fieldID.type = ec_field_GF2m; \
00200        CHECK_OK(hexString2SECItem(params->arena, &params->fieldID.u.poly, \
00201            curveParams->irr)); \
00202        CHECK_OK(hexString2SECItem(params->arena, &params->curve.a, \
00203            curveParams->curvea)); \
00204        CHECK_OK(hexString2SECItem(params->arena, &params->curve.b, \
00205            curveParams->curveb)); \
00206        genenc[0] = '0'; \
00207        genenc[1] = '4'; \
00208        genenc[2] = '\0'; \
00209        CHECK_OK(strcat(genenc, curveParams->genx)); \
00210        CHECK_OK(strcat(genenc, curveParams->geny)); \
00211        CHECK_OK(hexString2SECItem(params->arena, &params->base, \
00212            genenc)); \
00213        CHECK_OK(hexString2SECItem(params->arena, &params->order, \
00214            curveParams->order)); \
00215        params->cofactor = curveParams->cofactor;
00216 
00217     case SEC_OID_ANSIX962_EC_C2PNB163V1:
00218        /* Populate params for c2pnb163v1 */
00219        params->name = ECCurve_X9_62_CHAR2_PNB163V1;
00220        curveParams = ecCurve_map[params->name];
00221        GF2M_POPULATE
00222        break;
00223 
00224     case SEC_OID_ANSIX962_EC_C2PNB163V2:
00225        /* Populate params for c2pnb163v2 */
00226        params->name = ECCurve_X9_62_CHAR2_PNB163V2;
00227        curveParams = ecCurve_map[params->name];
00228        GF2M_POPULATE
00229        break;
00230 
00231     case SEC_OID_ANSIX962_EC_C2PNB163V3:
00232        /* Populate params for c2pnb163v3 */
00233        params->name = ECCurve_X9_62_CHAR2_PNB163V3;
00234        curveParams = ecCurve_map[params->name];
00235        GF2M_POPULATE
00236        break;
00237 
00238     case SEC_OID_ANSIX962_EC_C2PNB176V1:
00239        /* Populate params for c2pnb176v1 */
00240        params->name = ECCurve_X9_62_CHAR2_PNB176V1;
00241        curveParams = ecCurve_map[params->name];
00242        GF2M_POPULATE
00243        break;
00244 
00245     case SEC_OID_ANSIX962_EC_C2TNB191V1:
00246        /* Populate params for c2tnb191v1 */
00247        params->name = ECCurve_X9_62_CHAR2_TNB191V1;
00248        curveParams = ecCurve_map[params->name];
00249        GF2M_POPULATE
00250        break;
00251 
00252     case SEC_OID_ANSIX962_EC_C2TNB191V2:
00253        /* Populate params for c2tnb191v2 */
00254        params->name = ECCurve_X9_62_CHAR2_TNB191V2;
00255        curveParams = ecCurve_map[params->name];
00256        GF2M_POPULATE
00257        break;
00258 
00259     case SEC_OID_ANSIX962_EC_C2TNB191V3:
00260        /* Populate params for c2tnb191v3 */
00261        params->name = ECCurve_X9_62_CHAR2_TNB191V3;
00262        curveParams = ecCurve_map[params->name];
00263        GF2M_POPULATE
00264        break;
00265 
00266     case SEC_OID_ANSIX962_EC_C2PNB208W1:
00267        /* Populate params for c2pnb208w1 */
00268        params->name = ECCurve_X9_62_CHAR2_PNB208W1;
00269        curveParams = ecCurve_map[params->name];
00270        GF2M_POPULATE
00271        break;
00272 
00273     case SEC_OID_ANSIX962_EC_C2TNB239V1:
00274        /* Populate params for c2tnb239v1 */
00275        params->name = ECCurve_X9_62_CHAR2_TNB239V1;
00276        curveParams = ecCurve_map[params->name];
00277        GF2M_POPULATE
00278        break;
00279 
00280     case SEC_OID_ANSIX962_EC_C2TNB239V2:
00281        /* Populate params for c2tnb239v2 */
00282        params->name = ECCurve_X9_62_CHAR2_TNB239V2;
00283        curveParams = ecCurve_map[params->name];
00284        GF2M_POPULATE
00285        break;
00286 
00287     case SEC_OID_ANSIX962_EC_C2TNB239V3:
00288        /* Populate params for c2tnb239v3 */
00289        params->name = ECCurve_X9_62_CHAR2_TNB239V3;
00290        curveParams = ecCurve_map[params->name];
00291        GF2M_POPULATE
00292        break;
00293 
00294     case SEC_OID_ANSIX962_EC_C2PNB272W1:
00295        /* Populate params for c2pnb272w1 */
00296        params->name = ECCurve_X9_62_CHAR2_PNB272W1;
00297        curveParams = ecCurve_map[params->name];
00298        GF2M_POPULATE
00299        break;
00300 
00301     case SEC_OID_ANSIX962_EC_C2PNB304W1:
00302        /* Populate params for c2pnb304w1 */
00303        params->name = ECCurve_X9_62_CHAR2_PNB304W1;
00304        curveParams = ecCurve_map[params->name];
00305        GF2M_POPULATE
00306        break;
00307 
00308     case SEC_OID_ANSIX962_EC_C2TNB359V1:
00309        /* Populate params for c2tnb359v1 */
00310        params->name = ECCurve_X9_62_CHAR2_TNB359V1;
00311        curveParams = ecCurve_map[params->name];
00312        GF2M_POPULATE
00313        break;
00314 
00315     case SEC_OID_ANSIX962_EC_C2PNB368W1:
00316        /* Populate params for c2pnb368w1 */
00317        params->name = ECCurve_X9_62_CHAR2_PNB368W1;
00318        curveParams = ecCurve_map[params->name];
00319        GF2M_POPULATE
00320        break;
00321 
00322     case SEC_OID_ANSIX962_EC_C2TNB431R1:
00323        /* Populate params for c2tnb431r1 */
00324        params->name = ECCurve_X9_62_CHAR2_TNB431R1;
00325        curveParams = ecCurve_map[params->name];
00326        GF2M_POPULATE
00327        break;
00328        
00329     case SEC_OID_SECG_EC_SECT113R1:
00330        /* Populate params for sect113r1 */
00331        params->name = ECCurve_SECG_CHAR2_113R1;
00332        curveParams = ecCurve_map[params->name];
00333        GF2M_POPULATE
00334        break;
00335 
00336     case SEC_OID_SECG_EC_SECT113R2:
00337        /* Populate params for sect113r2 */
00338        params->name = ECCurve_SECG_CHAR2_113R2;
00339        curveParams = ecCurve_map[params->name];
00340        GF2M_POPULATE
00341        break;
00342 
00343     case SEC_OID_SECG_EC_SECT131R1:
00344        /* Populate params for sect131r1 */
00345        params->name = ECCurve_SECG_CHAR2_131R1;
00346        curveParams = ecCurve_map[params->name];
00347        GF2M_POPULATE
00348        break;
00349 
00350     case SEC_OID_SECG_EC_SECT131R2:
00351        /* Populate params for sect131r2 */
00352        params->name = ECCurve_SECG_CHAR2_131R2;
00353        curveParams = ecCurve_map[params->name];
00354        GF2M_POPULATE
00355        break;
00356 
00357     case SEC_OID_SECG_EC_SECT163K1:
00358        /* Populate params for sect163k1
00359         * (the NIST K-163 curve)
00360         */
00361        params->name = ECCurve_SECG_CHAR2_163K1;
00362        curveParams = ecCurve_map[params->name];
00363        GF2M_POPULATE
00364        break;
00365 
00366     case SEC_OID_SECG_EC_SECT163R1:
00367        /* Populate params for sect163r1 */
00368        params->name = ECCurve_SECG_CHAR2_163R1;
00369        curveParams = ecCurve_map[params->name];
00370        GF2M_POPULATE
00371        break;
00372 
00373     case SEC_OID_SECG_EC_SECT163R2:
00374        /* Populate params for sect163r2
00375         * (the NIST B-163 curve)
00376         */
00377        params->name = ECCurve_SECG_CHAR2_163R2;
00378        curveParams = ecCurve_map[params->name];
00379        GF2M_POPULATE
00380        break;
00381 
00382     case SEC_OID_SECG_EC_SECT193R1:
00383        /* Populate params for sect193r1 */
00384        params->name = ECCurve_SECG_CHAR2_193R1;
00385        curveParams = ecCurve_map[params->name];
00386        GF2M_POPULATE
00387        break;
00388 
00389     case SEC_OID_SECG_EC_SECT193R2:
00390        /* Populate params for sect193r2 */
00391        params->name = ECCurve_SECG_CHAR2_193R2;
00392        curveParams = ecCurve_map[params->name];
00393        GF2M_POPULATE
00394        break;
00395 
00396     case SEC_OID_SECG_EC_SECT233K1:
00397        /* Populate params for sect233k1
00398         * (the NIST K-233 curve)
00399         */
00400        params->name = ECCurve_SECG_CHAR2_233K1;
00401        curveParams = ecCurve_map[params->name];
00402        GF2M_POPULATE
00403        break;
00404 
00405     case SEC_OID_SECG_EC_SECT233R1:
00406        /* Populate params for sect233r1
00407         * (the NIST B-233 curve)
00408         */
00409        params->name = ECCurve_SECG_CHAR2_233R1;
00410        curveParams = ecCurve_map[params->name];
00411        GF2M_POPULATE
00412        break;
00413 
00414     case SEC_OID_SECG_EC_SECT239K1:
00415        /* Populate params for sect239k1 */
00416        params->name = ECCurve_SECG_CHAR2_239K1;
00417        curveParams = ecCurve_map[params->name];
00418        GF2M_POPULATE
00419        break;
00420 
00421     case SEC_OID_SECG_EC_SECT283K1:
00422         /* Populate params for sect283k1
00423         * (the NIST K-283 curve)
00424         */
00425        params->name = ECCurve_SECG_CHAR2_283K1;
00426        curveParams = ecCurve_map[params->name];
00427        GF2M_POPULATE
00428        break;
00429 
00430     case SEC_OID_SECG_EC_SECT283R1:
00431        /* Populate params for sect283r1
00432         * (the NIST B-283 curve)
00433         */
00434        params->name = ECCurve_SECG_CHAR2_283R1;
00435        curveParams = ecCurve_map[params->name];
00436        GF2M_POPULATE
00437        break;
00438 
00439     case SEC_OID_SECG_EC_SECT409K1:
00440        /* Populate params for sect409k1
00441         * (the NIST K-409 curve)
00442         */
00443        params->name = ECCurve_SECG_CHAR2_409K1;
00444        curveParams = ecCurve_map[params->name];
00445        GF2M_POPULATE
00446        break;
00447 
00448     case SEC_OID_SECG_EC_SECT409R1:
00449        /* Populate params for sect409r1
00450         * (the NIST B-409 curve)
00451         */
00452        params->name = ECCurve_SECG_CHAR2_409R1;
00453        curveParams = ecCurve_map[params->name];
00454        GF2M_POPULATE
00455        break;
00456 
00457     case SEC_OID_SECG_EC_SECT571K1:
00458        /* Populate params for sect571k1
00459         * (the NIST K-571 curve)
00460         */
00461        params->name = ECCurve_SECG_CHAR2_571K1;
00462        curveParams = ecCurve_map[params->name];
00463        GF2M_POPULATE
00464        break;
00465 
00466     case SEC_OID_SECG_EC_SECT571R1:
00467        /* Populate params for sect571r1
00468         * (the NIST B-571 curve)
00469         */
00470        params->name = ECCurve_SECG_CHAR2_571R1;
00471        curveParams = ecCurve_map[params->name];
00472        GF2M_POPULATE
00473        break;
00474 
00475 #define GFP_POPULATE \
00476        if ((params->name < ECCurve_noName) || \
00477            (params->name > ECCurve_pastLastCurve)) goto cleanup; \
00478        CHECK_OK(curveParams); \
00479        params->fieldID.size = curveParams->size; \
00480        params->fieldID.type = ec_field_GFp; \
00481        CHECK_OK(hexString2SECItem(params->arena, &params->fieldID.u.prime, \
00482            curveParams->irr)); \
00483        CHECK_OK(hexString2SECItem(params->arena, &params->curve.a, \
00484            curveParams->curvea)); \
00485        CHECK_OK(hexString2SECItem(params->arena, &params->curve.b, \
00486            curveParams->curveb)); \
00487        genenc[0] = '0'; \
00488        genenc[1] = '4'; \
00489        genenc[2] = '\0'; \
00490        CHECK_OK(strcat(genenc, curveParams->genx)); \
00491        CHECK_OK(strcat(genenc, curveParams->geny)); \
00492        CHECK_OK(hexString2SECItem(params->arena, &params->base, \
00493            genenc)); \
00494        CHECK_OK(hexString2SECItem(params->arena, &params->order, \
00495            curveParams->order)); \
00496        params->cofactor = curveParams->cofactor;
00497 
00498     case SEC_OID_ANSIX962_EC_PRIME192V1:
00499        /* Populate params for prime192v1 aka secp192r1 
00500         * (the NIST P-192 curve)
00501         */
00502        params->name = ECCurve_X9_62_PRIME_192V1;
00503        curveParams = ecCurve_map[params->name];
00504        GFP_POPULATE
00505        break;
00506 
00507     case SEC_OID_ANSIX962_EC_PRIME192V2:
00508        /* Populate params for prime192v2 */
00509        params->name = ECCurve_X9_62_PRIME_192V2;
00510        curveParams = ecCurve_map[params->name];
00511        GFP_POPULATE
00512        break;
00513 
00514     case SEC_OID_ANSIX962_EC_PRIME192V3:
00515        /* Populate params for prime192v3 */
00516        params->name = ECCurve_X9_62_PRIME_192V3;
00517        curveParams = ecCurve_map[params->name];
00518        GFP_POPULATE
00519        break;
00520        
00521     case SEC_OID_ANSIX962_EC_PRIME239V1:
00522        /* Populate params for prime239v1 */
00523        params->name = ECCurve_X9_62_PRIME_239V1;
00524        curveParams = ecCurve_map[params->name];
00525        GFP_POPULATE
00526        break;
00527 
00528     case SEC_OID_ANSIX962_EC_PRIME239V2:
00529        /* Populate params for prime239v2 */
00530        params->name = ECCurve_X9_62_PRIME_239V2;
00531        curveParams = ecCurve_map[params->name];
00532        GFP_POPULATE
00533        break;
00534 
00535     case SEC_OID_ANSIX962_EC_PRIME239V3:
00536        /* Populate params for prime239v3 */
00537        params->name = ECCurve_X9_62_PRIME_239V3;
00538        curveParams = ecCurve_map[params->name];
00539        GFP_POPULATE
00540        break;
00541 
00542     case SEC_OID_ANSIX962_EC_PRIME256V1:
00543        /* Populate params for prime256v1 aka secp256r1
00544         * (the NIST P-256 curve)
00545         */
00546        params->name = ECCurve_X9_62_PRIME_256V1;
00547        curveParams = ecCurve_map[params->name];
00548        GFP_POPULATE
00549        break;
00550 
00551     case SEC_OID_SECG_EC_SECP112R1:
00552         /* Populate params for secp112r1 */
00553        params->name = ECCurve_SECG_PRIME_112R1;
00554        curveParams = ecCurve_map[params->name];
00555        GFP_POPULATE
00556        break;
00557 
00558     case SEC_OID_SECG_EC_SECP112R2:
00559         /* Populate params for secp112r2 */
00560        params->name = ECCurve_SECG_PRIME_112R2;
00561        curveParams = ecCurve_map[params->name];
00562        GFP_POPULATE
00563        break;
00564 
00565     case SEC_OID_SECG_EC_SECP128R1:
00566         /* Populate params for secp128r1 */
00567        params->name = ECCurve_SECG_PRIME_128R1;
00568        curveParams = ecCurve_map[params->name];
00569        GFP_POPULATE
00570        break;
00571 
00572     case SEC_OID_SECG_EC_SECP128R2:
00573         /* Populate params for secp128r2 */
00574        params->name = ECCurve_SECG_PRIME_128R2;
00575        curveParams = ecCurve_map[params->name];
00576        GFP_POPULATE
00577        break;
00578        
00579     case SEC_OID_SECG_EC_SECP160K1:
00580         /* Populate params for secp160k1 */
00581        params->name = ECCurve_SECG_PRIME_160K1;
00582        curveParams = ecCurve_map[params->name];
00583        GFP_POPULATE
00584        break;
00585 
00586     case SEC_OID_SECG_EC_SECP160R1:
00587         /* Populate params for secp160r1 */
00588        params->name = ECCurve_SECG_PRIME_160R1;
00589        curveParams = ecCurve_map[params->name];
00590        GFP_POPULATE
00591        break;
00592 
00593     case SEC_OID_SECG_EC_SECP160R2:
00594        /* Populate params for secp160r1 */
00595        params->name = ECCurve_SECG_PRIME_160R2;
00596        curveParams = ecCurve_map[params->name];
00597        GFP_POPULATE
00598        break;
00599 
00600     case SEC_OID_SECG_EC_SECP192K1:
00601        /* Populate params for secp192k1 */
00602        params->name = ECCurve_SECG_PRIME_192K1;
00603        curveParams = ecCurve_map[params->name];
00604        GFP_POPULATE
00605        break;
00606 
00607     case SEC_OID_SECG_EC_SECP224K1:
00608        /* Populate params for secp224k1 */
00609        params->name = ECCurve_SECG_PRIME_224K1;
00610        curveParams = ecCurve_map[params->name];
00611        GFP_POPULATE
00612        break;
00613 
00614     case SEC_OID_SECG_EC_SECP224R1:
00615        /* Populate params for secp224r1 
00616         * (the NIST P-224 curve)
00617         */
00618        params->name = ECCurve_SECG_PRIME_224R1;
00619        curveParams = ecCurve_map[params->name];
00620        GFP_POPULATE
00621        break;
00622 
00623     case SEC_OID_SECG_EC_SECP256K1:
00624        /* Populate params for secp256k1 */
00625        params->name = ECCurve_SECG_PRIME_256K1;
00626        curveParams = ecCurve_map[params->name];
00627        GFP_POPULATE
00628        break;
00629 
00630     case SEC_OID_SECG_EC_SECP384R1:
00631        /* Populate params for secp384r1
00632         * (the NIST P-384 curve)
00633         */
00634        params->name = ECCurve_SECG_PRIME_384R1;
00635        curveParams = ecCurve_map[params->name];
00636        GFP_POPULATE
00637        break;
00638 
00639     case SEC_OID_SECG_EC_SECP521R1:
00640        /* Populate params for secp521r1 
00641         * (the NIST P-521 curve)
00642         */
00643        params->name = ECCurve_SECG_PRIME_521R1;
00644        curveParams = ecCurve_map[params->name];
00645        GFP_POPULATE
00646        break;
00647 
00648     default:
00649        break;
00650     };
00651 
00652 cleanup:
00653     if (!params->cofactor) {
00654        PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE);
00655 #if EC_DEBUG
00656        printf("Unrecognized curve, returning NULL params\n");
00657 #endif
00658        return SECFailure;
00659     }
00660 
00661     return SECSuccess;
00662 }
00663 
00664 SECStatus
00665 EC_DecodeParams(const SECItem *encodedParams, ECParams **ecparams)
00666 {
00667     PRArenaPool *arena;
00668     ECParams *params;
00669     SECStatus rv = SECFailure;
00670 
00671     /* Initialize an arena for the ECParams structure */
00672     if (!(arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE)))
00673        return SECFailure;
00674 
00675     params = (ECParams *)PORT_ArenaZAlloc(arena, sizeof(ECParams));
00676     if (!params) {
00677        PORT_FreeArena(arena, PR_TRUE);
00678        return SECFailure;
00679     }
00680 
00681     /* Copy the encoded params */
00682     SECITEM_AllocItem(arena, &(params->DEREncoding),
00683        encodedParams->len);
00684     memcpy(params->DEREncoding.data, encodedParams->data, encodedParams->len);
00685 
00686     /* Fill out the rest of the ECParams structure based on 
00687      * the encoded params 
00688      */
00689     rv = EC_FillParams(arena, encodedParams, params);
00690     if (rv == SECFailure) {
00691        PORT_FreeArena(arena, PR_TRUE);    
00692        return SECFailure;
00693     } else {
00694        *ecparams = params;;
00695        return SECSuccess;
00696     }
00697 }
00698 
00699 #endif /* NSS_ENABLE_ECC */