Back to index

lightning-sunbird  0.9+nobinonly
Functions
ecl.c File Reference
#include "mpi.h"
#include "mplogic.h"
#include "ecl.h"
#include "ecl-priv.h"
#include "ec2.h"
#include "ecp.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Functions

ECGroup * ECGroup_new ()
ECGroup * ECGroup_consGFp (const mp_int *irr, const mp_int *curvea, const mp_int *curveb, const mp_int *genx, const mp_int *geny, const mp_int *order, int cofactor)
ECGroup * ECGroup_consGFp_mont (const mp_int *irr, const mp_int *curvea, const mp_int *curveb, const mp_int *genx, const mp_int *geny, const mp_int *order, int cofactor)
ECGroup * ecgroup_fromNameAndHex (const ECCurveName name, const ECCurveParams *params)
ECGroup * ECGroup_fromHex (const ECCurveParams *params)
ECGroup * ECGroup_fromName (const ECCurveName name)
mp_err ECPoint_validate (const ECGroup *group, const mp_int *px, const mp_int *py)
void ECGroup_free (ECGroup *group)

Function Documentation

ECGroup* ECGroup_consGFp ( const mp_int irr,
const mp_int curvea,
const mp_int curveb,
const mp_int genx,
const mp_int geny,
const mp_int order,
int  cofactor 
)

Definition at line 87 of file ecl.c.

