2013-12-31 10:46:10 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "secretbox_easy2"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2017-02-23 03:22:36 -07:00
|
|
|
int
|
|
|
|
main(void)
|
2013-12-31 10:46:10 -07:00
|
|
|
{
|
2015-11-27 04:30:13 -07:00
|
|
|
unsigned char *m;
|
|
|
|
unsigned char *m2;
|
|
|
|
unsigned char *c;
|
|
|
|
unsigned char *nonce;
|
|
|
|
unsigned char *k;
|
|
|
|
unsigned char *mac;
|
2017-02-23 03:22:36 -07:00
|
|
|
size_t mlen;
|
|
|
|
size_t i;
|
2013-12-31 10:46:10 -07:00
|
|
|
|
2017-02-23 03:22:36 -07:00
|
|
|
mlen = (size_t) randombytes_uniform((uint32_t) 10000) + 1U;
|
|
|
|
m = (unsigned char *) sodium_malloc(mlen);
|
|
|
|
m2 = (unsigned char *) sodium_malloc(mlen);
|
|
|
|
c = (unsigned char *) sodium_malloc(crypto_secretbox_MACBYTES + mlen);
|
2015-11-27 04:30:13 -07:00
|
|
|
nonce = (unsigned char *) sodium_malloc(crypto_secretbox_NONCEBYTES);
|
2017-02-23 03:22:36 -07:00
|
|
|
k = (unsigned char *) sodium_malloc(crypto_secretbox_KEYBYTES);
|
|
|
|
mac = (unsigned char *) sodium_malloc(crypto_secretbox_MACBYTES);
|
2017-02-19 13:20:45 -07:00
|
|
|
crypto_secretbox_keygen(k);
|
2017-12-11 11:54:10 -07:00
|
|
|
randombytes_buf(m, mlen);
|
2015-11-27 04:30:13 -07:00
|
|
|
randombytes_buf(nonce, crypto_secretbox_NONCEBYTES);
|
2014-11-20 14:12:08 -07:00
|
|
|
crypto_secretbox_easy(c, m, (unsigned long long) mlen, nonce, k);
|
2017-02-23 03:22:36 -07:00
|
|
|
if (crypto_secretbox_open_easy(
|
|
|
|
m2, c, (unsigned long long) mlen + crypto_secretbox_MACBYTES, nonce,
|
|
|
|
k) != 0) {
|
2015-11-01 06:45:35 -07:00
|
|
|
printf("crypto_secretbox_open_easy() failed\n");
|
|
|
|
}
|
2013-12-31 10:46:10 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-07-11 12:23:52 -07:00
|
|
|
for (i = 0; i < mlen + crypto_secretbox_MACBYTES - 1; i++) {
|
2017-02-23 03:22:36 -07:00
|
|
|
if (crypto_secretbox_open_easy(m2, c, (unsigned long long) i, nonce,
|
|
|
|
k) == 0) {
|
2014-10-03 13:22:57 -07:00
|
|
|
printf("short open() should have failed\n");
|
2014-07-11 12:23:52 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 14:12:08 -07:00
|
|
|
crypto_secretbox_detached(c, mac, m, (unsigned long long) mlen, nonce, k);
|
2017-07-24 14:17:55 -07:00
|
|
|
if (crypto_secretbox_open_detached(NULL, c, mac, (unsigned long long) mlen,
|
|
|
|
nonce, k) != 0) {
|
|
|
|
printf("crypto_secretbox_open_detached() with a NULL message pointer failed\n");
|
|
|
|
}
|
2014-11-20 14:12:08 -07:00
|
|
|
if (crypto_secretbox_open_detached(m2, c, mac, (unsigned long long) mlen,
|
|
|
|
nonce, k) != 0) {
|
2014-10-03 13:22:57 -07:00
|
|
|
printf("crypto_secretbox_open_detached() failed\n");
|
|
|
|
}
|
2014-06-26 15:18:39 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-10-03 13:22:57 -07:00
|
|
|
memcpy(c, m, mlen);
|
2014-11-20 14:12:08 -07:00
|
|
|
crypto_secretbox_easy(c, c, (unsigned long long) mlen, nonce, k);
|
2014-10-03 13:22:57 -07:00
|
|
|
printf("%d\n", memcmp(m, c, mlen) == 0);
|
|
|
|
printf("%d\n", memcmp(m, c + crypto_secretbox_MACBYTES, mlen) == 0);
|
2017-02-23 03:22:36 -07:00
|
|
|
if (crypto_secretbox_open_easy(
|
|
|
|
c, c, (unsigned long long) mlen + crypto_secretbox_MACBYTES, nonce,
|
|
|
|
k) != 0) {
|
2014-10-03 13:22:57 -07:00
|
|
|
printf("crypto_secretbox_open_easy() failed\n");
|
|
|
|
}
|
|
|
|
printf("%d\n", memcmp(m, c, mlen));
|
|
|
|
|
2015-11-27 04:30:13 -07:00
|
|
|
sodium_free(m);
|
|
|
|
sodium_free(m2);
|
|
|
|
sodium_free(c);
|
|
|
|
sodium_free(nonce);
|
|
|
|
sodium_free(k);
|
|
|
|
sodium_free(mac);
|
|
|
|
|
2013-12-31 10:46:10 -07:00
|
|
|
return 0;
|
|
|
|
}
|