more error checking in *_test

This commit is contained in:
Karel Miko 2017-04-25 19:46:05 +02:00
parent 90b482aa1e
commit eb209aa2f9
2 changed files with 56 additions and 53 deletions

View File

@ -37,87 +37,88 @@ int chacha20poly1305_test(void)
unsigned char rfc7905_enc[] = { 0xE4, 0x62, 0x85, 0xB4, 0x29, 0x95, 0x34, 0x96, 0xAB, 0xFB, 0x67, 0xCD, 0xAE, 0xAC, 0x94, 0x1E };
unsigned char rfc7905_tag[] = { 0x16, 0x2C, 0x92, 0x48, 0x2A, 0xDB, 0xD3, 0x5D, 0x48, 0xBE, 0xC6, 0xFF, 0x10, 0x9C, 0xBA, 0xE4 };
unsigned char ct[1000], pt[1000], emac[16], dmac[16];
int err;
/* encrypt IV 96bit */
chacha20poly1305_init(&st1, k, sizeof(k));
chacha20poly1305_setiv(&st1, i12, sizeof(i12));
chacha20poly1305_add_aad(&st1, aad, sizeof(aad));
if ((err = chacha20poly1305_init(&st1, k, sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv(&st1, i12, sizeof(i12))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st1, aad, sizeof(aad))) != CRYPT_OK) return err;
/* 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);
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m, 25, ct)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m + 25, 10, ct + 25)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m + 35, 35, ct + 35)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m + 70, 5, ct + 70)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m + 75, 5, ct + 75)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m + 80, mlen - 80, ct + 80)) != CRYPT_OK) return err;
len = sizeof(emac);
chacha20poly1305_done(&st1, emac, &len);
if ((err = chacha20poly1305_done(&st1, emac, &len)) != CRYPT_OK) return err;
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 IV 96bit */
chacha20poly1305_init(&st2, k, len = sizeof(k));
chacha20poly1305_setiv(&st2, i12, len = sizeof(i12));
chacha20poly1305_add_aad(&st2, aad, len = sizeof(aad));
chacha20poly1305_decrypt(&st2, ct, 21, pt);
chacha20poly1305_decrypt(&st2, ct + 21, mlen - 21, pt + 21);
if ((err = chacha20poly1305_init(&st2, k, len = sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv(&st2, i12, len = sizeof(i12))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st2, aad, len = sizeof(aad))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_decrypt(&st2, ct, 21, pt)) != CRYPT_OK) return err;
if ((err = chacha20poly1305_decrypt(&st2, ct + 21, mlen - 21, pt + 21)) != CRYPT_OK) return err;
len = sizeof(dmac);
chacha20poly1305_done(&st2, dmac, &len);
if ((err = chacha20poly1305_done(&st2, dmac, &len)) != CRYPT_OK) return err;
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;
/* chacha20poly1305_memory - encrypt */
len = sizeof(emac);
chacha20poly1305_memory(k, sizeof(k), i12, sizeof(i12), aad, sizeof(aad),
(unsigned char *)m, mlen, ct, emac, &len, CHCHA20POLY1305_ENCRYPT);
if ((err = chacha20poly1305_memory(k, sizeof(k), i12, sizeof(i12), aad, sizeof(aad), (unsigned char *)m,
mlen, ct, emac, &len, CHCHA20POLY1305_ENCRYPT)) != CRYPT_OK) return err;
if (compare_testvector(ct, mlen, enc, sizeof(enc), "ENC-CT2", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if (compare_testvector(emac, len, tag, sizeof(tag), "ENC-TAG2", 2) != 0) return CRYPT_FAIL_TESTVECTOR;
/* chacha20poly1305_memory - decrypt */
len = sizeof(dmac);
chacha20poly1305_memory(k, sizeof(k), i12, sizeof(i12), aad, sizeof(aad),
ct, mlen, pt, dmac, &len, CHCHA20POLY1305_DECRYPT);
if ((err = chacha20poly1305_memory(k, sizeof(k), i12, sizeof(i12), aad, sizeof(aad),
ct, mlen, pt, dmac, &len, CHCHA20POLY1305_DECRYPT)) != CRYPT_OK) return err;
if (compare_testvector(pt, mlen, m, mlen, "DEC-PT2", 3) != 0) return CRYPT_FAIL_TESTVECTOR;
if (compare_testvector(dmac, len, tag, sizeof(tag), "DEC-TAG2", 4) != 0) return CRYPT_FAIL_TESTVECTOR;
/* encrypt - rfc7905 */
chacha20poly1305_init(&st1, k, sizeof(k));
chacha20poly1305_setiv_rfc7905(&st1, i12, sizeof(i12), CONST64(0x1122334455667788));
chacha20poly1305_add_aad(&st1, aad, sizeof(aad));
chacha20poly1305_encrypt(&st1, rfc7905_pt, 16, ct);
if ((err = chacha20poly1305_init(&st1, k, sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv_rfc7905(&st1, i12, sizeof(i12), CONST64(0x1122334455667788))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st1, aad, sizeof(aad))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, rfc7905_pt, 16, ct)) != CRYPT_OK) return err;
len = sizeof(emac);
chacha20poly1305_done(&st1, emac, &len);
if ((err = chacha20poly1305_done(&st1, emac, &len)) != CRYPT_OK) return err;
if (compare_testvector(ct, 16, rfc7905_enc, 16, "ENC-CT3", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if (compare_testvector(emac, len, rfc7905_tag, 16, "ENC-TAG3", 2) != 0) return CRYPT_FAIL_TESTVECTOR;
/* decrypt - rfc7905 */
chacha20poly1305_init(&st1, k, sizeof(k));
chacha20poly1305_setiv_rfc7905(&st1, i12, sizeof(i12), CONST64(0x1122334455667788));
chacha20poly1305_add_aad(&st1, aad, sizeof(aad));
chacha20poly1305_decrypt(&st1, ct, 16, pt);
if ((err = chacha20poly1305_init(&st1, k, sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv_rfc7905(&st1, i12, sizeof(i12), CONST64(0x1122334455667788))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st1, aad, sizeof(aad))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_decrypt(&st1, ct, 16, pt)) != CRYPT_OK) return err;
len = sizeof(dmac);
chacha20poly1305_done(&st1, dmac, &len);
if ((err = chacha20poly1305_done(&st1, dmac, &len)) != CRYPT_OK) return err;
if (compare_testvector(pt, 16, rfc7905_pt, 16, "DEC-CT3", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if (compare_testvector(dmac, len, rfc7905_tag, 16, "DEC-TAG3", 2) != 0) return CRYPT_FAIL_TESTVECTOR;
/* encrypt IV 64bit */
chacha20poly1305_init(&st1, k, sizeof(k));
chacha20poly1305_setiv(&st1, i8, sizeof(i8));
chacha20poly1305_add_aad(&st1, aad, sizeof(aad));
chacha20poly1305_encrypt(&st1, (unsigned char *)m, mlen, ct);
if ((err = chacha20poly1305_init(&st1, k, sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv(&st1, i8, sizeof(i8))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st1, aad, sizeof(aad))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_encrypt(&st1, (unsigned char *)m, mlen, ct)) != CRYPT_OK) return err;
len = sizeof(emac);
chacha20poly1305_done(&st1, emac, &len);
if ((err = chacha20poly1305_done(&st1, emac, &len)) != CRYPT_OK) return err;
/* decrypt IV 96bit */
chacha20poly1305_init(&st2, k, len = sizeof(k));
chacha20poly1305_setiv(&st2, i8, len = sizeof(i8));
chacha20poly1305_add_aad(&st2, aad, len = sizeof(aad));
chacha20poly1305_decrypt(&st2, ct, mlen, pt);
if ((err = chacha20poly1305_init(&st2, k, len = sizeof(k))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_setiv(&st2, i8, len = sizeof(i8))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_add_aad(&st2, aad, len = sizeof(aad))) != CRYPT_OK) return err;
if ((err = chacha20poly1305_decrypt(&st2, ct, mlen, pt)) != CRYPT_OK) return err;
len = sizeof(dmac);
chacha20poly1305_done(&st2, dmac, &len);
if ((err = chacha20poly1305_done(&st2, dmac, &len)) != CRYPT_OK) return err;
if (compare_testvector(pt, mlen, m, mlen, "DEC-PT4", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if (compare_testvector(dmac, len, emac, len, "DEC-TAG4", 2) != 0) return CRYPT_FAIL_TESTVECTOR;

View File

@ -28,21 +28,23 @@ int poly1305_test(void)
unsigned long len = 16, mlen = strlen(m);
unsigned char out[1000];
poly1305_state st;
int err;
/* process piece by piece */
poly1305_init(&st, k, 32);
poly1305_process(&st, (unsigned char*)m, 5);
poly1305_process(&st, (unsigned char*)m + 5, 4);
poly1305_process(&st, (unsigned char*)m + 9, 3);
poly1305_process(&st, (unsigned char*)m + 12, 2);
poly1305_process(&st, (unsigned char*)m + 14, 1);
poly1305_process(&st, (unsigned char*)m + 15, mlen - 15);
poly1305_done(&st, out, &len);
if (compare_testvector(out, len, tag, sizeof(tag), "POLY1305-TV1", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if ((err = poly1305_init(&st, k, 32)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m, 5)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m + 5, 4)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m + 9, 3)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m + 12, 2)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m + 14, 1)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m + 15, mlen - 15)) != CRYPT_OK) return err;
if ((err = poly1305_done(&st, out, &len)) != CRYPT_OK) return err;
if (compare_testvector(out, len, tag, sizeof(tag), "POLY1305-TV1", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
/* process in one go */
poly1305_init(&st, k, 32);
poly1305_process(&st, (unsigned char*)m, mlen);
poly1305_done(&st, out, &len);
if (compare_testvector(out, len, tag, sizeof(tag), "POLY1305-TV2", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
if ((err = poly1305_init(&st, k, 32)) != CRYPT_OK) return err;
if ((err = poly1305_process(&st, (unsigned char*)m, mlen)) != CRYPT_OK) return err;
if ((err = poly1305_done(&st, out, &len)) != CRYPT_OK) return err;
if (compare_testvector(out, len, tag, sizeof(tag), "POLY1305-TV2", 1) != 0) return CRYPT_FAIL_TESTVECTOR;
return CRYPT_OK;
#endif
}