From 9225a4550abebd26ff3642d8f5ed4f96b2e4bff7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 31 Aug 2014 16:21:24 +0200 Subject: Replace memcmp with a constant-time version in some places --- src/crypto.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/crypto.h') 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]; -- cgit v1.2.3