From: Tobias Brunner Date: Mon, 6 Jan 2014 16:31:07 +0000 (+0100) Subject: chunk: Fix chunk_mac/hash tests on big-endian systems X-Git-Tag: 5.1.2dr3~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d62a6ec3f9949d79bb634a5da030361d6681d531;p=thirdparty%2Fstrongswan.git 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. --- diff --git a/src/libstrongswan/tests/suites/test_chunk.c b/src/libstrongswan/tests/suites/test_chunk.c index 8e29971c12..1c0bac10e7 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);