os/ossrv/ssl/libcrypto/src/crypto/bn/bn_blind.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_blind.c */
sl@0
     2
/* ====================================================================
sl@0
     3
 * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
sl@0
     4
 *
sl@0
     5
 * Redistribution and use in source and binary forms, with or without
sl@0
     6
 * modification, are permitted provided that the following conditions
sl@0
     7
 * are met:
sl@0
     8
 *
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    11
 *
sl@0
    12
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer in
sl@0
    14
 *    the documentation and/or other materials provided with the
sl@0
    15
 *    distribution.
sl@0
    16
 *
sl@0
    17
 * 3. All advertising materials mentioning features or use of this
sl@0
    18
 *    software must display the following acknowledgment:
sl@0
    19
 *    "This product includes software developed by the OpenSSL Project
sl@0
    20
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    21
 *
sl@0
    22
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    23
 *    endorse or promote products derived from this software without
sl@0
    24
 *    prior written permission. For written permission, please contact
sl@0
    25
 *    openssl-core@openssl.org.
sl@0
    26
 *
sl@0
    27
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    28
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    29
 *    permission of the OpenSSL Project.
sl@0
    30
 *
sl@0
    31
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    32
 *    acknowledgment:
sl@0
    33
 *    "This product includes software developed by the OpenSSL Project
sl@0
    34
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    35
 *
sl@0
    36
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    37
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    39
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    45
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    46
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    47
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    48
 * ====================================================================
sl@0
    49
 *
sl@0
    50
 * This product includes cryptographic software written by Eric Young
sl@0
    51
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    52
 * Hudson (tjh@cryptsoft.com).
sl@0
    53
 *
sl@0
    54
 */
sl@0
    55
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
    56
 * All rights reserved.
sl@0
    57
 *
sl@0
    58
 * This package is an SSL implementation written
sl@0
    59
 * by Eric Young (eay@cryptsoft.com).
sl@0
    60
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
    61
 * 
sl@0
    62
 * This library is free for commercial and non-commercial use as long as
sl@0
    63
 * the following conditions are aheared to.  The following conditions
sl@0
    64
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    65
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    66
 * included with this distribution is covered by the same copyright terms
sl@0
    67
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    68
 * 
sl@0
    69
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    70
 * the code are not to be removed.
sl@0
    71
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    72
 * as the author of the parts of the library used.
sl@0
    73
 * This can be in the form of a textual message at program startup or
sl@0
    74
 * in documentation (online or textual) provided with the package.
sl@0
    75
 * 
sl@0
    76
 * Redistribution and use in source and binary forms, with or without
sl@0
    77
 * modification, are permitted provided that the following conditions
sl@0
    78
 * are met:
sl@0
    79
 * 1. Redistributions of source code must retain the copyright
sl@0
    80
 *    notice, this list of conditions and the following disclaimer.
sl@0
    81
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    82
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    83
 *    documentation and/or other materials provided with the distribution.
sl@0
    84
 * 3. All advertising materials mentioning features or use of this software
sl@0
    85
 *    must display the following acknowledgement:
sl@0
    86
 *    "This product includes cryptographic software written by
sl@0
    87
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    88
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    89
 *    being used are not cryptographic related :-).
sl@0
    90
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    91
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    92
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    93
 * 
sl@0
    94
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    95
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    96
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    97
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    98
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    99
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
   100
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   101
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
   102
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
   103
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
   104
 * SUCH DAMAGE.
sl@0
   105
 * 
sl@0
   106
 * The licence and distribution terms for any publically available version or
sl@0
   107
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
   108
 * copied and put under another distribution licence
sl@0
   109
 * [including the GNU Public Licence.]
sl@0
   110
 */
sl@0
   111
sl@0
   112
#include <stdio.h>
sl@0
   113
#include "cryptlib.h"
sl@0
   114
#include "bn_lcl.h"
sl@0
   115
sl@0
   116
#define BN_BLINDING_COUNTER	32
sl@0
   117
sl@0
   118
struct bn_blinding_st
sl@0
   119
	{
sl@0
   120
	BIGNUM *A;
sl@0
   121
	BIGNUM *Ai;
sl@0
   122
	BIGNUM *e;
sl@0
   123
	BIGNUM *mod; /* just a reference */
sl@0
   124
	unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b;
sl@0
   125
				  * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */
sl@0
   126
	unsigned int  counter;
sl@0
   127
	unsigned long flags;
sl@0
   128
	BN_MONT_CTX *m_ctx;
sl@0
   129
	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
sl@0
   130
			  const BIGNUM *m, BN_CTX *ctx,
sl@0
   131
			  BN_MONT_CTX *m_ctx);
sl@0
   132
	};
sl@0
   133
sl@0
   134
