summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2023-01-23 10:31:03 +0000
committerjsing <>2023-01-23 10:31:03 +0000
commite18a6c33767d9180e59054a8c34c6d6f865c97cb (patch)
tree2c0d2f05d5d07f457de45d8fb57e0308a29ef9d5 /src
parent28b5c32c404672e404306baeb4d86804c51f79c9 (diff)
downloadopenbsd-e18a6c33767d9180e59054a8c34c6d6f865c97cb.tar.gz
openbsd-e18a6c33767d9180e59054a8c34c6d6f865c97cb.tar.bz2
openbsd-e18a6c33767d9180e59054a8c34c6d6f865c97cb.zip
Move bn_add_words() and bn_sub_words from bn_asm.c to bn_add.c.
These are wrapped with #ifndef HAVE_BN_ADD_WORDS/HAVE_BN_SUB_WORDS, which are defined for architectures that provide their own assembly versions.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/arch/amd64/bn_arch.h6
-rw-r--r--src/lib/libcrypto/bn/arch/i386/bn_arch.h6
-rw-r--r--src/lib/libcrypto/bn/arch/mips64/bn_arch.h6
-rw-r--r--src/lib/libcrypto/bn/arch/powerpc/bn_arch.h6
-rw-r--r--src/lib/libcrypto/bn/arch/sparc/bn_arch.h6
-rw-r--r--src/lib/libcrypto/bn/bn_add.c159
-rw-r--r--src/lib/libcrypto/bn/bn_asm.c154
7 files changed, 184 insertions, 159 deletions
diff --git a/src/lib/libcrypto/bn/arch/amd64/bn_arch.h b/src/lib/libcrypto/bn/arch/amd64/bn_arch.h
index 7fb4e980e9..fd4a6cde7a 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.3 2023/01/21 17:30:50 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,6 +20,8 @@
20 20
21#ifndef OPENSSL_NO_ASM 21#ifndef OPENSSL_NO_ASM
22 22
23#define HAVE_BN_ADD_WORDS
24
23#define HAVE_BN_MUL_COMBA4 25#define HAVE_BN_MUL_COMBA4
24#define HAVE_BN_MUL_COMBA8 26#define HAVE_BN_MUL_COMBA8
25 27
@@ -27,5 +29,7 @@
27#define HAVE_BN_SQR_COMBA4 29#define HAVE_BN_SQR_COMBA4
28#define HAVE_BN_SQR_COMBA8 30#define HAVE_BN_SQR_COMBA8
29 31
32#define HAVE_BN_SUB_WORDS
33
30#endif 34#endif
31#endif 35#endif
diff --git a/src/lib/libcrypto/bn/arch/i386/bn_arch.h b/src/lib/libcrypto/bn/arch/i386/bn_arch.h
index 17d22f3cec..ab35efd56f 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.2 2023/01/20 17:31:52 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,11 +20,15 @@
20 20
21#ifndef OPENSSL_NO_ASM 21#ifndef OPENSSL_NO_ASM
22 22
23#define HAVE_BN_ADD_WORDS
24
23#define HAVE_BN_MUL_COMBA4 25#define HAVE_BN_MUL_COMBA4
24#define HAVE_BN_MUL_COMBA8 26#define HAVE_BN_MUL_COMBA8
25 27
26#define HAVE_BN_SQR_COMBA4 28#define HAVE_BN_SQR_COMBA4
27#define HAVE_BN_SQR_COMBA8 29#define HAVE_BN_SQR_COMBA8
28 30
31#define HAVE_BN_SUB_WORDS
32
29#endif 33#endif
30#endif 34#endif
diff --git a/src/lib/libcrypto/bn/arch/mips64/bn_arch.h b/src/lib/libcrypto/bn/arch/mips64/bn_arch.h
index 8e8fd1110f..e22d882970 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.3 2023/01/20 17:31:52 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.4 2023/01/23 10:31:03 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,6 +20,8 @@
20 20
21#ifndef OPENSSL_NO_ASM 21#ifndef OPENSSL_NO_ASM
22 22
23#define HAVE_BN_ADD_WORDS
24
23#define HAVE_BN_DIV_3_WORDS 25#define HAVE_BN_DIV_3_WORDS
24 26
25#define HAVE_BN_MUL_COMBA4 27#define HAVE_BN_MUL_COMBA4
@@ -28,5 +30,7 @@
28#define HAVE_BN_SQR_COMBA4 30#define HAVE_BN_SQR_COMBA4
29#define HAVE_BN_SQR_COMBA8 31#define HAVE_BN_SQR_COMBA8
30 32
33#define HAVE_BN_SUB_WORDS
34
31#endif 35#endif
32#endif 36#endif
diff --git a/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h b/src/lib/libcrypto/bn/arch/powerpc/bn_arch.h
index 17d22f3cec..ab35efd56f 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.2 2023/01/20 17:31:52 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,11 +20,15 @@
20 20
21#ifndef OPENSSL_NO_ASM 21#ifndef OPENSSL_NO_ASM
22 22
23#define HAVE_BN_ADD_WORDS
24
23#define HAVE_BN_MUL_COMBA4 25#define HAVE_BN_MUL_COMBA4
24#define HAVE_BN_MUL_COMBA8 26#define HAVE_BN_MUL_COMBA8
25 27
26#define HAVE_BN_SQR_COMBA4 28#define HAVE_BN_SQR_COMBA4
27#define HAVE_BN_SQR_COMBA8 29#define HAVE_BN_SQR_COMBA8
28 30
31#define HAVE_BN_SUB_WORDS
32
29#endif 33#endif
30#endif 34#endif
diff --git a/src/lib/libcrypto/bn/arch/sparc/bn_arch.h b/src/lib/libcrypto/bn/arch/sparc/bn_arch.h
index 17d22f3cec..ab35efd56f 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.2 2023/01/20 17:31:52 jsing Exp $ */ 1/* $OpenBSD: bn_arch.h,v 1.3 2023/01/23 10:31:03 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -20,11 +20,15 @@
20 20
21#ifndef OPENSSL_NO_ASM 21#ifndef OPENSSL_NO_ASM
22 22
23#define HAVE_BN_ADD_WORDS
24
23#define HAVE_BN_MUL_COMBA4 25#define HAVE_BN_MUL_COMBA4
24#define HAVE_BN_MUL_COMBA8 26#define HAVE_BN_MUL_COMBA8
25 27
26#define HAVE_BN_SQR_COMBA4 28#define HAVE_BN_SQR_COMBA4
27#define HAVE_BN_SQR_COMBA8 29#define HAVE_BN_SQR_COMBA8
28 30
31#define HAVE_BN_SUB_WORDS
32
29#endif 33#endif
30#endif 34#endif
diff --git a/src/lib/libcrypto/bn/bn_add.c b/src/lib/libcrypto/bn/bn_add.c
index 3352e0e1d5..b3fed564e9 100644
--- a/src/lib/libcrypto/bn/bn_add.c
+++ b/src/lib/libcrypto/bn/bn_add.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_add.c,v 1.17 2023/01/20 04:49:48 jsing Exp $ */ 1/* $OpenBSD: bn_add.c,v 1.18 2023/01/23 10:31:03 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,12 +56,169 @@
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/err.h> 62#include <openssl/err.h>
62 63
63#include "bn_local.h" 64#include "bn_local.h"
64 65
66#ifndef HAVE_BN_ADD_WORDS
67#ifdef BN_LLONG
68BN_ULONG
69bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
70{
71 BN_ULLONG ll = 0;
72
73 assert(n >= 0);
74 if (n <= 0)
75 return ((BN_ULONG)0);
76
77#ifndef OPENSSL_SMALL_FOOTPRINT
78 while (n & ~3) {
79 ll += (BN_ULLONG)a[0] + b[0];
80 r[0] = (BN_ULONG)ll & BN_MASK2;
81 ll >>= BN_BITS2;
82 ll += (BN_ULLONG)a[1] + b[1];
83 r[1] = (BN_ULONG)ll & BN_MASK2;
84 ll >>= BN_BITS2;
85 ll += (BN_ULLONG)a[2] + b[2];
86 r[2] = (BN_ULONG)ll & BN_MASK2;
87 ll >>= BN_BITS2;
88 ll += (BN_ULLONG)a[3] + b[3];
89 r[3] = (BN_ULONG)ll & BN_MASK2;
90 ll >>= BN_BITS2;
91 a += 4;
92 b += 4;
93 r += 4;
94 n -= 4;
95 }
96#endif
97 while (n) {
98 ll += (BN_ULLONG)a[0] + b[0];
99 r[0] = (BN_ULONG)ll & BN_MASK2;
100 ll >>= BN_BITS2;
101 a++;
102 b++;
103 r++;
104 n--;
105 }
106 return ((BN_ULONG)ll);
107}
108#else /* !BN_LLONG */
109BN_ULONG
110bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
111{
112 BN_ULONG c, l, t;
113
114 assert(n >= 0);
115 if (n <= 0)
116 return ((BN_ULONG)0);
117
118 c = 0;
119#ifndef OPENSSL_SMALL_FOOTPRINT
120 while (n & ~3) {
121 t = a[0];
122 t = (t + c) & BN_MASK2;
123 c = (t < c);
124 l = (t + b[0]) & BN_MASK2;
125 c += (l < t);
126 r[0] = l;
127 t = a[1];
128 t = (t + c) & BN_MASK2;
129 c = (t < c);
130 l = (t + b[1]) & BN_MASK2;
131 c += (l < t);
132 r[1] = l;
133 t = a[2];
134 t = (t + c) & BN_MASK2;
135 c = (t < c);
136 l = (t + b[2]) & BN_MASK2;
137 c += (l < t);
138 r[2] = l;
139 t = a[3];
140 t = (t + c) & BN_MASK2;
141 c = (t < c);
142 l = (t + b[3]) & BN_MASK2;
143 c += (l < t);
144 r[3] = l;
145 a += 4;
146 b += 4;
147 r += 4;
148 n -= 4;
149 }
150#endif
151 while (n) {
152 t = a[0];
153 t = (t + c) & BN_MASK2;
154 c = (t < c);
155 l = (t + b[0]) & BN_MASK2;
156 c += (l < t);
157 r[0] = l;
158 a++;
159 b++;
160 r++;
161 n--;
162 }
163 return ((BN_ULONG)c);
164}
165#endif /* !BN_LLONG */
166#endif
167
168#ifndef HAVE_BN_SUB_WORDS
169BN_ULONG
170bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
171{
172 BN_ULONG t1, t2;
173 int c = 0;
174
175 assert(n >= 0);
176 if (n <= 0)
177 return ((BN_ULONG)0);
178
179#ifndef OPENSSL_SMALL_FOOTPRINT
180 while (n&~3) {
181 t1 = a[0];
182 t2 = b[0];
183 r[0] = (t1 - t2 - c) & BN_MASK2;
184 if (t1 != t2)
185 c = (t1 < t2);
186 t1 = a[1];
187 t2 = b[1];
188 r[1] = (t1 - t2 - c) & BN_MASK2;
189 if (t1 != t2)
190 c = (t1 < t2);
191 t1 = a[2];
192 t2 = b[2];
193 r[2] = (t1 - t2 - c) & BN_MASK2;
194 if (t1 != t2)
195 c = (t1 < t2);
196 t1 = a[3];
197 t2 = b[3];
198 r[3] = (t1 - t2 - c) & BN_MASK2;
199 if (t1 != t2)
200 c = (t1 < t2);
201 a += 4;
202 b += 4;
203 r += 4;
204 n -= 4;
205 }
206#endif
207 while (n) {
208 t1 = a[0];
209 t2 = b[0];
210 r[0] = (t1 - t2 - c) & BN_MASK2;
211 if (t1 != t2)
212 c = (t1 < t2);
213 a++;
214 b++;
215 r++;
216 n--;
217 }
218 return (c);
219}
220#endif
221
65int 222int
66BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 223BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
67{ 224{
diff --git a/src/lib/libcrypto/bn/bn_asm.c b/src/lib/libcrypto/bn/bn_asm.c
index df4ddaea17..4224396c00 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.19 2023/01/20 17:31:52 jsing Exp $ */ 1/* $OpenBSD: bn_asm.c,v 1.20 2023/01/23 10:31:03 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 *
@@ -325,158 +325,6 @@ bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
325} 325}
326#endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */ 326#endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
327 327
328#ifdef BN_LLONG
329BN_ULONG
330bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
331{
332 BN_ULLONG ll = 0;
333
334 assert(n >= 0);
335 if (n <= 0)
336 return ((BN_ULONG)0);
337
338#ifndef OPENSSL_SMALL_FOOTPRINT
339 while (n & ~3) {
340 ll += (BN_ULLONG)a[0] + b[0];
341 r[0] = (BN_ULONG)ll & BN_MASK2;
342 ll >>= BN_BITS2;
343 ll += (BN_ULLONG)a[1] + b[1];
344 r[1] = (BN_ULONG)ll & BN_MASK2;
345 ll >>= BN_BITS2;
346 ll += (BN_ULLONG)a[2] + b[2];
347 r[2] = (BN_ULONG)ll & BN_MASK2;
348 ll >>= BN_BITS2;
349 ll += (BN_ULLONG)a[3] + b[3];
350 r[3] = (BN_ULONG)ll & BN_MASK2;
351 ll >>= BN_BITS2;
352 a += 4;
353 b += 4;
354 r += 4;
355 n -= 4;
356 }
357#endif
358 while (n) {
359 ll += (BN_ULLONG)a[0] + b[0];
360 r[0] = (BN_ULONG)ll & BN_MASK2;
361 ll >>= BN_BITS2;
362 a++;
363 b++;
364 r++;
365 n--;
366 }
367 return ((BN_ULONG)ll);
368}
369#else /* !BN_LLONG */
370BN_ULONG
371bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
372{
373 BN_ULONG c, l, t;
374
375 assert(n >= 0);
376 if (n <= 0)
377 return ((BN_ULONG)0);
378
379 c = 0;
380#ifndef OPENSSL_SMALL_FOOTPRINT
381 while (n & ~3) {
382 t = a[0];
383 t = (t + c) & BN_MASK2;
384 c = (t < c);
385 l = (t + b[0]) & BN_MASK2;
386 c += (l < t);
387 r[0] = l;
388 t = a[1];
389 t = (t + c) & BN_MASK2;
390 c = (t < c);
391 l = (t + b[1]) & BN_MASK2;
392 c += (l < t);
393 r[1] = l;
394 t = a[2];
395 t = (t + c) & BN_MASK2;
396 c = (t < c);
397 l = (t + b[2]) & BN_MASK2;
398 c += (l < t);
399 r[2] = l;
400 t = a[3];
401 t = (t + c) & BN_MASK2;
402 c = (t < c);
403 l = (t + b[3]) & BN_MASK2;
404 c += (l < t);
405 r[3] = l;
406 a += 4;
407 b += 4;
408 r += 4;
409 n -= 4;
410 }
411#endif
412 while (n) {
413 t = a[0];
414 t = (t + c) & BN_MASK2;
415 c = (t < c);
416 l = (t + b[0]) & BN_MASK2;
417 c += (l < t);
418 r[0] = l;
419 a++;
420 b++;
421 r++;
422 n--;
423 }
424 return ((BN_ULONG)c);
425}
426#endif /* !BN_LLONG */
427
428BN_ULONG
429bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
430{
431 BN_ULONG t1, t2;
432 int c = 0;
433
434 assert(n >= 0);
435 if (n <= 0)
436 return ((BN_ULONG)0);
437
438#ifndef OPENSSL_SMALL_FOOTPRINT
439 while (n&~3) {
440 t1 = a[0];
441 t2 = b[0];
442 r[0] = (t1 - t2 - c) & BN_MASK2;
443 if (t1 != t2)
444 c = (t1 < t2);
445 t1 = a[1];
446 t2 = b[1];
447 r[1] = (t1 - t2 - c) & BN_MASK2;
448 if (t1 != t2)
449 c = (t1 < t2);
450 t1 = a[2];
451 t2 = b[2];
452 r[2] = (t1 - t2 - c) & BN_MASK2;
453 if (t1 != t2)
454 c = (t1 < t2);
455 t1 = a[3];
456 t2 = b[3];
457 r[3] = (t1 - t2 - c) & BN_MASK2;
458 if (t1 != t2)
459 c = (t1 < t2);
460 a += 4;
461 b += 4;
462 r += 4;
463 n -= 4;
464 }
465#endif
466 while (n) {
467 t1 = a[0];
468 t2 = b[0];
469 r[0] = (t1 - t2 - c) & BN_MASK2;
470 if (t1 != t2)
471 c = (t1 < t2);
472 a++;
473 b++;
474 r++;
475 n--;
476 }
477 return (c);
478}
479
480#if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT) 328#if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
481 329
482#ifdef OPENSSL_NO_ASM 330#ifdef OPENSSL_NO_ASM