/* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. */ #include "tomcrypt.h" #ifdef LTC_CHACHA20POLY1305_MODE int chacha20poly1305_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else chachapoly_state st1, st2; unsigned char k[] = { 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 }; unsigned char iv[] = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 }; unsigned char aad[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; unsigned char enc[] = { 0xD3, 0x1A, 0x8D, 0x34, 0x64, 0x8E, 0x60, 0xDB, 0x7B, 0x86, 0xAF, 0xBC, 0x53, 0xEF, 0x7E, 0xC2, 0xA4, 0xAD, 0xED, 0x51, 0x29, 0x6E, 0x08, 0xFE, 0xA9, 0xE2, 0xB5, 0xA7, 0x36, 0xEE, 0x62, 0xD6, 0x3D, 0xBE, 0xA4, 0x5E, 0x8C, 0xA9, 0x67, 0x12, 0x82, 0xFA, 0xFB, 0x69, 0xDA, 0x92, 0x72, 0x8B, 0x1A, 0x71, 0xDE, 0x0A, 0x9E, 0x06, 0x0B, 0x29, 0x05, 0xD6, 0xA5, 0xB6, 0x7E, 0xCD, 0x3B, 0x36, 0x92, 0xDD, 0xBD, 0x7F, 0x2D, 0x77, 0x8B, 0x8C, 0x98, 0x03, 0xAE, 0xE3, 0x28, 0x09, 0x1B, 0x58, 0xFA, 0xB3, 0x24, 0xE4, 0xFA, 0xD6, 0x75, 0x94, 0x55, 0x85, 0x80, 0x8B, 0x48, 0x31, 0xD7, 0xBC, 0x3F, 0xF4, 0xDE, 0xF0, 0x8E, 0x4B, 0x7A, 0x9D, 0xE5, 0x76, 0xD2, 0x65, 0x86, 0xCE, 0xC6, 0x4B, 0x61, 0x16 }; unsigned char tag[] = { 0x1A, 0xE1, 0x0B, 0x59, 0x4F, 0x09, 0xE2, 0x6A, 0x7E, 0x90, 0x2E, 0xCB, 0xD0, 0x60, 0x06, 0x91 }; char m[] = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."; unsigned long mlen = strlen(m); unsigned long len; unsigned char ct[1000], pt[1000], emac[16], dmac[16]; /* encrypt */ chacha20poly1305_init(&st1, k, sizeof(k)); chacha20poly1305_setiv(&st1, iv, sizeof(iv)); chacha20poly1305_add_aad(&st1, aad, sizeof(aad)); /* encrypt piece by piece */ chacha20poly1305_encrypt(&st1, (unsigned char *)m, 25, ct); chacha20poly1305_encrypt(&st1, (unsigned char *)m + 25, 10, ct + 25); chacha20poly1305_encrypt(&st1, (unsigned char *)m + 35, 35, ct + 35); chacha20poly1305_encrypt(&st1, (unsigned char *)m + 70, 5, ct + 70); chacha20poly1305_encrypt(&st1, (unsigned char *)m + 75, 5, ct + 75); chacha20poly1305_encrypt(&st1, (unsigned char *)m + 80, mlen - 80, ct + 80); len = sizeof(emac); chacha20poly1305_done(&st1, emac, &len); if (compare_testvector(ct, mlen, enc, sizeof(enc), "ENC-CT", 1) != 0) return CRYPT_FAIL_TESTVECTOR; if (compare_testvector(emac, len, tag, sizeof(tag), "ENC-TAG", 2) != 0) return CRYPT_FAIL_TESTVECTOR; /* decrypt */ chacha20poly1305_init(&st2, k, len = sizeof(k)); chacha20poly1305_setiv(&st2, iv, len = sizeof(iv)); chacha20poly1305_add_aad(&st2, aad, len = sizeof(aad)); chacha20poly1305_decrypt(&st2, ct, 21, pt); chacha20poly1305_decrypt(&st2, ct + 21, mlen - 21, pt + 21); len = sizeof(dmac); chacha20poly1305_done(&st2, dmac, &len); if (compare_testvector(pt, mlen, m, mlen, "DEC-PT", 3) != 0) return CRYPT_FAIL_TESTVECTOR; if (compare_testvector(dmac, len, tag, sizeof(tag), "DEC-TAG", 4) != 0) return CRYPT_FAIL_TESTVECTOR; return CRYPT_OK; #endif }; #endif