From: Niels Möller Date: Tue, 17 Apr 2012 21:32:28 +0000 (+0200) Subject: Test that salsa20_crypt doesn't overwrite the destination area. X-Git-Tag: nettle_2.5_release_20120707~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=95c8eb724ca6f2508c9ea8b4b6763e365f25007d;p=thirdparty%2Fnettle.git Test that salsa20_crypt doesn't overwrite the destination area. --- diff --git a/ChangeLog b/ChangeLog index b5918b6b..3206c66f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2012-04-17 Niels Möller + * testsuite/salsa20-test.c (test_salsa20_stream): Check that + salsa20_crypt doesn't write beyond the given destination area. + (test_salsa20): Likewise. + * salsa20-crypt.c: Renamed file, from... * salsa20.c: ... old name. diff --git a/testsuite/salsa20-test.c b/testsuite/salsa20-test.c index 6be6d045..7a246b9f 100644 --- a/testsuite/salsa20-test.c +++ b/testsuite/salsa20-test.c @@ -27,15 +27,19 @@ test_salsa20_stream(unsigned key_length, { struct salsa20_ctx ctx; uint8_t data[STREAM_LENGTH + 1]; - uint8_t stream[STREAM_LENGTH]; + uint8_t stream[STREAM_LENGTH + 1]; uint8_t xor[SALSA20_BLOCK_SIZE]; unsigned j; salsa20_set_key(&ctx, key_length, key); salsa20_set_iv(&ctx, iv); - memset(stream, 0, STREAM_LENGTH); + memset(stream, 0, STREAM_LENGTH + 1); salsa20_crypt(&ctx, STREAM_LENGTH, stream, stream); - + if (stream[STREAM_LENGTH]) + { + fprintf(stderr, "Stream of %d bytes wrote too much!\n", STREAM_LENGTH); + FAIL(); + } if (!MEMEQ (64, stream, ciphertext)) { fprintf(stderr, "Error failed, offset 0:\n"); @@ -100,7 +104,7 @@ test_salsa20_stream(unsigned key_length, } if (!memzero_p (data + j, STREAM_LENGTH + 1 - j)) { - fprintf(stderr, "Encrypt failed for length, %u wrote too much:\n", j); + fprintf(stderr, "Encrypt failed for length %u, wrote too much:\n", j); fprintf(stderr, "\nOutput: "); print_hex(STREAM_LENGTH + 1 - j, data + j); fprintf(stderr, "\n"); @@ -118,12 +122,19 @@ test_salsa20(unsigned key_length, const uint8_t *ciphertext) { struct salsa20_ctx ctx; - uint8_t *data = xalloc(length); + uint8_t *data = xalloc(length + 1); salsa20_set_key(&ctx, key_length, key); salsa20_set_iv(&ctx, iv); + data[length] = 17; salsa20_crypt(&ctx, length, data, cleartext); - + if (data[length] != 17) + { + fprintf(stderr, "Encrypt of %u bytes wrote too much!\nInput:", length); + print_hex(length, cleartext); + fprintf(stderr, "\n"); + FAIL(); + } if (!MEMEQ(length, data, ciphertext)) { fprintf(stderr, "Encrypt failed:\nInput:");