diff options
| author | jsing <> | 2024-04-17 14:43:37 +0000 |
|---|---|---|
| committer | jsing <> | 2024-04-17 14:43:37 +0000 |
| commit | 9db0c14331bf4bc549caebe67f70dcdeed1108ed (patch) | |
| tree | 3af1426b7749bac9de8968ade9b3ca4fe3d580f4 /src | |
| parent | b917d4558ea4dd3da248be47d1d1c7e70aed6bcc (diff) | |
| download | openbsd-9db0c14331bf4bc549caebe67f70dcdeed1108ed.tar.gz openbsd-9db0c14331bf4bc549caebe67f70dcdeed1108ed.tar.bz2 openbsd-9db0c14331bf4bc549caebe67f70dcdeed1108ed.zip | |
Provide constant time operations for uint8_t.
These will be used in upcoming changes.
ok tb@
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/crypto_internal.h | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h index 924cf6db49..8229db2d68 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.9 2024/03/28 08:36:13 jsing Exp $ */ | 1 | /* $OpenBSD: crypto_internal.h,v 1.10 2024/04/17 14:43:37 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -27,6 +27,73 @@ | |||
| 27 | extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__)) | 27 | extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__)) |
| 28 | 28 | ||
| 29 | /* | 29 | /* |
| 30 | * Constant time operations for uint8_t. | ||
| 31 | */ | ||
| 32 | #ifndef HAVE_CRYPTO_CT_NE_ZERO_U8 | ||
| 33 | static inline int | ||
| 34 | crypto_ct_ne_zero_u8(uint8_t v) | ||
| 35 | { | ||
| 36 | return (uint8_t)(v | ~(v - 1)) >> ((sizeof(v) * 8) - 1); | ||
| 37 | } | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #ifndef HAVE_CRYPTO_CT_NE_ZERO_MASK_U8 | ||
| 41 | static inline uint8_t | ||
| 42 | crypto_ct_ne_zero_mask_u8(uint8_t v) | ||
| 43 | { | ||
| 44 | return 0 - crypto_ct_ne_zero_u8(v); | ||
| 45 | } | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #ifndef HAVE_CRYPTO_CT_EQ_ZERO_U8 | ||
| 49 | static inline int | ||
| 50 | crypto_ct_eq_zero_u8(uint8_t v) | ||
| 51 | { | ||
| 52 | return 1 - crypto_ct_ne_zero_u8(v); | ||
| 53 | } | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #ifndef HAVE_CRYPTO_CT_EQ_ZERO_MASK_U8 | ||
| 57 | static inline uint8_t | ||
| 58 | crypto_ct_eq_zero_mask_u8(uint8_t v) | ||
| 59 | { | ||
| 60 | return 0 - crypto_ct_eq_zero_u8(v); | ||
| 61 | } | ||
| 62 | #endif | ||
| 63 | |||
| 64 | #ifndef HAVE_CRYPTO_CT_NE_U8 | ||
| 65 | static inline int | ||
| 66 | crypto_ct_ne_u8(uint8_t a, uint8_t b) | ||
| 67 | { | ||
| 68 | return crypto_ct_ne_zero_u8(a - b); | ||
| 69 | } | ||
| 70 | #endif | ||
| 71 | |||
| 72 | #ifndef HAVE_CRYPTO_CT_NE_MASK_U8 | ||
| 73 | static inline uint8_t | ||
| 74 | crypto_ct_ne_mask_u8(uint8_t a, uint8_t b) | ||
| 75 | { | ||
| 76 | return 0 - crypto_ct_ne_u8(a, b); | ||
| 77 | } | ||
| 78 | #endif | ||
| 79 | |||
| 80 | #ifndef HAVE_CRYPTO_CT_EQ_U8 | ||
| 81 | static inline int | ||
| 82 | crypto_ct_eq_u8(uint8_t a, uint8_t b) | ||
| 83 | { | ||
| 84 | return crypto_ct_eq_zero_u8(a - b); | ||
| 85 | } | ||
| 86 | #endif | ||
| 87 | |||
| 88 | #ifndef HAVE_CRYPTO_CT_EQ_MASK_U8 | ||
| 89 | static inline uint8_t | ||
| 90 | crypto_ct_eq_mask_u8(uint8_t a, uint8_t b) | ||
| 91 | { | ||
| 92 | return 0 - crypto_ct_eq_u8(a, b); | ||
| 93 | } | ||
| 94 | #endif | ||
| 95 | |||
| 96 | /* | ||
| 30 | * crypto_load_be32toh() loads a 32 bit unsigned big endian value as a 32 bit | 97 | * crypto_load_be32toh() loads a 32 bit unsigned big endian value as a 32 bit |
| 31 | * unsigned host endian value, from the specified address in memory. The memory | 98 | * unsigned host endian value, from the specified address in memory. The memory |
| 32 | * address may have any alignment. | 99 | * address may have any alignment. |
