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 
     63 /* How many bignums are in each "pool item"; */
     64 #define BN_CTX_POOL_SIZE 16
     65 /* The stack frame info is resizing, set a first-time expansion size; */
     66 #define BN_CTX_START_FRAMES 32
     67 
     68 /* A bundle of bignums that can be linked with other bundles */
     69 typedef struct bignum_pool_item {
     70   /* The bignum values */
     71   BIGNUM vals[BN_CTX_POOL_SIZE];
     72   /* Linked-list admin */
     73   struct bignum_pool_item *prev, *next;
     74 } BN_POOL_ITEM;
     75 
     76 
     77 typedef struct bignum_pool {
     78   /* Linked-list admin */
     79   BN_POOL_ITEM *head, *current, *tail;
     80   /* Stack depth and allocation size */
     81   unsigned used, size;
     82 } BN_POOL;
     83 
     84 static void BN_POOL_init(BN_POOL *);
     85 static void BN_POOL_finish(BN_POOL *);
     86 static BIGNUM *BN_POOL_get(BN_POOL *);
     87 static void BN_POOL_release(BN_POOL *, unsigned int);
     88 
     89 /************/
     90 /* BN_STACK */
     91 /************/
     92 
     93 /* A wrapper to manage the "stack frames" */
     94 typedef struct bignum_ctx_stack {
     95   /* Array of indexes into the bignum stack */
     96   unsigned int *indexes;
     97   /* Number of stack frames, and the size of the allocated array */
     98   unsigned int depth, size;
     99 } BN_STACK;
    100 
    101 static void		BN_STACK_init(BN_STACK *);
    102 static void		BN_STACK_finish(BN_STACK *);
    103 static int		BN_STACK_push(BN_STACK *, unsigned int);
    104 static unsigned int	BN_STACK_pop(BN_STACK *);
    105 
    106 /**********/
    107 /* BN_CTX */
    108 /**********/
    109 
    110 /* The opaque BN_CTX type */
    111 struct bignum_ctx {
    112   /* The bignum bundles */
    113   BN_POOL pool;
    114   /* The "stack frames", if you will */
    115   BN_STACK stack;
    116   /* The number of bignums currently assigned */
    117   unsigned int used;
    118   /* Depth of stack overflow */
    119   int err_stack;
    120   /* Block "gets" until an "end" (compatibility behaviour) */
    121   int too_many;
    122 };
    123 
    124 BN_CTX *BN_CTX_new(void) {
    125   BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
    126   if (!ret) {
    127     OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
    128     return NULL;
    129   }
    130 
    131   /* Initialise the structure */
    132   BN_POOL_init(&ret->pool);
    133   BN_STACK_init(&ret->stack);
    134   ret->used = 0;
    135   ret->err_stack = 0;
    136   ret->too_many = 0;
    137   return ret;
    138 }
    139 
    140 void BN_CTX_free(BN_CTX *ctx) {
    141   if (ctx == NULL) {
    142     return;
    143   }
    144 
    145   BN_STACK_finish(&ctx->stack);
    146   BN_POOL_finish(&ctx->pool);
    147   OPENSSL_free(ctx);
    148 }
    149 
    150 void BN_CTX_start(BN_CTX *ctx) {
    151   /* If we're already overflowing ... */
    152   if (ctx->err_stack || ctx->too_many) {
    153     ctx->err_stack++;
    154   } else if (!BN_STACK_push(&ctx->stack, ctx->used)) {
    155     /* (Try to) get a new frame pointer */
    156     OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
    157     ctx->err_stack++;
    158   }
    159 }
    160 
    161 BIGNUM *BN_CTX_get(BN_CTX *ctx) {
    162   BIGNUM *ret;
    163   if (ctx->err_stack || ctx->too_many) {
    164     return NULL;
    165   }
    166 
    167   ret = BN_POOL_get(&ctx->pool);
    168   if (ret == NULL) {
    169     /* Setting too_many prevents repeated "get" attempts from
    170      * cluttering the error stack. */
    171     ctx->too_many = 1;
    172     OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES);
    173     return NULL;
    174   }
    175 
    176   /* OK, make sure the returned bignum is "zero" */
    177   BN_zero(ret);
    178   ctx->used++;
    179   return ret;
    180 }
    181 
    182 void BN_CTX_end(BN_CTX *ctx) {
    183   if (ctx->err_stack) {
    184     ctx->err_stack--;
    185   } else {
    186     unsigned int fp = BN_STACK_pop(&ctx->stack);
    187     /* Does this stack frame have anything to release? */
    188     if (fp < ctx->used) {
    189       BN_POOL_release(&ctx->pool, ctx->used - fp);
    190     }
    191 
    192     ctx->used = fp;
    193     /* Unjam "too_many" in case "get" had failed */
    194     ctx->too_many = 0;
    195   }
    196 }
    197 
    198 /************/
    199 /* BN_STACK */
    200 /************/
    201 
    202 static void BN_STACK_init(BN_STACK *st) {
    203   st->indexes = NULL;
    204   st->depth = st->size = 0;
    205 }
    206 
    207 static void BN_STACK_finish(BN_STACK *st) {
    208   OPENSSL_free(st->indexes);
    209 }
    210 
    211 static int BN_STACK_push(BN_STACK *st, unsigned int idx) {
    212   if (st->depth == st->size) {
    213     /* Need to expand */
    214     unsigned int newsize =
    215         (st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES);
    216     unsigned int *newitems = OPENSSL_malloc(newsize * sizeof(unsigned int));
    217     if (!newitems) {
    218       return 0;
    219     }
    220     if (st->depth) {
    221       memcpy(newitems, st->indexes, st->depth * sizeof(unsigned int));
    222     }
    223     OPENSSL_free(st->indexes);
    224     st->indexes = newitems;
    225     st->size = newsize;
    226   }
    227 
    228   st->indexes[(st->depth)++] = idx;
    229   return 1;
    230 }
    231 
    232 static unsigned int BN_STACK_pop(BN_STACK *st) {
    233   return st->indexes[--(st->depth)];
    234 }
    235 
    236 static void BN_POOL_init(BN_POOL *p) {
    237   p->head = p->current = p->tail = NULL;
    238   p->used = p->size = 0;
    239 }
    240 
    241 static void BN_POOL_finish(BN_POOL *p) {
    242   while (p->head) {
    243     unsigned int loop = 0;
    244     BIGNUM *bn = p->head->vals;
    245     while (loop++ < BN_CTX_POOL_SIZE) {
    246       if (bn->d) {
    247         BN_clear_free(bn);
    248       }
    249       bn++;
    250     }
    251 
    252     p->current = p->head->next;
    253     OPENSSL_free(p->head);
    254     p->head = p->current;
    255   }
    256 }
    257 
    258 static BIGNUM *BN_POOL_get(BN_POOL *p) {
    259   if (p->used == p->size) {
    260     BIGNUM *bn;
    261     unsigned int loop = 0;
    262     BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
    263     if (!item) {
    264       return NULL;
    265     }
    266 
    267     /* Initialise the structure */
    268     bn = item->vals;
    269     while (loop++ < BN_CTX_POOL_SIZE) {
    270       BN_init(bn++);
    271     }
    272 
    273     item->prev = p->tail;
    274     item->next = NULL;
    275     /* Link it in */
    276     if (!p->head) {
    277       p->head = p->current = p->tail = item;
    278     } else {
    279       p->tail->next = item;
    280       p->tail = item;
    281       p->current = item;
    282     }
    283 
    284     p->size += BN_CTX_POOL_SIZE;
    285     p->used++;
    286     /* Return the first bignum from the new pool */
    287     return item->vals;
    288   }
    289 
    290   if (!p->used) {
    291     p->current = p->head;
    292   } else if ((p->used % BN_CTX_POOL_SIZE) == 0) {
    293     p->current = p->current->next;
    294   }
    295 
    296   return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
    297 }
    298 
    299 static void BN_POOL_release(BN_POOL *p, unsigned int num) {
    300   unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
    301   p->used -= num;
    302 
    303   while (num--) {
    304     if (!offset) {
    305       offset = BN_CTX_POOL_SIZE - 1;
    306       p->current = p->current->prev;
    307     } else {
    308       offset--;
    309     }
    310   }
    311 }
    312