summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2018-07-10 21:52:07 +0000
committertb <>2018-07-10 21:52:07 +0000
commitfb314014bb665ac1760452bb6b491d58b505d4d2 (patch)
treecd4f7b6a8943b2259a548cfab47dadc074846fb4 /src
parent3e9d63e141c20976d81cba82a0f22542653c2d5a (diff)
downloadopenbsd-fb314014bb665ac1760452bb6b491d58b505d4d2.tar.gz
openbsd-fb314014bb665ac1760452bb6b491d58b505d4d2.tar.bz2
openbsd-fb314014bb665ac1760452bb6b491d58b505d4d2.zip
Provide BN_swap_ct(), a constant time function that conditionally swaps
two bignums. It's saner and substantially less ugly than the existing public BN_constantime_swap() function and will be used in forthcoming work on constant time ECC code. From Billy Brumley and his team. Thanks! ok jsing
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h5
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c50
2 files changed, 53 insertions, 2 deletions
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
index c010410cd1..ad9427fddc 100644
--- a/src/lib/libcrypto/bn/bn_lcl.h
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lcl.h,v 1.27 2017/01/25 06:15:44 beck Exp $ */ 1/* $OpenBSD: bn_lcl.h,v 1.28 2018/07/10 21:52:07 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 *
@@ -605,5 +605,8 @@ BIGNUM *BN_mod_inverse_nonct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n,
605 BN_CTX *ctx); 605 BN_CTX *ctx);
606int BN_gcd_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 606int BN_gcd_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
607int BN_gcd_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 607int BN_gcd_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
608
609int BN_swap_ct(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
610
608__END_HIDDEN_DECLS 611__END_HIDDEN_DECLS
609#endif 612#endif
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index c480ae8b9d..610e2447d3 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.40 2018/05/12 17:31:41 jsing Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.41 2018/07/10 21:52:07 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 *
@@ -889,6 +889,54 @@ BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
889#undef BN_CONSTTIME_SWAP 889#undef BN_CONSTTIME_SWAP
890} 890}
891 891
892/*
893 * Constant-time conditional swap of a and b.
894 * a and b are swapped if condition is not 0.
895 * The code assumes that at most one bit of condition is set. XXX add check!
896 * nwords is the number of words to swap.
897 */
898int
899BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
900{
901 BN_ULONG t;
902 int i;
903
904 if (a == b)
905 return 1;
906 if (bn_wexpand(a, nwords) == NULL || bn_wexpand(b, nwords) == NULL)
907 return 0;
908 if (a->top > nwords || b->top > nwords) {
909 BNerror(BN_R_INVALID_LENGTH);
910 return 0;
911 }
912
913 condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
914
915 /* swap top field */
916 t = (a->top ^ b->top) & condition;
917 a->top ^= t;
918 b->top ^= t;
919
920 /* swap neg field */
921 t = (a->neg ^ b->neg) & condition;
922 a->neg ^= t;
923 b->neg ^= t;
924
925 /* swap BN_FLG_CONSTTIME from flag field */
926 t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition;
927 a->flags ^= t;
928 b->flags ^= t;
929
930 /* swap the data */
931 for (i = 0; i < nwords; i++) {
932 t = (a->d[i] ^ b->d[i]) & condition;
933 a->d[i] ^= t;
934 b->d[i] ^= t;
935 }
936
937 return 1;
938}
939
892BN_GENCB * 940BN_GENCB *
893BN_GENCB_new(void) 941BN_GENCB_new(void)
894{ 942{