EXPORT_C BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
sl@0
   135
	{
sl@0
   136
	BN_BLINDING *ret=NULL;
sl@0
   137
sl@0
   138
	bn_check_top(mod);
sl@0
   139
sl@0
   140
	if ((ret=(BN_BLINDING *)OPENSSL_malloc(sizeof(BN_BLINDING))) == NULL)
sl@0
   141
		{
sl@0
   142
		BNerr(BN_F_BN_BLINDING_NEW,ERR_R_MALLOC_FAILURE);
sl@0
   143
		return(NULL);
sl@0
   144
		}
sl@0
   145
	memset(ret,0,sizeof(BN_BLINDING));
sl@0
   146
	if (A != NULL)
sl@0
   147
		{
sl@0
   148
		if ((ret->A  = BN_dup(A))  == NULL) goto err;
sl@0
   149
		}
sl@0
   150
	if (Ai != NULL)
sl@0
   151
		{
sl@0
   152
		if ((ret->Ai = BN_dup(Ai)) == NULL) goto err;
sl@0
   153
		}
sl@0
   154
	ret->mod = mod;
sl@0
   155
	/* save a copy of mod in the BN_BLINDING structure */
sl@0
   156
	if ((ret->mod = BN_dup(mod)) == NULL) goto err;
sl@0
   157
	if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
sl@0
   158
		BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
sl@0
   159
	ret->counter = BN_BLINDING_COUNTER;
sl@0
   160
	return(ret);
sl@0
   161
err:
sl@0
   162
	if (ret != NULL) BN_BLINDING_free(ret);
sl@0
   163
	return(NULL);
sl@0
   164
	}
sl@0
   165
sl@0
   166
EXPORT_C void BN_BLINDING_free(BN_BLINDING *r)
sl@0
   167
	{
sl@0
   168
	if(r == NULL)
sl@0
   169
	    return;
sl@0
   170
sl@0
   171
	if (r->A  != NULL) BN_free(r->A );
sl@0
   172
	if (r->Ai != NULL) BN_free(r->Ai);
sl@0
   173
	if (r->e  != NULL) BN_free(r->e );
sl@0
   174
	if (r->mod != NULL) BN_free(r->mod); 
sl@0
   175
	OPENSSL_free(r);
sl@0
   176
	}
sl@0
   177
sl@0
   178
EXPORT_C int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
sl@0
   179
	{
sl@0
   180
	int ret=0;
sl@0
   181
sl@0
   182
	if ((b->A == NULL) || (b->Ai == NULL))
sl@0
   183
		{
sl@0
   184
		BNerr(BN_F_BN_BLINDING_UPDATE,BN_R_NOT_INITIALIZED);
sl@0
   185
		goto err;
sl@0
   186
		}
sl@0
   187
sl@0
   188
	if (--(b->counter) == 0 && b->e != NULL &&
sl@0
   189
		!(b->flags & BN_BLINDING_NO_RECREATE))
sl@0
   190
		{
sl@0
   191
		/* re-create blinding parameters */
sl@0
   192
		if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
sl@0
   193
			goto err;
sl@0
   194
		}
sl@0
   195
	else if (!(b->flags & BN_BLINDING_NO_UPDATE))
sl@0
   196
		{
sl@0
   197
		if (!BN_mod_mul(b->A,b->A,b->A,b->mod,ctx)) goto err;
sl@0
   198
		if (!BN_mod_mul(b->Ai,b->Ai,b->Ai,b->mod,ctx)) goto err;
sl@0
   199
		}
sl@0
   200
sl@0
   201
	ret=1;
sl@0
   202
err:
sl@0
   203
	if (b->counter == 0)
sl@0
   204
		b->counter = BN_BLINDING_COUNTER;
sl@0
   205
	return(ret);
sl@0
   206
	}
sl@0
   207
sl@0
   208
EXPORT_C int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
sl@0
   209
	{
sl@0
   210
	return BN_BLINDING_convert_ex(n, NULL, b, ctx);
sl@0
   211
	}
sl@0
   212
sl@0
   213
EXPORT_C int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
sl@0
   214
	{
sl@0
   215
	int ret = 1;
sl@0
   216
sl@0
   217
	bn_check_top(n);
sl@0
   218
sl@0
   219
	if ((b->A == NULL) || (b->Ai == NULL))
sl@0
   220
		{
sl@0
   221
		BNerr(BN_F_BN_BLINDING_CONVERT_EX,BN_R_NOT_INITIALIZED);
sl@0
   222
		return(0);
sl@0
   223
		}
sl@0
   224
sl@0
   225
	if (r != NULL)
sl@0
   226
		{
sl@0
   227
		if (!BN_copy(r, b->Ai)) ret=0;
sl@0
   228
		}
sl@0
   229
sl@0
   230
	if (!BN_mod_mul(n,n,b->A,b->mod,ctx)) ret=0;
sl@0
   231
	
sl@0
   232
	return ret;
sl@0
   233
	}
sl@0
   234
sl@0
   235
EXPORT_C int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
sl@0
   236
	{
sl@0
   237
	return BN_BLINDING_invert_ex(n, NULL, b, ctx);
sl@0
   238
	}
sl@0
   239
sl@0
   240
