/* Free the memory allocated (if any) to an ECGroup object. */ void ECGroup_free(ECGroup *group) { 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) #ifdef _KERNEL kmem_free(group->text, group->text_len); #else free(group->text); #endif if (group->extra_free != NULL) group->extra_free(group); #ifdef _KERNEL kmem_free(group, sizeof (ECGroup)); #else free(group); #endif }
/* Construct a generic GFMethod for arithmetic over prime fields with * irreducible irr. */ GFMethod * GFMethod_consGFp(const mp_int *irr) { mp_err res = MP_OKAY; GFMethod *meth = NULL; meth = GFMethod_new(); if (meth == NULL) return NULL; MP_CHECKOK(mp_copy(irr, &meth->irr)); meth->irr_arr[0] = mpl_significant_bits(irr); meth->irr_arr[1] = meth->irr_arr[2] = meth->irr_arr[3] = meth->irr_arr[4] = 0; switch (MP_USED(&meth->irr)) { /* maybe we need 1 and 2 words here as well?*/ case 3: meth->field_add = &ec_GFp_add_3; meth->field_sub = &ec_GFp_sub_3; break; case 4: meth->field_add = &ec_GFp_add_4; meth->field_sub = &ec_GFp_sub_4; break; case 5: meth->field_add = &ec_GFp_add_5; meth->field_sub = &ec_GFp_sub_5; break; case 6: meth->field_add = &ec_GFp_add_6; meth->field_sub = &ec_GFp_sub_6; break; default: meth->field_add = &ec_GFp_add; meth->field_sub = &ec_GFp_sub; } meth->field_neg = &ec_GFp_neg; meth->field_mod = &ec_GFp_mod; meth->field_mul = &ec_GFp_mul; meth->field_sqr = &ec_GFp_sqr; meth->field_div = &ec_GFp_div; meth->field_enc = NULL; meth->field_dec = NULL; meth->extra1 = NULL; meth->extra2 = NULL; meth->extra_free = NULL; CLEANUP: if (res != MP_OKAY) { GFMethod_free(meth); return NULL; } return meth; }
/* Free the memory allocated (if any) to an ECGroup object. */ void ECGroup_free(ECGroup *group) { 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); }
/* Allocate memory for a new GFMethod object. */ GFMethod * GFMethod_new() { mp_err res = MP_OKAY; GFMethod *meth; meth = (GFMethod *)malloc(sizeof(GFMethod)); if (meth == NULL) return NULL; meth->constructed = MP_YES; MP_DIGITS(&meth->irr) = 0; meth->extra_free = NULL; MP_CHECKOK(mp_init(&meth->irr)); CLEANUP: if (res != MP_OKAY) { GFMethod_free(meth); return NULL; } return meth; }
/* Construct a generic GFMethod for arithmetic over prime fields with * irreducible irr. */ GFMethod * GFMethod_consGFp_mont(const mp_int *irr) { mp_err res = MP_OKAY; int i; GFMethod *meth = NULL; mp_mont_modulus *mmm; meth = GFMethod_consGFp(irr); if (meth == NULL) return NULL; mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus)); if (mmm == NULL) { res = MP_MEM; goto CLEANUP; } meth->field_mul = &ec_GFp_mul_mont; meth->field_sqr = &ec_GFp_sqr_mont; meth->field_div = &ec_GFp_div_mont; meth->field_enc = &ec_GFp_enc_mont; meth->field_dec = &ec_GFp_dec_mont; meth->extra1 = mmm; meth->extra2 = NULL; meth->extra_free = &ec_GFp_extra_free_mont; mmm->N = meth->irr; i = mpl_significant_bits(&meth->irr); i += MP_DIGIT_BIT - 1; mmm->b = i - i % MP_DIGIT_BIT; mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0)); CLEANUP: if (res != MP_OKAY) { GFMethod_free(meth); return NULL; } return meth; }