ct: add mask32

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-26 22:42:04 +00:00
parent dbf81b49d5
commit f62f07aee6
3 changed files with 16 additions and 6 deletions

1
README
View File

@ -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
View File

@ -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);
}

View File

@ -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);
/*