/src/openssl30/crypto/bn/bn_ctx.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 | 1.61G | #define BN_CTX_POOL_SIZE        16 | 
| 16 |  | /* The stack frame info is resizing, set a first-time expansion size; */ | 
| 17 | 1.00M | #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 | 1.92G |     OSSL_TRACE_BEGIN(BN_CTX) {      \ | 
| 111 | 0 |         ctxdbg(trc_out, str, ctx);  \ | 
| 112 | 1.92G |     } OSSL_TRACE_END(BN_CTX) | 
| 113 |  | #else | 
| 114 |  | /* We do not want tracing in FIPS module */ | 
| 115 |  | # define CTXDBG(str, ctx) do {} while(0) | 
| 116 |  | #endif /* FIPS_MODULE */ | 
| 117 |  |  | 
| 118 |  | BN_CTX *BN_CTX_new_ex(OSSL_LIB_CTX *ctx) | 
| 119 | 928k | { | 
| 120 | 928k |     BN_CTX *ret; | 
| 121 |  |  | 
| 122 | 928k |     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { | 
| 123 | 0 |         ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); | 
| 124 | 0 |         return NULL; | 
| 125 | 0 |     } | 
| 126 |  |     /* Initialise the structure */ | 
| 127 | 928k |     BN_POOL_init(&ret->pool); | 
| 128 | 928k |     BN_STACK_init(&ret->stack); | 
| 129 | 928k |     ret->libctx = ctx; | 
| 130 | 928k |     return ret; | 
| 131 | 928k | } | 
| 132 |  |  | 
| 133 |  | #ifndef FIPS_MODULE | 
| 134 |  | BN_CTX *BN_CTX_new(void) | 
| 135 | 63.6k | { | 
| 136 | 63.6k |     return BN_CTX_new_ex(NULL); | 
| 137 | 63.6k | } | 
| 138 |  | #endif | 
| 139 |  |  | 
| 140 |  | BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx) | 
| 141 | 1.44k | { | 
| 142 | 1.44k |     BN_CTX *ret = BN_CTX_new_ex(ctx); | 
| 143 |  |  | 
| 144 | 1.44k |     if (ret != NULL) | 
| 145 | 1.44k |         ret->flags = BN_FLG_SECURE; | 
| 146 | 1.44k |     return ret; | 
| 147 | 1.44k | } | 
| 148 |  |  | 
| 149 |  | #ifndef FIPS_MODULE | 
| 150 |  | BN_CTX *BN_CTX_secure_new(void) | 
| 151 |  | { | 
| 152 |  |     return BN_CTX_secure_new_ex(NULL); | 
| 153 |  | } | 
| 154 |  | #endif | 
| 155 |  |  | 
| 156 |  | void BN_CTX_free(BN_CTX *ctx) | 
| 157 | 2.83M | { | 
| 158 | 2.83M |     if (ctx == NULL) | 
| 159 | 1.90M |         return; | 
| 160 | 928k | #ifndef FIPS_MODULE | 
| 161 | 928k |     OSSL_TRACE_BEGIN(BN_CTX) { | 
| 162 | 0 |         BN_POOL_ITEM *pool = ctx->pool.head; | 
| 163 | 0 |         BIO_printf(trc_out, | 
| 164 | 0 |                    "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n", | 
| 165 | 0 |                    ctx->stack.size, ctx->pool.size); | 
| 166 | 0 |         BIO_printf(trc_out, "  dmaxs: "); | 
| 167 | 0 |         while (pool) { | 
| 168 | 0 |             unsigned loop = 0; | 
| 169 | 0 |             while (loop < BN_CTX_POOL_SIZE) | 
| 170 | 0 |                 BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax); | 
| 171 | 0 |             pool = pool->next; | 
| 172 | 0 |         } | 
| 173 | 0 |         BIO_printf(trc_out, "\n"); | 
| 174 | 928k |     } OSSL_TRACE_END(BN_CTX); | 
| 175 | 928k | #endif | 
| 176 | 928k |     BN_STACK_finish(&ctx->stack); | 
| 177 | 928k |     BN_POOL_finish(&ctx->pool); | 
| 178 | 928k |     OPENSSL_free(ctx); | 
| 179 | 928k | } | 
| 180 |  |  | 
| 181 |  | void BN_CTX_start(BN_CTX *ctx) | 
| 182 | 277M | { | 
| 183 | 277M |     CTXDBG("ENTER BN_CTX_start()", ctx); | 
| 184 |  |     /* If we're already overflowing ... */ | 
| 185 | 277M |     if (ctx->err_stack || ctx->too_many) | 
| 186 | 0 |         ctx->err_stack++; | 
| 187 |  |     /* (Try to) get a new frame pointer */ | 
| 188 | 277M |     else if (!BN_STACK_push(&ctx->stack, ctx->used)) { | 
| 189 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); | 
| 190 | 0 |         ctx->err_stack++; | 
| 191 | 0 |     } | 
| 192 | 277M |     CTXDBG("LEAVE BN_CTX_start()", ctx); | 
| 193 | 277M | } | 
| 194 |  |  | 
| 195 |  | void BN_CTX_end(BN_CTX *ctx) | 
| 196 | 277M | { | 
| 197 | 277M |     if (ctx == NULL) | 
| 198 | 1 |         return; | 
| 199 | 277M |     CTXDBG("ENTER BN_CTX_end()", ctx); | 
| 200 | 277M |     if (ctx->err_stack) | 
| 201 | 0 |         ctx->err_stack--; | 
| 202 | 277M |     else { | 
| 203 | 277M |         unsigned int fp = BN_STACK_pop(&ctx->stack); | 
| 204 |  |         /* Does this stack frame have anything to release? */ | 
| 205 | 277M |         if (fp < ctx->used) | 
| 206 | 262M |             BN_POOL_release(&ctx->pool, ctx->used - fp); | 
| 207 | 277M |         ctx->used = fp; | 
| 208 |  |         /* Unjam "too_many" in case "get" had failed */ | 
| 209 | 277M |         ctx->too_many = 0; | 
| 210 | 277M |     } | 
| 211 | 277M |     CTXDBG("LEAVE BN_CTX_end()", ctx); | 
| 212 | 277M | } | 
| 213 |  |  | 
| 214 |  | BIGNUM *BN_CTX_get(BN_CTX *ctx) | 
| 215 | 406M | { | 
| 216 | 406M |     BIGNUM *ret; | 
| 217 |  |  | 
| 218 | 406M |     CTXDBG("ENTER BN_CTX_get()", ctx); | 
| 219 | 406M |     if (ctx->err_stack || ctx->too_many) | 
| 220 | 0 |         return NULL; | 
| 221 | 406M |     if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) { | 
| 222 |  |         /* | 
| 223 |  |          * Setting too_many prevents repeated "get" attempts from cluttering | 
| 224 |  |          * the error stack. | 
| 225 |  |          */ | 
| 226 | 0 |         ctx->too_many = 1; | 
| 227 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); | 
| 228 | 0 |         return NULL; | 
| 229 | 0 |     } | 
| 230 |  |     /* OK, make sure the returned bignum is "zero" */ | 
| 231 | 406M |     BN_zero(ret); | 
| 232 |  |     /* clear BN_FLG_CONSTTIME if leaked from previous frames */ | 
| 233 | 406M |     ret->flags &= (~BN_FLG_CONSTTIME); | 
| 234 | 406M |     ctx->used++; | 
| 235 | 406M |     CTXDBG("LEAVE BN_CTX_get()", ctx); | 
| 236 | 406M |     return ret; | 
| 237 | 406M | } | 
| 238 |  |  | 
| 239 |  | OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx) | 
| 240 | 339k | { | 
| 241 | 339k |     if (ctx == NULL) | 
| 242 | 10.1k |         return NULL; | 
| 243 | 329k |     return ctx->libctx; | 
| 244 | 339k | } | 
| 245 |  |  | 
| 246 |  | /************/ | 
| 247 |  | /* BN_STACK */ | 
| 248 |  | /************/ | 
| 249 |  |  | 
| 250 |  | static void BN_STACK_init(BN_STACK *st) | 
| 251 | 1.13M | { | 
| 252 | 1.13M |     st->indexes = NULL; | 
| 253 | 1.13M |     st->depth = st->size = 0; | 
| 254 | 1.13M | } | 
| 255 |  |  | 
| 256 |  | static void BN_STACK_finish(BN_STACK *st) | 
| 257 | 1.13M | { | 
| 258 | 1.13M |     OPENSSL_free(st->indexes); | 
| 259 | 1.13M |     st->indexes = NULL; | 
| 260 | 1.13M | } | 
| 261 |  |  | 
| 262 |  |  | 
| 263 |  | static int BN_STACK_push(BN_STACK *st, unsigned int idx) | 
| 264 | 391M | { | 
| 265 | 391M |     if (st->depth == st->size) { | 
| 266 |  |         /* Need to expand */ | 
| 267 | 1.00M |         unsigned int newsize = | 
| 268 | 1.00M |             st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES; | 
| 269 | 1.00M |         unsigned int *newitems; | 
| 270 |  |  | 
| 271 | 1.00M |         if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) { | 
| 272 | 0 |             ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); | 
| 273 | 0 |             return 0; | 
| 274 | 0 |         } | 
| 275 | 1.00M |         if (st->depth) | 
| 276 | 0 |             memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth); | 
| 277 | 1.00M |         OPENSSL_free(st->indexes); | 
| 278 | 1.00M |         st->indexes = newitems; | 
| 279 | 1.00M |         st->size = newsize; | 
| 280 | 1.00M |     } | 
| 281 | 391M |     st->indexes[(st->depth)++] = idx; | 
| 282 | 391M |     return 1; | 
| 283 | 391M | } | 
| 284 |  |  | 
| 285 |  | static unsigned int BN_STACK_pop(BN_STACK *st) | 
| 286 | 391M | { | 
| 287 | 391M |     return st->indexes[--(st->depth)]; | 
| 288 | 391M | } | 
| 289 |  |  | 
| 290 |  | /***********/ | 
| 291 |  | /* BN_POOL */ | 
| 292 |  | /***********/ | 
| 293 |  |  | 
| 294 |  | static void BN_POOL_init(BN_POOL *p) | 
| 295 | 1.13M | { | 
| 296 | 1.13M |     p->head = p->current = p->tail = NULL; | 
| 297 | 1.13M |     p->used = p->size = 0; | 
| 298 | 1.13M | } | 
| 299 |  |  | 
| 300 |  | static void BN_POOL_finish(BN_POOL *p) | 
| 301 | 1.13M | { | 
| 302 | 1.13M |     unsigned int loop; | 
| 303 | 1.13M |     BIGNUM *bn; | 
| 304 |  |  | 
| 305 | 2.10M |     while (p->head) { | 
| 306 | 16.3M |         for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++) | 
| 307 | 15.3M |             if (bn->d) | 
| 308 | 8.60M |                 BN_clear_free(bn); | 
| 309 | 960k |         p->current = p->head->next; | 
| 310 | 960k |         OPENSSL_free(p->head); | 
| 311 | 960k |         p->head = p->current; | 
| 312 | 960k |     } | 
| 313 | 1.13M | } | 
| 314 |  |  | 
| 315 |  |  | 
| 316 |  | static BIGNUM *BN_POOL_get(BN_POOL *p, int flag) | 
| 317 | 580M | { | 
| 318 | 580M |     BIGNUM *bn; | 
| 319 | 580M |     unsigned int loop; | 
| 320 |  |  | 
| 321 |  |     /* Full; allocate a new pool item and link it in. */ | 
| 322 | 580M |     if (p->used == p->size) { | 
| 323 | 960k |         BN_POOL_ITEM *item; | 
| 324 |  |  | 
| 325 | 960k |         if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) { | 
| 326 | 0 |             ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); | 
| 327 | 0 |             return NULL; | 
| 328 | 0 |         } | 
| 329 | 16.3M |         for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) { | 
| 330 | 15.3M |             bn_init(bn); | 
| 331 | 15.3M |             if ((flag & BN_FLG_SECURE) != 0) | 
| 332 | 22.5k |                 BN_set_flags(bn, BN_FLG_SECURE); | 
| 333 | 15.3M |         } | 
| 334 | 960k |         item->prev = p->tail; | 
| 335 | 960k |         item->next = NULL; | 
| 336 |  |  | 
| 337 | 960k |         if (p->head == NULL) | 
| 338 | 869k |             p->head = p->current = p->tail = item; | 
| 339 | 91.1k |         else { | 
| 340 | 91.1k |             p->tail->next = item; | 
| 341 | 91.1k |             p->tail = item; | 
| 342 | 91.1k |             p->current = item; | 
| 343 | 91.1k |         } | 
| 344 | 960k |         p->size += BN_CTX_POOL_SIZE; | 
| 345 | 960k |         p->used++; | 
| 346 |  |         /* Return the first bignum from the new pool */ | 
| 347 | 960k |         return item->vals; | 
| 348 | 960k |     } | 
| 349 |  |  | 
| 350 | 580M |     if (!p->used) | 
| 351 | 1.22M |         p->current = p->head; | 
| 352 | 578M |     else if ((p->used % BN_CTX_POOL_SIZE) == 0) | 
| 353 | 51.4M |         p->current = p->current->next; | 
| 354 | 580M |     return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE); | 
| 355 | 580M | } | 
| 356 |  |  | 
| 357 |  | static void BN_POOL_release(BN_POOL *p, unsigned int num) | 
| 358 | 372M | { | 
| 359 | 372M |     unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; | 
| 360 |  |  | 
| 361 | 372M |     p->used -= num; | 
| 362 | 953M |     while (num--) { | 
| 363 | 580M |         bn_check_top(p->current->vals + offset); | 
| 364 | 580M |         if (offset == 0) { | 
| 365 | 53.6M |             offset = BN_CTX_POOL_SIZE - 1; | 
| 366 | 53.6M |             p->current = p->current->prev; | 
| 367 | 53.6M |         } else | 
| 368 | 527M |             offset--; | 
| 369 | 580M |     } | 
| 370 | 372M | } |