os/ossrv/ssl/libcrypto/src/crypto/bn/bn_mod.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_mod.c */
sl@0
     2
/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
sl@0
     3
 * for the OpenSSL project. */
sl@0
     4
/* ====================================================================
sl@0
     5
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
sl@0
     6
 *
sl@0
     7
 * Redistribution and use in source and binary forms, with or without
sl@0
     8
 * modification, are permitted provided that the following conditions
sl@0
     9
 * are met:
sl@0
    10
 *
sl@0
    11
 * 1. Redistributions of source code must retain the above copyright
sl@0
    12
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    13
 *
sl@0
    14
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    15
 *    notice, this list of conditions and the following disclaimer in
sl@0
    16
 *    the documentation and/or other materials provided with the
sl@0
    17
 *    distribution.
sl@0
    18
 *
sl@0
    19
 * 3. All advertising materials mentioning features or use of this
sl@0
    20
 *    software must display the following acknowledgment:
sl@0
    21
 *    "This product includes software developed by the OpenSSL Project
sl@0
    22
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    23
 *
sl@0
    24
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    25
 *    endorse or promote products derived from this software without
sl@0
    26
 *    prior written permission. For written permission, please contact
sl@0
    27
 *    openssl-core@openssl.org.
sl@0
    28
 *
sl@0
    29
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    30
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    31
 *    permission of the OpenSSL Project.
sl@0
    32
 *
sl@0
    33
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    34
 *    acknowledgment:
sl@0
    35
 *    "This product includes software developed by the OpenSSL Project
sl@0
    36
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    37
 *
sl@0
    38
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    39
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    40
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    41
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    42
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    43
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    44
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    45
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    46
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    47
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    48
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    49
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    50
 * ====================================================================
sl@0
    51
 *
sl@0
    52
 * This product includes cryptographic software written by Eric Young
sl@0
    53
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    54
 * Hudson (tjh@cryptsoft.com).
sl@0
    55
 *
sl@0
    56
 */
sl@0
    57
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
    58
 * All rights reserved.
sl@0
    59
 *
sl@0
    60
 * This package is an SSL implementation written
sl@0
    61
 * by Eric Young (eay@cryptsoft.com).
sl@0
    62
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
    63
 * 
sl@0
    64
 * This library is free for commercial and non-commercial use as long as
sl@0
    65
 * the following conditions are aheared to.  The following conditions
sl@0
    66
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    67
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    68
 * included with this distribution is covered by the same copyright terms
sl@0
    69
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    70
 * 
sl@0
    71
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    72
 * the code are not to be removed.
sl@0
    73
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    74
 * as the author of the parts of the library used.
sl@0
    75
 * This can be in the form of a textual message at program startup or
sl@0
    76
 * in documentation (online or textual) provided with the package.
sl@0
    77
 * 
sl@0
    78
 * Redistribution and use in source and binary forms, with or without
sl@0
    79
 * modification, are permitted provided that the following conditions
sl@0
    80
 * are met:
sl@0
    81
 * 1. Redistributions of source code must retain the copyright
sl@0
    82
 *    notice, this list of conditions and the following disclaimer.
sl@0
    83
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    84
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    85
 *    documentation and/or other materials provided with the distribution.
sl@0
    86
 * 3. All advertising materials mentioning features or use of this software
sl@0
    87
 *    must display the following acknowledgement:
sl@0
    88
 *    "This product includes cryptographic software written by
sl@0
    89
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    90
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    91
 *    being used are not cryptographic related :-).
sl@0
    92
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    93
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    94
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    95
 * 
sl@0
    96
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    97
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    98
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    99
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
   100
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
   101
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
   102
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   103
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
   104
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
   105
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
   106
 * SUCH DAMAGE.
sl@0
   107
 * 
sl@0
   108
 * The licence and distribution terms for any publically available version or
sl@0
   109
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
   110
 * copied and put under another distribution licence
sl@0
   111
 * [including the GNU Public Licence.]
sl@0
   112
 */
sl@0
   113
sl@0
   114
#include "cryptlib.h"
sl@0
   115
