1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-02 19:21:53 +03:00

test-siphash24: add a test for concatenating very short buffers

coverity seems to think that our siphash code can read past the
end of a short buffer. Add a test which adds very short buffers
with different combinations of length to the hash. Hashing is done
twice, once with zeros following "data", and once with some other
bytes following "data". The two results are then compared to
verify that the result does not depend on bytes past the specified
data length.

(This test passes.)
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2016-02-20 16:28:42 -05:00
parent 714c62b463
commit 56e3d0cf5d

View File

@ -22,9 +22,9 @@
#define ITERATIONS 10000000ULL #define ITERATIONS 10000000ULL
static int do_test(const uint8_t *in, size_t len, const uint8_t *key) { static void do_test(const uint8_t *in, size_t len, const uint8_t *key) {
struct siphash state = {}; struct siphash state = {};
uint64_t out = 0; uint64_t out;
unsigned i, j; unsigned i, j;
out = siphash24(in, len, key); out = siphash24(in, len, key);
@ -60,7 +60,46 @@ static int do_test(const uint8_t *in, size_t len, const uint8_t *key) {
assert_se(out == 0xa129ca6149be45e5); assert_se(out == 0xa129ca6149be45e5);
} }
} }
return 0; }
static void test_short_hashes(void) {
const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
const uint8_t key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07,
0x23, 0x09, 0x23, 0x14, 0x0c, 0x33, 0x0e, 0x0f};
uint8_t two[sizeof one] = {};
struct siphash state1 = {}, state2 = {};
unsigned i, j;
siphash24_init(&state1, key);
siphash24_init(&state2, key);
/* hashing 1, 2, 3, 4, 5, ..., 16 bytes, with the byte after the buffer different */
for (i = 1; i <= sizeof one; i++) {
siphash24_compress(one, i, &state1);
two[i-1] = one[i-1];
siphash24_compress(two, i, &state2);
assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
}
/* hashing n and 1, n and 2, n and 3, ..., n-1 and 1, n-2 and 2, ... */
for (i = sizeof one; i > 0; i--) {
zero(two);
for (j = 1; j <= sizeof one; j++) {
siphash24_compress(one, i, &state1);
siphash24_compress(one, j, &state1);
siphash24_compress(one, i, &state2);
two[j-1] = one[j-1];
siphash24_compress(two, j, &state2);
assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
}
}
} }
/* see https://131002.net/siphash/siphash.pdf, Appendix A */ /* see https://131002.net/siphash/siphash.pdf, Appendix A */
@ -80,4 +119,6 @@ int main(int argc, char *argv[]) {
do_test(in_buf + 2, sizeof(in), key); do_test(in_buf + 2, sizeof(in), key);
memcpy(in_buf + 4, in, sizeof(in)); memcpy(in_buf + 4, in, sizeof(in));
do_test(in_buf + 4, sizeof(in), key); do_test(in_buf + 4, sizeof(in), key);
test_short_hashes();
} }