{
       mp_err res = MP_OKAY;
       ECGroup *group = NULL;

       group = ECGroup_new();
       if (group == NULL)
              return NULL;

       group->meth = GFMethod_consGFp(irr);
       if (group->meth == NULL) {
              res = MP_MEM;
              goto CLEANUP;
       }
       MP_CHECKOK(mp_copy(curvea, &group->curvea));
       MP_CHECKOK(mp_copy(curveb, &group->curveb));
       MP_CHECKOK(mp_copy(genx, &group->genx));
       MP_CHECKOK(mp_copy(geny, &group->geny));
       MP_CHECKOK(mp_copy(order, &group->order));
       group->cofactor = cofactor;
       group->point_add = &ec_GFp_pt_add_aff;
       group->point_sub = &ec_GFp_pt_sub_aff;
       group->point_dbl = &ec_GFp_pt_dbl_aff;
       group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
       group->base_point_mul = NULL;
       group->points_mul = &ec_GFp_pts_mul_jac;
       group->validate_point = &ec_GFp_validate_point;

  CLEANUP:
       if (res != MP_OKAY) {
              ECGroup_free(group);
              return NULL;
       }
       return group;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ECGroup* ECGroup_consGFp_mont ( const mp_int irr,
const mp_int curvea,
const mp_int curveb,
const mp_int genx,
const mp_int geny,
const mp_int order,
int  cofactor 
)

Definition at line 128 of file ecl.c.

{
       mp_err res = MP_OKAY;
       ECGroup *group = NULL;

       group = ECGroup_new();
       if (group == NULL)
              return NULL;

       group->meth = GFMethod_consGFp_mont(irr);
       if (group->meth == NULL) {
              res = MP_MEM;
              goto CLEANUP;
       }
       MP_CHECKOK(group->meth->
                        field_enc(curvea, &group->curvea, group->meth));
       MP_CHECKOK(group->meth->
                        field_enc(curveb, &group->curveb, group->meth));
       MP_CHECKOK(group->meth->field_enc(genx, &group->genx, group->meth));
       MP_CHECKOK(group->meth->field_enc(geny, &group->geny, group->meth));
       MP_CHECKOK(mp_copy(order, &group->order));
       group->cofactor = cofactor;
       group->point_add = &ec_GFp_pt_add_aff;
       group->point_sub = &ec_GFp_pt_sub_aff;
       group->point_dbl = &ec_GFp_pt_dbl_aff;
       group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
       group->base_point_mul = NULL;
       group->points_mul = &ec_GFp_pts_mul_jac;
       group->validate_point = &ec_GFp_validate_point;

  CLEANUP:
       if (res != MP_OKAY) {
              ECGroup_free(group);
              return NULL;
       }
       return group;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ECGroup_free ( ECGroup *  group)

Definition at line 409 of file ecl.c.

{
       if (group == NULL)
              return;
       GFMethod_free(group->meth);
       if (group->constructed == MP_NO)
              return;
       mp_clear(&group->curvea);
       mp_clear(&group->curveb);
       mp_clear(&group->genx);
       mp_clear(&group->geny);
       mp_clear(&group->order);
       if (group->text != NULL)
              free(group->text);
       if (group->extra_free != NULL)
              group->extra_free(group);
       free(group);
}

Here is the caller graph for this function:

ECGroup* ECGroup_fromHex ( const ECCurveParams *  params)

Definition at line 357 of file ecl.c.

ECGroup* ECGroup_fromName ( const ECCurveName  name)

Definition at line 364 of file ecl.c.

{
       ECGroup *group = NULL;
       ECCurveParams *params = NULL;
       mp_err res = MP_OKAY;

       params = EC_GetNamedCurveParams(name);
       if (params == NULL) {
              res = MP_UNDEF;
              goto CLEANUP;
       }

       /* construct actual group */
       group = ecgroup_fromNameAndHex(name, params);
       if (group == NULL) {
              res = MP_UNDEF;
              goto CLEANUP;
       }

  CLEANUP:
       EC_FreeCurveParams(params);
       if (res != MP_OKAY) {
              ECGroup_free(group);
              return NULL;
       }
       return group;
}

Here is the caller graph for this function:

ECGroup* ecgroup_fromNameAndHex ( const ECCurveName  name,
const ECCurveParams *  params 
)

Definition at line 215 of file ecl.c.

{
       mp_int irr, curvea, curveb, genx, geny, order;
       int bits;
       ECGroup *group = NULL;
       mp_err res = MP_OKAY;

       /* initialize values */
       MP_DIGITS(&irr) = 0;
       MP_DIGITS(&curvea) = 0;
       MP_DIGITS(&curveb) = 0;
       MP_DIGITS(&genx) = 0;
       MP_DIGITS(&geny) = 0;
       MP_DIGITS(&order) = 0;
       MP_CHECKOK(mp_init(&irr));
       MP_CHECKOK(mp_init(&curvea));
       MP_CHECKOK(mp_init(&curveb));
       MP_CHECKOK(mp_init(&genx));
       MP_CHECKOK(mp_init(&geny));
       MP_CHECKOK(mp_init(&order));
       MP_CHECKOK(mp_read_radix(&irr, params->irr, 16));
       MP_CHECKOK(mp_read_radix(&curvea, params->curvea, 16));
       MP_CHECKOK(mp_read_radix(&curveb, params->curveb, 16));
       MP_CHECKOK(mp_read_radix(&genx, params->genx, 16));
       MP_CHECKOK(mp_read_radix(&geny, params->geny, 16));
       MP_CHECKOK(mp_read_radix(&order, params->order, 16));

       /* determine number of bits */
       bits = mpl_significant_bits(&irr) - 1;
       if (bits < MP_OKAY) {
              res = bits;
              goto CLEANUP;
       }

       /* determine which optimizations (if any) to use */
       if (params->field == ECField_GFp) {
#ifdef NSS_ECC_MORE_THAN_SUITE_B
           switch (name) {
#ifdef ECL_USE_FP
              case ECCurve_SECG_PRIME_160R1:
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_secp160r1_fp(group));
                     break;
#endif
              case ECCurve_SECG_PRIME_192R1:
#ifdef ECL_USE_FP
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_nistp192_fp(group));
#else
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_gfp192(group, name));
#endif
                     break;
              case ECCurve_SECG_PRIME_224R1:
#ifdef ECL_USE_FP
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_nistp224_fp(group));
#else
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_gfp224(group, name));
#endif
                     break;
              case ECCurve_SECG_PRIME_256R1:
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_gfp256(group, name));
                     break;
              case ECCurve_SECG_PRIME_521R1:
                     group =
                            ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
                                                        &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
                     MP_CHECKOK(ec_group_set_gfp521(group, name));
                     break;
              default:
                     /* use generic arithmetic */
