os/ossrv/ssl/libcrypto/src/crypto/bn/bn_ctx.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bn/bn_ctx.c */
sl@0
     2
/* Written by Ulf Moeller for the OpenSSL project. */
sl@0
     3
/* ====================================================================
sl@0
     4
 * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
sl@0
     5
 *
sl@0
     6
 * Redistribution and use in source and binary forms, with or without
sl@0
     7
 * modification, are permitted provided that the following conditions
sl@0
     8
 * are met:
sl@0
     9
 *
sl@0
    10
 * 1. Redistributions of source code must retain the above copyright
sl@0
    11
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    12
 *
sl@0
    13
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    14
 *    notice, this list of conditions and the following disclaimer in
sl@0
    15
 *    the documentation and/or other materials provided with the
sl@0
    16
 *    distribution.
sl@0
    17
 *
sl@0
    18
 * 3. All advertising materials mentioning features or use of this
sl@0
    19
 *    software must display the following acknowledgment:
sl@0
    20
 *    "This product includes software developed by the OpenSSL Project
sl@0
    21
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    22
 *
sl@0
    23
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    24
 *    endorse or promote products derived from this software without
sl@0
    25
 *    prior written permission. For written permission, please contact
sl@0
    26
 *    openssl-core@openssl.org.
sl@0
    27
 *
sl@0
    28
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    29
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    30
 *    permission of the OpenSSL Project.
sl@0
    31
 *
sl@0
    32
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    33
 *    acknowledgment:
sl@0
    34
 *    "This product includes software developed by the OpenSSL Project
sl@0
    35
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    36
 *
sl@0
    37
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    38
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    39
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    40
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    41
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    42
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    43
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    44
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    45
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    46
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    47
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    48
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    49
 * ====================================================================
sl@0
    50
 *
sl@0
    51
 * This product includes cryptographic software written by Eric Young
sl@0
    52
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    53
 * Hudson (tjh@cryptsoft.com).
sl@0
    54
 *
sl@0
    55
 */
sl@0
    56
sl@0
    57
#if !defined(BN_CTX_DEBUG) && !defined(BN_DEBUG)
sl@0
    58
#ifndef NDEBUG
sl@0
    59
#define NDEBUG
sl@0
    60
#endif
sl@0
    61
#endif
sl@0
    62
sl@0
    63
#include <stdio.h>
sl@0
    64
#include <assert.h>
sl@0
    65
sl@0
    66
#include "cryptlib.h"
sl@0
    67
#include "bn_lcl.h"
sl@0
    68
sl@0
    69
/* TODO list
sl@0
    70
 *
sl@0
    71
 * 1. Check a bunch of "(words+1)" type hacks in various bignum functions and
sl@0
    72
 * check they can be safely removed.
sl@0
    73
 *  - Check +1 and other ugliness in BN_from_montgomery()
sl@0
    74
 *
sl@0
    75
 * 2. Consider allowing a BN_new_ex() that, at least, lets you specify an
sl@0
    76
 * appropriate 'block' size that will be honoured by bn_expand_internal() to
sl@0
    77
 * prevent piddly little reallocations. OTOH, profiling bignum expansions in
sl@0
    78
 * BN_CTX doesn't show this to be a big issue.
sl@0
    79
 */
sl@0
    80
sl@0
    81
/* How many bignums are in each "pool item"; */
sl@0
    82
#define BN_CTX_POOL_SIZE	16
sl@0
    83
/* The stack frame info is resizing, set a first-time expansion size; */
sl@0
    84
#define BN_CTX_START_FRAMES	32
sl@0
    85
sl@0
    86
/***********/
sl@0
    87
/* BN_POOL */
sl@0
    88
/***********/
sl@0
    89
sl@0
    90
/* A bundle of bignums that can be linked with other bundles */
sl@0
    91
typedef struct bignum_pool_item
sl@0
    92
	{
sl@0
    93
	/* The bignum values */
sl@0
    94
	BIGNUM vals[BN_CTX_POOL_SIZE];
sl@0
    95
	/* Linked-list admin */
sl@0
    96
	struct bignum_pool_item *prev, *next;
sl@0
    97
	} BN_POOL_ITEM;
sl@0
    98
/* A linked-list of bignums grouped in bundles */
sl@0
    99
typedef struct bignum_pool
sl@0
   100
	{
sl@0
   101
	/* Linked-list admin */
sl@0
   102
	BN_POOL_ITEM *head, *current, *tail;
sl@0
   103
	/* Stack depth and allocation size */
sl@0
   104
	unsigned used, size;
sl@0
   105
	} BN_POOL;
