1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-23 12:05:11 -07:00

Add new tests for the crypto_box_*() precomputed interface

This commit is contained in:
Frank Denis 2015-01-06 21:18:36 +01:00
parent 2d380c97f3
commit 6104af1189
2 changed files with 31 additions and 4 deletions

View File

@ -14,6 +14,8 @@ int main(void)
unsigned char *bobsk;
unsigned char *mac;
unsigned char *nonce;
unsigned char *k1;
unsigned char *k2;
size_t mlen;
size_t i;
@ -23,6 +25,8 @@ int main(void)
bobsk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
mac = (unsigned char *) sodium_malloc(crypto_box_MACBYTES);
nonce = (unsigned char *) sodium_malloc(crypto_box_NONCEBYTES);
k1 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
k2 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
crypto_box_keypair(alicepk, alicesk);
crypto_box_keypair(bobpk, bobsk);
mlen = (size_t) randombytes_uniform((uint32_t)sizeof m);
@ -55,17 +59,38 @@ int main(void)
printf("crypto_box_open_easy() failed\n");
}
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);
crypto_box_beforenm(k1, alicepk, bobsk);
crypto_box_beforenm(k2, bobpk, alicesk);
memset(m2, 0, sizeof m2);
crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
crypto_box_open_easy_afternm(m2, c,
(unsigned long long) mlen + crypto_box_MACBYTES,
nonce, k2);
printf("%d\n", memcmp(m, m2, mlen));
memset(m2, 0, sizeof m2);
crypto_box_detached(c, mac, m, (unsigned long long) mlen,
nonce, alicepk, bobsk);
crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
nonce, bobpk, alicesk);
printf("%d\n", memcmp(m, m2, mlen));
memset(m2, 0, sizeof m2);
crypto_box_detached_afternm(c, mac, m, (unsigned long long) mlen,
nonce, k1);
crypto_box_open_detached_afternm(m2, c, mac, (unsigned long long) mlen,
nonce, k2);
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);
sodium_free(k1);
sodium_free(k2);
printf("OK\n");
return 0;

View File

@ -2,4 +2,6 @@
0
0
0
0
0
OK