bn_mul.c revision 1.38
1/* $OpenBSD: bn_mul.c,v 1.38 2023/06/12 16:17:24 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <assert.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/opensslconf.h>
64
65#include "bn_arch.h"
66#include "bn_internal.h"
67#include "bn_local.h"
68
69/*
70 * bn_mul_comba4() computes r[] = a[] * b[] using Comba multiplication
71 * (https://everything2.com/title/Comba+multiplication), where a and b are both
72 * four word arrays, producing an eight word array result.
73 */
74#ifndef HAVE_BN_MUL_COMBA4
75void
76bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
77{
78	BN_ULONG c0, c1, c2;
79
80	bn_mulw_addtw(a[0], b[0],  0,  0,  0, &c2, &c1, &r[0]);
81
82	bn_mulw_addtw(a[0], b[1],  0, c2, c1, &c2, &c1, &c0);
83	bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]);
84
85	bn_mulw_addtw(a[2], b[0],  0, c2, c1, &c2, &c1, &c0);
86	bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0);
87	bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]);
88
89	bn_mulw_addtw(a[0], b[3],  0, c2, c1, &c2, &c1, &c0);
90	bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0);
91	bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0);
92	bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]);
93
94	bn_mulw_addtw(a[3], b[1],  0, c2, c1, &c2, &c1, &c0);
95	bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0);
96	bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &r[4]);
97
98	bn_mulw_addtw(a[2], b[3],  0, c2, c1, &c2, &c1, &c0);
99	bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &r[5]);
100
101	bn_mulw_addtw(a[3], b[3],  0, c2, c1, &c2, &r[7], &r[6]);
102}
103#endif
104
105/*
106 * bn_mul_comba8() computes r[] = a[] * b[] using Comba multiplication
107 * (https://everything2.com/title/Comba+multiplication), where a and b are both
108 * eight word arrays, producing a 16 word array result.
109 */
110#ifndef HAVE_BN_MUL_COMBA8
111void
112bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
113{
114	BN_ULONG c0, c1, c2;
115
116	bn_mulw_addtw(a[0], b[0],  0,  0,  0, &c2, &c1, &r[0]);
117
118	bn_mulw_addtw(a[0], b[1],  0, c2, c1, &c2, &c1, &c0);
119	bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]);
120
121	bn_mulw_addtw(a[2], b[0],  0, c2, c1, &c2, &c1, &c0);
122	bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0);
123	bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]);
124
125	bn_mulw_addtw(a[0], b[3],  0, c2, c1, &c2, &c1, &c0);
126	bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0);
127	bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0);
128	bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]);
129
130	bn_mulw_addtw(a[4], b[0],  0, c2, c1, &c2, &c1, &c0);
131	bn_mulw_addtw(a[3], b[1], c2, c1, c0, &c2, &c1, &c0);
132	bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0);
133	bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &c0);
134	bn_mulw_addtw(a[0], b[4], c2, c1, c0, &c2, &c1, &r[4]);
135
136	bn_mulw_addtw(a[0], b[5],  0, c2, c1, &c2, &c1, &c0);
137	bn_mulw_addtw(a[1], b[4], c2, c1, c0, &c2, &c1, &c0);
138	bn_mulw_addtw(a[2], b[3], c2, c1, c0, &c2, &c1, &c0);
139	bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &c0);
140	bn_mulw_addtw(a[4], b[1], c2, c1, c0, &c2, &c1, &c0);
141	bn_mulw_addtw(a[5], b[0], c2, c1, c0, &c2, &c1, &r[5]);
142
143	bn_mulw_addtw(a[6], b[0],  0, c2, c1, &c2, &c1, &c0);
144	bn_mulw_addtw(a[5], b[1], c2, c1, c0, &c2, &c1, &c0);
145	bn_mulw_addtw(a[4], b[2], c2, c1, c0, &c2, &c1, &c0);
146	bn_mulw_addtw(a[3], b[3], c2, c1, c0, &c2, &c1, &c0);
147	bn_mulw_addtw(a[2], b[4], c2, c1, c0, &c2, &c1, &c0);
148	bn_mulw_addtw(a[1], b[5], c2, c1, c0, &c2, &c1, &c0);
149	bn_mulw_addtw(a[0], b[6], c2, c1, c0, &c2, &c1, &r[6]);
150
151	bn_mulw_addtw(a[0], b[7],  0, c2, c1, &c2, &c1, &c0);
152	bn_mulw_addtw(a[1], b[6], c2, c1, c0, &c2, &c1, &c0);
153	bn_mulw_addtw(a[2], b[5], c2, c1, c0, &c2, &c1, &c0);
154	bn_mulw_addtw(a[3], b[4], c2, c1, c0, &c2, &c1, &c0);
155	bn_mulw_addtw(a[4], b[3], c2, c1, c0, &c2, &c1, &c0);
156	bn_mulw_addtw(a[5], b[2], c2, c1, c0, &c2, &c1, &c0);
157	bn_mulw_addtw(a[6], b[1], c2, c1, c0, &c2, &c1, &c0);
158	bn_mulw_addtw(a[7], b[0], c2, c1, c0, &c2, &c1, &r[7]);
159
160	bn_mulw_addtw(a[7], b[1],  0, c2, c1, &c2, &c1, &c0);
161	bn_mulw_addtw(a[6], b[2], c2, c1, c0, &c2, &c1, &c0);
162	bn_mulw_addtw(a[5], b[3], c2, c1, c0, &c2, &c1, &c0);
163	bn_mulw_addtw(a[4], b[4], c2, c1, c0, &c2, &c1, &c0);
164	bn_mulw_addtw(a[3], b[5], c2, c1, c0, &c2, &c1, &c0);
165	bn_mulw_addtw(a[2], b[6], c2, c1, c0, &c2, &c1, &c0);
166	bn_mulw_addtw(a[1], b[7], c2, c1, c0, &c2, &c1, &r[8]);
167
168	bn_mulw_addtw(a[2], b[7],  0, c2, c1, &c2, &c1, &c0);
169	bn_mulw_addtw(a[3], b[6], c2, c1, c0, &c2, &c1, &c0);
170	bn_mulw_addtw(a[4], b[5], c2, c1, c0, &c2, &c1, &c0);
171	bn_mulw_addtw(a[5], b[4], c2, c1, c0, &c2, &c1, &c0);
172	bn_mulw_addtw(a[6], b[3], c2, c1, c0, &c2, &c1, &c0);
173	bn_mulw_addtw(a[7], b[2], c2, c1, c0, &c2, &c1, &r[9]);
174
175	bn_mulw_addtw(a[7], b[3],  0, c2, c1, &c2, &c1, &c0);
176	bn_mulw_addtw(a[6], b[4], c2, c1, c0, &c2, &c1, &c0);
177	bn_mulw_addtw(a[5], b[5], c2, c1, c0, &c2, &c1, &c0);
178	bn_mulw_addtw(a[4], b[6], c2, c1, c0, &c2, &c1, &c0);
179	bn_mulw_addtw(a[3], b[7], c2, c1, c0, &c2, &c1, &r[10]);
180
181	bn_mulw_addtw(a[4], b[7],  0, c2, c1, &c2, &c1, &c0);
182	bn_mulw_addtw(a[5], b[6], c2, c1, c0, &c2, &c1, &c0);
183	bn_mulw_addtw(a[6], b[5], c2, c1, c0, &c2, &c1, &c0);
184	bn_mulw_addtw(a[7], b[4], c2, c1, c0, &c2, &c1, &r[11]);
185
186	bn_mulw_addtw(a[7], b[5],  0, c2, c1, &c2, &c1, &c0);
187	bn_mulw_addtw(a[6], b[6], c2, c1, c0, &c2, &c1, &c0);
188	bn_mulw_addtw(a[5], b[7], c2, c1, c0, &c2, &c1, &r[12]);
189
190	bn_mulw_addtw(a[6], b[7],  0, c2, c1, &c2, &c1, &c0);
191	bn_mulw_addtw(a[7], b[6], c2, c1, c0, &c2, &c1, &r[13]);
192
193	bn_mulw_addtw(a[7], b[7],  0, c2, c1, &c2, &r[15], &r[14]);
194}
195#endif
196
197/*
198 * bn_mul_words() computes (carry:r[i]) = a[i] * w + carry, where a is an array
199 * of words and w is a single word. This should really be called bn_mulw_words()
200 * since only one input is an array. This is used as a step in the multiplication
201 * of word arrays.
202 */
203#ifndef HAVE_BN_MUL_WORDS
204BN_ULONG
205bn_mul_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w)
206{
207	BN_ULONG carry = 0;
208
209	assert(num >= 0);
210	if (num <= 0)
211		return 0;
212
213	while (num & ~3) {
214		bn_qwmulw_addw(a[3], a[2], a[1], a[0], w, carry, &carry,
215		    &r[3], &r[2], &r[1], &r[0]);
216		a += 4;
217		r += 4;
218		num -= 4;
219	}
220	while (num) {
221		bn_mulw_addw(a[0], w, carry, &carry, &r[0]);
222		a++;
223		r++;
224		num--;
225	}
226	return carry;
227}
228#endif
229
230/*
231 * bn_mul_add_words() computes (carry:r[i]) = a[i] * w + r[i] + carry, where
232 * a is an array of words and w is a single word. This should really be called
233 * bn_mulw_add_words() since only one input is an array. This is used as a step
234 * in the multiplication of word arrays.
235 */
236#ifndef HAVE_BN_MUL_ADD_WORDS
237BN_ULONG
238bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w)
239{
240	BN_ULONG carry = 0;
241
242	assert(num >= 0);
243	if (num <= 0)
244		return 0;
245
246	while (num & ~3) {
247		bn_qwmulw_addqw_addw(a[3], a[2], a[1], a[0], w,
248		    r[3], r[2], r[1], r[0], carry, &carry,
249		    &r[3], &r[2], &r[1], &r[0]);
250		a += 4;
251		r += 4;
252		num -= 4;
253	}
254	while (num) {
255		bn_mulw_addw_addw(a[0], w, r[0], carry, &carry, &r[0]);
256		a++;
257		r++;
258		num--;
259	}
260
261	return carry;
262}
263#endif
264
265void
266bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
267{
268	BN_ULONG *rr;
269
270
271	if (na < nb) {
272		int itmp;
273		BN_ULONG *ltmp;
274
275		itmp = na;
276		na = nb;
277		nb = itmp;
278		ltmp = a;
279		a = b;
280		b = ltmp;
281
282	}
283	rr = &(r[na]);
284	if (nb <= 0) {
285		(void)bn_mul_words(r, a, na, 0);
286		return;
287	} else
288		rr[0] = bn_mul_words(r, a, na, b[0]);
289
290	for (;;) {
291		if (--nb <= 0)
292			return;
293		rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
294		if (--nb <= 0)
295			return;
296		rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
297		if (--nb <= 0)
298			return;
299		rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
300		if (--nb <= 0)
301			return;
302		rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
303		rr += 4;
304		r += 4;
305		b += 4;
306	}
307}
308
309
310#ifndef HAVE_BN_MUL
311int
312bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx)
313{
314	bn_mul_normal(r->d, a->d, a->top, b->d, b->top);
315
316	return 1;
317}
318
319#endif /* HAVE_BN_MUL */
320
321int
322BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
323{
324	BIGNUM *rr;
325	int rn;
326	int ret = 0;
327
328	BN_CTX_start(ctx);
329
330	if (BN_is_zero(a) || BN_is_zero(b)) {
331		BN_zero(r);
332		goto done;
333	}
334
335	rr = r;
336	if (rr == a || rr == b)
337		rr = BN_CTX_get(ctx);
338	if (rr == NULL)
339		goto err;
340
341	rn = a->top + b->top;
342	if (rn < a->top)
343		goto err;
344	if (!bn_wexpand(rr, rn))
345		goto err;
346
347	if (a->top == 4 && b->top == 4) {
348		bn_mul_comba4(rr->d, a->d, b->d);
349	} else if (a->top == 8 && b->top == 8) {
350		bn_mul_comba8(rr->d, a->d, b->d);
351	} else {
352		if (!bn_mul(rr, a, b, rn, ctx))
353			goto err;
354	}
355
356	rr->top = rn;
357	bn_correct_top(rr);
358
359	BN_set_negative(rr, a->neg ^ b->neg);
360
361	if (!bn_copy(r, rr))
362		goto err;
363 done:
364	ret = 1;
365 err:
366	BN_CTX_end(ctx);
367
368	return ret;
369}
370