sl@0
   106
static void		BN_POOL_init(BN_POOL *);
sl@0
   107
static void		BN_POOL_finish(BN_POOL *);
sl@0
   108
#ifndef OPENSSL_NO_DEPRECATED
sl@0
   109
static void		BN_POOL_reset(BN_POOL *);
sl@0
   110
#endif
sl@0
   111
static BIGNUM *		BN_POOL_get(BN_POOL *);
sl@0
   112
static void		BN_POOL_release(BN_POOL *, unsigned int);
sl@0
   113
sl@0
   114
/************/
sl@0
   115
/* BN_STACK */
sl@0
   116
/************/
sl@0
   117
sl@0
   118
/* A wrapper to manage the "stack frames" */
sl@0
   119
typedef struct bignum_ctx_stack
sl@0
   120
	{
sl@0
   121
	/* Array of indexes into the bignum stack */
sl@0
   122
	unsigned int *indexes;
sl@0
   123
	/* Number of stack frames, and the size of the allocated array */
sl@0
   124
	unsigned int depth, size;
sl@0
   125
	} BN_STACK;
sl@0
   126
static void		BN_STACK_init(BN_STACK *);
sl@0
   127
static void		BN_STACK_finish(BN_STACK *);
sl@0
   128
#ifndef OPENSSL_NO_DEPRECATED
sl@0
   129
static void		BN_STACK_reset(BN_STACK *);
sl@0
   130
#endif
sl@0
   131
static int		BN_STACK_push(BN_STACK *, unsigned int);
sl@0
   132
static unsigned int	BN_STACK_pop(BN_STACK *);
sl@0
   133
sl@0
   134
/**********/
sl@0
   135
/* BN_CTX */
sl@0
   136
/**********/
sl@0
   137
sl@0
   138
/* The opaque BN_CTX type */
sl@0
   139
struct bignum_ctx
sl@0
   140
	{
sl@0
   141
	/* The bignum bundles */
sl@0
   142
	BN_POOL pool;
sl@0
   143
	/* The "stack frames", if you will */
sl@0
   144
	BN_STACK stack;
sl@0
   145
	/* The number of bignums currently assigned */
sl@0
   146
	unsigned int used;
sl@0
   147
	/* Depth of stack overflow */
sl@0
   148
	int err_stack;
sl@0
   149
	/* Block "gets" until an "end" (compatibility behaviour) */
sl@0
   150
	int too_many;
sl@0
   151
	};
sl@0
   152
sl@0
   153
/* Enable this to find BN_CTX bugs */
sl@0
   154
#ifdef BN_CTX_DEBUG
sl@0
   155
static const char *ctxdbg_cur = NULL;
sl@0
   156
static void ctxdbg(BN_CTX *ctx)
sl@0
   157
	{
sl@0
   158
	unsigned int bnidx = 0, fpidx = 0;
sl@0
   159
	BN_POOL_ITEM *item = ctx->pool.head;
sl@0
   160
	BN_STACK *stack = &ctx->stack;
sl@0
   161
	fprintf(stderr,"(%08x): ", (unsigned int)ctx);
sl@0
   162
	while(bnidx < ctx->used)
sl@0
   163
		{
sl@0
   164
		fprintf(stderr,"%02x ", item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax);
sl@0
   165
		if(!(bnidx % BN_CTX_POOL_SIZE))
sl@0
   166
			item = item->next;
sl@0
   167
		}
sl@0
   168
	fprintf(stderr,"\n");
sl@0
   169
	bnidx = 0;
sl@0
   170
	fprintf(stderr,"          : ");
sl@0
   171
	while(fpidx < stack->depth)
sl@0
   172
		{
sl@0
   173
		while(bnidx++ < stack->indexes[fpidx])
sl@0
   174
			fprintf(stderr,"   ");
sl@0
   175
		fprintf(stderr,"^^ ");
sl@0
   176
		bnidx++;
sl@0
   177
		fpidx++;
sl@0
   178
		}
sl@0
   179
	fprintf(stderr,"\n");
sl@0
   180
	}
sl@0
   181
#define CTXDBG_ENTRY(str, ctx)	do { \
sl@0
   182
				ctxdbg_cur = (str); \
sl@0
   183
				fprintf(stderr,"Starting %s\n", ctxdbg_cur); \
sl@0
   184
				ctxdbg(ctx); \