#include "bn_lcl.h"
sl@0
   116
sl@0
   117
sl@0
   118
#if 0 /* now just a #define */
sl@0
   119
EXPORT_C int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
sl@0
   120
	{
sl@0
   121
	return(BN_div(NULL,rem,m,d,ctx));
sl@0
   122
	/* note that  rem->neg == m->neg  (unless the remainder is zero) */
sl@0
   123
	}
sl@0
   124
#endif
sl@0
   125
sl@0
   126
sl@0
   127
EXPORT_C int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
sl@0
   128
	{
sl@0
   129
	/* like BN_mod, but returns non-negative remainder
sl@0
   130
	 * (i.e.,  0 <= r < |d|  always holds) */
sl@0
   131
sl@0
   132
	if (!(BN_mod(r,m,d,ctx)))
sl@0
   133
		return 0;
sl@0
   134
	if (!r->neg)
sl@0
   135
		return 1;
sl@0
   136
	/* now   -|d| < r < 0,  so we have to set  r := r + |d| */
sl@0
   137
	return (d->neg ? BN_sub : BN_add)(r, r, d);
sl@0
   138
}
sl@0
   139
sl@0
   140
sl@0
   141
EXPORT_C int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
sl@0
   142
	{
sl@0
   143
	if (!BN_add(r, a, b)) return 0;
sl@0
   144
	return BN_nnmod(r, r, m, ctx);
sl@0
   145
	}
sl@0
   146
sl@0
   147
sl@0
   148
/* BN_mod_add variant that may be used if both  a  and  b  are non-negative
sl@0
   149
 * and less than  m */
sl@0
   150
EXPORT_C int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m)
sl@0
   151
	{
sl@0
   152
	if (!BN_uadd(r, a, b)) return 0;
sl@0
   153
	if (BN_ucmp(r, m) >= 0)
sl@0
   154
		return BN_usub(r, r, m);
sl@0
   155
	return 1;
sl@0
   156
	}
sl@0
   157
sl@0
   158
sl@0
   159
EXPORT_C int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
sl@0
   160
	{
sl@0
   161
	if (!BN_sub(r, a, b)) return 0;
sl@0
   162
	return BN_nnmod(r, r, m, ctx);
sl@0
   163
	}
sl@0
   164
sl@0
   165
sl@0
   166
/* BN_mod_sub variant that may be used if both  a  and  b  are non-negative
sl@0
   167
 * and less than  m */
sl@0
   168
EXPORT_C int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m)
sl@0
   169
	{
sl@0
   170
	if (!BN_sub(r, a, b)) return 0;
sl@0
   171
	if (r->neg)
sl@0
   172
		return BN_add(r, r, m);
sl@0
   173
	return 1;
sl@0
   174
	}
sl@0
   175
sl@0
   176
sl@0
   177
/* slow but works */
sl@0
   178
EXPORT_C int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
sl@0
   179
	BN_CTX *ctx)
sl@0
   180
	{
sl@0
   181
	BIGNUM *t;
sl@0
   182
	int ret=0;
sl@0
   183
sl@0
   184
	bn_check_top(a);
sl@0
   185
	bn_check_top(b);
sl@0
   186
	bn_check_top(m);
sl@0
   187
sl@0
   188
	BN_CTX_start(ctx);
sl@0
   189
	if ((t = BN_CTX_get(ctx)) == NULL) goto err;
sl@0
   190
	if (a == b)
sl@0
   191
		{ if (!BN_sqr(t,a,ctx)) goto err; }
sl@0
   192
	else
sl@0
   193
		{ if (!BN_mul(t,a,b,ctx)) goto err; }
sl@0
   194
	if (!BN_nnmod(r,t,m,ctx)) goto err;
sl@0
   195
	bn_check_top(r);
sl@0
   196
	ret=1;
sl@0
   197
err:
sl@0
   198
	BN_CTX_end(ctx);
sl@0
   199
	return(ret);
sl@0
   200
	}
sl@0
   201
sl@0
   202
sl@0
   203
