2014-06-19 21:44:07 -04:00
|
|
|
|
2014-06-26 11:48:13 -04:00
|
|
|
#define TEST_NAME "aead_chacha20poly1305"
|
2014-06-19 21:44:07 -04:00
|
|
|
#include "cmptest.h"
|
|
|
|
|
2015-02-11 06:34:29 -05:00
|
|
|
static int
|
|
|
|
tv(void)
|
2014-06-19 21:44:07 -04:00
|
|
|
{
|
2016-03-23 10:55:45 -04:00
|
|
|
#undef MLEN
|
|
|
|
#define MLEN 10U
|
|
|
|
#undef ADLEN
|
|
|
|
#define ADLEN 10U
|
|
|
|
#undef CLEN
|
|
|
|
#define CLEN (MLEN + crypto_aead_chacha20poly1305_ABYTES)
|
2016-03-23 10:30:30 -04:00
|
|
|
static const unsigned char firstkey[crypto_aead_chacha20poly1305_KEYBYTES]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0x42, 0x90, 0xbc, 0xb1, 0x54, 0x17, 0x35, 0x31, 0xf3, 0x14, 0xaf,
|
|
|
|
0x57, 0xf3, 0xbe, 0x3b, 0x50, 0x06, 0xda, 0x37, 0x1e, 0xce, 0x27,
|
|
|
|
0x2a, 0xfa, 0x1b, 0x5d, 0xbd, 0xd1, 0x10, 0x0a, 0x10, 0x07 };
|
2016-03-23 10:55:45 -04:00
|
|
|
static const unsigned char m[MLEN]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0x86, 0xd0, 0x99, 0x74, 0x84, 0x0b, 0xde, 0xd2, 0xa5, 0xca };
|
2016-03-23 10:30:30 -04:00
|
|
|
static const unsigned char nonce[crypto_aead_chacha20poly1305_NPUBBYTES]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0xcd, 0x7c, 0xf6, 0x7b, 0xe3, 0x9c, 0x79, 0x4a };
|
2016-03-23 10:55:45 -04:00
|
|
|
static const unsigned char ad[ADLEN]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0x87, 0xe2, 0x29, 0xd4, 0x50, 0x08, 0x45, 0xa0, 0x79, 0xc0 };
|
2016-03-23 12:22:35 -04:00
|
|
|
unsigned char *c = (unsigned char *) sodium_malloc(CLEN);
|
|
|
|
unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);
|
|
|
|
unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_chacha20poly1305_ABYTES);
|
|
|
|
unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);
|
2016-03-23 10:30:30 -04:00
|
|
|
unsigned long long found_clen;
|
|
|
|
unsigned long long found_maclen;
|
2014-06-26 11:56:17 -04:00
|
|
|
unsigned long long m2len;
|
2014-09-14 14:32:55 -04:00
|
|
|
size_t i;
|
2014-06-19 21:44:07 -04:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
crypto_aead_chacha20poly1305_encrypt(c, &found_clen, m, MLEN,
|
|
|
|
ad, ADLEN,
|
2014-06-26 11:48:13 -04:00
|
|
|
NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != CLEN) {
|
2016-03-23 10:30:30 -04:00
|
|
|
printf("found_clen is not properly set\n");
|
2014-06-26 11:56:17 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2014-06-19 21:44:07 -04:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2016-03-23 10:30:30 -04:00
|
|
|
crypto_aead_chacha20poly1305_encrypt_detached(detached_c,
|
|
|
|
mac, &found_maclen,
|
2016-03-23 10:55:45 -04:00
|
|
|
m, MLEN, ad, ADLEN,
|
2016-03-23 10:20:23 -04:00
|
|
|
NULL, nonce, firstkey);
|
2016-03-23 10:30:30 -04:00
|
|
|
if (found_maclen != crypto_aead_chacha20poly1305_abytes()) {
|
|
|
|
printf("found_maclen is not properly set\n");
|
2016-03-23 10:20:23 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(detached_c, c, MLEN) != 0) {
|
2016-03-23 10:20:23 -04:00
|
|
|
printf("detached ciphertext is bogus\n");
|
|
|
|
}
|
2014-06-19 21:44:07 -04:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, CLEN,
|
|
|
|
ad, ADLEN,
|
2016-03-23 10:20:23 -04:00
|
|
|
nonce, firstkey) != 0) {
|
2014-06-26 11:56:17 -04:00
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt() failed\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2014-06-26 11:56:17 -04:00
|
|
|
printf("m2len is not properly set\n");
|
2014-06-19 21:44:07 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2014-06-19 21:44:07 -04:00
|
|
|
printf("m != m2\n");
|
|
|
|
}
|
2016-03-23 10:20:23 -04:00
|
|
|
memset(m2, 0, m2len);
|
2017-07-21 10:52:01 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_decrypt_detached(NULL, NULL,
|
|
|
|
c, MLEN, mac,
|
|
|
|
ad, ADLEN,
|
|
|
|
nonce, firstkey) == 0);
|
2016-03-30 15:20:34 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt_detached(m2, NULL,
|
2016-03-23 10:55:45 -04:00
|
|
|
c, MLEN, mac,
|
|
|
|
ad, ADLEN,
|
2016-03-23 10:20:23 -04:00
|
|
|
nonce, firstkey) != 0) {
|
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt_detached() failed\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2016-03-23 10:20:23 -04:00
|
|
|
printf("detached m != m2\n");
|
|
|
|
}
|
2014-06-19 21:44:07 -04:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; i++) {
|
2014-06-19 21:44:07 -04:00
|
|
|
c[i] ^= (i + 1U);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(m2, NULL, NULL, c, CLEN,
|
|
|
|
ad, ADLEN, nonce, firstkey)
|
|
|
|
== 0 || memcmp(m, m2, MLEN) == 0) {
|
2014-06-19 21:44:07 -04:00
|
|
|
printf("message can be forged\n");
|
|
|
|
}
|
|
|
|
c[i] ^= (i + 1U);
|
|
|
|
}
|
2014-06-30 19:23:34 -04:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
crypto_aead_chacha20poly1305_encrypt(c, &found_clen, m, MLEN,
|
2016-03-23 10:30:30 -04:00
|
|
|
NULL, 0U, NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != CLEN) {
|
2016-03-23 10:30:30 -04:00
|
|
|
printf("found_clen is not properly set (adlen=0)\n");
|
2014-06-30 19:23:34 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2014-06-30 19:23:34 -04:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, CLEN,
|
2014-09-14 14:32:55 -04:00
|
|
|
NULL, 0U, nonce, firstkey) != 0) {
|
2014-06-30 19:23:34 -04:00
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt() failed (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2014-06-30 19:23:34 -04:00
|
|
|
printf("m2len is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2014-06-30 19:23:34 -04:00
|
|
|
printf("m != m2 (adlen=0)\n");
|
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
m2len = 1;
|
2014-09-14 14:32:55 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(
|
2016-04-04 04:47:54 -04:00
|
|
|
m2, &m2len, NULL, NULL,
|
2016-04-04 04:40:23 -04:00
|
|
|
randombytes_uniform(crypto_aead_chacha20poly1305_ABYTES),
|
|
|
|
NULL, 0U, nonce, firstkey) != -1) {
|
2014-09-14 14:32:55 -04:00
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt() worked with a short "
|
|
|
|
"ciphertext\n");
|
2014-09-13 17:51:21 -04:00
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
if (m2len != 0) {
|
|
|
|
printf("Message length should have been set to zero after a failure\n");
|
|
|
|
}
|
|
|
|
m2len = 1;
|
2014-09-14 14:32:55 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,
|
2014-09-13 17:51:21 -04:00
|
|
|
nonce, firstkey) != -1) {
|
2014-09-14 14:32:55 -04:00
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt() worked with an empty "
|
|
|
|
"ciphertext\n");
|
2014-09-13 17:51:21 -04:00
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
if (m2len != 0) {
|
|
|
|
printf("Message length should have been set to zero after a failure\n");
|
|
|
|
}
|
2014-09-13 17:51:21 -04:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
memcpy(c, m, MLEN);
|
|
|
|
crypto_aead_chacha20poly1305_encrypt(c, &found_clen, c, MLEN,
|
2016-03-23 10:30:30 -04:00
|
|
|
NULL, 0U, NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != CLEN) {
|
2016-03-23 10:30:30 -04:00
|
|
|
printf("found_clen is not properly set (adlen=0)\n");
|
2014-10-05 03:02:45 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2014-10-05 03:02:45 -04:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_decrypt(c, &m2len, NULL, c, CLEN,
|
2014-10-05 03:02:45 -04:00
|
|
|
NULL, 0U, nonce, firstkey) != 0) {
|
|
|
|
printf("crypto_aead_chacha20poly1305_decrypt() failed (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2014-10-05 03:02:45 -04:00
|
|
|
printf("m2len is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, c, MLEN) != 0) {
|
2014-10-05 03:02:45 -04:00
|
|
|
printf("m != c (adlen=0)\n");
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:23:40 -04:00
|
|
|
sodium_free(c);
|
|
|
|
sodium_free(detached_c);
|
|
|
|
sodium_free(mac);
|
|
|
|
sodium_free(m2);
|
|
|
|
|
2014-09-13 17:11:12 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_keybytes() > 0U);
|
|
|
|
assert(crypto_aead_chacha20poly1305_npubbytes() > 0U);
|
|
|
|
assert(crypto_aead_chacha20poly1305_nsecbytes() == 0U);
|
2017-09-18 17:13:50 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_messagebytes_max() > 0U);
|
|
|
|
assert(crypto_aead_chacha20poly1305_messagebytes_max() == crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX);
|
|
|
|
assert(crypto_aead_chacha20poly1305_keybytes() == crypto_aead_chacha20poly1305_KEYBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_nsecbytes() == crypto_aead_chacha20poly1305_NSECBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_npubbytes() == crypto_aead_chacha20poly1305_NPUBBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_abytes() == crypto_aead_chacha20poly1305_ABYTES);
|
2014-09-13 17:11:12 -04:00
|
|
|
|
2014-06-19 21:44:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-11 06:34:29 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
tv_ietf(void)
|
|
|
|
{
|
2016-03-23 10:55:45 -04:00
|
|
|
#undef MLEN
|
|
|
|
#define MLEN 114U
|
|
|
|
#undef ADLEN
|
|
|
|
#define ADLEN 12U
|
|
|
|
#undef CLEN
|
2016-03-23 11:23:40 -04:00
|
|
|
#define CLEN (MLEN + crypto_aead_chacha20poly1305_ietf_ABYTES)
|
2016-03-23 10:30:30 -04:00
|
|
|
static const unsigned char firstkey[crypto_aead_chacha20poly1305_ietf_KEYBYTES]
|
2015-02-11 06:34:29 -05:00
|
|
|
= {
|
|
|
|
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
|
|
|
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
|
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
|
|
|
|
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
|
|
|
|
};
|
2016-03-23 10:30:30 -04:00
|
|
|
#undef MESSAGE
|
2015-02-11 06:34:29 -05:00
|
|
|
#define MESSAGE "Ladies and Gentlemen of the class of '99: If I could offer you " \
|
|
|
|
"only one tip for the future, sunscreen would be it."
|
2016-03-23 12:22:35 -04:00
|
|
|
unsigned char *m = (unsigned char *) sodium_malloc(MLEN);
|
2016-03-23 10:30:30 -04:00
|
|
|
static const unsigned char nonce[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0x07, 0x00, 0x00, 0x00,
|
|
|
|
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
|
2016-03-23 10:55:45 -04:00
|
|
|
static const unsigned char ad[ADLEN]
|
2015-02-11 06:34:29 -05:00
|
|
|
= { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
|
2016-03-23 12:22:35 -04:00
|
|
|
unsigned char *c = (unsigned char *) sodium_malloc(CLEN);
|
|
|
|
unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);
|
|
|
|
unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_chacha20poly1305_ietf_ABYTES);
|
|
|
|
unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);
|
2016-03-23 10:30:30 -04:00
|
|
|
unsigned long long found_clen;
|
|
|
|
unsigned long long found_maclen;
|
2015-02-11 06:34:29 -05:00
|
|
|
unsigned long long m2len;
|
|
|
|
size_t i;
|
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
assert(sizeof MESSAGE - 1U == MLEN);
|
|
|
|
memcpy(m, MESSAGE, MLEN);
|
|
|
|
crypto_aead_chacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
|
|
|
|
ad, ADLEN,
|
2015-02-11 06:34:29 -05:00
|
|
|
NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != MLEN + crypto_aead_chacha20poly1305_ietf_abytes()) {
|
2016-03-23 10:30:30 -04:00
|
|
|
printf("found_clen is not properly set\n");
|
2015-02-11 06:34:29 -05:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2015-02-11 06:34:29 -05:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2016-03-23 10:30:30 -04:00
|
|
|
crypto_aead_chacha20poly1305_ietf_encrypt_detached(detached_c,
|
|
|
|
mac, &found_maclen,
|
2016-03-23 10:55:45 -04:00
|
|
|
m, MLEN,
|
|
|
|
ad, ADLEN,
|
2016-03-23 10:20:23 -04:00
|
|
|
NULL, nonce, firstkey);
|
2016-03-23 10:30:30 -04:00
|
|
|
if (found_maclen != crypto_aead_chacha20poly1305_ietf_abytes()) {
|
|
|
|
printf("found_maclen is not properly set\n");
|
2016-03-23 10:20:23 -04:00
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(detached_c, c, MLEN) != 0) {
|
2016-03-23 10:20:23 -04:00
|
|
|
printf("detached ciphertext is bogus\n");
|
|
|
|
}
|
2015-02-11 06:34:29 -05:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad,
|
|
|
|
ADLEN, nonce, firstkey) != 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt() failed\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m2len is not properly set\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m != m2\n");
|
|
|
|
}
|
2016-03-23 10:20:23 -04:00
|
|
|
memset(m2, 0, m2len);
|
2017-07-21 10:52:01 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_decrypt_detached(NULL, NULL,
|
|
|
|
c, MLEN, mac,
|
|
|
|
ad, ADLEN,
|
|
|
|
nonce, firstkey) == 0);
|
2016-03-30 15:20:34 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt_detached(m2, NULL,
|
2016-03-23 10:55:45 -04:00
|
|
|
c, MLEN, mac,
|
|
|
|
ad, ADLEN,
|
2016-03-23 10:20:23 -04:00
|
|
|
nonce, firstkey) != 0) {
|
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt_detached() failed\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2016-03-23 10:20:23 -04:00
|
|
|
printf("detached m != m2\n");
|
|
|
|
}
|
2015-02-11 06:34:29 -05:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; i++) {
|
2015-02-11 06:34:29 -05:00
|
|
|
c[i] ^= (i + 1U);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN,
|
|
|
|
ad, ADLEN, nonce, firstkey)
|
|
|
|
== 0 || memcmp(m, m2, MLEN) == 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("message can be forged\n");
|
|
|
|
}
|
|
|
|
c[i] ^= (i + 1U);
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
crypto_aead_chacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
|
2016-03-23 10:30:30 -04:00
|
|
|
NULL, 0U, NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != CLEN) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("clen is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2015-02-11 06:34:29 -05:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN,
|
2015-02-11 06:34:29 -05:00
|
|
|
NULL, 0U, nonce, firstkey) != 0) {
|
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m2len is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, m2, MLEN) != 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m != m2 (adlen=0)\n");
|
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
m2len = 1;
|
2015-02-11 06:34:29 -05:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(
|
2016-04-04 04:47:54 -04:00
|
|
|
m2, &m2len, NULL, NULL,
|
2016-04-04 04:40:23 -04:00
|
|
|
randombytes_uniform(crypto_aead_chacha20poly1305_ietf_ABYTES),
|
|
|
|
NULL, 0U, nonce, firstkey) != -1) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt() worked with a short "
|
|
|
|
"ciphertext\n");
|
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
if (m2len != 0) {
|
|
|
|
printf("Message length should have been set to zero after a failure\n");
|
|
|
|
}
|
|
|
|
m2len = 1;
|
2015-02-11 06:34:29 -05:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,
|
|
|
|
nonce, firstkey) != -1) {
|
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt() worked with an empty "
|
|
|
|
"ciphertext\n");
|
|
|
|
}
|
2016-03-30 15:20:34 -04:00
|
|
|
if (m2len != 0) {
|
|
|
|
printf("Message length should have been set to zero after a failure\n");
|
|
|
|
}
|
2015-02-11 06:34:29 -05:00
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
memcpy(c, m, MLEN);
|
|
|
|
crypto_aead_chacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,
|
2016-03-23 10:30:30 -04:00
|
|
|
NULL, 0U, NULL, nonce, firstkey);
|
2016-03-23 10:55:45 -04:00
|
|
|
if (found_clen != CLEN) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("clen is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
for (i = 0U; i < CLEN; ++i) {
|
|
|
|
printf(",0x%02x", (unsigned int) c[i]);
|
2015-02-11 06:34:29 -05:00
|
|
|
if (i % 8 == 7) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2016-03-23 10:55:45 -04:00
|
|
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
|
2016-04-04 04:40:23 -04:00
|
|
|
NULL, 0U, nonce, firstkey) != 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("crypto_aead_chacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (m2len != MLEN) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m2len is not properly set (adlen=0)\n");
|
|
|
|
}
|
2016-03-23 10:55:45 -04:00
|
|
|
if (memcmp(m, c, MLEN) != 0) {
|
2015-02-11 06:34:29 -05:00
|
|
|
printf("m != c (adlen=0)\n");
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:23:40 -04:00
|
|
|
sodium_free(c);
|
|
|
|
sodium_free(detached_c);
|
|
|
|
sodium_free(mac);
|
|
|
|
sodium_free(m2);
|
|
|
|
sodium_free(m);
|
|
|
|
|
2016-01-16 06:36:30 -05:00
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_keybytes() > 0U);
|
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_keybytes() == crypto_aead_chacha20poly1305_keybytes());
|
2015-02-11 06:34:29 -05:00
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_npubbytes() > 0U);
|
2016-01-16 06:36:30 -05:00
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_npubbytes() > crypto_aead_chacha20poly1305_npubbytes());
|
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_nsecbytes() == 0U);
|
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_nsecbytes() == crypto_aead_chacha20poly1305_nsecbytes());
|
2017-08-03 07:34:31 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_ietf_messagebytes_max() == crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX);
|
2016-01-16 06:36:30 -05:00
|
|
|
assert(crypto_aead_chacha20poly1305_IETF_KEYBYTES == crypto_aead_chacha20poly1305_ietf_KEYBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_IETF_NSECBYTES == crypto_aead_chacha20poly1305_ietf_NSECBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_IETF_NPUBBYTES == crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
|
|
|
|
assert(crypto_aead_chacha20poly1305_IETF_ABYTES == crypto_aead_chacha20poly1305_ietf_ABYTES);
|
2017-08-03 07:34:31 -04:00
|
|
|
assert(crypto_aead_chacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX);
|
2015-02-11 06:34:29 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
tv();
|
|
|
|
tv_ietf();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|