sl@0
   185
				} while(0)
sl@0
   186
#define CTXDBG_EXIT(ctx)	do { \
sl@0
   187
				fprintf(stderr,"Ending %s\n", ctxdbg_cur); \
sl@0
   188
				ctxdbg(ctx); \
sl@0
   189
				} while(0)
sl@0
   190
#define CTXDBG_RET(ctx,ret)
sl@0
   191
#else
sl@0
   192
#define CTXDBG_ENTRY(str, ctx)
sl@0
   193
#define CTXDBG_EXIT(ctx)
sl@0
   194
#define CTXDBG_RET(ctx,ret)
sl@0
   195
#endif
sl@0
   196
sl@0
   197
/* This function is an evil legacy and should not be used. This implementation
sl@0
   198
 * is WYSIWYG, though I've done my best. */
sl@0
   199
#ifndef OPENSSL_NO_DEPRECATED
sl@0
   200
EXPORT_C void BN_CTX_init(BN_CTX *ctx)
sl@0
   201
	{
sl@0
   202
	/* Assume the caller obtained the context via BN_CTX_new() and so is
sl@0
   203
	 * trying to reset it for use. Nothing else makes sense, least of all
sl@0
   204
	 * binary compatibility from a time when they could declare a static
sl@0
   205
	 * variable. */
sl@0
   206
	BN_POOL_reset(&ctx->pool);
sl@0
   207
	BN_STACK_reset(&ctx->stack);
sl@0
   208
	ctx->used = 0;
sl@0
   209
	ctx->err_stack = 0;
sl@0
   210
	ctx->too_many = 0;
sl@0
   211
	}
sl@0
   212
#endif
sl@0
   213
sl@0
   214
EXPORT_C BN_CTX *BN_CTX_new(void)
sl@0
   215
	{
sl@0
   216
	BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
sl@0
   217
	if(!ret)
sl@0
   218
		{
sl@0
   219
		BNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);
sl@0
   220
		return NULL;
sl@0
   221
		}
sl@0
   222
	/* Initialise the structure */
sl@0
   223
	BN_POOL_init(&ret->pool);
sl@0
   224
	BN_STACK_init(&ret->stack);
sl@0
   225
	ret->used = 0;
sl@0
   226
	ret->err_stack = 0;
sl@0
   227
	ret->too_many = 0;
sl@0
   228
	return ret;
sl@0
   229
	}
sl@0
   230
sl@0
   231
EXPORT_C void BN_CTX_free(BN_CTX *ctx)
sl@0
   232
	{
sl@0
   233
	if (ctx == NULL)
sl@0
   234
		return;
sl@0
   235
#ifdef BN_CTX_DEBUG
sl@0
   236
	{
sl@0
   237
	BN_POOL_ITEM *pool = ctx->pool.head;
sl@0
   238
	fprintf(stderr,"BN_CTX_free, stack-size=%d, pool-bignums=%d\n",
sl@0
   239
		ctx->stack.size, ctx->pool.size);
sl@0
   240
	fprintf(stderr,"dmaxs: ");
sl@0
   241
	while(pool) {
sl@0
   242
		unsigned loop = 0;
sl@0
   243
		while(loop < BN_CTX_POOL_SIZE)
sl@0
   244
			fprintf(stderr,"%02x ", pool->vals[loop++].dmax);
sl@0
   245
		pool = pool->next;
sl@0
   246
	}
sl@0
   247
	fprintf(stderr,"\n");
sl@0
   248
	}
sl@0
   249
#endif
sl@0
   250
	BN_STACK_finish(&ctx->stack);
sl@0
   251
	BN_POOL_finish(&ctx->pool);
sl@0
   252
	OPENSSL_free(ctx);
sl@0
   253
	}
sl@0
   254
sl@0
   255
EXPORT_C void BN_CTX_start(BN_CTX *ctx)
sl@0
   256
	{
sl@0
   257
	CTXDBG_ENTRY("BN_CTX_start", ctx);
sl@0
   258
	/* If we're already overflowing ... */
sl@0
   259
	if(ctx->err_stack || ctx->too_many)
sl@0
   260
		ctx->err_stack++;
sl@0
   261
	/* (Try to) get a new frame pointer */
sl@0
   262
	else if(!BN_STACK_push(&ctx->stack, ctx->used))
sl@0
   263
		{
sl@0
   264
		BNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);
sl@0
   265
		ctx->err_stack++;
sl@0
   266
		}
