]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_ctx.c
Fix strict-warnings build on FreeBSD
[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"
c62b26fd 12#include "bn_lcl.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;
0f113f3e 89};
9b141126 90
6e810f2d
RL
91/* Debugging functionality */
92static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
0f113f3e
MC
93{
94 unsigned int bnidx = 0, fpidx = 0;
95 BN_POOL_ITEM *item = ctx->pool.head;
96 BN_STACK *stack = &ctx->stack;
6e810f2d
RL
97
98 BIO_printf(channel, "%s\n", text);
99 BIO_printf(channel, " (%16p): ", (void*)ctx);
0f113f3e 100 while (bnidx < ctx->used) {
6e810f2d
RL
101 BIO_printf(channel, "%03x ",
102 item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
0f113f3e
MC
103 if (!(bnidx % BN_CTX_POOL_SIZE))
104 item = item->next;
105 }
6e810f2d 106 BIO_printf(channel, "\n");
0f113f3e 107 bnidx = 0;
6e810f2d 108 BIO_printf(channel, " %16s : ", "");
0f113f3e
MC
109 while (fpidx < stack->depth) {
110 while (bnidx++ < stack->indexes[fpidx])
6e810f2d
RL
111 BIO_printf(channel, " ");
112 BIO_printf(channel, "^^^ ");
0f113f3e
MC
113 bnidx++;
114 fpidx++;
115 }
6e810f2d 116 BIO_printf(channel, "\n");
0f113f3e
MC
117}
118
6e810f2d
RL
119#define CTXDBG(str, ctx) \
120 OSSL_TRACE_BEGIN(BN_CTX) { \
121 ctxdbg(trc_out, str, ctx); \
122 } OSSL_TRACE_END(BN_CTX)
5c98b2ca 123
9b141126 124
2ce90b9b 125BN_CTX *BN_CTX_new(void)
0f113f3e 126{
74924dcb
RS
127 BN_CTX *ret;
128
64b25758 129 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
0f113f3e
MC
130 BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);
131 return NULL;
132 }
133 /* Initialise the structure */
134 BN_POOL_init(&ret->pool);
135 BN_STACK_init(&ret->stack);
74924dcb
RS
136 return ret;
137}
138
139BN_CTX *BN_CTX_secure_new(void)
140{
141 BN_CTX *ret = BN_CTX_new();
142
90945fa3 143 if (ret != NULL)
74924dcb 144 ret->flags = BN_FLG_SECURE;
0f113f3e
MC
145 return ret;
146}
2ce90b9b 147
9b141126 148void BN_CTX_free(BN_CTX *ctx)
0f113f3e 149{
e6e9170d
RS
150 if (ctx == NULL)
151 return;
6e810f2d 152 OSSL_TRACE_BEGIN(BN_CTX) {
0f113f3e 153 BN_POOL_ITEM *pool = ctx->pool.head;
6e810f2d
RL
154 BIO_printf(trc_out,
155 "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n",
156 ctx->stack.size, ctx->pool.size);
157 BIO_printf(trc_out, " dmaxs: ");
0f113f3e
MC
158 while (pool) {
159 unsigned loop = 0;
160 while (loop < BN_CTX_POOL_SIZE)
6e810f2d 161 BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax);
0f113f3e
MC
162 pool = pool->next;
163 }
6e810f2d
RL
164 BIO_printf(trc_out, "\n");
165 } OSSL_TRACE_END(BN_CTX);
0f113f3e
MC
166 BN_STACK_finish(&ctx->stack);
167 BN_POOL_finish(&ctx->pool);
168 OPENSSL_free(ctx);
169}
9b141126
UM
170
171void BN_CTX_start(BN_CTX *ctx)
0f113f3e 172{
6e810f2d 173 CTXDBG("ENTER BN_CTX_start()", ctx);
0f113f3e
MC
174 /* If we're already overflowing ... */
175 if (ctx->err_stack || ctx->too_many)
176 ctx->err_stack++;
177 /* (Try to) get a new frame pointer */
178 else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
179 BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
180 ctx->err_stack++;
181 }
6e810f2d 182 CTXDBG("LEAVE BN_CTX_start()", ctx);
0f113f3e 183}
9b141126 184
5c98b2ca 185void BN_CTX_end(BN_CTX *ctx)
0f113f3e 186{
6e810f2d 187 CTXDBG("ENTER BN_CTX_end()", ctx);
0f113f3e
MC
188 if (ctx->err_stack)
189 ctx->err_stack--;
190 else {
191 unsigned int fp = BN_STACK_pop(&ctx->stack);
192 /* Does this stack frame have anything to release? */
193 if (fp < ctx->used)
194 BN_POOL_release(&ctx->pool, ctx->used - fp);
195 ctx->used = fp;
196 /* Unjam "too_many" in case "get" had failed */
197 ctx->too_many = 0;
198 }
6e810f2d 199 CTXDBG("LEAVE BN_CTX_end()", ctx);
0f113f3e 200}
7f7b8d68 201
9b141126 202BIGNUM *BN_CTX_get(BN_CTX *ctx)
0f113f3e
MC
203{
204 BIGNUM *ret;
74924dcb 205
6e810f2d 206 CTXDBG("ENTER BN_CTX_get()", ctx);
0f113f3e
MC
207 if (ctx->err_stack || ctx->too_many)
208 return NULL;
74924dcb 209 if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
0f113f3e
MC
210 /*
211 * Setting too_many prevents repeated "get" attempts from cluttering
212 * the error stack.
213 */
214 ctx->too_many = 1;
215 BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
216 return NULL;
217 }
218 /* OK, make sure the returned bignum is "zero" */
219 BN_zero(ret);
c8147d37
NT
220 /* clear BN_FLG_CONSTTIME if leaked from previous frames */
221 ret->flags &= (~BN_FLG_CONSTTIME);
0f113f3e 222 ctx->used++;
6e810f2d 223 CTXDBG("LEAVE BN_CTX_get()", ctx);
0f113f3e
MC
224 return ret;
225}
9b141126 226
5c98b2ca
GT
227/************/
228/* BN_STACK */
229/************/
230
231static void BN_STACK_init(BN_STACK *st)
0f113f3e
MC
232{
233 st->indexes = NULL;
234 st->depth = st->size = 0;
235}
5c98b2ca
GT
236
237static void BN_STACK_finish(BN_STACK *st)
0f113f3e 238{
74924dcb
RS
239 OPENSSL_free(st->indexes);
240 st->indexes = NULL;
0f113f3e 241}
37e48b88 242
5c98b2ca
GT
243
244static int BN_STACK_push(BN_STACK *st, unsigned int idx)
0f113f3e 245{
74924dcb 246 if (st->depth == st->size) {
0f113f3e 247 /* Need to expand */
74924dcb
RS
248 unsigned int newsize =
249 st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
cdb10bae 250 unsigned int *newitems;
df443918 251
cdb10bae
RS
252 if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
253 BNerr(BN_F_BN_STACK_PUSH, ERR_R_MALLOC_FAILURE);
0f113f3e 254 return 0;
cdb10bae 255 }
0f113f3e 256 if (st->depth)
74924dcb
RS
257 memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
258 OPENSSL_free(st->indexes);
0f113f3e
MC
259 st->indexes = newitems;
260 st->size = newsize;
261 }
262 st->indexes[(st->depth)++] = idx;
263 return 1;
264}
5c98b2ca
GT
265
266static unsigned int BN_STACK_pop(BN_STACK *st)
0f113f3e
MC
267{
268 return st->indexes[--(st->depth)];
269}
5c98b2ca
GT
270
271/***********/
272/* BN_POOL */
273/***********/
274
275static void BN_POOL_init(BN_POOL *p)
0f113f3e
MC
276{
277 p->head = p->current = p->tail = NULL;
278 p->used = p->size = 0;
279}
5c98b2ca
GT
280
281static void BN_POOL_finish(BN_POOL *p)
0f113f3e 282{
74924dcb
RS
283 unsigned int loop;
284 BIGNUM *bn;
285
0f113f3e 286 while (p->head) {
74924dcb 287 for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
0f113f3e
MC
288 if (bn->d)
289 BN_clear_free(bn);
0f113f3e
MC
290 p->current = p->head->next;
291 OPENSSL_free(p->head);
292 p->head = p->current;
293 }
294}
5c98b2ca 295
5c98b2ca 296
74924dcb 297static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
0f113f3e 298{
74924dcb
RS
299 BIGNUM *bn;
300 unsigned int loop;
301
302 /* Full; allocate a new pool item and link it in. */
0f113f3e 303 if (p->used == p->size) {
cdb10bae 304 BN_POOL_ITEM *item;
df443918 305
cdb10bae
RS
306 if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
307 BNerr(BN_F_BN_POOL_GET, ERR_R_MALLOC_FAILURE);
0f113f3e 308 return NULL;
cdb10bae 309 }
74924dcb 310 for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
d59c7c81 311 bn_init(bn);
74924dcb
RS
312 if ((flag & BN_FLG_SECURE) != 0)
313 BN_set_flags(bn, BN_FLG_SECURE);
314 }
0f113f3e
MC
315 item->prev = p->tail;
316 item->next = NULL;
74924dcb
RS
317
318 if (p->head == NULL)
0f113f3e
MC
319 p->head = p->current = p->tail = item;
320 else {
321 p->tail->next = item;
322 p->tail = item;
323 p->current = item;
324 }
325 p->size += BN_CTX_POOL_SIZE;
326 p->used++;
327 /* Return the first bignum from the new pool */
328 return item->vals;
329 }
74924dcb 330
0f113f3e
MC
331 if (!p->used)
332 p->current = p->head;
333 else if ((p->used % BN_CTX_POOL_SIZE) == 0)
334 p->current = p->current->next;
335 return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
336}
5c98b2ca
GT
337
338static void BN_POOL_release(BN_POOL *p, unsigned int num)
0f113f3e
MC
339{
340 unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
74924dcb 341
0f113f3e
MC
342 p->used -= num;
343 while (num--) {
344 bn_check_top(p->current->vals + offset);
74924dcb 345 if (offset == 0) {
0f113f3e
MC
346 offset = BN_CTX_POOL_SIZE - 1;
347 p->current = p->current->prev;
348 } else
349 offset--;
350 }
351}