2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2018-04-17 21:18:40 +08:00
|
|
|
* Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2000-02-05 22:17:32 +08:00
|
|
|
*
|
2016-05-18 02:51:04 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2000-02-05 22:17:32 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2001-03-08 23:56:15 +08:00
|
|
|
#include "bn_lcl.h"
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2015-01-05 08:34:00 +08:00
|
|
|
/*-
|
|
|
|
* TODO list
|
2004-03-25 12:16:14 +08:00
|
|
|
*
|
|
|
|
* 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
|
|
|
|
* check they can be safely removed.
|
|
|
|
* - Check +1 and other ugliness in BN_from_montgomery()
|
|
|
|
*
|
|
|
|
* 2. Consider allowing a BN_new_ex() that, at least, lets you specify an
|
|
|
|
* appropriate 'block' size that will be honoured by bn_expand_internal() to
|
|
|
|
* prevent piddly little reallocations. OTOH, profiling bignum expansions in
|
|
|
|
* BN_CTX doesn't show this to be a big issue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* How many bignums are in each "pool item"; */
|
2015-01-22 11:40:55 +08:00
|
|
|
#define BN_CTX_POOL_SIZE 16
|
2004-03-25 12:16:14 +08:00
|
|
|
/* The stack frame info is resizing, set a first-time expansion size; */
|
2015-01-22 11:40:55 +08:00
|
|
|
#define BN_CTX_START_FRAMES 32
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
/***********/
|
|
|
|
/* BN_POOL */
|
|
|
|
/***********/
|
|
|
|
|
|
|
|
/* A bundle of bignums that can be linked with other bundles */
|
2015-01-22 11:40:55 +08:00
|
|
|
typedef struct bignum_pool_item {
|
|
|
|
/* The bignum values */
|
|
|
|
BIGNUM vals[BN_CTX_POOL_SIZE];
|
|
|
|
/* Linked-list admin */
|
|
|
|
struct bignum_pool_item *prev, *next;
|
|
|
|
} BN_POOL_ITEM;
|
2004-03-25 12:16:14 +08:00
|
|
|
/* A linked-list of bignums grouped in bundles */
|
2015-01-22 11:40:55 +08:00
|
|
|
typedef struct bignum_pool {
|
|
|
|
/* Linked-list admin */
|
|
|
|
BN_POOL_ITEM *head, *current, *tail;
|
|
|
|
/* Stack depth and allocation size */
|
|
|
|
unsigned used, size;
|
|
|
|
} BN_POOL;
|
|
|
|
static void BN_POOL_init(BN_POOL *);
|
|
|
|
static void BN_POOL_finish(BN_POOL *);
|
2015-04-25 04:39:40 +08:00
|
|
|
static BIGNUM *BN_POOL_get(BN_POOL *, int);
|
2015-01-22 11:40:55 +08:00
|
|
|
static void BN_POOL_release(BN_POOL *, unsigned int);
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
/************/
|
|
|
|
/* BN_STACK */
|
|
|
|
/************/
|
|
|
|
|
|
|
|
/* A wrapper to manage the "stack frames" */
|
2015-01-22 11:40:55 +08:00
|
|
|
typedef struct bignum_ctx_stack {
|
|
|
|
/* Array of indexes into the bignum stack */
|
|
|
|
unsigned int *indexes;
|
|
|
|
/* Number of stack frames, and the size of the allocated array */
|
|
|
|
unsigned int depth, size;
|
|
|
|
} BN_STACK;
|
|
|
|
static void BN_STACK_init(BN_STACK *);
|
|
|
|
static void BN_STACK_finish(BN_STACK *);
|
|
|
|
static int BN_STACK_push(BN_STACK *, unsigned int);
|
|
|
|
static unsigned int BN_STACK_pop(BN_STACK *);
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
/**********/
|
|
|
|
/* BN_CTX */
|
|
|
|
/**********/
|
|
|
|
|
|
|
|
/* The opaque BN_CTX type */
|
2015-01-22 11:40:55 +08:00
|
|
|
struct bignum_ctx {
|
|
|
|
/* The bignum bundles */
|
|
|
|
BN_POOL pool;
|
|
|
|
/* The "stack frames", if you will */
|
|
|
|
BN_STACK stack;
|
|
|
|
/* The number of bignums currently assigned */
|
|
|
|
unsigned int used;
|
|
|
|
/* Depth of stack overflow */
|
|
|
|
int err_stack;
|
|
|
|
/* Block "gets" until an "end" (compatibility behaviour) */
|
|
|
|
int too_many;
|
2015-04-25 04:39:40 +08:00
|
|
|
/* Flags. */
|
|
|
|
int flags;
|
2015-01-22 11:40:55 +08:00
|
|
|
};
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
/* Enable this to find BN_CTX bugs */
|
|
|
|
#ifdef BN_CTX_DEBUG
|
|
|
|
static const char *ctxdbg_cur = NULL;
|
|
|
|
static void ctxdbg(BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned int bnidx = 0, fpidx = 0;
|
|
|
|
BN_POOL_ITEM *item = ctx->pool.head;
|
|
|
|
BN_STACK *stack = &ctx->stack;
|
|
|
|
fprintf(stderr, "(%16p): ", ctx);
|
|
|
|
while (bnidx < ctx->used) {
|
|
|
|
fprintf(stderr, "%03x ", item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
|
|
|
|
if (!(bnidx % BN_CTX_POOL_SIZE))
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
bnidx = 0;
|
|
|
|
fprintf(stderr, " : ");
|
|
|
|
while (fpidx < stack->depth) {
|
|
|
|
while (bnidx++ < stack->indexes[fpidx])
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
fprintf(stderr, "^^^ ");
|
|
|
|
bnidx++;
|
|
|
|
fpidx++;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
# define CTXDBG_ENTRY(str, ctx) do { \
|
|
|
|
ctxdbg_cur = (str); \
|
|
|
|
fprintf(stderr,"Starting %s\n", ctxdbg_cur); \
|
|
|
|
ctxdbg(ctx); \
|
|
|
|
} while(0)
|
|
|
|
# define CTXDBG_EXIT(ctx) do { \
|
|
|
|
fprintf(stderr,"Ending %s\n", ctxdbg_cur); \
|
|
|
|
ctxdbg(ctx); \
|
|
|
|
} while(0)
|
|
|
|
# define CTXDBG_RET(ctx,ret)
|
2003-10-30 02:04:37 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
# define CTXDBG_ENTRY(str, ctx)
|
|
|
|
# define CTXDBG_EXIT(ctx)
|
|
|
|
# define CTXDBG_RET(ctx,ret)
|
2003-10-30 02:04:37 +08:00
|
|
|
#endif
|
2004-03-25 12:16:14 +08:00
|
|
|
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2003-10-30 02:04:37 +08:00
|
|
|
BN_CTX *BN_CTX_new(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
BN_CTX *ret;
|
|
|
|
|
2015-09-03 21:15:26 +08:00
|
|
|
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Initialise the structure */
|
|
|
|
BN_POOL_init(&ret->pool);
|
|
|
|
BN_STACK_init(&ret->stack);
|
2015-04-25 04:39:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_CTX *BN_CTX_secure_new(void)
|
|
|
|
{
|
|
|
|
BN_CTX *ret = BN_CTX_new();
|
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ret != NULL)
|
2015-04-25 04:39:40 +08:00
|
|
|
ret->flags = BN_FLG_SECURE;
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2003-10-30 02:04:37 +08:00
|
|
|
|
2000-02-05 22:17:32 +08:00
|
|
|
void BN_CTX_free(BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2018-03-28 04:25:08 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2004-03-25 12:16:14 +08:00
|
|
|
#ifdef BN_CTX_DEBUG
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BN_POOL_ITEM *pool = ctx->pool.head;
|
|
|
|
fprintf(stderr, "BN_CTX_free, stack-size=%d, pool-bignums=%d\n",
|
|
|
|
ctx->stack.size, ctx->pool.size);
|
|
|
|
fprintf(stderr, "dmaxs: ");
|
|
|
|
while (pool) {
|
|
|
|
unsigned loop = 0;
|
|
|
|
while (loop < BN_CTX_POOL_SIZE)
|
|
|
|
fprintf(stderr, "%02x ", pool->vals[loop++].dmax);
|
|
|
|
pool = pool->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_STACK_finish(&ctx->stack);
|
|
|
|
BN_POOL_finish(&ctx->pool);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
2000-02-05 22:17:32 +08:00
|
|
|
|
|
|
|
void BN_CTX_start(BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
CTXDBG_ENTRY("BN_CTX_start", ctx);
|
|
|
|
/* If we're already overflowing ... */
|
|
|
|
if (ctx->err_stack || ctx->too_many)
|
|
|
|
ctx->err_stack++;
|
|
|
|
/* (Try to) get a new frame pointer */
|
|
|
|
else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
|
|
|
|
BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
|
|
|
ctx->err_stack++;
|
|
|
|
}
|
|
|
|
CTXDBG_EXIT(ctx);
|
|
|
|
}
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
void BN_CTX_end(BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
CTXDBG_ENTRY("BN_CTX_end", ctx);
|
|
|
|
if (ctx->err_stack)
|
|
|
|
ctx->err_stack--;
|
|
|
|
else {
|
|
|
|
unsigned int fp = BN_STACK_pop(&ctx->stack);
|
|
|
|
/* Does this stack frame have anything to release? */
|
|
|
|
if (fp < ctx->used)
|
|
|
|
BN_POOL_release(&ctx->pool, ctx->used - fp);
|
|
|
|
ctx->used = fp;
|
|
|
|
/* Unjam "too_many" in case "get" had failed */
|
|
|
|
ctx->too_many = 0;
|
|
|
|
}
|
|
|
|
CTXDBG_EXIT(ctx);
|
|
|
|
}
|
2000-11-08 18:05:34 +08:00
|
|
|
|
2000-02-05 22:17:32 +08:00
|
|
|
BIGNUM *BN_CTX_get(BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIGNUM *ret;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
CTXDBG_ENTRY("BN_CTX_get", ctx);
|
|
|
|
if (ctx->err_stack || ctx->too_many)
|
|
|
|
return NULL;
|
2015-04-25 04:39:40 +08:00
|
|
|
if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Setting too_many prevents repeated "get" attempts from cluttering
|
|
|
|
* the error stack.
|
|
|
|
*/
|
|
|
|
ctx->too_many = 1;
|
|
|
|
BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* OK, make sure the returned bignum is "zero" */
|
|
|
|
BN_zero(ret);
|
|
|
|
ctx->used++;
|
|
|
|
CTXDBG_RET(ctx, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
/************/
|
|
|
|
/* BN_STACK */
|
|
|
|
/************/
|
|
|
|
|
|
|
|
static void BN_STACK_init(BN_STACK *st)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
st->indexes = NULL;
|
|
|
|
st->depth = st->size = 0;
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
static void BN_STACK_finish(BN_STACK *st)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
OPENSSL_free(st->indexes);
|
|
|
|
st->indexes = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-02-06 03:29:00 +08:00
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
static int BN_STACK_push(BN_STACK *st, unsigned int idx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
if (st->depth == st->size) {
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Need to expand */
|
2015-04-25 04:39:40 +08:00
|
|
|
unsigned int newsize =
|
|
|
|
st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
|
2018-04-03 23:31:16 +08:00
|
|
|
unsigned int *newitems;
|
|
|
|
|
|
|
|
if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
|
|
|
|
BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2018-04-03 23:31:16 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (st->depth)
|
2015-04-25 04:39:40 +08:00
|
|
|
memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
|
|
|
|
OPENSSL_free(st->indexes);
|
2015-01-22 11:40:55 +08:00
|
|
|
st->indexes = newitems;
|
|
|
|
st->size = newsize;
|
|
|
|
}
|
|
|
|
st->indexes[(st->depth)++] = idx;
|
|
|
|
return 1;
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
static unsigned int BN_STACK_pop(BN_STACK *st)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return st->indexes[--(st->depth)];
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
/***********/
|
|
|
|
/* BN_POOL */
|
|
|
|
/***********/
|
|
|
|
|
|
|
|
static void BN_POOL_init(BN_POOL *p)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
p->head = p->current = p->tail = NULL;
|
|
|
|
p->used = p->size = 0;
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
static void BN_POOL_finish(BN_POOL *p)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
unsigned int loop;
|
|
|
|
BIGNUM *bn;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
while (p->head) {
|
2015-04-25 04:39:40 +08:00
|
|
|
for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
|
2015-01-22 11:40:55 +08:00
|
|
|
if (bn->d)
|
|
|
|
BN_clear_free(bn);
|
|
|
|
p->current = p->head->next;
|
|
|
|
OPENSSL_free(p->head);
|
|
|
|
p->head = p->current;
|
|
|
|
}
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
|
2015-04-25 04:39:40 +08:00
|
|
|
static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
BIGNUM *bn;
|
|
|
|
unsigned int loop;
|
|
|
|
|
|
|
|
/* Full; allocate a new pool item and link it in. */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (p->used == p->size) {
|
2018-04-03 23:31:16 +08:00
|
|
|
BN_POOL_ITEM *item;
|
|
|
|
|
|
|
|
if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
|
|
|
|
BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2018-04-03 23:31:16 +08:00
|
|
|
}
|
2015-04-25 04:39:40 +08:00
|
|
|
for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
|
2015-11-21 21:27:46 +08:00
|
|
|
bn_init(bn);
|
2015-04-25 04:39:40 +08:00
|
|
|
if ((flag & BN_FLG_SECURE) != 0)
|
|
|
|
BN_set_flags(bn, BN_FLG_SECURE);
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
item->prev = p->tail;
|
|
|
|
item->next = NULL;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
|
|
|
if (p->head == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
p->head = p->current = p->tail = item;
|
|
|
|
else {
|
|
|
|
p->tail->next = item;
|
|
|
|
p->tail = item;
|
|
|
|
p->current = item;
|
|
|
|
}
|
|
|
|
p->size += BN_CTX_POOL_SIZE;
|
|
|
|
p->used++;
|
|
|
|
/* Return the first bignum from the new pool */
|
|
|
|
return item->vals;
|
|
|
|
}
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!p->used)
|
|
|
|
p->current = p->head;
|
|
|
|
else if ((p->used % BN_CTX_POOL_SIZE) == 0)
|
|
|
|
p->current = p->current->next;
|
|
|
|
return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
|
|
|
|
static void BN_POOL_release(BN_POOL *p, unsigned int num)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
p->used -= num;
|
|
|
|
while (num--) {
|
|
|
|
bn_check_top(p->current->vals + offset);
|
2015-04-25 04:39:40 +08:00
|
|
|
if (offset == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
offset = BN_CTX_POOL_SIZE - 1;
|
|
|
|
p->current = p->current->prev;
|
|
|
|
} else
|
|
|
|
offset--;
|
|
|
|
}
|
|
|
|
}
|