sl@0
   267
	CTXDBG_EXIT(ctx);
sl@0
   268
	}
sl@0
   269
sl@0
   270
EXPORT_C void BN_CTX_end(BN_CTX *ctx)
sl@0
   271
	{
sl@0
   272
	CTXDBG_ENTRY("BN_CTX_end", ctx);
sl@0
   273
	if(ctx->err_stack)
sl@0
   274
		ctx->err_stack--;
sl@0
   275
	else
sl@0
   276
		{
sl@0
   277
		unsigned int fp = BN_STACK_pop(&ctx->stack);
sl@0
   278
		/* Does this stack frame have anything to release? */
sl@0
   279
		if(fp < ctx->used)
sl@0
   280
			BN_POOL_release(&ctx->pool, ctx->used - fp);
sl@0
   281
		ctx->used = fp;
sl@0
   282
		/* Unjam "too_many" in case "get" had failed */
sl@0
   283
		ctx->too_many = 0;
sl@0
   284
		}
sl@0
   285
	CTXDBG_EXIT(ctx);
sl@0
   286
	}
sl@0
   287
sl@0
   288
EXPORT_C BIGNUM *BN_CTX_get(BN_CTX *ctx)
sl@0
   289
	{
sl@0
   290
	BIGNUM *ret;
sl@0
   291
	CTXDBG_ENTRY("BN_CTX_get", ctx);
sl@0
   292
	if(ctx->err_stack || ctx->too_many) return NULL;
sl@0
   293
	if((ret = BN_POOL_get(&ctx->pool)) == NULL)
sl@0
   294
		{
sl@0
   295
		/* Setting too_many prevents repeated "get" attempts from
sl@0
   296
		 * cluttering the error stack. */
sl@0
   297
		ctx->too_many = 1;
sl@0
   298
		BNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);
sl@0
   299
		return NULL;
sl@0
   300
		}
sl@0
   301
	/* OK, make sure the returned bignum is "zero" */
sl@0
   302
	BN_zero(ret);
sl@0
   303
	ctx->used++;
sl@0
   304
	CTXDBG_RET(ctx, ret);
sl@0
   305
	return ret;
sl@0
   306
	}
sl@0
   307
sl@0
   308
/************/
sl@0
   309
/* BN_STACK */
sl@0
   310
/************/
sl@0
   311
sl@0
   312
static void BN_STACK_init(BN_STACK *st)
sl@0
   313
	{
sl@0
   314
	st->indexes = NULL;
sl@0
   315
	st->depth = st->size = 0;
sl@0
   316
	}
sl@0
   317
sl@0
   318
static void BN_STACK_finish(BN_STACK *st)
sl@0
   319
	{
sl@0
   320
	if(st->size) OPENSSL_free(st->indexes);
sl@0
   321
	}
sl@0
   322
sl@0
   323
#ifndef OPENSSL_NO_DEPRECATED
sl@0
   324
static void BN_STACK_reset(BN_STACK *st)
sl@0
   325
	{
sl@0
   326
	st->depth = 0;
sl@0
   327
	}
sl@0
   328
#endif
sl@0
   329
sl@0
   330
static int BN_STACK_push(BN_STACK *st, unsigned int idx)
sl@0
   331
	{
sl@0
   332
	if(st->depth == st->size)
sl@0
   333
		/* Need to expand */
sl@0
   334
		{
sl@0
   335
		unsigned int newsize = (st->size ?
sl@0
   336
				(st->size * 3 / 2) : BN_CTX_START_FRAMES);
sl@0
   337
		unsigned int *newitems = OPENSSL_malloc(newsize *
sl@0
   338
						sizeof(unsigned int));
sl@0
   339
		if(!newitems) return 0;
sl@0
   340
		if(st->depth)
sl@0
   341
			memcpy(newitems, st->indexes, st->depth *
sl@0
   342
						sizeof(unsigned int));
sl@0
   343
		if(st->size) OPENSSL_free(st->indexes);
sl@0
   344
		st->indexes = newitems;
sl@0
   345
		st->size = newsize;
sl@0
   346
		}
sl@0
   347
	st->indexes[(st->depth)++] = idx;
sl@0
   348
	return 1;
sl@0
   349
	}
sl@0
   350
sl@0
   351
static unsigned int BN_STACK_pop(BN_STACK *st)
sl@0
   352
	{
sl@0
   353
	return st->indexes[--(st->depth)];
sl@0
   354
	}
sl@0
   355
sl@0
   356
