diff options
author | Tobias Brunner <tobias@strongswan.org> | 2014-01-06 17:31:07 +0100 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2014-01-06 17:31:07 +0100 |
commit | d62a6ec3f9949d79bb634a5da030361d6681d531 (patch) | |
tree | c154fda96f4d81e5a213b2fe989fecf31bef59bf | |
parent | 13f2d3a2f637fc5277fc0752392f56d1af7a1ccb (diff) | |
download | strongswan-d62a6ec3f9949d79bb634a5da030361d6681d531.tar.bz2 strongswan-d62a6ec3f9949d79bb634a5da030361d6681d531.tar.xz |
chunk: Fix chunk_mac/hash tests on big-endian systems
Our SipHash-2-4 implementation returns the result in host order, while
the test vectors are little-endian. Use a custom comparison function to
account for this.
Fixes #478.
-rw-r--r-- | src/libstrongswan/tests/suites/test_chunk.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/libstrongswan/tests/suites/test_chunk.c b/src/libstrongswan/tests/suites/test_chunk.c index 8e29971c1..1c0bac10e 100644 --- a/src/libstrongswan/tests/suites/test_chunk.c +++ b/src/libstrongswan/tests/suites/test_chunk.c @@ -672,6 +672,31 @@ static const u_char sip_vectors[64][8] = { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, } }; +/** + * Our SipHash-2-4 implementation returns the result in host order, which + * doesn't matter for practical purposes and even avoids a byte swap. But + * because the test vectors are in little-endian we have to account for this + * with this custom comparison function. + */ +static inline bool sipeq(const void *a, const void *b, size_t n) +{ + u_char *ap = (u_char*)a, *bp = (u_char*)b; + int i; + + for (i = 0; i < n; i++) + { +#ifdef WORDS_BIGENDIAN + if (ap[i] != bp[n - i - 1]) +#else + if (ap[i] != bp[i]) +#endif + { + return FALSE; + } + } + return TRUE; +} + START_TEST(test_chunk_mac) { chunk_t in; @@ -692,7 +717,7 @@ START_TEST(test_chunk_mac) in.ptr[i] = i; in.len = i; out = chunk_mac(in, key); - fail_unless(memeq(&out, sip_vectors[i], 8), + fail_unless(sipeq(&out, sip_vectors[i], 8), "test vector failed for %d bytes", i); } } @@ -739,7 +764,7 @@ START_TEST(test_chunk_hash_static) in.len = i; /* compared to chunk_mac() we only get half the value back */ out = chunk_hash_static(in); - fail_unless(memeq(&out, sip_vectors[i], 4), + fail_unless(sipeq(&out, sip_vectors[i], 4), "test vector failed for %d bytes", i); } hash_a = chunk_hash_static_inc(in, out); |