10Sstevel@tonic-gate /* crypto/bn/bn_mod.c */
20Sstevel@tonic-gate /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
30Sstevel@tonic-gate * for the OpenSSL project. */
40Sstevel@tonic-gate /* ====================================================================
50Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
80Sstevel@tonic-gate * modification, are permitted provided that the following conditions
90Sstevel@tonic-gate * are met:
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
150Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
160Sstevel@tonic-gate * the documentation and/or other materials provided with the
170Sstevel@tonic-gate * distribution.
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
200Sstevel@tonic-gate * software must display the following acknowledgment:
210Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
220Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
230Sstevel@tonic-gate *
240Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
250Sstevel@tonic-gate * endorse or promote products derived from this software without
260Sstevel@tonic-gate * prior written permission. For written permission, please contact
270Sstevel@tonic-gate * openssl-core@openssl.org.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
300Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
310Sstevel@tonic-gate * permission of the OpenSSL Project.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
340Sstevel@tonic-gate * acknowledgment:
350Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
360Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
390Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
400Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
410Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
420Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
430Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
440Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
450Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
460Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
470Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
480Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
490Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
500Sstevel@tonic-gate * ====================================================================
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
530Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
540Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
550Sstevel@tonic-gate *
560Sstevel@tonic-gate */
570Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
580Sstevel@tonic-gate * All rights reserved.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * This package is an SSL implementation written
610Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
620Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
650Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
660Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
670Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
680Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
690Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
720Sstevel@tonic-gate * the code are not to be removed.
730Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
740Sstevel@tonic-gate * as the author of the parts of the library used.
750Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
760Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
790Sstevel@tonic-gate * modification, are permitted provided that the following conditions
800Sstevel@tonic-gate * are met:
810Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
820Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
830Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
840Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
850Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
860Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
870Sstevel@tonic-gate * must display the following acknowledgement:
880Sstevel@tonic-gate * "This product includes cryptographic software written by
890Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
900Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
910Sstevel@tonic-gate * being used are not cryptographic related :-).
920Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
930Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
940Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
950Sstevel@tonic-gate *
960Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
970Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
980Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
990Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1000Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1010Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1020Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1030Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1040Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1050Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1060Sstevel@tonic-gate * SUCH DAMAGE.
1070Sstevel@tonic-gate *
1080Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
1090Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
1100Sstevel@tonic-gate * copied and put under another distribution licence
1110Sstevel@tonic-gate * [including the GNU Public Licence.]
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate #include "cryptlib.h"
1150Sstevel@tonic-gate #include "bn_lcl.h"
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate #if 0 /* now just a #define */
1190Sstevel@tonic-gate int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate return(BN_div(NULL,rem,m,d,ctx));
1220Sstevel@tonic-gate /* note that rem->neg == m->neg (unless the remainder is zero) */
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate
BN_nnmod(BIGNUM * r,const BIGNUM * m,const BIGNUM * d,BN_CTX * ctx)1270Sstevel@tonic-gate int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate /* like BN_mod, but returns non-negative remainder
1300Sstevel@tonic-gate * (i.e., 0 <= r < |d| always holds) */
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate if (!(BN_mod(r,m,d,ctx)))
1330Sstevel@tonic-gate return 0;
1340Sstevel@tonic-gate if (!r->neg)
1350Sstevel@tonic-gate return 1;
1360Sstevel@tonic-gate /* now -|d| < r < 0, so we have to set r := r + |d| */
1370Sstevel@tonic-gate return (d->neg ? BN_sub : BN_add)(r, r, d);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate
BN_mod_add(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)1410Sstevel@tonic-gate int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate if (!BN_add(r, a, b)) return 0;
1440Sstevel@tonic-gate return BN_nnmod(r, r, m, ctx);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /* BN_mod_add variant that may be used if both a and b are non-negative
1490Sstevel@tonic-gate * and less than m */
BN_mod_add_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m)1500Sstevel@tonic-gate int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m)
1510Sstevel@tonic-gate {
152*2139Sjp161948 if (!BN_uadd(r, a, b)) return 0;
1530Sstevel@tonic-gate if (BN_ucmp(r, m) >= 0)
1540Sstevel@tonic-gate return BN_usub(r, r, m);
1550Sstevel@tonic-gate return 1;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate
BN_mod_sub(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)1590Sstevel@tonic-gate int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate if (!BN_sub(r, a, b)) return 0;
1620Sstevel@tonic-gate return BN_nnmod(r, r, m, ctx);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /* BN_mod_sub variant that may be used if both a and b are non-negative
1670Sstevel@tonic-gate * and less than m */
BN_mod_sub_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m)1680Sstevel@tonic-gate int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate if (!BN_sub(r, a, b)) return 0;
1710Sstevel@tonic-gate if (r->neg)
1720Sstevel@tonic-gate return BN_add(r, r, m);
1730Sstevel@tonic-gate return 1;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /* slow but works */
BN_mod_mul(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)1780Sstevel@tonic-gate int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
1790Sstevel@tonic-gate BN_CTX *ctx)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate BIGNUM *t;
1820Sstevel@tonic-gate int ret=0;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate bn_check_top(a);
1850Sstevel@tonic-gate bn_check_top(b);
1860Sstevel@tonic-gate bn_check_top(m);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate BN_CTX_start(ctx);
1890Sstevel@tonic-gate if ((t = BN_CTX_get(ctx)) == NULL) goto err;
1900Sstevel@tonic-gate if (a == b)
1910Sstevel@tonic-gate { if (!BN_sqr(t,a,ctx)) goto err; }
1920Sstevel@tonic-gate else
1930Sstevel@tonic-gate { if (!BN_mul(t,a,b,ctx)) goto err; }
1940Sstevel@tonic-gate if (!BN_nnmod(r,t,m,ctx)) goto err;
195*2139Sjp161948 bn_check_top(r);
1960Sstevel@tonic-gate ret=1;
1970Sstevel@tonic-gate err:
1980Sstevel@tonic-gate BN_CTX_end(ctx);
1990Sstevel@tonic-gate return(ret);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate
BN_mod_sqr(BIGNUM * r,const BIGNUM * a,const BIGNUM * m,BN_CTX * ctx)2030Sstevel@tonic-gate int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate if (!BN_sqr(r, a, ctx)) return 0;
2060Sstevel@tonic-gate /* r->neg == 0, thus we don't need BN_nnmod */
2070Sstevel@tonic-gate return BN_mod(r, r, m, ctx);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate
BN_mod_lshift1(BIGNUM * r,const BIGNUM * a,const BIGNUM * m,BN_CTX * ctx)2110Sstevel@tonic-gate int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate if (!BN_lshift1(r, a)) return 0;
214*2139Sjp161948 bn_check_top(r);
2150Sstevel@tonic-gate return BN_nnmod(r, r, m, ctx);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /* BN_mod_lshift1 variant that may be used if a is non-negative
2200Sstevel@tonic-gate * and less than m */
BN_mod_lshift1_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * m)2210Sstevel@tonic-gate int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate if (!BN_lshift1(r, a)) return 0;
224*2139Sjp161948 bn_check_top(r);
2250Sstevel@tonic-gate if (BN_cmp(r, m) >= 0)
2260Sstevel@tonic-gate return BN_sub(r, r, m);
2270Sstevel@tonic-gate return 1;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate
BN_mod_lshift(BIGNUM * r,const BIGNUM * a,int n,const BIGNUM * m,BN_CTX * ctx)2310Sstevel@tonic-gate int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate BIGNUM *abs_m = NULL;
2340Sstevel@tonic-gate int ret;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (!BN_nnmod(r, a, m, ctx)) return 0;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (m->neg)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate abs_m = BN_dup(m);
2410Sstevel@tonic-gate if (abs_m == NULL) return 0;
2420Sstevel@tonic-gate abs_m->neg = 0;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
246*2139Sjp161948 bn_check_top(r);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate if (abs_m)
2490Sstevel@tonic-gate BN_free(abs_m);
2500Sstevel@tonic-gate return ret;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /* BN_mod_lshift variant that may be used if a is non-negative
2550Sstevel@tonic-gate * and less than m */
BN_mod_lshift_quick(BIGNUM * r,const BIGNUM * a,int n,const BIGNUM * m)2560Sstevel@tonic-gate int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate if (r != a)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate if (BN_copy(r, a) == NULL) return 0;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate while (n > 0)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate int max_shift;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /* 0 < r < m */
2680Sstevel@tonic-gate max_shift = BN_num_bits(m) - BN_num_bits(r);
2690Sstevel@tonic-gate /* max_shift >= 0 */
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if (max_shift < 0)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);
2740Sstevel@tonic-gate return 0;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate if (max_shift > n)
2780Sstevel@tonic-gate max_shift = n;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (max_shift)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate if (!BN_lshift(r, r, max_shift)) return 0;
2830Sstevel@tonic-gate n -= max_shift;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate else
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate if (!BN_lshift1(r, r)) return 0;
2880Sstevel@tonic-gate --n;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /* BN_num_bits(r) <= BN_num_bits(m) */
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate if (BN_cmp(r, m) >= 0)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate if (!BN_sub(r, r, m)) return 0;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate }
298*2139Sjp161948 bn_check_top(r);
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate return 1;
3010Sstevel@tonic-gate }
302