Home | History | Annotate | Download | only in bn
      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 (at) 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 (at) cryptsoft.com).  This product includes software written by Tim
     52  * Hudson (tjh (at) 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 
     62 #include "../internal.h"
     63 
     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) {
    129     OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
    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 */
    158     OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
    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;
    174     OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
    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) {
    210   OPENSSL_free(st->indexes);
    211 }
    212 
    213 static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
    214   if (st->depth == st->size) {
    215     /* Need to expand */
    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) {
    223       OPENSSL_memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
    224     }
    225     OPENSSL_free(st->indexes);
    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) {
    245     unsigned int loop = 0;
    246     BIGNUM *bn = p->head->vals;
    247     while (loop++ < BN_CTX_POOL_SIZE) {
    248       if (bn->d) {
    249         BN_clear_free(bn);
    250       }
    251       bn++;
    252     }
    253 
    254     p->current = p->head->next;
    255     OPENSSL_free(p->head);
    256     p->head = p->current;
    257   }
    258 }
    259 
    260 static BIGNUM *BN_POOL_get(BN_POOL *p) {
    261   if (p->used == p->size) {
    262     BIGNUM *bn;
    263     unsigned int loop = 0;
    264     BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
    265     if (!item) {
    266       return NULL;
    267     }
    268 
    269     /* Initialise the structure */
    270     bn = item->vals;
    271     while (loop++ < BN_CTX_POOL_SIZE) {
    272       BN_init(bn++);
    273     }
    274 
    275     item->prev = p->tail;
    276     item->next = NULL;
    277     /* Link it in */
    278     if (!p->head) {
    279       p->head = p->current = p->tail = item;
    280     } else {
    281       p->tail->next = item;
    282       p->tail = item;
    283       p->current = item;
    284     }
    285 
    286     p->size += BN_CTX_POOL_SIZE;
    287     p->used++;
    288     /* Return the first bignum from the new pool */
    289     return item->vals;
    290   }
    291 
    292   if (!p->used) {
    293     p->current = p->head;
    294   } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
    295     p->current = p->current->next;
    296   }
    297 
    298   return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
    299 }
    300 
    301 static void BN_POOL_release(BN_POOL *p, unsigned int num) {
    302   unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
    303   p->used -= num;
    304 
    305   while (num--) {
    306     if (!offset) {
    307       offset = BN_CTX_POOL_SIZE - 1;
    308       p->current = p->current->prev;
    309     } else {
    310       offset--;
    311     }
    312   }
    313 }
    314