summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2023-04-01 11:08:43 +0000
committertb <>2023-04-01 11:08:43 +0000
commit987c184e9a8b224c8b67f826017fc9a2e631e5c2 (patch)
treeb440b6132aac99760a59c1ebaa2c5c6c5628a76e /src
parent742754666f2864e0b435338bd43c39eab747e662 (diff)
downloadopenbsd-987c184e9a8b224c8b67f826017fc9a2e631e5c2.tar.gz
openbsd-987c184e9a8b224c8b67f826017fc9a2e631e5c2.tar.bz2
openbsd-987c184e9a8b224c8b67f826017fc9a2e631e5c2.zip
Group the non-constant time gcd functions together
The only consumer of euclid() is BN_gcd(), which, in turn is only used by BN_gcd_nonct(). Group them together rather than having parts of the constant time implementation separate them. This moves two functions to a different place in the file.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c90
1 files changed, 45 insertions, 45 deletions
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 4a79f26c6f..8a399725e5 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gcd.c,v 1.23 2023/03/27 10:25:02 tb Exp $ */ 1/* $OpenBSD: bn_gcd.c,v 1.24 2023/04/01 11:08:43 tb 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 *
@@ -180,6 +180,50 @@ err:
180 return (NULL); 180 return (NULL);
181} 181}
182 182
183int
184BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
185{
186 BIGNUM *a, *b, *t;
187 int ret = 0;
188
189
190 BN_CTX_start(ctx);
191 if ((a = BN_CTX_get(ctx)) == NULL)
192 goto err;
193 if ((b = BN_CTX_get(ctx)) == NULL)
194 goto err;
195
196 if (!bn_copy(a, in_a))
197 goto err;
198 if (!bn_copy(b, in_b))
199 goto err;
200 a->neg = 0;
201 b->neg = 0;
202
203 if (BN_cmp(a, b) < 0) {
204 t = a;
205 a = b;
206 b = t;
207 }
208 t = euclid(a, b);
209 if (t == NULL)
210 goto err;
211
212 if (!bn_copy(r, t))
213 goto err;
214 ret = 1;
215
216err:
217 BN_CTX_end(ctx);
218 return (ret);
219}
220
221int
222BN_gcd_nonct(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
223{
224 return BN_gcd(r, in_a, in_b, ctx);
225}
226
183/* 227/*
184 * BN_gcd_no_branch is a special version of BN_mod_inverse_no_branch. 228 * BN_gcd_no_branch is a special version of BN_mod_inverse_no_branch.
185 * that returns the GCD. 229 * that returns the GCD.
@@ -325,44 +369,6 @@ err:
325} 369}
326 370
327int 371int
328BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
329{
330 BIGNUM *a, *b, *t;
331 int ret = 0;
332
333
334 BN_CTX_start(ctx);
335 if ((a = BN_CTX_get(ctx)) == NULL)
336 goto err;
337 if ((b = BN_CTX_get(ctx)) == NULL)
338 goto err;
339
340 if (!bn_copy(a, in_a))
341 goto err;
342 if (!bn_copy(b, in_b))
343 goto err;
344 a->neg = 0;
345 b->neg = 0;
346
347 if (BN_cmp(a, b) < 0) {
348 t = a;
349 a = b;
350 b = t;
351 }
352 t = euclid(a, b);
353 if (t == NULL)
354 goto err;
355
356 if (!bn_copy(r, t))
357 goto err;
358 ret = 1;
359
360err:
361 BN_CTX_end(ctx);
362 return (ret);
363}
364
365int
366BN_gcd_ct(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) 372BN_gcd_ct(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
367{ 373{
368 if (BN_gcd_no_branch(r, in_a, in_b, ctx) == NULL) 374 if (BN_gcd_no_branch(r, in_a, in_b, ctx) == NULL)
@@ -370,12 +376,6 @@ BN_gcd_ct(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
370 return 1; 376 return 1;
371} 377}
372 378
373int
374BN_gcd_nonct(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
375{
376 return BN_gcd(r, in_a, in_b, ctx);
377}
378
379/* BN_mod_inverse_no_branch is a special version of BN_mod_inverse. 379/* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
380 * It does not contain branches that may leak sensitive information. 380 * It does not contain branches that may leak sensitive information.
381 */ 381 */