Coverage Report

Created: 2025-12-31 06:58

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
3.02G
#define BN_CTX_POOL_SIZE 16
16
/* The stack frame info is resizing, set a first-time expansion size; */
17
2.94M
#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.96G
    OSSL_TRACE_BEGIN(BN_CTX)       \
111
4.96G
    {                              \
112
0
        ctxdbg(trc_out, str, ctx); \
113
0
    }                              \
114
4.96G
    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.59M
{
124
3.59M
    BN_CTX *ret;
125
126
3.59M
    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.59M
    BN_POOL_init(&ret->pool);
132
3.59M
    BN_STACK_init(&ret->stack);
133
3.59M
    ret->libctx = ctx;
134
3.59M
    return ret;
135
3.59M
}
136
137
#ifndef FIPS_MODULE
138
BN_CTX *BN_CTX_new(void)
139
241k
{
140
241k
    return BN_CTX_new_ex(NULL);
141
241k
}
142
#endif
143
144
BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx)
145
9.41k
{
146
9.41k
    BN_CTX *ret = BN_CTX_new_ex(ctx);
147
148
9.41k
    if (ret != NULL)
149
9.41k
        ret->flags = BN_FLG_SECURE;
150
9.41k
    return ret;
151
9.41k
}
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.3M
{
162
10.3M
    if (ctx == NULL)
163
6.72M
        return;
164
3.59M
#ifndef FIPS_MODULE
165
3.59M
    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.59M
    OSSL_TRACE_END(BN_CTX);
181
3.59M
#endif
182
3.59M
    BN_STACK_finish(&ctx->stack);
183
3.59M
    BN_POOL_finish(&ctx->pool);
184
3.59M
    OPENSSL_free(ctx);
185
3.59M
}
186
187
void BN_CTX_start(BN_CTX *ctx)
188
700M
{
189
700M
    CTXDBG("ENTER BN_CTX_start()", ctx);
190
    /* If we're already overflowing ... */
191
700M
    if (ctx->err_stack || ctx->too_many)
192
0
        ctx->err_stack++;
193
    /* (Try to) get a new frame pointer */
194
700M
    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
700M
    CTXDBG("LEAVE BN_CTX_start()", ctx);
199
700M
}
200
201
void BN_CTX_end(BN_CTX *ctx)
202
700M
{
203
700M
    if (ctx == NULL)
204
1.41k
        return;
205
700M
    CTXDBG("ENTER BN_CTX_end()", ctx);
206
700M
    if (ctx->err_stack)
207
0
        ctx->err_stack--;
208
700M
    else {
209
700M
        unsigned int fp = BN_STACK_pop(&ctx->stack);
210
        /* Does this stack frame have anything to release? */
211
700M
        if (fp < ctx->used)
212
669M
            BN_POOL_release(&ctx->pool, ctx->used - fp);
213
700M
        ctx->used = fp;
214
        /* Unjam "too_many" in case "get" had failed */
215
700M
        ctx->too_many = 0;
216
700M
    }
217
700M
    CTXDBG("LEAVE BN_CTX_end()", ctx);
218
700M
}
219
220
BIGNUM *BN_CTX_get(BN_CTX *ctx)
221
1.08G
{
222
1.08G
    BIGNUM *ret;
223
224
1.08G
    CTXDBG("ENTER BN_CTX_get()", ctx);
225
1.08G
    if (ctx->err_stack || ctx->too_many)
226
0
        return NULL;
227
1.08G
    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.08G
    BN_zero(ret);
238
    /* clear BN_FLG_CONSTTIME if leaked from previous frames */
239
1.08G
    ret->flags &= (~BN_FLG_CONSTTIME);
240
1.08G
    ctx->used++;
241
1.08G
    CTXDBG("LEAVE BN_CTX_get()", ctx);
242
1.08G
    return ret;
243
1.08G
}
244
245
OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx)
246
1.04M
{
247
1.04M
    if (ctx == NULL)
248
23.3k
        return NULL;
249
1.02M
    return ctx->libctx;
250
1.04M
}
251
252
/************/
253
/* BN_STACK */
254
/************/
255
256
static void BN_STACK_init(BN_STACK *st)
257
3.59M
{
258
3.59M
    st->indexes = NULL;
259
3.59M
    st->depth = st->size = 0;
260
3.59M
}
261
262
static void BN_STACK_finish(BN_STACK *st)
263
3.59M
{
264
3.59M
    OPENSSL_free(st->indexes);
265
3.59M
    st->indexes = NULL;
266
3.59M
}
267
268
static int BN_STACK_push(BN_STACK *st, unsigned int idx)
269
700M
{
270
700M
    if (st->depth == st->size) {
271
        /* Need to expand */
272
2.94M
        unsigned int newsize = st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;
273
2.94M
        unsigned int *newitems;
274
275
2.94M
        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
2.94M
        if (st->depth)
280
0
            memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);
281
2.94M
        OPENSSL_free(st->indexes);
282
2.94M
        st->indexes = newitems;
283
2.94M
        st->size = newsize;
284
2.94M
    }
