X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=crypto%2Fec%2Fecp_nistp521.c;h=04c3f08cc2e4da641cf7e4133ca7956ce8a0ad7b;hb=d6755bb6ac6676cf0f219cd4caf352ac48907206;hp=febf5e94b7445ffeae52f704ec84761fcb19385f;hpb=b51bce942023325e727ca4225252d06c49d8f2b7;p=oweals%2Fopenssl.git diff --git a/crypto/ec/ecp_nistp521.c b/crypto/ec/ecp_nistp521.c index febf5e94b7..04c3f08cc2 100644 --- a/crypto/ec/ecp_nistp521.c +++ b/crypto/ec/ecp_nistp521.c @@ -1,4 +1,3 @@ -/* crypto/ec/ecp_nistp521.c */ /* * Written by Adam Langley (Google) for the OpenSSL project */ @@ -1585,10 +1584,10 @@ static void batch_mul(felem x_out, felem y_out, felem z_out, } /* Precomputation for the group generator. */ -typedef struct { +struct nistp521_pre_comp_st { felem g_pre_comp[16][3]; int references; -} NISTP521_PRE_COMP; +}; const EC_METHOD *EC_GFp_nistp521_method(void) { @@ -1646,7 +1645,7 @@ static NISTP521_PRE_COMP *nistp521_pre_comp_new() { NISTP521_PRE_COMP *ret = OPENSSL_zalloc(sizeof(*ret)); - if (!ret) { + if (ret == NULL) { ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE); return ret; } @@ -1654,44 +1653,19 @@ static NISTP521_PRE_COMP *nistp521_pre_comp_new() return ret; } -static void *nistp521_pre_comp_dup(void *src_) +NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *p) { - NISTP521_PRE_COMP *src = src_; - - /* no need to actually copy, these objects never change! */ - CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP); - - return src_; -} - -static void nistp521_pre_comp_free(void *pre_) -{ - int i; - NISTP521_PRE_COMP *pre = pre_; - - if (!pre) - return; - - i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP); - if (i > 0) - return; - - OPENSSL_free(pre); + if (p != NULL) + CRYPTO_add(&p->references, 1, CRYPTO_LOCK_EC_PRE_COMP); + return p; } -static void nistp521_pre_comp_clear_free(void *pre_) +void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *p) { - int i; - NISTP521_PRE_COMP *pre = pre_; - - if (!pre) - return; - - i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP); - if (i > 0) + if (p == NULL + || CRYPTO_add(&p->references, -1, CRYPTO_LOCK_EC_PRE_COMP) > 0) return; - - OPENSSL_clear_free(pre, sizeof(*pre)); + OPENSSL_free(p); } /******************************************************************************/ @@ -1858,10 +1832,7 @@ int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r, goto err; if (scalar != NULL) { - pre = EC_EX_DATA_get_data(group->extra_data, - nistp521_pre_comp_dup, - nistp521_pre_comp_free, - nistp521_pre_comp_clear_free); + pre = group->pre_comp.nistp521; if (pre) /* we have precomputation, try to use it */ g_pre_comp = &pre->g_pre_comp[0]; @@ -2036,9 +2007,7 @@ int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx) felem tmp_felems[16]; /* throw away old precomputation */ - EC_EX_DATA_free_data(&group->extra_data, nistp521_pre_comp_dup, - nistp521_pre_comp_free, - nistp521_pre_comp_clear_free); + EC_pre_comp_free(group); if (ctx == NULL) if ((ctx = new_ctx = BN_CTX_new()) == NULL) return 0; @@ -2062,8 +2031,7 @@ int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx) */ if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) { memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp)); - ret = 1; - goto err; + goto done; } if ((!BN_to_felem(pre->g_pre_comp[1][0], group->generator->X)) || (!BN_to_felem(pre->g_pre_comp[1][1], group->generator->Y)) || @@ -2121,29 +2089,21 @@ int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx) } make_points_affine(15, &(pre->g_pre_comp[1]), tmp_felems); - if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp521_pre_comp_dup, - nistp521_pre_comp_free, - nistp521_pre_comp_clear_free)) - goto err; + done: + SETPRECOMP(group, nistp521, pre); ret = 1; pre = NULL; err: BN_CTX_end(ctx); EC_POINT_free(generator); BN_CTX_free(new_ctx); - nistp521_pre_comp_free(pre); + EC_nistp521_pre_comp_free(pre); return ret; } int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group) { - if (EC_EX_DATA_get_data(group->extra_data, nistp521_pre_comp_dup, - nistp521_pre_comp_free, - nistp521_pre_comp_clear_free) - != NULL) - return 1; - else - return 0; + return HAVEPRECOMP(group, nistp521); } #else