2013-12-31 08:24:28 -05:00
|
|
|
|
|
|
|
#define TEST_NAME "box_easy2"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
|
|
|
unsigned char m[10000];
|
|
|
|
unsigned char m2[10000];
|
|
|
|
unsigned char c[crypto_box_MACBYTES + 10000];
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-12-17 18:00:19 -05:00
|
|
|
unsigned char *alicepk;
|
|
|
|
unsigned char *alicesk;
|
|
|
|
unsigned char *bobpk;
|
|
|
|
unsigned char *bobsk;
|
|
|
|
unsigned char *mac;
|
|
|
|
unsigned char *nonce;
|
|
|
|
size_t mlen;
|
|
|
|
size_t i;
|
2013-12-31 08:24:28 -05:00
|
|
|
|
2014-12-17 18:00:19 -05:00
|
|
|
alicepk = sodium_malloc(crypto_box_PUBLICKEYBYTES);
|
|
|
|
alicesk = sodium_malloc(crypto_box_SECRETKEYBYTES);
|
|
|
|
bobpk = sodium_malloc(crypto_box_PUBLICKEYBYTES);
|
|
|
|
bobsk = sodium_malloc(crypto_box_SECRETKEYBYTES);
|
|
|
|
mac = sodium_malloc(crypto_box_MACBYTES);
|
|
|
|
nonce = sodium_malloc(crypto_box_NONCEBYTES);
|
2013-12-31 08:24:28 -05:00
|
|
|
crypto_box_keypair(alicepk, alicesk);
|
|
|
|
crypto_box_keypair(bobpk, bobsk);
|
2014-11-20 16:12:08 -05:00
|
|
|
mlen = (size_t) randombytes_uniform((uint32_t)sizeof m);
|
2013-12-31 08:24:28 -05:00
|
|
|
randombytes_buf(m, mlen);
|
|
|
|
randombytes_buf(nonce, sizeof nonce);
|
|
|
|
crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
|
2014-11-20 16:12:08 -05:00
|
|
|
if (crypto_box_open_easy(m2, c,
|
|
|
|
(unsigned long long) mlen + crypto_box_MACBYTES,
|
|
|
|
nonce, alicepk, bobsk) != 0) {
|
2014-07-11 15:23:52 -04:00
|
|
|
printf("open() failed");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-12-31 08:24:28 -05:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
|
2014-07-11 15:23:52 -04:00
|
|
|
for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
|
2014-11-20 16:12:08 -05:00
|
|
|
if (crypto_box_open_easy(m2, c, (unsigned long long) i,
|
|
|
|
nonce, alicepk, bobsk) == 0) {
|
2014-07-11 15:23:52 -04:00
|
|
|
printf("short open() should have failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 02:53:19 -04:00
|
|
|
|
|
|
|
memcpy(c, m, mlen);
|
2014-11-20 16:12:08 -05:00
|
|
|
crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
|
2014-10-05 02:53:19 -04:00
|
|
|
printf("%d\n", memcmp(m, c, mlen) == 0);
|
|
|
|
printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
|
2014-11-20 16:12:08 -05:00
|
|
|
if (crypto_box_open_easy(c, c,
|
|
|
|
(unsigned long long) mlen + crypto_box_MACBYTES,
|
|
|
|
nonce, alicepk, bobsk) != 0) {
|
2014-10-05 02:53:19 -04:00
|
|
|
printf("crypto_box_open_easy() failed\n");
|
|
|
|
}
|
|
|
|
|
2014-11-20 16:12:08 -05:00
|
|
|
crypto_box_detached(c, mac, m, (unsigned long long) mlen,
|
|
|
|
nonce, bobsk, alicepk);
|
|
|
|
crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
|
|
|
|
nonce, alicepk, bobsk);
|
2014-12-17 18:00:19 -05:00
|
|
|
printf("%d\n", memcmp(m, m2, mlen));
|
|
|
|
sodium_free(alicepk);
|
|
|
|
sodium_free(alicesk);
|
|
|
|
sodium_free(bobpk);
|
|
|
|
sodium_free(bobsk);
|
|
|
|
sodium_free(mac);
|
|
|
|
sodium_free(nonce);
|
|
|
|
printf("OK\n");
|
2014-07-11 15:28:52 -04:00
|
|
|
|
2013-12-31 08:24:28 -05:00
|
|
|
return 0;
|
|
|
|
}
|