From ad7b1c58a884283c6537f7b1bbaa2a20583ca0d6 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 11 Jul 2014 12:23:52 -0700 Subject: [PATCH] Add tests for short messages with (secret)box_easy --- test/default/box_easy2.c | 14 ++++++++++++-- test/default/secretbox_easy2.c | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test/default/box_easy2.c b/test/default/box_easy2.c index a2f1d139..c8178e69 100644 --- a/test/default/box_easy2.c +++ b/test/default/box_easy2.c @@ -16,6 +16,7 @@ unsigned char bobsk[crypto_box_SECRETKEYBYTES]; int main(void) { unsigned long long mlen; + unsigned long long i; crypto_box_keypair(alicepk, alicesk); crypto_box_keypair(bobpk, bobsk); @@ -23,9 +24,18 @@ int main(void) randombytes_buf(m, mlen); randombytes_buf(nonce, sizeof nonce); crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk); - crypto_box_open_easy(m2, c, mlen + crypto_box_MACBYTES, - nonce, alicepk, bobsk); + if (crypto_box_open_easy(m2, c, mlen + crypto_box_MACBYTES, + nonce, alicepk, bobsk) != 0) { + printf("open() failed"); + return 1; + } printf("%d\n", memcmp(m, m2, mlen)); + for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) { + if (crypto_box_open_easy(m2, c, i, nonce, alicepk, bobsk) == 0) { + printf("short open() should have failed"); + return 1; + } + } return 0; } diff --git a/test/default/secretbox_easy2.c b/test/default/secretbox_easy2.c index d1f2b995..63105b64 100644 --- a/test/default/secretbox_easy2.c +++ b/test/default/secretbox_easy2.c @@ -14,6 +14,7 @@ unsigned char mac[crypto_secretbox_MACBYTES]; int main(void) { unsigned long long mlen; + unsigned long long i; randombytes_buf(k, sizeof k); mlen = (unsigned long long) randombytes_uniform((uint32_t) sizeof m); @@ -24,6 +25,12 @@ int main(void) nonce, k); printf("%d\n", memcmp(m, m2, mlen)); + for (i = 0; i < mlen + crypto_secretbox_MACBYTES - 1; i++) { + if (crypto_secretbox_open_easy(m2, c, i, nonce, k) == 0) { + printf("short open() should have failed"); + return 1; + } + } crypto_secretbox_detached(c, mac, m, mlen, nonce, k); crypto_secretbox_open_detached(m2, c, mac, mlen, nonce, k); printf("%d\n", memcmp(m, m2, mlen));