2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2000-02-05 22:17:32 +08:00
|
|
|
*
|
2018-12-06 20:22:12 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2018-12-13 19:35:48 +08:00
|
|
|
#include <openssl/trace.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "bn_local.h"
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
/* How many bignums are in each "pool item"; */
|
|
|
|
#define BN_CTX_POOL_SIZE 16
|
|
|
|
/* The stack frame info is resizing, set a first-time expansion size; */
|
|
|
|
#define BN_CTX_START_FRAMES 32
|
|
|
|
|
|
|
|
/***********/
|
|
|
|
/* BN_POOL */
|
|
|
|
/***********/
|
|
|
|
|
|
|
|
/* A bundle of bignums that can be linked with other bundles */
|
|
|
|
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;
|
|
|
|
/* A linked-list of bignums grouped in bundles */
|
|
|
|
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);
|
2004-03-25 12:16:14 +08:00
|
|
|
static void BN_POOL_release(BN_POOL *, unsigned int);
|
|
|
|
|
|
|
|
/************/
|
|
|
|
/* BN_STACK */
|
|
|
|
/************/
|
|
|
|
|
|
|
|
/* A wrapper to manage the "stack frames" */
|
|
|
|
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 *);
|
|
|
|
|
|
|
|
/**********/
|
|
|
|
/* BN_CTX */
|
|
|
|
/**********/
|
|
|
|
|
|
|
|
/* The opaque BN_CTX type */
|
2003-10-30 02:04:37 +08:00
|
|
|
struct bignum_ctx {
|
2004-03-25 12:16:14 +08:00
|
|
|
/* 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) */
|
2003-10-30 02:04:37 +08:00
|
|
|
int too_many;
|
2015-04-25 04:39:40 +08:00
|
|
|
/* Flags. */
|
|
|
|
int flags;
|
2019-05-30 00:03:53 +08:00
|
|
|
/* The library context */
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2003-10-30 02:04:37 +08:00
|
|
|
};
|
2000-02-05 22:17:32 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2018-12-13 19:35:48 +08:00
|
|
|
/* Debugging functionality */
|
|
|
|
static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
|
2004-03-25 12:16:14 +08:00
|
|
|
{
|
|
|
|
unsigned int bnidx = 0, fpidx = 0;
|
|
|
|
BN_POOL_ITEM *item = ctx->pool.head;
|
|
|
|
BN_STACK *stack = &ctx->stack;
|
2018-12-13 19:35:48 +08:00
|
|
|
|
|
|
|
BIO_printf(channel, "%s\n", text);
|
|
|
|
BIO_printf(channel, " (%16p): ", (void*)ctx);
|
2004-03-25 12:16:14 +08:00
|
|
|
while (bnidx < ctx->used) {
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(channel, "%03x ",
|
|
|
|
item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
|
2004-03-25 12:16:14 +08:00
|
|
|
if (!(bnidx % BN_CTX_POOL_SIZE))
|
|
|
|
item = item->next;
|
|
|
|
}
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(channel, "\n");
|
2004-03-25 12:16:14 +08:00
|
|
|
bnidx = 0;
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(channel, " %16s : ", "");
|
2004-03-25 12:16:14 +08:00
|
|
|
while (fpidx < stack->depth) {
|
|
|
|
while (bnidx++ < stack->indexes[fpidx])
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(channel, " ");
|
|
|
|
BIO_printf(channel, "^^^ ");
|
2004-03-25 12:16:14 +08:00
|
|
|
bnidx++;
|
|
|
|
fpidx++;
|
|
|
|
}
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(channel, "\n");
|
2004-03-25 12:16:14 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-05-30 00:31:22 +08:00
|
|
|
# define CTXDBG(str, ctx) \
|
2018-12-13 19:35:48 +08:00
|
|
|
OSSL_TRACE_BEGIN(BN_CTX) { \
|
|
|
|
ctxdbg(trc_out, str, ctx); \
|
|
|
|
} OSSL_TRACE_END(BN_CTX)
|
2019-05-30 00:31:22 +08:00
|
|
|
#else
|
2021-03-03 00:17:46 +08:00
|
|
|
/* We do not want tracing in FIPS module */
|
2019-05-30 00:31:22 +08:00
|
|
|
# define CTXDBG(str, ctx) do {} while(0)
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|
2004-03-25 12:16:14 +08:00
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
BN_CTX *BN_CTX_new_ex(OSSL_LIB_CTX *ctx)
|
2003-10-30 02:04:37 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
BN_CTX *ret;
|
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
|
2004-03-25 12:16:14 +08:00
|
|
|
return NULL;
|
|
|
|
/* Initialise the structure */
|
|
|
|
BN_POOL_init(&ret->pool);
|
|
|
|
BN_STACK_init(&ret->stack);
|
2019-05-30 00:03:53 +08:00
|
|
|
ret->libctx = ctx;
|
2015-04-25 04:39:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2019-05-30 00:03:53 +08:00
|
|
|
BN_CTX *BN_CTX_new(void)
|
2015-04-25 04:39:40 +08:00
|
|
|
{
|
2019-05-30 00:03:53 +08:00
|
|
|
return BN_CTX_new_ex(NULL);
|
|
|
|
}
|
2019-07-04 22:41:17 +08:00
|
|
|
#endif
|
2019-05-30 00:03:53 +08:00
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx)
|
2019-05-30 00:03:53 +08:00
|
|
|
{
|
|
|
|
BN_CTX *ret = BN_CTX_new_ex(ctx);
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ret != NULL)
|
2015-04-25 04:39:40 +08:00
|
|
|
ret->flags = BN_FLG_SECURE;
|
2004-03-25 12:16:14 +08:00
|
|
|
return ret;
|
2003-10-30 02:04:37 +08:00
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2019-05-30 00:03:53 +08:00
|
|
|
BN_CTX *BN_CTX_secure_new(void)
|
|
|
|
{
|
|
|
|
return BN_CTX_secure_new_ex(NULL);
|
|
|
|
}
|
2019-07-04 22:41:17 +08:00
|
|
|
#endif
|
2019-05-30 00:03:53 +08:00
|
|
|
|
2000-02-05 22:17:32 +08:00
|
|
|
void BN_CTX_free(BN_CTX *ctx)
|
|
|
|
{
|
2018-03-28 04:25:08 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2018-12-13 19:35:48 +08:00
|
|
|
OSSL_TRACE_BEGIN(BN_CTX) {
|
2004-03-25 12:16:14 +08:00
|
|
|
BN_POOL_ITEM *pool = ctx->pool.head;
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(trc_out,
|
|
|
|
"BN_CTX_free(): stack-size=%d, pool-bignums=%d\n",
|
|
|
|
ctx->stack.size, ctx->pool.size);
|
|
|
|
BIO_printf(trc_out, " dmaxs: ");
|
2004-03-25 12:16:14 +08:00
|
|
|
while (pool) {
|
|
|
|
unsigned loop = 0;
|
|
|
|
while (loop < BN_CTX_POOL_SIZE)
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax);
|
2004-03-25 12:16:14 +08:00
|
|
|
pool = pool->next;
|
2003-11-05 06:54:49 +08:00
|
|
|
}
|
2018-12-13 19:35:48 +08:00
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(BN_CTX);
|
2019-06-14 16:07:29 +08:00
|
|
|
#endif
|
2004-03-25 12:16:14 +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)
|
|
|
|
{
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("ENTER BN_CTX_start()", ctx);
|
2004-03-25 12:16:14 +08:00
|
|
|
/* 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)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
2004-03-25 12:16:14 +08:00
|
|
|
ctx->err_stack++;
|
|
|
|
}
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("LEAVE BN_CTX_start()", ctx);
|
2000-02-05 22:17:32 +08:00
|
|
|
}
|
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
void BN_CTX_end(BN_CTX *ctx)
|
|
|
|
{
|
2019-03-19 07:58:09 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("ENTER BN_CTX_end()", ctx);
|
2004-03-25 12:16:14 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("LEAVE BN_CTX_end()", ctx);
|
2004-03-25 12:16:14 +08:00
|
|
|
}
|
2000-11-08 18:05:34 +08:00
|
|
|
|
2000-02-05 22:17:32 +08:00
|
|
|
BIGNUM *BN_CTX_get(BN_CTX *ctx)
|
|
|
|
{
|
2004-03-14 06:10:15 +08:00
|
|
|
BIGNUM *ret;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("ENTER BN_CTX_get()", ctx);
|
2004-03-25 12:16:14 +08:00
|
|
|
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) {
|
2004-03-25 12:16:14 +08:00
|
|
|
/*
|
|
|
|
* Setting too_many prevents repeated "get" attempts from cluttering
|
|
|
|
* the error stack.
|
|
|
|
*/
|
|
|
|
ctx->too_many = 1;
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
|
2000-02-05 22:17:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
/* OK, make sure the returned bignum is "zero" */
|
2004-03-14 06:10:15 +08:00
|
|
|
BN_zero(ret);
|
2019-02-08 18:42:25 +08:00
|
|
|
/* clear BN_FLG_CONSTTIME if leaked from previous frames */
|
|
|
|
ret->flags &= (~BN_FLG_CONSTTIME);
|
2004-03-25 12:16:14 +08:00
|
|
|
ctx->used++;
|
2018-12-13 19:35:48 +08:00
|
|
|
CTXDBG("LEAVE BN_CTX_get()", ctx);
|
2004-03-14 06:10:15 +08:00
|
|
|
return ret;
|
2000-02-05 22:17:32 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:15:41 +08:00
|
|
|
OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx)
|
2019-05-30 00:31:22 +08:00
|
|
|
{
|
2019-06-28 18:24:51 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
2019-05-30 00:31:22 +08:00
|
|
|
return ctx->libctx;
|
|
|
|
}
|
|
|
|
|
2004-03-25 12:16:14 +08:00
|
|
|
/************/
|
|
|
|
/* BN_STACK */
|
|
|
|
/************/
|
|
|
|
|
|
|
|
static void BN_STACK_init(BN_STACK *st)
|
|
|
|
{
|
|
|
|
st->indexes = NULL;
|
|
|
|
st->depth = st->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BN_STACK_finish(BN_STACK *st)
|
2000-02-05 22:17:32 +08:00
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
OPENSSL_free(st->indexes);
|
|
|
|
st->indexes = NULL;
|
2004-03-25 12:16:14 +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-04-25 04:39:40 +08:00
|
|
|
if (st->depth == st->size) {
|
2004-03-25 12:16:14 +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;
|
2019-02-01 01:55:30 +08:00
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL)
|
2004-03-25 12:16:14 +08:00
|
|
|
return 0;
|
|
|
|
if (st->depth)
|
2015-04-25 04:39:40 +08:00
|
|
|
memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
|
|
|
|
OPENSSL_free(st->indexes);
|
2004-03-25 12:16:14 +08:00
|
|
|
st->indexes = newitems;
|
|
|
|
st->size = newsize;
|
|
|
|
}
|
|
|
|
st->indexes[(st->depth)++] = idx;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int BN_STACK_pop(BN_STACK *st)
|
|
|
|
{
|
|
|
|
return st->indexes[--(st->depth)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********/
|
|
|
|
/* BN_POOL */
|
|
|
|
/***********/
|
|
|
|
|
|
|
|
static void BN_POOL_init(BN_POOL *p)
|
|
|
|
{
|
|
|
|
p->head = p->current = p->tail = NULL;
|
|
|
|
p->used = p->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BN_POOL_finish(BN_POOL *p)
|
|
|
|
{
|
2015-04-25 04:39:40 +08:00
|
|
|
unsigned int loop;
|
|
|
|
BIGNUM *bn;
|
|
|
|
|
2004-03-25 12:16:14 +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++)
|
2004-03-25 12:16:14 +08:00
|
|
|
if (bn->d)
|
|
|
|
BN_clear_free(bn);
|
|
|
|
p->current = p->head->next;
|
|
|
|
OPENSSL_free(p->head);
|
|
|
|
p->head = p->current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-25 04:39:40 +08:00
|
|
|
static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
|
2004-03-25 12:16:14 +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. */
|
2004-03-25 12:16:14 +08:00
|
|
|
if (p->used == p->size) {
|
2018-04-03 23:31:16 +08:00
|
|
|
BN_POOL_ITEM *item;
|
2019-02-01 01:55:30 +08:00
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((item = OPENSSL_malloc(sizeof(*item))) == NULL)
|
2004-03-25 12:16:14 +08:00
|
|
|
return NULL;
|
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);
|
|
|
|
}
|
2004-03-25 12:16:14 +08:00
|
|
|
item->prev = p->tail;
|
|
|
|
item->next = NULL;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
|
|
|
if (p->head == NULL)
|
2004-03-25 12:16:14 +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
|
|
|
|
2004-03-25 12:16:14 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BN_POOL_release(BN_POOL *p, unsigned int num)
|
|
|
|
{
|
|
|
|
unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
|
2015-04-25 04:39:40 +08:00
|
|
|
|
2004-03-25 12:16:14 +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) {
|
2004-03-25 12:16:14 +08:00
|
|
|
offset = BN_CTX_POOL_SIZE - 1;
|
|
|
|
p->current = p->current->prev;
|
|
|
|
} else
|
|
|
|
offset--;
|
2000-02-05 22:17:32 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|