Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bn/bn_ctx.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <openssl/trace.h>
11
#include "internal/cryptlib.h"
12
#include "bn_local.h"
13
14
/* How many bignums are in each "pool item"; */
15
2.93G
#define BN_CTX_POOL_SIZE 16
16
/* The stack frame info is resizing, set a first-time expansion size; */
17
3.05M
#define BN_CTX_START_FRAMES 32
18
19
/***********/
20
/* BN_POOL */
21
/***********/
22
23
/* A bundle of bignums that can be linked with other bundles */
24
typedef struct bignum_pool_item {
25
    /* The bignum values */
26
    BIGNUM vals[BN_CTX_POOL_SIZE];
27
    /* Linked-list admin */
28
    struct bignum_pool_item *prev, *next;
29
} BN_POOL_ITEM;
30
/* A linked-list of bignums grouped in bundles */
31
typedef struct bignum_pool {
32
    /* Linked-list admin */
33
    BN_POOL_ITEM *head, *current, *tail;
34
    /* Stack depth and allocation size */
35
    unsigned used, size;
36
} BN_POOL;
37
static void BN_POOL_init(BN_POOL *);
38
static void BN_POOL_finish(BN_POOL *);
39
static BIGNUM *BN_POOL_get(BN_POOL *, int);
40
static void BN_POOL_release(BN_POOL *, unsigned int);
41
42
/************/
43
/* BN_STACK */
44
/************/
45
46
/* A wrapper to manage the "stack frames" */
47
typedef struct bignum_ctx_stack {
48
    /* Array of indexes into the bignum stack */
49
    unsigned int *indexes;
50
    /* Number of stack frames, and the size of the allocated array */
51
    unsigned int depth, size;
52
} BN_STACK;
53
static void BN_STACK_init(BN_STACK *);
54
static void BN_STACK_finish(BN_STACK *);
55
static int BN_STACK_push(BN_STACK *, unsigned int);
56
static unsigned int BN_STACK_pop(BN_STACK *);
57
58
/**********/
59
/* BN_CTX */
60
/**********/
61
62
/* The opaque BN_CTX type */
63
struct bignum_ctx {
64
    /* The bignum bundles */
65
    BN_POOL pool;
66
    /* The "stack frames", if you will */
67
    BN_STACK stack;
68
    /* The number of bignums currently assigned */
69
    unsigned int used;
70
    /* Depth of stack overflow */
71
    int err_stack;
72
    /* Block "gets" until an "end" (compatibility behaviour) */
73
    int too_many;
74
    /* Flags. */
75
    int flags;
76
    /* The library context */
77
    OSSL_LIB_CTX *libctx;
78
};
79
80
#ifndef FIPS_MODULE
81
/* Debugging functionality */
82
static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
83
0
{
84
0
    unsigned int bnidx = 0, fpidx = 0;
85
0
    BN_POOL_ITEM *item = ctx->pool.head;
86
0
    BN_STACK *stack = &ctx->stack;
87
0
88
0
    BIO_printf(channel, "%s\n", text);
89
0
    BIO_printf(channel, "  (%16p): ", (void *)ctx);
90
0
    while (bnidx < ctx->used) {
91
0
        BIO_printf(channel, "%03x ",
92
0
            item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
93
0
        if (!(bnidx % BN_CTX_POOL_SIZE))
94
0
            item = item->next;
95
0
    }
96
0
    BIO_printf(channel, "\n");
97
0
    bnidx = 0;
98
0
    BIO_printf(channel, "   %16s : ", "");
99
0
    while (fpidx < stack->depth) {
100
0
        while (bnidx++ < stack->indexes[fpidx])
101
0
            BIO_printf(channel, "    ");
102
0
        BIO_printf(channel, "^^^ ");
103
0
        bnidx++;
104
0
        fpidx++;
105
0
    }
106
0
    BIO_printf(channel, "\n");
107
0
}
108
109
#define CTXDBG(str, ctx)           \
110
4.73G
    OSSL_TRACE_BEGIN(BN_CTX)       \
111
4.73G
    {                              \
112
0
        ctxdbg(trc_out, str, ctx); \
113
0
    }                              \
114
4.73G
    OSSL_TRACE_END(BN_CTX)
115
#else
116
/* We do not want tracing in FIPS module */
117
#define CTXDBG(str, ctx) \
118
    do {                 \
119
    } while (0)
120
#endif /* FIPS_MODULE */
121
122
BN_CTX *BN_CTX_new_ex(OSSL_LIB_CTX *ctx)
123
3.71M
{
124
3.71M
    BN_CTX *ret;
125
126
3.71M
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
127
0
        ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
128
0
        return NULL;
129
0
    }
130
    /* Initialise the structure */
131
3.71M
    BN_POOL_init(&ret->pool);
132
3.71M
    BN_STACK_init(&ret->stack);
133
3.71M
    ret->libctx = ctx;
134
3.71M
    return ret;
135
3.71M
}
136
137
#ifndef FIPS_MODULE
138
BN_CTX *BN_CTX_new(void)
139
269k
{
140
269k
    return BN_CTX_new_ex(NULL);
141
269k
}
142
#endif
143
144
BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx)
145
9.78k
{
146
9.78k
    BN_CTX *ret = BN_CTX_new_ex(ctx);
147
148
9.78k
    if (ret != NULL)
149
9.78k
        ret->flags = BN_FLG_SECURE;
150
9.78k
    return ret;
151
9.78k
}
152
153
#ifndef FIPS_MODULE
154
BN_CTX *BN_CTX_secure_new(void)
155
0
{
156
0
    return BN_CTX_secure_new_ex(NULL);
157
0
}
158
#endif
159
160
void BN_CTX_free(BN_CTX *ctx)
161
10.4M
{
162
10.4M
    if (ctx == NULL)
163
6.76M
        return;
164
3.71M
#ifndef FIPS_MODULE
165
3.71M
    OSSL_TRACE_BEGIN(BN_CTX)
166
0
    {
167
0
        BN_POOL_ITEM *pool = ctx->pool.head;
168
0
        BIO_printf(trc_out,
169
0
            "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n",
170
0
            ctx->stack.size, ctx->pool.size);
171
0
        BIO_printf(trc_out, "  dmaxs: ");
172
0
        while (pool) {
173
0
            unsigned loop = 0;
174
0
            while (loop < BN_CTX_POOL_SIZE)
175
0
                BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax);
176
0
            pool = pool->next;
177
0
        }
178
0
        BIO_printf(trc_out, "\n");
179
0
    }
