summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2024-11-08 14:05:43 +0000
committerjsing <>2024-11-08 14:05:43 +0000
commitf8d4ff27774341b0c829214c59e2da5f0225ba1a (patch)
treedf906355dd5c12f140d12d3dba239a01af129e4c /src/lib
parent00706f427c9e58afa96c67c4b88a12253b656f4a (diff)
downloadopenbsd-f8d4ff27774341b0c829214c59e2da5f0225ba1a.tar.gz
openbsd-f8d4ff27774341b0c829214c59e2da5f0225ba1a.tar.bz2
openbsd-f8d4ff27774341b0c829214c59e2da5f0225ba1a.zip
Provide constant time comparison functions for size_t.
These will be used in an upcoming change. ok beck@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/crypto_internal.h70
1 files changed, 69 insertions, 1 deletions
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h
index 2c2e63cc0d..c5de5b7b51 100644
--- a/src/lib/libcrypto/crypto_internal.h
+++ b/src/lib/libcrypto/crypto_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_internal.h,v 1.13 2024/10/17 14:27:57 jsing Exp $ */ 1/* $OpenBSD: crypto_internal.h,v 1.14 2024/11/08 14:05:43 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -29,6 +29,74 @@
29 extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__)) 29 extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__))
30 30
31/* 31/*
32 * Constant time functions for size_t.
33 */
34#ifndef HAVE_CRYPTO_CT_NE_ZERO
35static inline int
36crypto_ct_ne_zero(size_t v)
37{
38 return (v | ~(v - 1)) >> ((sizeof(v) * 8) - 1);
39}
40#endif
41
42#ifndef HAVE_CRYPTO_CT_NE_ZERO_MASK
43static inline size_t
44crypto_ct_ne_zero_mask(size_t v)
45{
46 return 0 - crypto_ct_ne_zero(v);
47}
48#endif
49
50#ifndef HAVE_CRYPTO_CT_EQ_ZERO
51static inline int
52crypto_ct_eq_zero(size_t v)
53{
54 return 1 - crypto_ct_ne_zero(v);
55}
56#endif
57
58#ifndef HAVE_CRYPTO_CT_EQ_ZERO_MASK_U8
59static inline size_t
60crypto_ct_eq_zero_mask(size_t v)
61{
62 return 0 - crypto_ct_eq_zero(v);
63}
64#endif
65
66#ifndef HAVE_CRYPTO_CT_LT
67static inline int
68crypto_ct_lt(size_t a, size_t b)
69{
70 return (((a - b) | (b & ~a)) & (b | ~a)) >>
71 (sizeof(size_t) * 8 - 1);
72}
73#endif
74
75#ifndef HAVE_CRYPTO_CT_LT_MASK
76static inline size_t
77crypto_ct_lt_mask(size_t a, size_t b)
78{
79 return 0 - crypto_ct_lt(a, b);
80}
81#endif
82
83#ifndef HAVE_CRYPTO_CT_GT
84static inline int
85crypto_ct_gt(size_t a, size_t b)
86{
87 return crypto_ct_lt(b, a);
88}
89#endif
90
91#ifndef HAVE_CRYPTO_CT_GT_MASK
92static inline size_t
93crypto_ct_gt_mask(size_t a, size_t b)
94{
95 return 0 - crypto_ct_gt(a, b);
96}
97#endif
98
99/*
32 * Constant time operations for uint8_t. 100 * Constant time operations for uint8_t.
33 */ 101 */
34#ifndef HAVE_CRYPTO_CT_NE_ZERO_U8 102#ifndef HAVE_CRYPTO_CT_NE_ZERO_U8