diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-08-31 16:21:24 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-08-31 16:21:24 +0200 |
commit | 9225a4550abebd26ff3642d8f5ed4f96b2e4bff7 (patch) | |
tree | 5afffac11e8dff1dda0004df3ac5bed56a6bdd87 /src/crypto.h | |
parent | 46a82c570cf940479be968864eb9638407d7d84f (diff) | |
download | fastd-9225a4550abebd26ff3642d8f5ed4f96b2e4bff7.tar fastd-9225a4550abebd26ff3642d8f5ed4f96b2e4bff7.zip |
Replace memcmp with a constant-time version in some places
Diffstat (limited to 'src/crypto.h')
-rw-r--r-- | src/crypto.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/crypto.h b/src/crypto.h index 07b7d46..561eb27 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -96,6 +96,28 @@ static inline void secure_memzero(void *s, size_t n) { __asm__ volatile("" : : "m"(s)); } +static inline bool secure_memequal(const void *s1, const void *s2, size_t n) { + uint8_t v = 0; + const uint8_t *i1 = s1, *i2 = s2; + size_t i; + + for (i = 0; i < n; i++) + v |= i1[i] ^ i2[i]; + + return (v == 0); +} + +static inline bool block_equal(const fastd_block128_t *a, const fastd_block128_t *b) { + uint32_t v = 0; + + v |= a->dw[0] ^ b->dw[0]; + v |= a->dw[1] ^ b->dw[1]; + v |= a->dw[2] ^ b->dw[2]; + v |= a->dw[3] ^ b->dw[3]; + + return (v == 0); +} + /** XORs two blocks of data */ static inline void xor(fastd_block128_t *x, const fastd_block128_t *a, const fastd_block128_t *b) { x->qw[0] = a->qw[0] ^ b->qw[0]; |