xref: /openbsd/lib/libcrypto/bn/bn_word.c (revision ca1d80d6)
1*ca1d80d6Sbeck /* $OpenBSD: bn_word.c,v 1.21 2023/07/08 12:21:58 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
595b37fcf3Sryker #include <stdio.h>
60b6ab114eSjsing 
61c9675a23Stb #include "bn_local.h"
625b37fcf3Sryker 
632bd9bb84Sjsing BN_ULONG
BN_mod_word(const BIGNUM * a,BN_ULONG w)642bd9bb84Sjsing BN_mod_word(const BIGNUM *a, BN_ULONG w)
655b37fcf3Sryker {
665b37fcf3Sryker #ifndef BN_LLONG
675b37fcf3Sryker 	BN_ULONG ret = 0;
685b37fcf3Sryker #else
695b37fcf3Sryker 	BN_ULLONG ret = 0;
705b37fcf3Sryker #endif
715b37fcf3Sryker 	int i;
725b37fcf3Sryker 
734fcf65c5Sdjm 	if (w == 0)
744fcf65c5Sdjm 		return (BN_ULONG) - 1;
754fcf65c5Sdjm 
76c17ab57aSbcook #ifndef BN_ULLONG
77c17ab57aSbcook 	/* If |w| is too long and we don't have |BN_ULLONG| then we need to fall back
78c17ab57aSbcook 	* to using |BN_div_word|. */
79c17ab57aSbcook 	if (w > ((BN_ULONG)1 << BN_BITS4)) {
80c17ab57aSbcook 		BIGNUM *tmp = BN_dup(a);
81c17ab57aSbcook 		if (tmp == NULL) {
82c17ab57aSbcook 			return (BN_ULONG)-1;
83c17ab57aSbcook 		}
84c17ab57aSbcook 		ret = BN_div_word(tmp, w);
85c17ab57aSbcook 		BN_free(tmp);
86c17ab57aSbcook 		return ret;
87c17ab57aSbcook 	}
88c17ab57aSbcook #endif
89c17ab57aSbcook 
905b37fcf3Sryker 	w &= BN_MASK2;
912bd9bb84Sjsing 	for (i = a->top - 1; i >= 0; i--) {
925b37fcf3Sryker #ifndef BN_LLONG
932bd9bb84Sjsing 		ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) &
942bd9bb84Sjsing 		    BN_MASK2l)) % w;
95913ec974Sbeck 		ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
965b37fcf3Sryker #else
972bd9bb84Sjsing 		ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) |
982bd9bb84Sjsing 		    a->d[i]) % (BN_ULLONG)w);
995b37fcf3Sryker #endif
1005b37fcf3Sryker 	}
1015b37fcf3Sryker 	return ((BN_ULONG)ret);
1025b37fcf3Sryker }
103*ca1d80d6Sbeck LCRYPTO_ALIAS(BN_mod_word);
1045b37fcf3Sryker 
1052bd9bb84Sjsing BN_ULONG
BN_div_word(BIGNUM * a,BN_ULONG w)1062bd9bb84Sjsing BN_div_word(BIGNUM *a, BN_ULONG w)
1075b37fcf3Sryker {
1084fcf65c5Sdjm 	BN_ULONG ret = 0;
1094fcf65c5Sdjm 	int i, j;
1105b37fcf3Sryker 
1115b37fcf3Sryker 	w &= BN_MASK2;
1124fcf65c5Sdjm 
1134fcf65c5Sdjm 	if (!w)
1144fcf65c5Sdjm 		/* actually this an error (division by zero) */
1154fcf65c5Sdjm 		return (BN_ULONG) - 1;
1164fcf65c5Sdjm 	if (a->top == 0)
1174fcf65c5Sdjm 		return 0;
1184fcf65c5Sdjm 
1194fcf65c5Sdjm 	/* normalize input (so bn_div_words doesn't complain) */
1204fcf65c5Sdjm 	j = BN_BITS2 - BN_num_bits_word(w);
1214fcf65c5Sdjm 	w <<= j;
1224fcf65c5Sdjm 	if (!BN_lshift(a, a, j))
1234fcf65c5Sdjm 		return (BN_ULONG) - 1;
1244fcf65c5Sdjm 
1252bd9bb84Sjsing 	for (i = a->top - 1; i >= 0; i--) {
1265b37fcf3Sryker 		BN_ULONG l, d;
1275b37fcf3Sryker 
1285b37fcf3Sryker 		l = a->d[i];
129519d53cbSjsing 		bn_div_rem_words(ret, l, w, &d, &ret);
1305b37fcf3Sryker 		a->d[i] = d;
1315b37fcf3Sryker 	}
132913ec974Sbeck 	if ((a->top > 0) && (a->d[a->top - 1] == 0))
1335b37fcf3Sryker 		a->top--;
1344fcf65c5Sdjm 	ret >>= j;
135b5c6728dSjsing 
136b5c6728dSjsing 	/* Set negative again, to handle -0 case. */
137b5c6728dSjsing 	BN_set_negative(a, a->neg);
138b5c6728dSjsing 
1395b37fcf3Sryker 	return (ret);
1405b37fcf3Sryker }
141*ca1d80d6Sbeck LCRYPTO_ALIAS(BN_div_word);
1425b37fcf3Sryker 
1432bd9bb84Sjsing int
BN_add_word(BIGNUM * a,BN_ULONG w)1442bd9bb84Sjsing BN_add_word(BIGNUM *a, BN_ULONG w)
1455b37fcf3Sryker {
1465b37fcf3Sryker 	BN_ULONG l;
1475b37fcf3Sryker 	int i;
1485b37fcf3Sryker 
1494fcf65c5Sdjm 	w &= BN_MASK2;
150370d1fc7Sotto 
1514fcf65c5Sdjm 	/* degenerate case: w is zero */
1522bd9bb84Sjsing 	if (!w)
1532bd9bb84Sjsing 		return 1;
1544fcf65c5Sdjm 	/* degenerate case: a is zero */
1552bd9bb84Sjsing 	if (BN_is_zero(a))
1562bd9bb84Sjsing 		return BN_set_word(a, w);
1574fcf65c5Sdjm 	/* handle 'a' when negative */
1582bd9bb84Sjsing 	if (a->neg) {
1595b37fcf3Sryker 		a->neg = 0;
1605b37fcf3Sryker 		i = BN_sub_word(a, w);
1612e57f206Sjsing 		BN_set_negative(a, !a->neg);
1625b37fcf3Sryker 		return (i);
1635b37fcf3Sryker 	}
1642bd9bb84Sjsing 	for (i = 0; w != 0 && i < a->top; i++) {
165c5957159Smarkus 		a->d[i] = l = (a->d[i] + w) & BN_MASK2;
166c5957159Smarkus 		w = (w > l) ? 1 : 0;
1675b37fcf3Sryker 	}
1682bd9bb84Sjsing 	if (w && i == a->top) {
169e729bd5aSjsing 		if (!bn_wexpand(a, a->top + 1))
1702bd9bb84Sjsing 			return 0;
1715b37fcf3Sryker 		a->top++;
172c5957159Smarkus 		a->d[i] = w;
173c5957159Smarkus 	}
1745b37fcf3Sryker 	return (1);
1755b37fcf3Sryker }
176*ca1d80d6Sbeck LCRYPTO_ALIAS(BN_add_word);
1775b37fcf3Sryker 
1782bd9bb84Sjsing int
BN_sub_word(BIGNUM * a,BN_ULONG w)1792bd9bb84Sjsing BN_sub_word(BIGNUM *a, BN_ULONG w)
1805b37fcf3Sryker {
1815b37fcf3Sryker 	int i;
1825b37fcf3Sryker 
1834fcf65c5Sdjm 	w &= BN_MASK2;
1843b996182Sotto 
1854fcf65c5Sdjm 	/* degenerate case: w is zero */
1862bd9bb84Sjsing 	if (!w)
1872bd9bb84Sjsing 		return 1;
1884fcf65c5Sdjm 	/* degenerate case: a is zero */
1892bd9bb84Sjsing 	if (BN_is_zero(a)) {
1904fcf65c5Sdjm 		i = BN_set_word(a, w);
1914fcf65c5Sdjm 		if (i != 0)
1924fcf65c5Sdjm 			BN_set_negative(a, 1);
1934fcf65c5Sdjm 		return i;
1944fcf65c5Sdjm 	}
1954fcf65c5Sdjm 	/* handle 'a' when negative */
1962bd9bb84Sjsing 	if (a->neg) {
1975b37fcf3Sryker 		a->neg = 0;
1985b37fcf3Sryker 		i = BN_add_word(a, w);
1992e57f206Sjsing 		BN_set_negative(a, !a->neg);
2005b37fcf3Sryker 		return (i);
2015b37fcf3Sryker 	}
2025b37fcf3Sryker 
2032bd9bb84Sjsing 	if ((a->top == 1) && (a->d[0] < w)) {
2045b37fcf3Sryker 		a->d[0] = w - a->d[0];
205896da13fSjsing 		BN_set_negative(a, 1);
2065b37fcf3Sryker 		return (1);
2075b37fcf3Sryker 	}
2085b37fcf3Sryker 	i = 0;
2092bd9bb84Sjsing 	for (;;) {
2102bd9bb84Sjsing 		if (a->d[i] >= w) {
2115b37fcf3Sryker 			a->d[i] -= w;
2125b37fcf3Sryker 			break;
2132bd9bb84Sjsing 		} else {
2145b37fcf3Sryker 			a->d[i] = (a->d[i] - w) & BN_MASK2;
2155b37fcf3Sryker 			i++;
2165b37fcf3Sryker 			w = 1;
2175b37fcf3Sryker 		}
2185b37fcf3Sryker 	}
2195b37fcf3Sryker 	if ((a->d[i] == 0) && (i == (a->top - 1)))
2205b37fcf3Sryker 		a->top--;
2215b37fcf3Sryker 	return (1);
2225b37fcf3Sryker }
223*ca1d80d6Sbeck LCRYPTO_ALIAS(BN_sub_word);
2245b37fcf3Sryker 
2252bd9bb84Sjsing int
BN_mul_word(BIGNUM * a,BN_ULONG w)2262bd9bb84Sjsing BN_mul_word(BIGNUM *a, BN_ULONG w)
2275b37fcf3Sryker {
2285b37fcf3Sryker 	BN_ULONG ll;
2295b37fcf3Sryker 
2305b37fcf3Sryker 	w &= BN_MASK2;
2312bd9bb84Sjsing 	if (a->top) {
232c109e398Sbeck 		if (w == 0)
233c109e398Sbeck 			BN_zero(a);
2342bd9bb84Sjsing 		else {
2355b37fcf3Sryker 			ll = bn_mul_words(a->d, a->d, a->top, w);
2362bd9bb84Sjsing 			if (ll) {
237e729bd5aSjsing 				if (!bn_wexpand(a, a->top + 1))
2382bd9bb84Sjsing 					return (0);
2395b37fcf3Sryker 				a->d[a->top++] = ll;
2405b37fcf3Sryker 			}
2415b37fcf3Sryker 		}
242c109e398Sbeck 	}
243913ec974Sbeck 	return (1);
2445b37fcf3Sryker }
245*ca1d80d6Sbeck LCRYPTO_ALIAS(BN_mul_word);
246