2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/trace.h>
11 #include "internal/cryptlib.h"
17 * 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
18 * check they can be safely removed.
19 * - Check +1 and other ugliness in BN_from_montgomery()
21 * 2. Consider allowing a BN_new_ex() that, at least, lets you specify an
22 * appropriate 'block' size that will be honoured by bn_expand_internal() to
23 * prevent piddly little reallocations. OTOH, profiling bignum expansions in
24 * BN_CTX doesn't show this to be a big issue.
27 /* How many bignums are in each "pool item"; */
28 #define BN_CTX_POOL_SIZE 16
29 /* The stack frame info is resizing, set a first-time expansion size; */
30 #define BN_CTX_START_FRAMES 32
36 /* A bundle of bignums that can be linked with other bundles */
37 typedef struct bignum_pool_item {
38 /* The bignum values */
39 BIGNUM vals[BN_CTX_POOL_SIZE];
40 /* Linked-list admin */
41 struct bignum_pool_item *prev, *next;
43 /* A linked-list of bignums grouped in bundles */
44 typedef struct bignum_pool {
45 /* Linked-list admin */
46 BN_POOL_ITEM *head, *current, *tail;
47 /* Stack depth and allocation size */
50 static void BN_POOL_init(BN_POOL *);
51 static void BN_POOL_finish(BN_POOL *);
52 static BIGNUM *BN_POOL_get(BN_POOL *, int);
53 static void BN_POOL_release(BN_POOL *, unsigned int);
59 /* A wrapper to manage the "stack frames" */
60 typedef struct bignum_ctx_stack {
61 /* Array of indexes into the bignum stack */
62 unsigned int *indexes;
63 /* Number of stack frames, and the size of the allocated array */
64 unsigned int depth, size;
66 static void BN_STACK_init(BN_STACK *);
67 static void BN_STACK_finish(BN_STACK *);
68 static int BN_STACK_push(BN_STACK *, unsigned int);
69 static unsigned int BN_STACK_pop(BN_STACK *);
75 /* The opaque BN_CTX type */
77 /* The bignum bundles */
79 /* The "stack frames", if you will */
81 /* The number of bignums currently assigned */
83 /* Depth of stack overflow */
85 /* Block "gets" until an "end" (compatibility behaviour) */
89 /* The library context */
94 /* Debugging functionality */
95 static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
97 unsigned int bnidx = 0, fpidx = 0;
98 BN_POOL_ITEM *item = ctx->pool.head;
99 BN_STACK *stack = &ctx->stack;
101 BIO_printf(channel, "%s\n", text);
102 BIO_printf(channel, " (%16p): ", (void*)ctx);
103 while (bnidx < ctx->used) {
104 BIO_printf(channel, "%03x ",
105 item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
106 if (!(bnidx % BN_CTX_POOL_SIZE))
109 BIO_printf(channel, "\n");
111 BIO_printf(channel, " %16s : ", "");
112 while (fpidx < stack->depth) {
113 while (bnidx++ < stack->indexes[fpidx])
114 BIO_printf(channel, " ");
115 BIO_printf(channel, "^^^ ");
119 BIO_printf(channel, "\n");
122 # define CTXDBG(str, ctx) \
123 OSSL_TRACE_BEGIN(BN_CTX) { \
124 ctxdbg(trc_out, str, ctx); \
125 } OSSL_TRACE_END(BN_CTX)
127 /* TODO(3.0): Consider if we want to do this in FIPS mode */
128 # define CTXDBG(str, ctx) do {} while(0)
129 #endif /* FIPS_MODULE */
131 BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx)
135 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
136 BNerr(BN_F_BN_CTX_NEW_EX, ERR_R_MALLOC_FAILURE);
139 /* Initialise the structure */
140 BN_POOL_init(&ret->pool);
141 BN_STACK_init(&ret->stack);
147 BN_CTX *BN_CTX_new(void)
149 return BN_CTX_new_ex(NULL);
153 BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx)
155 BN_CTX *ret = BN_CTX_new_ex(ctx);
158 ret->flags = BN_FLG_SECURE;
163 BN_CTX *BN_CTX_secure_new(void)
165 return BN_CTX_secure_new_ex(NULL);
169 void BN_CTX_free(BN_CTX *ctx)
174 OSSL_TRACE_BEGIN(BN_CTX) {
175 BN_POOL_ITEM *pool = ctx->pool.head;
177 "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n",
178 ctx->stack.size, ctx->pool.size);
179 BIO_printf(trc_out, " dmaxs: ");
182 while (loop < BN_CTX_POOL_SIZE)
183 BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax);
186 BIO_printf(trc_out, "\n");
187 } OSSL_TRACE_END(BN_CTX);
189 BN_STACK_finish(&ctx->stack);
190 BN_POOL_finish(&ctx->pool);
194 void BN_CTX_start(BN_CTX *ctx)
196 CTXDBG("ENTER BN_CTX_start()", ctx);
197 /* If we're already overflowing ... */
198 if (ctx->err_stack || ctx->too_many)
200 /* (Try to) get a new frame pointer */
201 else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
202 BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
205 CTXDBG("LEAVE BN_CTX_start()", ctx);
208 void BN_CTX_end(BN_CTX *ctx)
212 CTXDBG("ENTER BN_CTX_end()", ctx);
216 unsigned int fp = BN_STACK_pop(&ctx->stack);
217 /* Does this stack frame have anything to release? */
219 BN_POOL_release(&ctx->pool, ctx->used - fp);
221 /* Unjam "too_many" in case "get" had failed */
224 CTXDBG("LEAVE BN_CTX_end()", ctx);
227 BIGNUM *BN_CTX_get(BN_CTX *ctx)
231 CTXDBG("ENTER BN_CTX_get()", ctx);
232 if (ctx->err_stack || ctx->too_many)
234 if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
236 * Setting too_many prevents repeated "get" attempts from cluttering
240 BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
243 /* OK, make sure the returned bignum is "zero" */
245 /* clear BN_FLG_CONSTTIME if leaked from previous frames */
246 ret->flags &= (~BN_FLG_CONSTTIME);
248 CTXDBG("LEAVE BN_CTX_get()", ctx);
252 OPENSSL_CTX *bn_get_lib_ctx(BN_CTX *ctx)
263 static void BN_STACK_init(BN_STACK *st)
266 st->depth = st->size = 0;
269 static void BN_STACK_finish(BN_STACK *st)
271 OPENSSL_free(st->indexes);
276 static int BN_STACK_push(BN_STACK *st, unsigned int idx)
278 if (st->depth == st->size) {
280 unsigned int newsize =
281 st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
282 unsigned int *newitems;
284 if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
285 BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
289 memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
290 OPENSSL_free(st->indexes);
291 st->indexes = newitems;
294 st->indexes[(st->depth)++] = idx;
298 static unsigned int BN_STACK_pop(BN_STACK *st)
300 return st->indexes[--(st->depth)];
307 static void BN_POOL_init(BN_POOL *p)
309 p->head = p->current = p->tail = NULL;
310 p->used = p->size = 0;
313 static void BN_POOL_finish(BN_POOL *p)
319 for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
322 p->current = p->head->next;
323 OPENSSL_free(p->head);
324 p->head = p->current;
329 static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
334 /* Full; allocate a new pool item and link it in. */
335 if (p->used == p->size) {
338 if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
339 BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
342 for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
344 if ((flag & BN_FLG_SECURE) != 0)
345 BN_set_flags(bn, BN_FLG_SECURE);
347 item->prev = p->tail;
351 p->head = p->current = p->tail = item;
353 p->tail->next = item;
357 p->size += BN_CTX_POOL_SIZE;
359 /* Return the first bignum from the new pool */
364 p->current = p->head;
365 else if ((p->used % BN_CTX_POOL_SIZE) == 0)
366 p->current = p->current->next;
367 return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
370 static void BN_POOL_release(BN_POOL *p, unsigned int num)
372 unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
376 bn_check_top(p->current->vals + offset);
378 offset = BN_CTX_POOL_SIZE - 1;
379 p->current = p->current->prev;