diff options
| author | jsing <> | 2024-11-08 14:05:43 +0000 |
|---|---|---|
| committer | jsing <> | 2024-11-08 14:05:43 +0000 |
| commit | b2a6eb24975b8c8b656efa38d6970a1a1e8fa548 (patch) | |
| tree | df906355dd5c12f140d12d3dba239a01af129e4c /src | |
| parent | 269c43df8ea33b942e8a43354b9b48fb60ba4ba7 (diff) | |
| download | openbsd-b2a6eb24975b8c8b656efa38d6970a1a1e8fa548.tar.gz openbsd-b2a6eb24975b8c8b656efa38d6970a1a1e8fa548.tar.bz2 openbsd-b2a6eb24975b8c8b656efa38d6970a1a1e8fa548.zip | |
Provide constant time comparison functions for size_t.
These will be used in an upcoming change.
ok beck@ tb@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/crypto_internal.h | 70 |
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 | ||
| 35 | static inline int | ||
| 36 | crypto_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 | ||
| 43 | static inline size_t | ||
| 44 | crypto_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 | ||
| 51 | static inline int | ||
| 52 | crypto_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 | ||
| 59 | static inline size_t | ||
| 60 | crypto_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 | ||
| 67 | static inline int | ||
| 68 | crypto_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 | ||
| 76 | static inline size_t | ||
| 77 | crypto_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 | ||
| 84 | static inline int | ||
| 85 | crypto_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 | ||
| 92 | static inline size_t | ||
| 93 | crypto_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 |
