summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2024-11-08 14:05:44 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2024-11-08 14:05:44 +0000
commite307e7cbf6632ecf160c93f620ec4d19efa70216 (patch)
treed0447492a7c9579ab4ef8148af4ead77e8a1af2d
parent247f864c9133505d3978547b1c1fe549b06c0bf4 (diff)
Provide constant time comparison functions for size_t.
These will be used in an upcoming change. ok beck@ tb@
-rw-r--r--lib/libcrypto/crypto_internal.h70
1 files changed, 69 insertions, 1 deletions
diff --git a/lib/libcrypto/crypto_internal.h b/lib/libcrypto/crypto_internal.h
index 2c2e63cc0d2..c5de5b7b517 100644
--- a/lib/libcrypto/crypto_internal.h
+++ b/lib/libcrypto/crypto_internal.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: crypto_internal.h,v 1.13 2024/10/17 14:27:57 jsing Exp $ */
+/* $OpenBSD: crypto_internal.h,v 1.14 2024/11/08 14:05:43 jsing Exp $ */
/*
* Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
*
@@ -29,6 +29,74 @@
extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__))
/*
+ * Constant time functions for size_t.
+ */
+#ifndef HAVE_CRYPTO_CT_NE_ZERO
+static inline int
+crypto_ct_ne_zero(size_t v)
+{
+ return (v | ~(v - 1)) >> ((sizeof(v) * 8) - 1);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_NE_ZERO_MASK
+static inline size_t
+crypto_ct_ne_zero_mask(size_t v)
+{
+ return 0 - crypto_ct_ne_zero(v);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_EQ_ZERO
+static inline int
+crypto_ct_eq_zero(size_t v)
+{
+ return 1 - crypto_ct_ne_zero(v);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_EQ_ZERO_MASK_U8
+static inline size_t
+crypto_ct_eq_zero_mask(size_t v)
+{
+ return 0 - crypto_ct_eq_zero(v);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_LT
+static inline int
+crypto_ct_lt(size_t a, size_t b)
+{
+ return (((a - b) | (b & ~a)) & (b | ~a)) >>
+ (sizeof(size_t) * 8 - 1);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_LT_MASK
+static inline size_t
+crypto_ct_lt_mask(size_t a, size_t b)
+{
+ return 0 - crypto_ct_lt(a, b);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_GT
+static inline int
+crypto_ct_gt(size_t a, size_t b)
+{
+ return crypto_ct_lt(b, a);
+}
+#endif
+
+#ifndef HAVE_CRYPTO_CT_GT_MASK
+static inline size_t
+crypto_ct_gt_mask(size_t a, size_t b)
+{
+ return 0 - crypto_ct_gt(a, b);
+}
+#endif
+
+/*
* Constant time operations for uint8_t.
*/
#ifndef HAVE_CRYPTO_CT_NE_ZERO_U8