Path: blob/main/sys/contrib/libsodium/test/default/aead_xchacha20poly1305.c
48375 views
1#define TEST_NAME "aead_xchacha20poly1305"2#include "cmptest.h"34static int5tv(void)6{7#undef MLEN8#define MLEN 114U9#undef ADLEN10#define ADLEN 12U11#undef CLEN12#define CLEN (MLEN + crypto_aead_xchacha20poly1305_ietf_ABYTES)13static const unsigned char firstkey[crypto_aead_xchacha20poly1305_ietf_KEYBYTES]14= {150x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,160x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,170x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,180x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f19};20#undef MESSAGE21#define MESSAGE "Ladies and Gentlemen of the class of '99: If I could offer you " \22"only one tip for the future, sunscreen would be it."23unsigned char *m = (unsigned char *) sodium_malloc(MLEN);24static const unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES]25= { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,260x48, 0x49, 0x4a, 0x4b };27static const unsigned char ad[ADLEN]28= { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };29unsigned char *c = (unsigned char *) sodium_malloc(CLEN);30unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);31unsigned char *key2 = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES);32unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_ABYTES);33unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);34unsigned long long found_clen;35unsigned long long found_maclen;36unsigned long long m2len;37size_t i;3839assert(sizeof MESSAGE - 1U == MLEN);40memcpy(m, MESSAGE, MLEN);41crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,42ad, ADLEN,43NULL, nonce, firstkey);44if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) {45printf("found_clen is not properly set\n");46}47for (i = 0U; i < CLEN; ++i) {48printf(",0x%02x", (unsigned int) c[i]);49if (i % 8 == 7) {50printf("\n");51}52}53printf("\n");54crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c,55mac, &found_maclen,56m, MLEN,57ad, ADLEN,58NULL, nonce, firstkey);59if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) {60printf("found_maclen is not properly set\n");61}62if (memcmp(detached_c, c, MLEN) != 0) {63printf("detached ciphertext is bogus\n");64}6566if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad,67ADLEN, nonce, firstkey) != 0) {68printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n");69}70if (m2len != MLEN) {71printf("m2len is not properly set\n");72}73if (memcmp(m, m2, MLEN) != 0) {74printf("m != m2\n");75}76memset(m2, 0, m2len);77if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL,78c, MLEN, mac,79ad, ADLEN,80nonce, firstkey) != 0) {81printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n");82}83if (memcmp(m, m2, MLEN) != 0) {84printf("detached m != m2\n");85}8687for (i = 0U; i < CLEN; i++) {88c[i] ^= (i + 1U);89if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN,90ad, ADLEN, nonce, firstkey)91== 0 || memcmp(m, m2, MLEN) == 0) {92printf("message can be forged\n");93}94c[i] ^= (i + 1U);95}96crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,97NULL, 0U, NULL, nonce, firstkey);98if (found_clen != CLEN) {99printf("clen is not properly set (adlen=0)\n");100}101for (i = 0U; i < CLEN; ++i) {102printf(",0x%02x", (unsigned int) c[i]);103if (i % 8 == 7) {104printf("\n");105}106}107printf("\n");108if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN,109NULL, 0U, nonce, firstkey) != 0) {110printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");111}112if (m2len != MLEN) {113printf("m2len is not properly set (adlen=0)\n");114}115if (memcmp(m, m2, MLEN) != 0) {116printf("m != m2 (adlen=0)\n");117}118m2len = 1;119if (crypto_aead_xchacha20poly1305_ietf_decrypt(120m2, &m2len, NULL, NULL,121randombytes_uniform(crypto_aead_xchacha20poly1305_ietf_ABYTES),122NULL, 0U, nonce, firstkey) != -1) {123printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with a short "124"ciphertext\n");125}126if (m2len != 0) {127printf("Message length should have been set to zero after a failure\n");128}129m2len = 1;130if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,131nonce, firstkey) != -1) {132printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with an empty "133"ciphertext\n");134}135if (m2len != 0) {136printf("Message length should have been set to zero after a failure\n");137}138139memcpy(c, m, MLEN);140crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,141NULL, 0U, NULL, nonce, firstkey);142if (found_clen != CLEN) {143printf("clen is not properly set (adlen=0)\n");144}145for (i = 0U; i < CLEN; ++i) {146printf(",0x%02x", (unsigned int) c[i]);147if (i % 8 == 7) {148printf("\n");149}150}151printf("\n");152153if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,154NULL, 0U, nonce, firstkey) != 0) {155printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");156}157if (m2len != MLEN) {158printf("m2len is not properly set (adlen=0)\n");159}160if (memcmp(m, c, MLEN) != 0) {161printf("m != c (adlen=0)\n");162}163164crypto_aead_xchacha20poly1305_ietf_keygen(key2);165if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,166NULL, 0U, nonce, key2) == 0) {167printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n");168}169170sodium_free(c);171sodium_free(detached_c);172sodium_free(key2);173sodium_free(mac);174sodium_free(m2);175sodium_free(m);176177assert(crypto_aead_xchacha20poly1305_ietf_abytes() == crypto_aead_xchacha20poly1305_ietf_ABYTES);178assert(crypto_aead_xchacha20poly1305_ietf_keybytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);179assert(crypto_aead_xchacha20poly1305_ietf_npubbytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);180assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == 0U);181assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);182assert(crypto_aead_xchacha20poly1305_ietf_messagebytes_max() == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);183assert(crypto_aead_xchacha20poly1305_IETF_KEYBYTES == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);184assert(crypto_aead_xchacha20poly1305_IETF_NSECBYTES == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);185assert(crypto_aead_xchacha20poly1305_IETF_NPUBBYTES == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);186assert(crypto_aead_xchacha20poly1305_IETF_ABYTES == crypto_aead_xchacha20poly1305_ietf_ABYTES);187assert(crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);188189return 0;190}191192int193main(void)194{195tv();196197return 0;198}199200201