]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_ctx.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / bn / bn_ctx.c
CommitLineData
4f22f405 1/*
c8147d37 2 * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
9b141126 3 *
367ace68 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
9b141126
UM
8 */
9
6e810f2d 10#include <openssl/trace.h>
b39fc560 11#include "internal/cryptlib.h"
706457b7 12#include "bn_local.h"
9b141126 13
3a83462d
MC
14/*-
15 * TODO list
5c98b2ca
GT
16 *
17 * 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
18 * check they can be safely removed.
5c98b2ca 19 * - Check +1 and other ugliness in BN_from_montgomery()
5c98b2ca
GT
20 *
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.
25 */
26
27/* How many bignums are in each "pool item"; */
0f113f3e 28#define BN_CTX_POOL_SIZE 16
5c98b2ca 29/* The stack frame info is resizing, set a first-time expansion size; */
0f113f3e 30#define BN_CTX_START_FRAMES 32
5c98b2ca
GT
31
32/***********/
33/* BN_POOL */
34/***********/
35
36/* A bundle of bignums that can be linked with other bundles */
0f113f3e
MC
37typedef 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;
42} BN_POOL_ITEM;
5c98b2ca 43/* A linked-list of bignums grouped in bundles */
0f113f3e
MC
44typedef struct bignum_pool {
45 /* Linked-list admin */
46 BN_POOL_ITEM *head, *current, *tail;
47 /* Stack depth and allocation size */
48 unsigned used, size;
49} BN_POOL;
50static void BN_POOL_init(BN_POOL *);
51static void BN_POOL_finish(BN_POOL *);
74924dcb 52static BIGNUM *BN_POOL_get(BN_POOL *, int);
0f113f3e 53static void BN_POOL_release(BN_POOL *, unsigned int);
5c98b2ca
GT
54
55/************/
56/* BN_STACK */
57/************/
58
59/* A wrapper to manage the "stack frames" */
0f113f3e
MC
60typedef 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;
65} BN_STACK;
66static void BN_STACK_init(BN_STACK *);
67static void BN_STACK_finish(BN_STACK *);
0f113f3e
MC
68static int BN_STACK_push(BN_STACK *, unsigned int);
69static unsigned int BN_STACK_pop(BN_STACK *);
5c98b2ca
GT
70
71/**********/
72/* BN_CTX */
73/**********/
74
75/* The opaque BN_CTX type */
0f113f3e
MC
76struct bignum_ctx {
77 /* The bignum bundles */
78 BN_POOL pool;
79 /* The "stack frames", if you will */
80 BN_STACK stack;
81 /* The number of bignums currently assigned */
82 unsigned int used;
83 /* Depth of stack overflow */
84 int err_stack;
85 /* Block "gets" until an "end" (compatibility behaviour) */
86 int too_many;
74924dcb
RS
87 /* Flags. */
88 int flags;
7bc081dd
MC
89 /* The library context */
90 OPENSSL_CTX *libctx;
0f113f3e 91};
9b141126 92
636b087e 93#ifndef FIPS_MODE
6e810f2d
RL
94/* Debugging functionality */
95static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
0f113f3e
MC
96{
97 unsigned int bnidx = 0, fpidx = 0;
98 BN_POOL_ITEM *item = ctx->pool.head;
99 BN_STACK *stack = &ctx->stack;
6e810f2d
RL
100
101 BIO_printf(channel, "%s\n", text);
102 BIO_printf(channel, " (%16p): ", (void*)ctx);
0f113f3e 103 while (bnidx < ctx->used) {
6e810f2d
RL
104 BIO_printf(channel, "%03x ",
105 item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
0f113f3e
MC
106 if (!(bnidx % BN_CTX_POOL_SIZE))
107 item = item->next;
108 }
6e810f2d 109 BIO_printf(channel, "\n");
0f113f3e 110 bnidx = 0;
6e810f2d 111 BIO_printf(channel, " %16s : ", "");
0f113f3e
MC
112 while (fpidx < stack->depth) {
113 while (bnidx++ < stack->indexes[fpidx])
6e810f2d
RL
114 BIO_printf(channel, " ");
115 BIO_printf(channel, "^^^ ");
0f113f3e
MC
116 bnidx++;
117 fpidx++;
118 }
6e810f2d 119 BIO_printf(channel, "\n");
0f113f3e
MC
120}
121
636b087e 122# define CTXDBG(str, ctx) \
6e810f2d
RL
123 OSSL_TRACE_BEGIN(BN_CTX) { \
124 ctxdbg(trc_out, str, ctx); \
125 } OSSL_TRACE_END(BN_CTX)
636b087e
MC
126#else
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_MODE */
5c98b2ca 130
7bc081dd 131BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx)
0f113f3e 132{
74924dcb
RS
133 BN_CTX *ret;
134
64b25758 135 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
7bc081dd 136 BNerr(BN_F_BN_CTX_NEW_EX, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
137 return NULL;
138 }
139 /* Initialise the structure */
140 BN_POOL_init(&ret->pool);
141 BN_STACK_init(&ret->stack);
7bc081dd 142 ret->libctx = ctx;
74924dcb
RS
143 return ret;
144}
145
2934be91 146#ifndef FIPS_MODE
7bc081dd 147BN_CTX *BN_CTX_new(void)
74924dcb 148{
7bc081dd
MC
149 return BN_CTX_new_ex(NULL);
150}
2934be91 151#endif
7bc081dd
MC
152
153BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx)
154{
155 BN_CTX *ret = BN_CTX_new_ex(ctx);
74924dcb 156
90945fa3 157 if (ret != NULL)
74924dcb 158 ret->flags = BN_FLG_SECURE;
0f113f3e
MC
159 return ret;
160}
2ce90b9b 161
2934be91 162#ifndef FIPS_MODE
7bc081dd
MC
163BN_CTX *BN_CTX_secure_new(void)
164{
165 return BN_CTX_secure_new_ex(NULL);
166}
2934be91 167#endif
7bc081dd 168
9b141126 169void BN_CTX_free(BN_CTX *ctx)
0f113f3e 170{
e6e9170d
RS
171 if (ctx == NULL)
172 return;
aff96597 173#ifndef FIPS_MODE
6e810f2d 174 OSSL_TRACE_BEGIN(BN_CTX) {
0f113f3e 175 BN_POOL_ITEM *pool = ctx->pool.head;
6e810f2d
RL
176 BIO_printf(trc_out,
177 "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n",
178 ctx->stack.size, ctx->pool.size);
179 BIO_printf(trc_out, " dmaxs: ");
0f113f3e
MC
180 while (pool) {
181 unsigned loop = 0;
182 while (loop < BN_CTX_POOL_SIZE)
6e810f2d 183 BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax);
0f113f3e
MC
184 pool = pool->next;
185 }
6e810f2d
RL
186 BIO_printf(trc_out, "\n");
187 } OSSL_TRACE_END(BN_CTX);
aff96597 188#endif
0f113f3e
MC
189 BN_STACK_finish(&ctx->stack);
190 BN_POOL_finish(&ctx->pool);
191 OPENSSL_free(ctx);
192}
9b141126
UM
193
194void BN_CTX_start(BN_CTX *ctx)
0f113f3e 195{
6e810f2d 196 CTXDBG("ENTER BN_CTX_start()", ctx);
0f113f3e
MC
197 /* If we're already overflowing ... */
198 if (ctx->err_stack || ctx->too_many)
199 ctx->err_stack++;
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);
203 ctx->err_stack++;
204 }
6e810f2d 205 CTXDBG("LEAVE BN_CTX_start()", ctx);
0f113f3e 206}
9b141126 207
5c98b2ca 208void BN_CTX_end(BN_CTX *ctx)
0f113f3e 209{
ce1415ed
SL
210 if (ctx == NULL)
211 return;
6e810f2d 212 CTXDBG("ENTER BN_CTX_end()", ctx);
0f113f3e
MC
213 if (ctx->err_stack)
214 ctx->err_stack--;
215 else {
216 unsigned int fp = BN_STACK_pop(&ctx->stack);
217 /* Does this stack frame have anything to release? */
218 if (fp < ctx->used)
219 BN_POOL_release(&ctx->pool, ctx->used - fp);
220 ctx->used = fp;
221 /* Unjam "too_many" in case "get" had failed */
222 ctx->too_many = 0;
223 }
6e810f2d 224 CTXDBG("LEAVE BN_CTX_end()", ctx);
0f113f3e 225}
7f7b8d68 226
9b141126 227BIGNUM *BN_CTX_get(BN_CTX *ctx)
0f113f3e
MC
228{
229 BIGNUM *ret;
74924dcb 230
6e810f2d 231 CTXDBG("ENTER BN_CTX_get()", ctx);
0f113f3e
MC
232 if (ctx->err_stack || ctx->too_many)
233 return NULL;
74924dcb 234 if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
0f113f3e
MC
235 /*
236 * Setting too_many prevents repeated "get" attempts from cluttering
237 * the error stack.
238 */
239 ctx->too_many = 1;
240 BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
241 return NULL;
242 }
243 /* OK, make sure the returned bignum is "zero" */
244 BN_zero(ret);
c8147d37
NT
245 /* clear BN_FLG_CONSTTIME if leaked from previous frames */
246 ret->flags &= (~BN_FLG_CONSTTIME);
0f113f3e 247 ctx->used++;
6e810f2d 248 CTXDBG("LEAVE BN_CTX_get()", ctx);
0f113f3e
MC
249 return ret;
250}
9b141126 251
636b087e
MC
252OPENSSL_CTX *bn_get_lib_ctx(BN_CTX *ctx)
253{
ee1d4f3d
MC
254 if (ctx == NULL)
255 return NULL;
636b087e
MC
256 return ctx->libctx;
257}
258
5c98b2ca
GT
259/************/
260/* BN_STACK */
261/************/
262
263static void BN_STACK_init(BN_STACK *st)
0f113f3e
MC
264{
265 st->indexes = NULL;
266 st->depth = st->size = 0;
267}
5c98b2ca
GT
268
269static void BN_STACK_finish(BN_STACK *st)
0f113f3e 270{
74924dcb
RS
271 OPENSSL_free(st->indexes);
272 st->indexes = NULL;
0f113f3e 273}
37e48b88 274
5c98b2ca
GT
275
276static int BN_STACK_push(BN_STACK *st, unsigned int idx)
0f113f3e 277{
74924dcb 278 if (st->depth == st->size) {
0f113f3e 279 /* Need to expand */
74924dcb
RS
280 unsigned int newsize =
281 st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
cdb10bae 282 unsigned int *newitems;
df443918 283
cdb10bae
RS
284 if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
285 BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
0f113f3e 286 return 0;
cdb10bae 287 }
0f113f3e 288 if (st->depth)
74924dcb
RS
289 memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
290 OPENSSL_free(st->indexes);
0f113f3e
MC
291 st->indexes = newitems;
292 st->size = newsize;
293 }
294 st->indexes[(st->depth)++] = idx;
295 return 1;
296}
5c98b2ca
GT
297
298static unsigned int BN_STACK_pop(BN_STACK *st)
0f113f3e
MC
299{
300 return st->indexes[--(st->depth)];
301}
5c98b2ca
GT
302
303/***********/
304/* BN_POOL */
305/***********/
306
307static void BN_POOL_init(BN_POOL *p)
0f113f3e
MC
308{
309 p->head = p->current = p->tail = NULL;
310 p->used = p->size = 0;
311}
5c98b2ca
GT
312
313static void BN_POOL_finish(BN_POOL *p)
0f113f3e 314{
74924dcb
RS
315 unsigned int loop;
316 BIGNUM *bn;
317
0f113f3e 318 while (p->head) {
74924dcb 319 for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
0f113f3e
MC
320 if (bn->d)
321 BN_clear_free(bn);
0f113f3e
MC
322 p->current = p->head->next;
323 OPENSSL_free(p->head);
324 p->head = p->current;
325 }
326}
5c98b2ca 327
5c98b2ca 328
74924dcb 329static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
0f113f3e 330{
74924dcb
RS
331 BIGNUM *bn;
332 unsigned int loop;
333
334 /* Full; allocate a new pool item and link it in. */
0f113f3e 335 if (p->used == p->size) {
cdb10bae 336 BN_POOL_ITEM *item;
df443918 337
cdb10bae
RS
338 if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
339 BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
0f113f3e 340 return NULL;
cdb10bae 341 }
74924dcb 342 for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
d59c7c81 343 bn_init(bn);
74924dcb
RS
344 if ((flag & BN_FLG_SECURE) != 0)
345 BN_set_flags(bn, BN_FLG_SECURE);
346 }
0f113f3e
MC
347 item->prev = p->tail;
348 item->next = NULL;
74924dcb
RS
349
350 if (p->head == NULL)
0f113f3e
MC
351 p->head = p->current = p->tail = item;
352 else {
353 p->tail->next = item;
354 p->tail = item;
355 p->current = item;
356 }
357 p->size += BN_CTX_POOL_SIZE;
358 p->used++;
359 /* Return the first bignum from the new pool */
360 return item->vals;
361 }
74924dcb 362
0f113f3e
MC
363 if (!p->used)
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);
368}
5c98b2ca
GT
369
370static void BN_POOL_release(BN_POOL *p, unsigned int num)
0f113f3e
MC
371{
372 unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
74924dcb 373
0f113f3e
MC
374 p->used -= num;
375 while (num--) {
376 bn_check_top(p->current->vals + offset);
74924dcb 377 if (offset == 0) {
0f113f3e
MC
378 offset = BN_CTX_POOL_SIZE - 1;
379 p->current = p->current->prev;
380 } else
381 offset--;
382 }
383}