ct: add mask32
This commit is contained in:
parent
dbf81b49d5
commit
f62f07aee6
1
README
1
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
|
||||
|
20
ct.c
20
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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user