2013-12-31 12:46:10 -05:00
|
|
|
|
|
|
|
#define TEST_NAME "secretbox_easy2"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
|
|
|
unsigned char m[10000];
|
|
|
|
unsigned char m2[10000];
|
|
|
|
unsigned char c[crypto_secretbox_MACBYTES + 10000];
|
|
|
|
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
|
|
|
unsigned char k[crypto_secretbox_KEYBYTES];
|
2014-06-26 18:18:39 -04:00
|
|
|
unsigned char mac[crypto_secretbox_MACBYTES];
|
2013-12-31 12:46:10 -05:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
unsigned long long mlen;
|
2014-07-11 15:23:52 -04:00
|
|
|
unsigned long long i;
|
2013-12-31 12:46:10 -05:00
|
|
|
|
|
|
|
randombytes_buf(k, sizeof k);
|
2014-09-14 14:32:55 -04:00
|
|
|
mlen = (unsigned long long)randombytes_uniform((uint32_t)sizeof m);
|
2013-12-31 12:46:10 -05:00
|
|
|
randombytes_buf(m, mlen);
|
|
|
|
randombytes_buf(nonce, sizeof nonce);
|
|
|
|
crypto_secretbox_easy(c, m, mlen, nonce, k);
|
2014-09-14 14:32:55 -04:00
|
|
|
crypto_secretbox_open_easy(m2, c, mlen + crypto_secretbox_MACBYTES, nonce,
|
|
|
|
k);
|
2013-12-31 12:46:10 -05:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-07-11 15:23:52 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 21:18:10 -04:00
|
|
|
crypto_secretbox_detached(c, mac, m, mlen, nonce, k);
|
|
|
|
crypto_secretbox_open_detached(m2, c, mac, mlen, nonce, k);
|
2014-06-26 18:18:39 -04:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2013-12-31 12:46:10 -05:00
|
|
|
return 0;
|
|
|
|
}
|