285
700M
    st->indexes[(st->depth)++] = idx;
286
700M
    return 1;
287
700M
}
288
289
static unsigned int BN_STACK_pop(BN_STACK *st)
290
700M
{
291
700M
    return st->indexes[--(st->depth)];
292
700M
}
293
294
/***********/
295
/* BN_POOL */
296
/***********/
297
298
static void BN_POOL_init(BN_POOL *p)
299
3.59M
{
300
3.59M
    p->head = p->current = p->tail = NULL;
301
3.59M
    p->used = p->size = 0;
302
3.59M
}
303
304
static void BN_POOL_finish(BN_POOL *p)
305
3.59M
{
306
3.59M
    unsigned int loop;
307
3.59M
    BIGNUM *bn;
308
309
6.23M
    while (p->head) {
310
44.9M
        for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)
311
42.2M
            if (bn->d)
312
20.8M
                BN_clear_free(bn);
313
2.64M
        p->current = p->head->next;
314
2.64M
        OPENSSL_free(p->head);
315
2.64M
        p->head = p->current;
316
2.64M
    }
317
3.59M
}
318
319
static BIGNUM *BN_POOL_get(BN_POOL *p, int flag)
320
1.08G
{
321
1.08G
    BIGNUM *bn;
322
1.08G
    unsigned int loop;
323
324
    /* Full; allocate a new pool item and link it in. */
325
1.08G
    if (p->used == p->size) {
326
2.64M
        BN_POOL_ITEM *item;
327
328
2.64M
        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
44.9M
        for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) {
333
42.2M
            bn_init(bn);
334
42.2M
            if ((flag & BN_FLG_SECURE) != 0)
335
78.0k
                BN_set_flags(bn, BN_FLG_SECURE);
336
42.2M
        }
337
2.64M
        item->prev = p->tail;
338
2.64M
        item->next = NULL;
339
340
2.64M
        if (p->head == NULL)
341
2.43M
            p->head = p->current = p->tail = item;
342
210k
        else {
343
210k
            p->tail->next = item;
344
210k
            p->tail = item;
345
210k
            p->current = item;
346
210k
        }
347
2.64M
        p->size += BN_CTX_POOL_SIZE;
348
2.64M
        p->used++;
349
        /* Return the first bignum from the new pool */
350
2.64M
        return item->vals;
351
2.64M
    }
352
353
1.07G
    if (!p->used)
354
3.62M
        p->current = p->head;
355
1.07G
    else if ((p->used % BN_CTX_POOL_SIZE) == 0)
356
98.8M
        p->current = p->current->next;
357
1.07G
    return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
358
1.08G
}
359
360
static void BN_POOL_release(BN_POOL *p, unsigned int num)
361
669M
{
362
669M
    unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
363
364
669M
    p->used -= num;
365
1.75G
    while (num--) {
366
1.08G
        bn_check_top(p->current->vals + offset);
367
1.08G
        if (offset == 0) {
368
105M
            offset = BN_CTX_POOL_SIZE - 1;
369
105M
            p->current = p->current->prev;
370
105M
        } else
371
977M
            offset--;
372
1.08G
    }
373
669M
}