Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Written by Ulf Moeller for the OpenSSL project. */ |
| 2 | /* ==================================================================== |
| 3 | * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * openssl-core@openssl.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | * This product includes cryptographic software written by Eric Young |
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). */ |
| 53 | |
| 54 | |
| 55 | #include <openssl/bn.h> |
| 56 | |
| 57 | #include <string.h> |
| 58 | |
| 59 | #include <openssl/err.h> |
| 60 | #include <openssl/mem.h> |
| 61 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 62 | #include "../internal.h" |
| 63 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 64 | |
| 65 | /* How many bignums are in each "pool item"; */ |
| 66 | #define BN_CTX_POOL_SIZE 16 |
| 67 | /* The stack frame info is resizing, set a first-time expansion size; */ |
| 68 | #define BN_CTX_START_FRAMES 32 |
| 69 | |
| 70 | /* A bundle of bignums that can be linked with other bundles */ |
| 71 | typedef struct bignum_pool_item { |
| 72 | /* The bignum values */ |
| 73 | BIGNUM vals[BN_CTX_POOL_SIZE]; |
| 74 | /* Linked-list admin */ |
| 75 | struct bignum_pool_item *prev, *next; |
| 76 | } BN_POOL_ITEM; |
| 77 | |
| 78 | |
| 79 | typedef struct bignum_pool { |
| 80 | /* Linked-list admin */ |
| 81 | BN_POOL_ITEM *head, *current, *tail; |
| 82 | /* Stack depth and allocation size */ |
| 83 | unsigned used, size; |
| 84 | } BN_POOL; |
| 85 | |
| 86 | static void BN_POOL_init(BN_POOL *); |
| 87 | static void BN_POOL_finish(BN_POOL *); |
| 88 | static BIGNUM *BN_POOL_get(BN_POOL *); |
| 89 | static void BN_POOL_release(BN_POOL *, unsigned int); |
| 90 | |
| 91 | /************/ |
| 92 | /* BN_STACK */ |
| 93 | /************/ |
| 94 | |
| 95 | /* A wrapper to manage the "stack frames" */ |
| 96 | typedef struct bignum_ctx_stack { |
| 97 | /* Array of indexes into the bignum stack */ |
| 98 | unsigned int *indexes; |
| 99 | /* Number of stack frames, and the size of the allocated array */ |
| 100 | unsigned int depth, size; |
| 101 | } BN_STACK; |
| 102 | |
| 103 | static void BN_STACK_init(BN_STACK *); |
| 104 | static void BN_STACK_finish(BN_STACK *); |
| 105 | static int BN_STACK_push(BN_STACK *, unsigned int); |
| 106 | static unsigned int BN_STACK_pop(BN_STACK *); |
| 107 | |
| 108 | /**********/ |
| 109 | /* BN_CTX */ |
| 110 | /**********/ |
| 111 | |
| 112 | /* The opaque BN_CTX type */ |
| 113 | struct bignum_ctx { |
| 114 | /* The bignum bundles */ |
| 115 | BN_POOL pool; |
| 116 | /* The "stack frames", if you will */ |
| 117 | BN_STACK stack; |
| 118 | /* The number of bignums currently assigned */ |
| 119 | unsigned int used; |
| 120 | /* Depth of stack overflow */ |
| 121 | int err_stack; |
| 122 | /* Block "gets" until an "end" (compatibility behaviour) */ |
| 123 | int too_many; |
| 124 | }; |
| 125 | |
| 126 | BN_CTX *BN_CTX_new(void) { |
| 127 | BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX)); |
| 128 | if (!ret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 129 | OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | /* Initialise the structure */ |
| 134 | BN_POOL_init(&ret->pool); |
| 135 | BN_STACK_init(&ret->stack); |
| 136 | ret->used = 0; |
| 137 | ret->err_stack = 0; |
| 138 | ret->too_many = 0; |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | void BN_CTX_free(BN_CTX *ctx) { |
| 143 | if (ctx == NULL) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | BN_STACK_finish(&ctx->stack); |
| 148 | BN_POOL_finish(&ctx->pool); |
| 149 | OPENSSL_free(ctx); |
| 150 | } |
| 151 | |
| 152 | void BN_CTX_start(BN_CTX *ctx) { |
| 153 | /* If we're already overflowing ... */ |
| 154 | if (ctx->err_stack || ctx->too_many) { |
| 155 | ctx->err_stack++; |
| 156 | } else if (!BN_STACK_push(&ctx->stack, ctx->used)) { |
| 157 | /* (Try to) get a new frame pointer */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 158 | OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | ctx->err_stack++; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | BIGNUM *BN_CTX_get(BN_CTX *ctx) { |
| 164 | BIGNUM *ret; |
| 165 | if (ctx->err_stack || ctx->too_many) { |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | ret = BN_POOL_get(&ctx->pool); |
| 170 | if (ret == NULL) { |
| 171 | /* Setting too_many prevents repeated "get" attempts from |
| 172 | * cluttering the error stack. */ |
| 173 | ctx->too_many = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 174 | OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | /* OK, make sure the returned bignum is "zero" */ |
| 179 | BN_zero(ret); |
| 180 | ctx->used++; |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | void BN_CTX_end(BN_CTX *ctx) { |
| 185 | if (ctx->err_stack) { |
| 186 | ctx->err_stack--; |
| 187 | } else { |
| 188 | unsigned int fp = BN_STACK_pop(&ctx->stack); |
| 189 | /* Does this stack frame have anything to release? */ |
| 190 | if (fp < ctx->used) { |
| 191 | BN_POOL_release(&ctx->pool, ctx->used - fp); |
| 192 | } |
| 193 | |
| 194 | ctx->used = fp; |
| 195 | /* Unjam "too_many" in case "get" had failed */ |
| 196 | ctx->too_many = 0; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /************/ |
| 201 | /* BN_STACK */ |
| 202 | /************/ |
| 203 | |
| 204 | static void BN_STACK_init(BN_STACK *st) { |
| 205 | st->indexes = NULL; |
| 206 | st->depth = st->size = 0; |
| 207 | } |
| 208 | |
| 209 | static void BN_STACK_finish(BN_STACK *st) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 210 | OPENSSL_free(st->indexes); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int BN_STACK_push(BN_STACK *st, unsigned int idx) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 214 | if (st->depth == st->size) { |
| 215 | /* Need to expand */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | unsigned int newsize = |
| 217 | (st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES); |
| 218 | unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int)); |
| 219 | if (!newitems) { |
| 220 | return 0; |
| 221 | } |
| 222 | if (st->depth) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 223 | OPENSSL_memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 224 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 225 | OPENSSL_free(st->indexes); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | st->indexes = newitems; |
| 227 | st->size = newsize; |
| 228 | } |
| 229 | |
| 230 | st->indexes[(st->depth)++] = idx; |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | static unsigned int BN_STACK_pop(BN_STACK *st) { |
| 235 | return st->indexes[--(st->depth)]; |
| 236 | } |
| 237 | |
| 238 | static void BN_POOL_init(BN_POOL *p) { |
| 239 | p->head = p->current = p->tail = NULL; |
| 240 | p->used = p->size = 0; |
| 241 | } |
| 242 | |
| 243 | static void BN_POOL_finish(BN_POOL *p) { |
| 244 | while (p->head) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 245 | for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) { |
| 246 | BN_clear_free(&p->head->vals[i]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | p->current = p->head->next; |
| 250 | OPENSSL_free(p->head); |
| 251 | p->head = p->current; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static BIGNUM *BN_POOL_get(BN_POOL *p) { |
| 256 | if (p->used == p->size) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM)); |
| 258 | if (!item) { |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | /* Initialise the structure */ |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 263 | for (size_t i = 0; i < BN_CTX_POOL_SIZE; i++) { |
| 264 | BN_init(&item->vals[i]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | item->prev = p->tail; |
| 268 | item->next = NULL; |
| 269 | /* Link it in */ |
| 270 | if (!p->head) { |
| 271 | p->head = p->current = p->tail = item; |
| 272 | } else { |
| 273 | p->tail->next = item; |
| 274 | p->tail = item; |
| 275 | p->current = item; |
| 276 | } |
| 277 | |
| 278 | p->size += BN_CTX_POOL_SIZE; |
| 279 | p->used++; |
| 280 | /* Return the first bignum from the new pool */ |
| 281 | return item->vals; |
| 282 | } |
| 283 | |
| 284 | if (!p->used) { |
| 285 | p->current = p->head; |
| 286 | } else if ((p->used % BN_CTX_POOL_SIZE) == 0) { |
| 287 | p->current = p->current->next; |
| 288 | } |
| 289 | |
| 290 | return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE); |
| 291 | } |
| 292 | |
| 293 | static void BN_POOL_release(BN_POOL *p, unsigned int num) { |
| 294 | unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; |
| 295 | p->used -= num; |
| 296 | |
| 297 | while (num--) { |
| 298 | if (!offset) { |
| 299 | offset = BN_CTX_POOL_SIZE - 1; |
| 300 | p->current = p->current->prev; |
| 301 | } else { |
| 302 | offset--; |
| 303 | } |
| 304 | } |
| 305 | } |