summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_add.c
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/lib/libcrypto/bn/bn_add.c
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/lib/libcrypto/bn/bn_add.c')
-rw-r--r--src/lib/libcrypto/bn/bn_add.c159
1 files changed, 158 insertions, 1 deletions
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{