diff options
author | Nick Mathewson <nickm@torproject.org> | 2012-12-25 22:22:07 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-01-03 11:29:48 -0500 |
commit | f07a5125cb5bb9ee5968ded163cfdd73e5ad028c (patch) | |
tree | 03444d1dbfe2a58e242e9923728971c7303717b9 /src/common/di_ops.c | |
parent | 92d6a83e9895da874eae81e20e14df20231f25bf (diff) | |
download | tor-f07a5125cb5bb9ee5968ded163cfdd73e5ad028c.tar.gz tor-f07a5125cb5bb9ee5968ded163cfdd73e5ad028c.zip |
Implement a constant-time safe_mem_is_zero.
Diffstat (limited to 'src/common/di_ops.c')
-rw-r--r-- | src/common/di_ops.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/common/di_ops.c b/src/common/di_ops.c index b73a3cc492..e299e31507 100644 --- a/src/common/di_ops.c +++ b/src/common/di_ops.c @@ -203,3 +203,20 @@ dimap_search(const di_digest256_map_t *map, const uint8_t *key, return (void *)result; } +/** + * Return true iff the <b>sz</b> bytes at <b>mem</b> are all zero. Runs in + * time independent of the contents of <b>mem</b>. + */ +int +safe_mem_is_zero(const void *mem, size_t sz) +{ + uint32_t total = 0; + const uint8_t *ptr = mem; + + while (sz--) { + total |= *ptr++; + } + + return 1 & ((total - 1) >> 8); +} + |