EXPORT_C int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
sl@0
   204
	{
sl@0
   205
	if (!BN_sqr(r, a, ctx)) return 0;
sl@0
   206
	/* r->neg == 0,  thus we don't need BN_nnmod */
sl@0
   207
	return BN_mod(r, r, m, ctx);
sl@0
   208
	}
sl@0
   209
sl@0
   210
sl@0
   211
EXPORT_C int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
sl@0
   212
	{
sl@0
   213
	if (!BN_lshift1(r, a)) return 0;
sl@0
   214
	bn_check_top(r);
sl@0
   215
	return BN_nnmod(r, r, m, ctx);
sl@0
   216
	}
sl@0
   217
sl@0
   218
sl@0
   219
/* BN_mod_lshift1 variant that may be used if  a  is non-negative
sl@0
   220
 * and less than  m */
sl@0
   221
EXPORT_C int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
sl@0
   222
	{
sl@0
   223
	if (!BN_lshift1(r, a)) return 0;
sl@0
   224
	bn_check_top(r);
sl@0
   225
	if (BN_cmp(r, m) >= 0)
sl@0
   226
		return BN_sub(r, r, m);
sl@0
   227
	return 1;
sl@0
   228
	}
sl@0
   229
sl@0
   230
sl@0
   231
EXPORT_C int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx)
sl@0
   232
	{
sl@0
   233
	BIGNUM *abs_m = NULL;
sl@0
   234
	int ret;
sl@0
   235
sl@0
   236
	if (!BN_nnmod(r, a, m, ctx)) return 0;
sl@0
   237
sl@0
   238
	if (m->neg)
sl@0
   239
		{
sl@0
   240
		abs_m = BN_dup(m);
sl@0
   241
		if (abs_m == NULL) return 0;
sl@0
   242
		abs_m->neg = 0;
sl@0
   243
		}
sl@0
   244
	
sl@0
   245
	ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
sl@0
   246
	bn_check_top(r);
sl@0
   247
sl@0
   248
	if (abs_m)
sl@0
   249
		BN_free(abs_m);
sl@0
   250
	return ret;
sl@0
   251
	}
sl@0
   252
sl@0
   253
sl@0
   254
/* BN_mod_lshift variant that may be used if  a  is non-negative
sl@0
   255
 * and less than  m */
sl@0
   256
EXPORT_C int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
sl@0
   257
	{
sl@0
   258
	if (r != a)
sl@0
   259
		{
sl@0
   260
		if (BN_copy(r, a) == NULL) return 0;
sl@0
   261
		}
sl@0
   262
sl@0
   263
	while (n > 0)
sl@0
   264
		{
sl@0
   265
		int max_shift;
sl@0
   266
		
sl@0
   267
		/* 0 < r < m */
sl@0
   268
		max_shift = BN_num_bits(m) - BN_num_bits(r);
sl@0
   269
		/* max_shift >= 0 */
sl@0
   270
sl@0
   271
		if (max_shift < 0)
sl@0
   272
			{
sl@0
   273
			BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);
sl@0
   274
			return 0;
sl@0
   275
			}
sl@0
   276
sl@0
   277
		if (max_shift > n)
sl@0
   278
			max_shift = n;
sl@0
   279
sl@0
   280
		if (max_shift)
sl@0
   281
			{
sl@0
   282
			if (!BN_lshift(r, r, max_shift)) return 0;
sl@0
   283
			n -= max_shift;
sl@0
   284
			}
sl@0
   285
		else
sl@0
   286
			{
sl@0
   287
			if (!BN_lshift1(r, r)) return 0;
sl@0
   288
			--n;
sl@0
   289
			}
sl@0
   290
sl@0
   291
		/* BN_num_bits(r) <= BN_num_bits(m) */
sl@0
   292
sl@0
   293
		if (BN_cmp(r, m) >= 0) 
sl@0
   294
			{
sl@0
   295
			if (!BN_sub(r, r, m)) return 0;
sl@0
   296
			}
sl@0
   297
		}
sl@0
   298
	bn_check_top(r);
sl@0
   299
	
sl@0
   300
	return 1;
sl@0
   301
	}