180
3.71M
    OSSL_TRACE_END(BN_CTX);
181
3.71M
#endif
182
3.71M
    BN_STACK_finish(&ctx->stack);
183
3.71M
    BN_POOL_finish(&ctx->pool);
184
3.71M
    OPENSSL_free(ctx);
185
3.71M
}
186
187
void BN_CTX_start(BN_CTX *ctx)
188
656M
{
189
656M
    CTXDBG("ENTER BN_CTX_start()", ctx);
190
    /* If we're already overflowing ... */
191
656M
    if (ctx->err_stack || ctx->too_many)
192
0
        ctx->err_stack++;
193
    /* (Try to) get a new frame pointer */
194
656M
    else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
195
0
        ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
196
0
        ctx->err_stack++;
197
0
    }
198
656M
    CTXDBG("LEAVE BN_CTX_start()", ctx);
199
656M
}
200
201
void BN_CTX_end(BN_CTX *ctx)
202
656M
{
203
656M
    if (ctx == NULL)
204
1.83k
        return;
205
656M
    CTXDBG("ENTER BN_CTX_end()", ctx);
206
656M
    if (ctx->err_stack)
207
0
        ctx->err_stack--;
208
656M
    else {
209
656M
        unsigned int fp = BN_STACK_pop(&ctx->stack);
210
        /* Does this stack frame have anything to release? */
211
656M
        if (fp < ctx->used)
212
626M
            BN_POOL_release(&ctx->pool, ctx->used - fp);
213
656M
        ctx->used = fp;
214
        /* Unjam "too_many" in case "get" had failed */
215
656M
        ctx->too_many = 0;
216
656M
    }
217
656M
    CTXDBG("LEAVE BN_CTX_end()", ctx);
218
656M
}
219
220
BIGNUM *BN_CTX_get(BN_CTX *ctx)
221
1.05G
{
222
1.05G
    BIGNUM *ret;
223
224
1.05G
    CTXDBG("ENTER BN_CTX_get()", ctx);
225
1.05G
    if (ctx->err_stack || ctx->too_many)
226
0
        return NULL;
227
1.05G
    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {
228
        /*
229
         * Setting too_many prevents repeated "get" attempts from cluttering
230
         * the error stack.
231
         */
232
0
        ctx->too_many = 1;
233
0
        ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
234
0
        return NULL;
235
0
    }
236
    /* OK, make sure the returned bignum is "zero" */
237
1.05G
    BN_zero(ret);
238
    /* clear BN_FLG_CONSTTIME if leaked from previous frames */
239
1.05G
    ret->flags &= (~BN_FLG_CONSTTIME);
240
1.05G
    ctx->used++;
241
1.05G
    CTXDBG("LEAVE BN_CTX_get()", ctx);
242
1.05G
    return ret;
243
1.05G
}
244
245
OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx)
246
1.06M
{
247
1.06M
    if (ctx == NULL)
248
21.9k
        return NULL;
249
1.04M
    return ctx->libctx;
250
1.06M
}
251
252
/************/
253
/* BN_STACK */
254
/************/
255
256
static void BN_STACK_init(BN_STACK *st)
257
3.71M
{
258
3.71M
    st->indexes = NULL;
259
3.71M
    st->depth = st->size = 0;
260
3.71M
}
261
262
static void BN_STACK_finish(BN_STACK *st)
263
3.71M
{
264
3.71M
    OPENSSL_free(st->indexes);
265
3.71M
    st->indexes = NULL;
266
3.71M
}
267
268
static int BN_STACK_push(BN_STACK *st, unsigned int idx)
269
656M
{
270
656M
    if (st->depth == st->size) {
271
        /* Need to expand */
272
3.05M
        unsigned int newsize = st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
273
3.05M
        unsigned int *newitems;
274
275
3.05M
        if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) {
276
0
            ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
277
0
            return 0;
278
0
        }
279
3.05M
        if (st->depth)
280
0
            memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
281
3.05M
        OPENSSL_free(st->indexes);
282
3.05M
        st->indexes = newitems;
283
3.05M
        st->size = newsize;
284
3.05M
    }
