Commit 19b8d06a authored by Bodo Möller's avatar Bodo Möller
Browse files

clean up new code for NIST primes

create new lock CRYPTO_LOCK_BN to avoid race condition
parent 5c6bf031
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@
     if applicable.
     [Nils Larsch <nla@trustcenter.de>]

  *) Add new lock type (CRYPTO_LOCK_BN).
     [Bodo Moeller]

  *) Change the ENGINE framework to automatically load engines
     dynamically from specific directories unless they could be
     found to already be built in or loaded.  Move all the
+0 −8
Original line number Diff line number Diff line
@@ -547,14 +547,6 @@ BIGNUM *bn_dup_expand(const BIGNUM *a, int words);
		} \
	}

#define bn_clear_top2max(a) \
	{ \
	int      index = (a)->dmax - (a)->top; \
	BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
	for (; index != 0; index--) \
		*(++ftl) = 0x0; \
	}

BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
void     bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
+10 −0
Original line number Diff line number Diff line
@@ -239,6 +239,16 @@ struct bignum_ctx
#define Lw(t)    (((BN_ULONG)(t))&BN_MASK2)
#define Hw(t)    (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)


#define bn_clear_top2max(a) \
	{ \
	int      index = (a)->dmax - (a)->top; \
	BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
	for (; index != 0; index--) \
		*(++ftl) = 0x0; \
	}


/* This is used for internal error checking and is not normally used */
#ifdef BN_DEBUG
# include <assert.h>
+28 −13
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@
 */

#include "bn_lcl.h"
#include "cryptlib.h"

#define BN_NIST_192_TOP	(192+BN_BITS2-1)/BN_BITS2
#define BN_NIST_224_TOP	(224+BN_BITS2-1)/BN_BITS2
@@ -314,7 +315,7 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
	if (top == 0)
		return BN_zero(r);
	else if (top > 0)
		return (r == a)? 1 : !!BN_copy(r ,a);
		return (r == a)? 1 : (BN_copy(r ,a) != NULL);

	if (r != a)
		if (!BN_ncopy(r, a, BN_NIST_192_TOP))
@@ -353,7 +354,7 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
		}
	r->top = BN_NIST_192_TOP;

#if 0
#if 1
	bn_clear_top2max(r);
#endif
	bn_fix_top(r);
@@ -393,7 +394,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
	if (tmp_int == 0)
		return BN_zero(r);
	else if (tmp_int > 0)
		return (r == a)? 1 : !!BN_copy(r ,a);
		return (r == a)? 1 : (BN_copy(r ,a) != NULL);

	if (r != a)
		if (!BN_ncopy(r, a, BN_NIST_224_TOP))
@@ -445,7 +446,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
			}

	r->top = BN_NIST_224_TOP;
#if 0
#if 1
	bn_clear_top2max(r);
#endif
	bn_fix_top(r);
@@ -502,14 +503,21 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
	BN_32_BIT_BUF(12) BN_32_BIT_BUF(13)
	BN_32_BIT_BUF(14) BN_32_BIT_BUF(15)

	if (!_is_set_256_data)
		{
		CRYPTO_w_lock(CRYPTO_LOCK_BN);
		
		if (!_is_set_256_data)
			_init_256_data();
		
		CRYPTO_w_unlock(CRYPTO_LOCK_BN);
		}
	
	tmp_int = BN_ucmp(field, a);
	if (tmp_int == 0)
		return BN_zero(r);
	else if (tmp_int > 0)
		return (r == a)? 1 : !!BN_copy(r ,a);
		return (r == a)? 1 : (BN_copy(r ,a) != NULL);

	if (r != a)
		if (!BN_ncopy(r, a, BN_NIST_256_TOP))
@@ -596,7 +604,7 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
		}

	r->top = BN_NIST_256_TOP;
#if 0
#if 1
	bn_clear_top2max(r);
#endif
	bn_fix_top(r);
@@ -656,14 +664,21 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
	BN_32_BIT_BUF(20) BN_32_BIT_BUF(21)
	BN_32_BIT_BUF(22) BN_32_BIT_BUF(23)

	if (!_is_set_384_data)
		{
		CRYPTO_w_lock(CRYPTO_LOCK_BN);
		
		if (!_is_set_384_data)
			_init_384_data();

		CRYPTO_w_unlock(CRYPTO_LOCK_BN);
		}

	tmp_int = BN_ucmp(field, a);
	if (tmp_int == 0)
		return BN_zero(r);
	else if (tmp_int > 0)
		return (r == a)? 1 : !!BN_copy(r ,a);
		return (r == a)? 1 : (BN_copy(r ,a) != NULL);

	if (r != a)
		if (!BN_ncopy(r, a, BN_NIST_384_TOP))
@@ -757,7 +772,7 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
		}

	r->top = BN_NIST_384_TOP;
#if 0
#if 1
	bn_clear_top2max(r);
#endif
	bn_fix_top(r);
@@ -793,7 +808,7 @@ int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
	top = a->top;
	if (top < BN_NIST_521_TOP  || ( top == BN_NIST_521_TOP &&
           (!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
		return (r == a)? 1 : !!BN_copy(r ,a);
		return (r == a)? 1 : (BN_copy(r ,a) != NULL);

	BN_CTX_start(ctx);
	tmp = BN_CTX_get(ctx);
+55 −1
Original line number Diff line number Diff line
/* crypto/cryptlib.c */
/* ====================================================================
 * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
 * All rights reserved.
 *
@@ -112,7 +165,8 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
	"ecdsa",
	"ec",
	"ecdh",
#if CRYPTO_NUM_LOCKS != 35
	"bn",
#if CRYPTO_NUM_LOCKS != 36
# error "Inconsistency between crypto.h and cryptlib.c"
#endif
	};
Loading