diff --git a/README b/README index baff0fa..2fdc3b9 100644 --- a/README +++ b/README @@ -16,6 +16,7 @@ Utilities - Constant-time operations - [x] compare + - [x] mask32: return a 1s mask if any bit is set, 0 otherwise - Hexdump Hash diff --git a/ct.c b/ct.c index 9599c49..3dd6e43 100644 --- a/ct.c +++ b/ct.c @@ -25,10 +25,18 @@ lc_ct_cmp(const uint8_t *x, const uint8_t *y, size_t l) for (; l > 0; l--) r |= *x++ ^ *y++; - /* Ensures that if any bit is set, then LSB is set. */ - r |= r >> 4; - r |= r >> 2; - r |= r >> 1; - - return 0xffffffff + (r & 1); + return lc_ct_mask32(r); +} + +uint32_t +lc_ct_mask32(uint32_t x) +{ + /* Ensures that if any bit is set, then LSB is set. */ + x |= x >> 16; + x |= x >> 8; + x |= x >> 4; + x |= x >> 2; + x |= x >> 1; + + return UINT32_MAX + (x & 1); } diff --git a/lilcrypto.h b/lilcrypto.h index bd125c2..0473270 100644 --- a/lilcrypto.h +++ b/lilcrypto.h @@ -137,6 +137,7 @@ struct lc_hkdf_params { */ uint32_t lc_ct_cmp(const uint8_t *, const uint8_t *, size_t); +uint32_t lc_ct_mask32(uint32_t); /*