summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.h4
-rw-r--r--src/lib/libcrypto/bn/arch/i386/bn_arch.h4
-rw-r--r--src/lib/libcrypto/bn/arch/mips64/bn_arch.h3
-rw-r--r--src/lib/libcrypto/bn/arch/powerpc/bn_arch.h4
-rw-r--r--src/lib/libcrypto/bn/arch/sparc/bn_arch.h4
-rw-r--r--src/lib/libcrypto/bn/bn_asm.c81
-rw-r--r--src/lib/libcrypto/bn/bn_div.c84
7 files changed, 98 insertions, 86 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
index fd4a6cde7a..2d11fbd7c2 100644
--- a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.5 2023/01/23 12:02:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,8 @@
22 22
23#define HAVE_BN_ADD_WORDS 23#define HAVE_BN_ADD_WORDS
24 24
25#define HAVE_BN_DIV_WORDS
26
25#define HAVE_BN_MUL_COMBA4 27#define HAVE_BN_MUL_COMBA4
26#define HAVE_BN_MUL_COMBA8 28#define HAVE_BN_MUL_COMBA8
27 29
diff --git a/src/lib/libcrypto/bn/arch/i386/bn_arch.h b/src/lib/libcrypto/bn/arch/i386/bn_arch.h
index ab35efd56f..18d7e519ec 100644
--- a/src/lib/libcrypto/bn/arch/i386/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/i386/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 12:02:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,8 @@
22 22
23#define HAVE_BN_ADD_WORDS 23#define HAVE_BN_ADD_WORDS
24 24
25#define HAVE_BN_DIV_WORDS
26
25#define HAVE_BN_MUL_COMBA4 27#define HAVE_BN_MUL_COMBA4
26#define HAVE_BN_MUL_COMBA8 28#define HAVE_BN_MUL_COMBA8
27 29
diff --git a/src/lib/libcrypto/bn/arch/mips64/bn_arch.h b/src/lib/libcrypto/bn/arch/mips64/bn_arch.h
index e22d882970..f53c75472c 100644
--- a/src/lib/libcrypto/bn/arch/mips64/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/mips64/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.5 2023/01/23 12:02:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,7 @@
22 22
23#define HAVE_BN_ADD_WORDS 23#define HAVE_BN_ADD_WORDS
24 24
25#define HAVE_BN_DIV_WORDS
25#define HAVE_BN_DIV_3_WORDS 26#define HAVE_BN_DIV_3_WORDS
26 27
27#define HAVE_BN_MUL_COMBA4 28#define HAVE_BN_MUL_COMBA4
diff --git a/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h b/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h
index ab35efd56f..18d7e519ec 100644
--- a/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 12:02:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,8 @@
22 22
23#define HAVE_BN_ADD_WORDS 23#define HAVE_BN_ADD_WORDS
24 24
25#define HAVE_BN_DIV_WORDS
26
25#define HAVE_BN_MUL_COMBA4 27#define HAVE_BN_MUL_COMBA4
26#define HAVE_BN_MUL_COMBA8 28#define HAVE_BN_MUL_COMBA8
27 29
diff --git a/src/lib/libcrypto/bn/arch/sparc/bn_arch.h b/src/lib/libcrypto/bn/arch/sparc/bn_arch.h
index ab35efd56f..18d7e519ec 100644
--- a/src/lib/libcrypto/bn/arch/sparc/bn_arch.h
+++ b/src/lib/libcrypto/bn/arch/sparc/bn_arch.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 12:02:48 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,6 +22,8 @@
22 22
23#define HAVE_BN_ADD_WORDS 23#define HAVE_BN_ADD_WORDS
24 24
25#define HAVE_BN_DIV_WORDS
26
25#define HAVE_BN_MUL_COMBA4 27#define HAVE_BN_MUL_COMBA4
26#define HAVE_BN_MUL_COMBA8 28#define HAVE_BN_MUL_COMBA8
27 29
diff --git a/src/lib/libcrypto/bn/bn_asm.c b/src/lib/libcrypto/bn/bn_asm.c
index 4224396c00..a7f288fc1c 100644
--- a/src/lib/libcrypto/bn/bn_asm.c
+++ b/src/lib/libcrypto/bn/bn_asm.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_asm.c,v 1.20 2023/01/23 10:31:03 jsing Exp $ */ 1/* $OpenBSD: bn_asm.c,v 1.21 2023/01/23 12:02:48 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -246,85 +246,6 @@ bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
246 246
247#endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */ 247#endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
248 248
249#if defined(BN_LLONG) && defined(BN_DIV2W)
250
251BN_ULONG
252bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
253{
254 return ((BN_ULONG)(((((BN_ULLONG)h) << BN_BITS2)|l)/(BN_ULLONG)d));
255}
256
257#else
258
259/* Divide h,l by d and return the result. */
260/* I need to test this some more :-( */
261BN_ULONG
262bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
263{
264 BN_ULONG dh, dl, q,ret = 0, th, tl, t;
265 int i, count = 2;
266
267 if (d == 0)
268 return (BN_MASK2);
269
270 i = BN_num_bits_word(d);
271 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
272
273 i = BN_BITS2 - i;
274 if (h >= d)
275 h -= d;
276
277 if (i) {
278 d <<= i;
279 h = (h << i) | (l >> (BN_BITS2 - i));
280 l <<= i;
281 }
282 dh = (d & BN_MASK2h) >> BN_BITS4;
283 dl = (d & BN_MASK2l);
284 for (;;) {
285 if ((h >> BN_BITS4) == dh)
286 q = BN_MASK2l;
287 else
288 q = h / dh;
289
290 th = q * dh;
291 tl = dl * q;
292 for (;;) {
293 t = h - th;
294 if ((t & BN_MASK2h) ||
295 ((tl) <= (
296 (t << BN_BITS4) |
297 ((l & BN_MASK2h) >> BN_BITS4))))
298 break;
299 q--;
300 th -= dh;
301 tl -= dl;
302 }
303 t = (tl >> BN_BITS4);
304 tl = (tl << BN_BITS4) & BN_MASK2h;
305 th += t;
306
307 if (l < tl)
308 th++;
309 l -= tl;
310 if (h < th) {
311 h += d;
312 q--;
313 }
314 h -= th;
315
316 if (--count == 0)
317 break;
318
319 ret = q << BN_BITS4;
320 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
321 l = (l & BN_MASK2l) << BN_BITS4;
322 }
323 ret |= q;
324 return (ret);
325}
326#endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
327
328#if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT) 249#if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
329 250
330#ifdef OPENSSL_NO_ASM 251#ifdef OPENSSL_NO_ASM
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index df4b7517d7..8ec2e01831 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_div.c,v 1.32 2023/01/20 10:07:52 jsing Exp $ */ 1/* $OpenBSD: bn_div.c,v 1.33 2023/01/23 12:02:48 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -56,6 +56,7 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <assert.h>
59#include <stdio.h> 60#include <stdio.h>
60 61
61#include <openssl/opensslconf.h> 62#include <openssl/opensslconf.h>
@@ -68,6 +69,87 @@
68 69
69BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0); 70BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0);
70 71
72#ifndef HAVE_BN_DIV_WORDS
73#if defined(BN_LLONG) && defined(BN_DIV2W)
74
75BN_ULONG
76bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
77{
78 return ((BN_ULONG)(((((BN_ULLONG)h) << BN_BITS2)|l)/(BN_ULLONG)d));
79}
80
81#else
82
83/* Divide h,l by d and return the result. */
84/* I need to test this some more :-( */
85BN_ULONG
86bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
87{
88 BN_ULONG dh, dl, q,ret = 0, th, tl, t;
89 int i, count = 2;
90
91 if (d == 0)
92 return (BN_MASK2);
93
94 i = BN_num_bits_word(d);
95 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
96
97 i = BN_BITS2 - i;
98 if (h >= d)
99 h -= d;
100
101 if (i) {
102 d <<= i;
103 h = (h << i) | (l >> (BN_BITS2 - i));
104 l <<= i;
105 }
106 dh = (d & BN_MASK2h) >> BN_BITS4;
107 dl = (d & BN_MASK2l);
108 for (;;) {
109 if ((h >> BN_BITS4) == dh)
110 q = BN_MASK2l;
111 else
112 q = h / dh;
113
114 th = q * dh;
115 tl = dl * q;
116 for (;;) {
117 t = h - th;
118 if ((t & BN_MASK2h) ||
119 ((tl) <= (
120 (t << BN_BITS4) |
121 ((l & BN_MASK2h) >> BN_BITS4))))
122 break;
123 q--;
124 th -= dh;
125 tl -= dl;
126 }
127 t = (tl >> BN_BITS4);
128 tl = (tl << BN_BITS4) & BN_MASK2h;
129 th += t;
130
131 if (l < tl)
132 th++;
133 l -= tl;
134 if (h < th) {
135 h += d;
136 q--;
137 }
138 h -= th;
139
140 if (--count == 0)
141 break;
142
143 ret = q << BN_BITS4;
144 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
145 l = (l & BN_MASK2l) << BN_BITS4;
146 }
147 ret |= q;
148 return (ret);
149}
150#endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
151#endif
152
71#ifndef HAVE_BN_DIV_3_WORDS 153#ifndef HAVE_BN_DIV_3_WORDS
72 154
73#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) 155#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)