EC_POINT **points; /* array with pre-calculated multiples of generator:
* 'num' pointers to EC_POINT objects followed by a NULL */
size_t num; /* numblocks * 2^(w-1) */
+ int references;
} EC_PRE_COMP;
/* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
ret->w = 4; /* default */
ret->points = NULL;
ret->num = 0;
+ ret->references = 1;
return ret;
}
static void *ec_pre_comp_dup(void *src_)
{
- const EC_PRE_COMP *src = src_;
- EC_PRE_COMP *ret = NULL;
-
- ret = ec_pre_comp_new(src->group);
- if (!ret)
- return ret;
- ret->blocksize = src->blocksize;
- ret->numblocks = src->numblocks;
- ret->w = src->w;
- ret->num = 0;
-
- if (src->points)
- {
- EC_POINT **src_var, **dest_var;
-
- ret->points = (EC_POINT **)OPENSSL_malloc((src->num + 1) * sizeof(EC_POINT *));
- if (!ret->points)
- {
- ec_pre_comp_free(ret);
- return NULL;
- }
+ EC_PRE_COMP *src = src_;
- for (dest_var = ret->points, src_var = src->points; *src_var != NULL; src_var++, dest_var++)
- {
- *dest_var = EC_POINT_dup(*src_var, src->group);
- if (*dest_var == NULL)
- {
- ec_pre_comp_free(ret);
- return NULL;
- }
- ret->num++;
- }
+ /* no need to actually copy, these objects never change! */
- ret->points[ret->num] = NULL;
- if (ret->num != src->num)
- {
- ec_pre_comp_free(ret);
- ECerr(EC_F_EC_PRE_COMP_DUP, ERR_R_INTERNAL_ERROR);
- return NULL;
- }
- }
+ CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
- return ret;
+ return src_;
}
static void ec_pre_comp_free(void *pre_)
{
+ int i;
EC_PRE_COMP *pre = pre_;
if (!pre)
return;
+
+ i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
+ if (i > 0)
+ return;
+
if (pre->points)
{
- EC_POINT **var;
+ EC_POINT **p;
- for (var = pre->points; *var != NULL; var++)
- EC_POINT_free(*var);
+ for (p = pre->points; *p != NULL; p++)
+ EC_POINT_free(*p);
OPENSSL_free(pre->points);
}
OPENSSL_free(pre);
static void ec_pre_comp_clear_free(void *pre_)
{
+ int i;
EC_PRE_COMP *pre = pre_;
if (!pre)
return;
+
+ i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
+ if (i > 0)
+ return;
+
if (pre->points)
{
EC_POINT **p;
next_bit = bit << 1; /* at most 256 */
mask = next_bit - 1; /* at most 255 */
- if (BN_get_sign(scalar))
+ if (BN_is_negative(scalar))
{
sign = -1;
}
* (thus the boundaries should be increased)
*/
#define EC_window_bits_for_scalar_size(b) \
- ((b) >= 2000 ? 6 : \
- (b) >= 800 ? 5 : \
- (b) >= 300 ? 4 : \
- (b) >= 70 ? 3 : \
- (b) >= 20 ? 2 : \
- 1)
+ ((size_t) \
+ ((b) >= 2000 ? 6 : \
+ (b) >= 800 ? 5 : \
+ (b) >= 300 ? 4 : \
+ (b) >= 70 ? 3 : \
+ (b) >= 20 ? 2 : \
+ 1))
/* Compute
* \sum scalars[i]*points[i],
size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
{
BN_CTX *new_ctx = NULL;
- EC_POINT *generator = NULL;
+ const EC_POINT *generator = NULL;
EC_POINT *tmp = NULL;
size_t totalnum;
size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
EC_POINT **val = NULL; /* precomputation */
EC_POINT **v;
EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or 'pre_comp->points' */
- EC_PRE_COMP *pre_comp = NULL;
+ const EC_PRE_COMP *pre_comp = NULL;
int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be treated like other scalars,
* i.e. precomputation is not available */
int ret = 0;
/* look if we can use precomputed multiples of generator */
- pre_comp = EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
+ pre_comp = EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0))
{
BIGNUM *order;
size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
EC_POINT **points = NULL;
- EC_PRE_COMP *pre_comp, *new_pre_comp = NULL;
+ EC_PRE_COMP *pre_comp;
int ret = 0;
- pre_comp = EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
- if (pre_comp == NULL)
- if ((pre_comp = new_pre_comp = ec_pre_comp_new(group)) == NULL)
- return 0;
+ /* if there is an old EC_PRE_COMP object, throw it away */
+ EC_EX_DATA_free_data(&group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
+
+ if ((pre_comp = ec_pre_comp_new(group)) == NULL)
+ return 0;
generator = EC_GROUP_get0_generator(group);
if (generator == NULL)
pre_comp->blocksize = blocksize;
pre_comp->numblocks = numblocks;
pre_comp->w = w;
- if (pre_comp->points)
- {
- EC_POINT **p;
-
- for (p = pre_comp->points; *p != NULL; p++)
- EC_POINT_free(*p);
- OPENSSL_free(pre_comp->points);
- }
pre_comp->points = points;
points = NULL;
pre_comp->num = num;
- if (new_pre_comp)
- {
- if (!EC_GROUP_set_extra_data(group, new_pre_comp, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
- goto err;
- new_pre_comp = NULL;
- }
+ if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
+ ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
+ goto err;
+ pre_comp = NULL;
ret = 1;
err:
BN_CTX_end(ctx);
if (new_ctx != NULL)
BN_CTX_free(new_ctx);
- if (new_pre_comp)
- ec_pre_comp_free(new_pre_comp);
+ if (pre_comp)
+ ec_pre_comp_free(pre_comp);
if (points)
{
EC_POINT **p;
int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
{
- if (EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free) != NULL)
+ if (EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free) != NULL)
return 1;
else
return 0;