285
656M
    st->indexes[(st->depth)++] = idx;
286
656M
    return 1;
287
656M
}
288
289
static unsigned int BN_STACK_pop(BN_STACK *st)
290
656M
{
291
656M
    return st->indexes[--(st->depth)];
292
656M
}
293
294
/***********/
295
/* BN_POOL */
296
/***********/
297
298
static void BN_POOL_init(BN_POOL *p)
299
3.71M
{
300
3.71M
    p->head = p->current = p->tail = NULL;
301
3.71M
    p->used = p->size = 0;
302
3.71M
}
303
304
static void BN_POOL_finish(BN_POOL *p)
305
3.71M
{
306
3.71M
    unsigned int loop;
307
3.71M
    BIGNUM *bn;
308
309
6.45M
    while (p->head) {
310
46.5M
        for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
311
43.8M
            if (bn->d)
312
21.8M
                BN_clear_free(bn);
313
2.74M
        p->current = p->head->next;
314
2.74M
        OPENSSL_free(p->head);
315
2.74M
        p->head = p->current;
316
2.74M
    }
317
3.71M
}
318
319
static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
320
1.05G
{
321
1.05G
    BIGNUM *bn;
322
1.05G
    unsigned int loop;
323
324
    /* Full; allocate a new pool item and link it in. */
325
1.05G
    if (p->used == p->size) {
326
2.74M
        BN_POOL_ITEM *item;
327
328
2.74M
        if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
329
0
            ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
330
0
            return NULL;
331
0
        }
332
46.5M
        for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
333
43.8M
            bn_init(bn);
334
43.8M
            if ((flag & BN_FLG_SECURE) != 0)
335
80.1k
                BN_set_flags(bn, BN_FLG_SECURE);
336
43.8M
        }
337
2.74M
        item->prev = p->tail;
338
2.74M
        item->next = NULL;
339
340
2.74M
        if (p->head == NULL)
341
2.52M
            p->head = p->current = p->tail = item;
342
220k
        else {
343
220k
            p->tail->next = item;
344
220k
            p->tail = item;
345
220k
            p->current = item;
346
220k
        }
347
2.74M
        p->size += BN_CTX_POOL_SIZE;
348
2.74M
        p->used++;
349
        /* Return the first bignum from the new pool */
350
2.74M
        return item->vals;
351
2.74M
    }
352
353
1.05G
    if (!p->used)
354
3.58M
        p->current = p->head;
355
1.05G
    else if ((p->used % BN_CTX_POOL_SIZE) == 0)
356
104M
        p->current = p->current->next;
357
1.05G
    return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
358
1.05G
}
359
360
static void BN_POOL_release(BN_POOL *p, unsigned int num)
361
626M
{
362
626M
    unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
363
364
626M
    p->used -= num;
365
1.68G
    while (num--) {
366
1.05G
        bn_check_top(p->current->vals + offset);
367
1.05G
        if (offset == 0) {
368
110M
            offset = BN_CTX_POOL_SIZE - 1;
369
110M
            p->current = p->current->prev;
370
110M
        } else
371
945M
            offset--;
372
1.05G
    }
373
626M
}