EXPORT_C int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
sl@0
   241
	{
sl@0
   242
	int ret;
sl@0
   243
sl@0
   244
	bn_check_top(n);
sl@0
   245
	if ((b->A == NULL) || (b->Ai == NULL))
sl@0
   246
		{
sl@0
   247
		BNerr(BN_F_BN_BLINDING_INVERT_EX,BN_R_NOT_INITIALIZED);
sl@0
   248
		return(0);
sl@0
   249
		}
sl@0
   250
sl@0
   251
	if (r != NULL)
sl@0
   252
		ret = BN_mod_mul(n, n, r, b->mod, ctx);
sl@0
   253
	else
sl@0
   254
		ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
sl@0
   255
sl@0
   256
	if (ret >= 0)
sl@0
   257
		{
sl@0
   258
		if (!BN_BLINDING_update(b,ctx))
sl@0
   259
			return(0);
sl@0
   260
		}
sl@0
   261
	bn_check_top(n);
sl@0
   262
	return(ret);
sl@0
   263
	}
sl@0
   264
sl@0
   265
EXPORT_C unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *b)
sl@0
   266
	{
sl@0
   267
	return b->thread_id;
sl@0
   268
	}
sl@0
   269
sl@0
   270
EXPORT_C void BN_BLINDING_set_thread_id(BN_BLINDING *b, unsigned long n)
sl@0
   271
	{
sl@0
   272
	b->thread_id = n;
sl@0
   273
	}
sl@0
   274
sl@0
   275
EXPORT_C unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
sl@0
   276
	{
sl@0
   277
	return b->flags;
sl@0
   278
	}
sl@0
   279
sl@0
   280
EXPORT_C void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
sl@0
   281
	{
sl@0
   282
	b->flags = flags;
sl@0
   283
	}
sl@0
   284
sl@0
   285
EXPORT_C BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
sl@0
   286
	const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
sl@0
   287
	int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
sl@0
   288
			  const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
sl@0
   289
	BN_MONT_CTX *m_ctx)
sl@0
   290
{
sl@0
   291
	int    retry_counter = 32;
sl@0
   292
	BN_BLINDING *ret = NULL;
sl@0
   293
sl@0
   294
	if (b == NULL)
sl@0
   295
		ret = BN_BLINDING_new(NULL, NULL, m);
sl@0
   296
	else
sl@0
   297
		ret = b;
sl@0
   298
sl@0
   299
	if (ret == NULL)
sl@0
   300
		goto err;
sl@0
   301
sl@0
   302
	if (ret->A  == NULL && (ret->A  = BN_new()) == NULL)
sl@0
   303
		goto err;
sl@0
   304
	if (ret->Ai == NULL && (ret->Ai	= BN_new()) == NULL)
sl@0
   305
		goto err;
sl@0
   306
sl@0
   307
	if (e != NULL)
sl@0
   308
		{
sl@0
   309
		if (ret->e != NULL)
sl@0
   310
			BN_free(ret->e);
sl@0
   311
		ret->e = BN_dup(e);
sl@0
   312
		}
sl@0
   313
	if (ret->e == NULL)
sl@0
   314
		goto err;
sl@0
   315
sl@0
   316
	if (bn_mod_exp != NULL)
sl@0
   317
		ret->bn_mod_exp = bn_mod_exp;
sl@0
   318
	if (m_ctx != NULL)
sl@0
   319
		ret->m_ctx = m_ctx;
sl@0
   320
sl@0
   321
	do {
sl@0
   322
		if (!BN_rand_range(ret->A, ret->mod)) goto err;
sl@0
   323
		if (BN_mod_inverse(ret->Ai, ret->A, ret->mod, ctx) == NULL)
sl@0
   324
			{
sl@0
   325
			/* this should almost never happen for good RSA keys */
sl@0
   326
			unsigned long error = ERR_peek_last_error();
sl@0
   327
			if (ERR_GET_REASON(error) == BN_R_NO_INVERSE)
sl@0
   328
				{
sl@0
   329
				if (retry_counter-- == 0)
sl@0
   330
				{
sl@0
   331
					BNerr(BN_F_BN_BLINDING_CREATE_PARAM,
sl@0
   332
						BN_R_TOO_MANY_ITERATIONS);
sl@0
   333
					goto err;
sl@0
   334
				}
sl@0
   335
				ERR_clear_error();
sl@0
   336
				}
sl@0
   337
			else
sl@0
   338
				goto err;
sl@0
   339
			}
sl@0
   340
		else
sl@0
   341
			break;
sl@0
   342
	} while (1);
sl@0
   343
sl@0
   344
	if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL)
sl@0
   345
		{
sl@0
   346
		if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
sl@0
   347
			goto err;
sl@0
   348
		}
sl@0
   349
	else
sl@0
   350
		{
sl@0
   351
		if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
sl@0
   352
			goto err;
sl@0
   353
		}
sl@0
   354
sl@0
   355
	return ret;
sl@0
   356
err:
sl@0
   357
	if (b == NULL && ret != NULL)
sl@0
   358
		{
sl@0
   359
		BN_BLINDING_free(ret);
sl@0
   360
		ret = NULL;
sl@0
   361
		}
sl@0
   362
sl@0
   363
	return ret;
sl@0
   364
}