/***********/
sl@0
   357
/* BN_POOL */
sl@0
   358
/***********/
sl@0
   359
sl@0
   360
static void BN_POOL_init(BN_POOL *p)
sl@0
   361
	{
sl@0
   362
	p->head = p->current = p->tail = NULL;
sl@0
   363
	p->used = p->size = 0;
sl@0
   364
	}
sl@0
   365
sl@0
   366
static void BN_POOL_finish(BN_POOL *p)
sl@0
   367
	{
sl@0
   368
	while(p->head)
sl@0
   369
		{
sl@0
   370
		unsigned int loop = 0;
sl@0
   371
		BIGNUM *bn = p->head->vals;
sl@0
   372
		while(loop++ < BN_CTX_POOL_SIZE)
sl@0
   373
			{
sl@0
   374
			if(bn->d) BN_clear_free(bn);
sl@0
   375
			bn++;
sl@0
   376
			}
sl@0
   377
		p->current = p->head->next;
sl@0
   378
		OPENSSL_free(p->head);
sl@0
   379
		p->head = p->current;
sl@0
   380
		}
sl@0
   381
	}
sl@0
   382
sl@0
   383
#ifndef OPENSSL_NO_DEPRECATED
sl@0
   384
static void BN_POOL_reset(BN_POOL *p)
sl@0
   385
	{
sl@0
   386
	BN_POOL_ITEM *item = p->head;
sl@0
   387
	while(item)
sl@0
   388
		{
sl@0
   389
		unsigned int loop = 0;
sl@0
   390
		BIGNUM *bn = item->vals;
sl@0
   391
		while(loop++ < BN_CTX_POOL_SIZE)
sl@0
   392
			{
sl@0
   393
			if(bn->d) BN_clear(bn);
sl@0
   394
			bn++;
sl@0
   395
			}
sl@0
   396
		item = item->next;
sl@0
   397
		}
sl@0
   398
	p->current = p->head;
sl@0
   399
	p->used = 0;
sl@0
   400
	}
sl@0
   401
#endif
sl@0
   402
sl@0
   403
static BIGNUM *BN_POOL_get(BN_POOL *p)
sl@0
   404
	{
sl@0
   405
	if(p->used == p->size)
sl@0
   406
		{
sl@0
   407
		BIGNUM *bn;
sl@0
   408
		unsigned int loop = 0;
sl@0
   409
		BN_POOL_ITEM *item = OPENSSL_malloc(sizeof(BN_POOL_ITEM));
sl@0
   410
		if(!item) return NULL;
sl@0
   411
		/* Initialise the structure */
sl@0
   412
		bn = item->vals;
sl@0
   413
		while(loop++ < BN_CTX_POOL_SIZE)
sl@0
   414
			BN_init(bn++);
sl@0
   415
		item->prev = p->tail;
sl@0
   416
		item->next = NULL;
sl@0
   417
		/* Link it in */
sl@0
   418
		if(!p->head)
sl@0
   419
			p->head = p->current = p->tail = item;
sl@0
   420
		else
sl@0
   421
			{
sl@0
   422
			p->tail->next = item;
sl@0
   423
			p->tail = item;
sl@0
   424
			p->current = item;
sl@0
   425
			}
sl@0
   426
		p->size += BN_CTX_POOL_SIZE;
sl@0
   427
		p->used++;
sl@0
   428
		/* Return the first bignum from the new pool */
sl@0
   429
		return item->vals;
sl@0
   430
		}
sl@0
   431
	if(!p->used)
sl@0
   432
		p->current = p->head;
sl@0
   433
	else if((p->used % BN_CTX_POOL_SIZE) == 0)
sl@0
   434
		p->current = p->current->next;
sl@0
   435
	return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE);
sl@0
   436
	}
sl@0
   437
sl@0
   438
static void BN_POOL_release(BN_POOL *p, unsigned int num)
sl@0
   439
	{
sl@0
   440
	unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;
sl@0
   441
	p->used -= num;
sl@0
   442
	while(num--)
sl@0
   443
		{
sl@0
   444
		bn_check_top(p->current->vals + offset);
sl@0
   445
		if(!offset)
sl@0
   446
			{
sl@0
   447
			offset = BN_CTX_POOL_SIZE - 1;
sl@0
   448
			p->current = p->current->prev;
sl@0
   449
			}
sl@0
   450
		else
sl@0
   451
			offset--;
sl@0
   452
		}
sl@0
   453
	}
sl@0
   454