2013-12-31 06:24:28 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "box_easy2"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2015-11-23 08:07:13 -07:00
|
|
|
static unsigned char m[10000];
|
|
|
|
static unsigned char m2[10000];
|
|
|
|
static unsigned char c[crypto_box_MACBYTES + 10000];
|
2013-12-31 06:24:28 -07:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-12-17 16:00:19 -07:00
|
|
|
unsigned char *alicepk;
|
|
|
|
unsigned char *alicesk;
|
|
|
|
unsigned char *bobpk;
|
|
|
|
unsigned char *bobsk;
|
|
|
|
unsigned char *mac;
|
|
|
|
unsigned char *nonce;
|
2015-01-06 13:18:36 -07:00
|
|
|
unsigned char *k1;
|
|
|
|
unsigned char *k2;
|
2014-12-17 16:00:19 -07:00
|
|
|
size_t mlen;
|
|
|
|
size_t i;
|
2015-11-16 16:43:12 -07:00
|
|
|
int ret;
|
2013-12-31 06:24:28 -07:00
|
|
|
|
2014-12-17 16:12:50 -07:00
|
|
|
alicepk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
|
|
|
|
alicesk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
|
|
|
|
bobpk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
|
|
|
|
bobsk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
|
|
|
|
mac = (unsigned char *) sodium_malloc(crypto_box_MACBYTES);
|
|
|
|
nonce = (unsigned char *) sodium_malloc(crypto_box_NONCEBYTES);
|
2015-01-06 13:18:36 -07:00
|
|
|
k1 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
|
|
|
|
k2 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
|
2013-12-31 06:24:28 -07:00
|
|
|
crypto_box_keypair(alicepk, alicesk);
|
|
|
|
crypto_box_keypair(bobpk, bobsk);
|
2014-11-20 14:12:08 -07:00
|
|
|
mlen = (size_t) randombytes_uniform((uint32_t)sizeof m);
|
2013-12-31 06:24:28 -07:00
|
|
|
randombytes_buf(m, mlen);
|
2015-02-17 10:42:57 -07:00
|
|
|
randombytes_buf(nonce, crypto_box_NONCEBYTES);
|
2015-11-16 16:43:12 -07:00
|
|
|
ret = crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
|
|
|
|
assert(ret == 0);
|
2014-11-20 14:12:08 -07:00
|
|
|
if (crypto_box_open_easy(m2, c,
|
|
|
|
(unsigned long long) mlen + crypto_box_MACBYTES,
|
|
|
|
nonce, alicepk, bobsk) != 0) {
|
2014-07-11 12:23:52 -07:00
|
|
|
printf("open() failed");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-12-31 06:24:28 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-07-11 12:23:52 -07:00
|
|
|
for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
|
2014-11-20 14:12:08 -07:00
|
|
|
if (crypto_box_open_easy(m2, c, (unsigned long long) i,
|
|
|
|
nonce, alicepk, bobsk) == 0) {
|
2014-07-11 12:23:52 -07:00
|
|
|
printf("short open() should have failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-10-04 23:53:19 -07:00
|
|
|
|
|
|
|
memcpy(c, m, mlen);
|
2015-11-16 16:43:12 -07:00
|
|
|
ret = crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
|
|
|
|
assert(ret == 0);
|
2014-10-04 23:53:19 -07:00
|
|
|
printf("%d\n", memcmp(m, c, mlen) == 0);
|
|
|
|
printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
|
2014-11-20 14:12:08 -07:00
|
|
|
if (crypto_box_open_easy(c, c,
|
|
|
|
(unsigned long long) mlen + crypto_box_MACBYTES,
|
|
|
|
nonce, alicepk, bobsk) != 0) {
|
2014-10-04 23:53:19 -07:00
|
|
|
printf("crypto_box_open_easy() failed\n");
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:43:12 -07:00
|
|
|
ret = crypto_box_beforenm(k1, alicepk, bobsk);
|
|
|
|
assert(ret == 0);
|
|
|
|
ret = crypto_box_beforenm(k2, bobpk, alicesk);
|
|
|
|
assert(ret == 0);
|
2015-01-06 13:18:36 -07:00
|
|
|
|
|
|
|
memset(m2, 0, sizeof m2);
|
2015-01-15 06:37:26 -07:00
|
|
|
|
|
|
|
if (crypto_box_easy_afternm(c, m, SIZE_MAX - 1U, nonce, k1) == 0) {
|
|
|
|
printf("crypto_box_easy_afternm() with a short ciphertext should have failed\n");
|
|
|
|
}
|
2015-01-06 13:18:36 -07:00
|
|
|
crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
|
2015-11-01 05:57:51 -07:00
|
|
|
if (crypto_box_open_easy_afternm(m2, c,
|
|
|
|
(unsigned long long) mlen + crypto_box_MACBYTES,
|
|
|
|
nonce, k2) != 0) {
|
|
|
|
printf("crypto_box_open_easy_afternm() failed\n");
|
|
|
|
}
|
2015-01-06 13:18:36 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
2015-01-15 06:37:26 -07:00
|
|
|
if (crypto_box_open_easy_afternm(m2, c, crypto_box_MACBYTES - 1U,
|
|
|
|
nonce, k2) == 0) {
|
|
|
|
printf("crypto_box_open_easy_afternm() with a huge ciphertext should have failed\n");
|
|
|
|
}
|
2015-01-06 13:18:36 -07:00
|
|
|
memset(m2, 0, sizeof m2);
|
2015-11-16 16:43:12 -07:00
|
|
|
ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen,
|
|
|
|
nonce, alicepk, bobsk);
|
|
|
|
assert(ret == 0);
|
2015-11-01 05:57:51 -07:00
|
|
|
if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
|
|
|
|
nonce, bobpk, alicesk) != 0) {
|
|
|
|
printf("crypto_box_open_detached() failed\n");
|
|
|
|
}
|
2014-12-17 16:12:50 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
2015-01-06 13:18:36 -07:00
|
|
|
|
|
|
|
memset(m2, 0, sizeof m2);
|
|
|
|
crypto_box_detached_afternm(c, mac, m, (unsigned long long) mlen,
|
|
|
|
nonce, k1);
|
2015-11-01 05:57:51 -07:00
|
|
|
if (crypto_box_open_detached_afternm(m2, c, mac, (unsigned long long) mlen,
|
|
|
|
nonce, k2) != 0) {
|
|
|
|
printf("crypto_box_open_detached_afternm() failed\n");
|
|
|
|
}
|
2015-01-06 13:18:36 -07:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-12-17 16:00:19 -07:00
|
|
|
sodium_free(alicepk);
|
|
|
|
sodium_free(alicesk);
|
|
|
|
sodium_free(bobpk);
|
|
|
|
sodium_free(bobsk);
|
|
|
|
sodium_free(mac);
|
|
|
|
sodium_free(nonce);
|
2015-01-06 13:18:36 -07:00
|
|
|
sodium_free(k1);
|
|
|
|
sodium_free(k2);
|
2014-12-17 16:00:19 -07:00
|
|
|
printf("OK\n");
|
2014-07-11 12:28:52 -07:00
|
|
|
|
2013-12-31 06:24:28 -07:00
|
|
|
return 0;
|
|
|
|
}
|