#endif
                     group =
                            ECGroup_consGFp_mont(&irr, &curvea, &curveb, &genx, &geny,
                                                                &order, params->cofactor);
                     if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
#ifdef NSS_ECC_MORE_THAN_SUITE_B
              }
       } else if (params->field == ECField_GF2m) {
              group = ECGroup_consGF2m(&irr, NULL, &curvea, &curveb, &genx, &geny, &order, params->cofactor);
              if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
              if ((name == ECCurve_NIST_K163) ||
                  (name == ECCurve_NIST_B163) ||
                  (name == ECCurve_SECG_CHAR2_163R1)) {
                     MP_CHECKOK(ec_group_set_gf2m163(group, name));
              } else if ((name == ECCurve_SECG_CHAR2_193R1) ||
                         (name == ECCurve_SECG_CHAR2_193R2)) {
                     MP_CHECKOK(ec_group_set_gf2m193(group, name));
              } else if ((name == ECCurve_NIST_K233) ||
                         (name == ECCurve_NIST_B233)) {
                     MP_CHECKOK(ec_group_set_gf2m233(group, name));
              }
#endif
       }

       /* set name, if any */
       if (params->text != NULL) {
              group->text = strdup(params->text);
              if (group->text == NULL) {
                     res = MP_MEM;
              }
       }

  CLEANUP:
       mp_clear(&irr);
       mp_clear(&curvea);
       mp_clear(&curveb);
       mp_clear(&genx);
       mp_clear(&geny);
       mp_clear(&order);
       if (res != MP_OKAY) {
              ECGroup_free(group);
              return NULL;
       }
       return group;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ECGroup* ECGroup_new ( )

Definition at line 50 of file ecl.c.

{
       mp_err res = MP_OKAY;
       ECGroup *group;
       group = (ECGroup *) malloc(sizeof(ECGroup));
       if (group == NULL)
              return NULL;
       group->constructed = MP_YES;
        group->meth = NULL;
       group->text = NULL;
       MP_DIGITS(&group->curvea) = 0;
       MP_DIGITS(&group->curveb) = 0;
       MP_DIGITS(&group->genx) = 0;
       MP_DIGITS(&group->geny) = 0;
       MP_DIGITS(&group->order) = 0;
       group->base_point_mul = NULL;
       group->points_mul = NULL;
       group->validate_point = NULL;
       group->extra1 = NULL;
       group->extra2 = NULL;
       group->extra_free = NULL;
       MP_CHECKOK(mp_init(&group->curvea));
       MP_CHECKOK(mp_init(&group->curveb));
       MP_CHECKOK(mp_init(&group->genx));
       MP_CHECKOK(mp_init(&group->geny));
       MP_CHECKOK(mp_init(&group->order));

  CLEANUP:
       if (res != MP_OKAY) {
              ECGroup_free(group);
              return NULL;
       }
       return group;
}

Here is the call graph for this function:

Here is the caller graph for this function:

mp_err ECPoint_validate ( const ECGroup *  group,
const mp_int px,
const mp_int py 
)

Definition at line 393 of file ecl.c.

{
    /* 1: Verify that publicValue is not the point at infinity */
    /* 2: Verify that the coordinates of publicValue are elements 
     *    of the field.
     */
    /* 3: Verify that publicValue is on the curve. */
    /* 4: Verify that the order of the curve times the publicValue
     *    is the point at infinity.
     */
       return group->validate_point(px, py, group);
}

Here is the caller graph for this function: