os/ossrv/ssl/libcrypto/src/crypto/bn/bn_sqrt.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_sqrt.c */
sl@0
     2
/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
sl@0
     3
 * and Bodo Moeller 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
sl@0
    58
#include "cryptlib.h"
sl@0
    59
#include "bn_lcl.h"
sl@0
    60
sl@0
    61
sl@0
    62
EXPORT_C BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) 
sl@0
    63
/* Returns 'ret' such that
sl@0
    64
 *      ret^2 == a (mod p),
sl@0
    65
 * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course
sl@0
    66
 * in Algebraic Computational Number Theory", algorithm 1.5.1).
sl@0
    67
 * 'p' must be prime!
sl@0
    68
 */
sl@0
    69
	{
sl@0
    70
	BIGNUM *ret = in;
sl@0
    71
	int err = 1;
sl@0
    72
	int r;
sl@0
    73
	BIGNUM *A, *b, *q, *t, *x, *y;
sl@0
    74
	int e, i, j;
sl@0
    75
	
sl@0
    76
	if (!BN_is_odd(p) || BN_abs_is_word(p, 1))
sl@0
    77
		{
sl@0
    78
		if (BN_abs_is_word(p, 2))
sl@0
    79
			{
sl@0
    80
			if (ret == NULL)
sl@0
    81
				ret = BN_new();
sl@0
    82
			if (ret == NULL)
sl@0
    83
				goto end;
sl@0
    84
			if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
sl@0
    85
				{
sl@0
    86
				if (ret != in)
sl@0
    87
					BN_free(ret);
sl@0
    88
				return NULL;
sl@0
    89
				}
sl@0
    90
			bn_check_top(ret);
sl@0
    91
			return ret;
sl@0
    92
			}
sl@0
    93
sl@0
    94
		BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
sl@0
    95
		return(NULL);
sl@0
    96
		}
sl@0
    97
sl@0
    98
	if (BN_is_zero(a) || BN_is_one(a))
sl@0
    99
		{
sl@0
   100
		if (ret == NULL)
sl@0
   101
			ret = BN_new();
sl@0
   102
		if (ret == NULL)
sl@0
   103
			goto end;
sl@0
   104
		if (!BN_set_word(ret, BN_is_one(a)))
sl@0
   105
			{
sl@0
   106
			if (ret != in)
sl@0
   107
				BN_free(ret);
sl@0
   108
			return NULL;
sl@0
   109
			}
sl@0
   110
		bn_check_top(ret);
sl@0
   111
		return ret;
sl@0
   112
		}
sl@0
   113
sl@0
   114
	BN_CTX_start(ctx);
sl@0
   115
	A = BN_CTX_get(ctx);
sl@0
   116
	b = BN_CTX_get(ctx);
sl@0
   117
	q = BN_CTX_get(ctx);
sl@0
   118
	t = BN_CTX_get(ctx);
sl@0
   119
	x = BN_CTX_get(ctx);
sl@0
   120
	y = BN_CTX_get(ctx);
sl@0
   121
	if (y == NULL) goto end;
sl@0
   122
	
sl@0
   123
	if (ret == NULL)
sl@0
   124
		ret = BN_new();
sl@0
   125
	if (ret == NULL) goto end;
sl@0
   126
sl@0
   127
	/* A = a mod p */
sl@0
   128
	if (!BN_nnmod(A, a, p, ctx)) goto end;
sl@0
   129
sl@0
   130
	/* now write  |p| - 1  as  2^e*q  where  q  is odd */
sl@0
   131
	e = 1;
sl@0
   132
	while (!BN_is_bit_set(p, e))
sl@0
   133
		e++;
sl@0
   134
	/* we'll set  q  later (if needed) */
sl@0
   135
sl@0
   136
	if (e == 1)
sl@0
   137
		{
sl@0
   138
		/* The easy case:  (|p|-1)/2  is odd, so 2 has an inverse
sl@0
   139
		 * modulo  (|p|-1)/2,  and square roots can be computed
sl@0
   140
		 * directly by modular exponentiation.
sl@0
   141
		 * We have
sl@0
   142
		 *     2 * (|p|+1)/4 == 1   (mod (|p|-1)/2),
sl@0
   143
		 * so we can use exponent  (|p|+1)/4,  i.e.  (|p|-3)/4 + 1.
sl@0
   144
		 */
sl@0
   145
		if (!BN_rshift(q, p, 2)) goto end;
sl@0
   146
		q->neg = 0;
sl@0
   147
		if (!BN_add_word(q, 1)) goto end;
sl@0
   148
		if (!BN_mod_exp(ret, A, q, p, ctx)) goto end;
sl@0
   149
		err = 0;
sl@0
   150
		goto vrfy;
sl@0
   151
		}
sl@0
   152
	
sl@0
   153
	if (e == 2)
sl@0
   154
		{
sl@0
   155
		/* |p| == 5  (mod 8)
sl@0
   156
		 *
sl@0
   157
		 * In this case  2  is always a non-square since
sl@0
   158
		 * Legendre(2,p) = (-1)^((p^2-1)/8)  for any odd prime.
sl@0
   159
		 * So if  a  really is a square, then  2*a  is a non-square.
sl@0
   160
		 * Thus for
sl@0
   161
		 *      b := (2*a)^((|p|-5)/8),
sl@0
   162
		 *      i := (2*a)*b^2
sl@0
   163
		 * we have
sl@0
   164
		 *     i^2 = (2*a)^((1 + (|p|-5)/4)*2)
sl@0
   165
		 *         = (2*a)^((p-1)/2)
sl@0
   166
		 *         = -1;
sl@0
   167
		 * so if we set
sl@0
   168
		 *      x := a*b*(i-1),
sl@0
   169
		 * then
sl@0
   170
		 *     x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
sl@0
   171
		 *         = a^2 * b^2 * (-2*i)
sl@0
   172
		 *         = a*(-i)*(2*a*b^2)
sl@0
   173
		 *         = a*(-i)*i
sl@0
   174
		 *         = a.
sl@0
   175
		 *
sl@0
   176
		 * (This is due to A.O.L. Atkin, 
sl@0
   177
		 * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
sl@0
   178
		 * November 1992.)
sl@0
   179
		 */
sl@0
   180
sl@0
   181
		/* t := 2*a */
sl@0
   182
		if (!BN_mod_lshift1_quick(t, A, p)) goto end;
sl@0
   183
sl@0
   184
		/* b := (2*a)^((|p|-5)/8) */
sl@0
   185
		if (!BN_rshift(q, p, 3)) goto end;
sl@0
   186
		q->neg = 0;
sl@0
   187
		if (!BN_mod_exp(b, t, q, p, ctx)) goto end;
sl@0
   188
sl@0
   189
		/* y := b^2 */
sl@0
   190
		if (!BN_mod_sqr(y, b, p, ctx)) goto end;
sl@0
   191
sl@0
   192
		/* t := (2*a)*b^2 - 1*/
sl@0
   193
		if (!BN_mod_mul(t, t, y, p, ctx)) goto end;
sl@0
   194
		if (!BN_sub_word(t, 1)) goto end;
sl@0
   195
sl@0
   196
		/* x = a*b*t */
sl@0
   197
		if (!BN_mod_mul(x, A, b, p, ctx)) goto end;
sl@0
   198
		if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
sl@0
   199
sl@0
   200
		if (!BN_copy(ret, x)) goto end;
sl@0
   201
		err = 0;
sl@0
   202
		goto vrfy;
sl@0
   203
		}
sl@0
   204
	
sl@0
   205
	/* e > 2, so we really have to use the Tonelli/Shanks algorithm.
sl@0
   206
	 * First, find some  y  that is not a square. */
sl@0
   207
	if (!BN_copy(q, p)) goto end; /* use 'q' as temp */
sl@0
   208
	q->neg = 0;
sl@0
   209
	i = 2;
sl@0
   210
	do
sl@0
   211
		{
sl@0
   212
		/* For efficiency, try small numbers first;
sl@0
   213
		 * if this fails, try random numbers.
sl@0
   214
		 */
sl@0
   215
		if (i < 22)
sl@0
   216
			{
sl@0
   217
			if (!BN_set_word(y, i)) goto end;
sl@0
   218
			}
sl@0
   219
		else
sl@0
   220
			{
sl@0
   221
			if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;
sl@0
   222
			if (BN_ucmp(y, p) >= 0)
sl@0
   223
				{
sl@0
   224
				if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;
sl@0
   225
				}
sl@0
   226
			/* now 0 <= y < |p| */
sl@0
   227
			if (BN_is_zero(y))
sl@0
   228
				if (!BN_set_word(y, i)) goto end;
sl@0
   229
			}
sl@0
   230
		
sl@0
   231
		r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
sl@0
   232
		if (r < -1) goto end;
sl@0
   233
		if (r == 0)
sl@0
   234
			{
sl@0
   235
			/* m divides p */
sl@0
   236
			BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
sl@0
   237
			goto end;
sl@0
   238
			}
sl@0
   239
		}
sl@0
   240
	while (r == 1 && ++i < 82);
sl@0
   241
	
sl@0
   242
	if (r != -1)
sl@0
   243
		{
sl@0
   244
		/* Many rounds and still no non-square -- this is more likely
sl@0
   245
		 * a bug than just bad luck.
sl@0
   246
		 * Even if  p  is not prime, we should have found some  y
sl@0
   247
		 * such that r == -1.
sl@0
   248
		 */
sl@0
   249
		BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
sl@0
   250
		goto end;
sl@0
   251
		}
sl@0
   252
sl@0
   253
	/* Here's our actual 'q': */
sl@0
   254
	if (!BN_rshift(q, q, e)) goto end;
sl@0
   255
sl@0
   256
	/* Now that we have some non-square, we can find an element
sl@0
   257
	 * of order  2^e  by computing its q'th power. */
sl@0
   258
	if (!BN_mod_exp(y, y, q, p, ctx)) goto end;
sl@0
   259
	if (BN_is_one(y))
sl@0
   260
		{
sl@0
   261
		BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
sl@0
   262
		goto end;
sl@0
   263
		}
sl@0
   264
sl@0
   265
	/* Now we know that (if  p  is indeed prime) there is an integer
sl@0
   266
	 * k,  0 <= k < 2^e,  such that
sl@0
   267
	 *
sl@0
   268
	 *      a^q * y^k == 1   (mod p).
sl@0
   269
	 *
sl@0
   270
	 * As  a^q  is a square and  y  is not,  k  must be even.
sl@0
   271
	 * q+1  is even, too, so there is an element
sl@0
   272
	 *
sl@0
   273
	 *     X := a^((q+1)/2) * y^(k/2),
sl@0
   274
	 *
sl@0
   275
	 * and it satisfies
sl@0
   276
	 *
sl@0
   277
	 *     X^2 = a^q * a     * y^k
sl@0
   278
	 *         = a,
sl@0
   279
	 *
sl@0
   280
	 * so it is the square root that we are looking for.
sl@0
   281
	 */
sl@0
   282
	
sl@0
   283
	/* t := (q-1)/2  (note that  q  is odd) */
sl@0
   284
	if (!BN_rshift1(t, q)) goto end;
sl@0
   285
	
sl@0
   286
	/* x := a^((q-1)/2) */
sl@0
   287
	if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
sl@0
   288
		{
sl@0
   289
		if (!BN_nnmod(t, A, p, ctx)) goto end;
sl@0
   290
		if (BN_is_zero(t))
sl@0
   291
			{
sl@0
   292
			/* special case: a == 0  (mod p) */
sl@0
   293
			BN_zero(ret);
sl@0
   294
			err = 0;
sl@0
   295
			goto end;
sl@0
   296
			}
sl@0
   297
		else
sl@0
   298
			if (!BN_one(x)) goto end;
sl@0
   299
		}
sl@0
   300
	else
sl@0
   301
		{
sl@0
   302
		if (!BN_mod_exp(x, A, t, p, ctx)) goto end;
sl@0
   303
		if (BN_is_zero(x))
sl@0
   304
			{
sl@0
   305
			/* special case: a == 0  (mod p) */
sl@0
   306
			BN_zero(ret);
sl@0
   307
			err = 0;
sl@0
   308
			goto end;
sl@0
   309
			}
sl@0
   310
		}
sl@0
   311
sl@0
   312
	/* b := a*x^2  (= a^q) */
sl@0
   313
	if (!BN_mod_sqr(b, x, p, ctx)) goto end;
sl@0
   314
	if (!BN_mod_mul(b, b, A, p, ctx)) goto end;
sl@0
   315
	
sl@0
   316
	/* x := a*x    (= a^((q+1)/2)) */
sl@0
   317
	if (!BN_mod_mul(x, x, A, p, ctx)) goto end;
sl@0
   318
sl@0
   319
	while (1)
sl@0
   320
		{
sl@0
   321
		/* Now  b  is  a^q * y^k  for some even  k  (0 <= k < 2^E
sl@0
   322
		 * where  E  refers to the original value of  e,  which we
sl@0
   323
		 * don't keep in a variable),  and  x  is  a^((q+1)/2) * y^(k/2).
sl@0
   324
		 *
sl@0
   325
		 * We have  a*b = x^2,
sl@0
   326
		 *    y^2^(e-1) = -1,
sl@0
   327
		 *    b^2^(e-1) = 1.
sl@0
   328
		 */
sl@0
   329
sl@0
   330
		if (BN_is_one(b))
sl@0
   331
			{
sl@0
   332
			if (!BN_copy(ret, x)) goto end;
sl@0
   333
			err = 0;
sl@0
   334
			goto vrfy;
sl@0
   335
			}
sl@0
   336
sl@0
   337
sl@0
   338
		/* find smallest  i  such that  b^(2^i) = 1 */
sl@0
   339
		i = 1;
sl@0
   340
		if (!BN_mod_sqr(t, b, p, ctx)) goto end;
sl@0
   341
		while (!BN_is_one(t))
sl@0
   342
			{
sl@0
   343
			i++;
sl@0
   344
			if (i == e)
sl@0
   345
				{
sl@0
   346
				BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
sl@0
   347
				goto end;
sl@0
   348
				}
sl@0
   349
			if (!BN_mod_mul(t, t, t, p, ctx)) goto end;
sl@0
   350
			}
sl@0
   351
		
sl@0
   352
sl@0
   353
		/* t := y^2^(e - i - 1) */
sl@0
   354
		if (!BN_copy(t, y)) goto end;
sl@0
   355
		for (j = e - i - 1; j > 0; j--)
sl@0
   356
			{
sl@0
   357
			if (!BN_mod_sqr(t, t, p, ctx)) goto end;
sl@0
   358
			}
sl@0
   359
		if (!BN_mod_mul(y, t, t, p, ctx)) goto end;
sl@0
   360
		if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
sl@0
   361
		if (!BN_mod_mul(b, b, y, p, ctx)) goto end;
sl@0
   362
		e = i;
sl@0
   363
		}
sl@0
   364
sl@0
   365
 vrfy:
sl@0
   366
	if (!err)
sl@0
   367
		{
sl@0
   368
		/* verify the result -- the input might have been not a square
sl@0
   369
		 * (test added in 0.9.8) */
sl@0
   370
		
sl@0
   371
		if (!BN_mod_sqr(x, ret, p, ctx))
sl@0
   372
			err = 1;
sl@0
   373
		
sl@0
   374
		if (!err && 0 != BN_cmp(x, A))
sl@0
   375
			{
sl@0
   376
			BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
sl@0
   377
			err = 1;
sl@0
   378
			}
sl@0
   379
		}
sl@0
   380
sl@0
   381
 end:
sl@0
   382
	if (err)
sl@0
   383
		{
sl@0
   384
		if (ret != NULL && ret != in)
sl@0
   385
			{
sl@0
   386
			BN_clear_free(ret);
sl@0
   387
			}
sl@0
   388
		ret = NULL;
sl@0
   389
		}
sl@0
   390
	BN_CTX_end(ctx);
sl@0
   391
	bn_check_top(ret);
sl@0
   392
	return ret;
sl@0
   393
	}