also run testvectors on new CCM API

[skip ci]
This commit is contained in:
Steffen Jaeckel 2014-10-31 19:41:14 +01:00
parent 4efa27ead1
commit 943a858d6c

View File

@ -17,6 +17,10 @@
#ifdef LTC_CCM_MODE #ifdef LTC_CCM_MODE
#if defined(LTC_CCM_TEST_DBG) && !defined(LTC_NO_TEST)
void print_hex(const char* what, const unsigned char* p, const unsigned long l);
#endif
int ccm_test(void) int ccm_test(void)
{ {
#ifndef LTC_TEST #ifndef LTC_TEST
@ -32,7 +36,7 @@ int ccm_test(void)
int ptlen; int ptlen;
unsigned char ct[64]; unsigned char ct[64];
unsigned char tag[16]; unsigned char tag[16];
int taglen; unsigned long taglen;
} tests[] = { } tests[] = {
/* 13 byte nonce, 8 byte auth, 23 byte pt */ /* 13 byte nonce, 8 byte auth, 23 byte pt */
@ -113,10 +117,11 @@ int ccm_test(void)
}, },
}; };
unsigned long taglen, x; unsigned long taglen, x, y;
unsigned char buf[64], buf2[64], tag2[16], tag[16]; unsigned char buf[64], buf2[64], tag2[16], tag[16];
int err, idx; int err, idx;
symmetric_key skey; symmetric_key skey;
ccm_state ccm;
idx = find_cipher("aes"); idx = find_cipher("aes");
if (idx == -1) { if (idx == -1) {
@ -127,47 +132,108 @@ int ccm_test(void)
} }
for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) { for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
for (y = 0; y < 2; y++) {
taglen = tests[x].taglen; taglen = tests[x].taglen;
if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) { if (y == 0) {
return err; if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
} return err;
}
if ((err = ccm_memory(idx, if ((err = ccm_memory(idx,
tests[x].key, 16, tests[x].key, 16,
&skey, &skey,
tests[x].nonce, tests[x].noncelen, tests[x].nonce, tests[x].noncelen,
tests[x].header, tests[x].headerlen, tests[x].header, tests[x].headerlen,
(unsigned char*)tests[x].pt, tests[x].ptlen, (unsigned char*)tests[x].pt, tests[x].ptlen,
buf, buf,
tag, &taglen, 0)) != CRYPT_OK) { tag, &taglen, 0)) != CRYPT_OK) {
return err; return err;
}
} else {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, 0)) != CRYPT_OK) {
return err;
}
if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
return err;
}
if ((err = ccm_process(&ccm, (unsigned char*)tests[x].pt, tests[x].ptlen, buf, CCM_ENCRYPT)) != CRYPT_OK) {
return err;
}
if ((err = ccm_done(&ccm, tag, &taglen)) != CRYPT_OK) {
return err;
}
} }
if (XMEMCMP(buf, tests[x].ct, tests[x].ptlen)) { if (XMEMCMP(buf, tests[x].ct, tests[x].ptlen)) {
#if defined(LTC_CCM_TEST_DBG)
printf("\n%d: x=%lu y=%lu\n", __LINE__, x, y);
print_hex("ct is ", tag, taglen);
print_hex("ct should", tests[x].tag, taglen);
#endif
return CRYPT_FAIL_TESTVECTOR;
}
if (tests[x].taglen != taglen) {
#if defined(LTC_CCM_TEST_DBG)
printf("\n%d: x=%lu y=%lu\n", __LINE__, x, y);
printf("taglen %lu (is) %lu (should)\n", taglen, tests[x].taglen);
#endif
return CRYPT_FAIL_TESTVECTOR; return CRYPT_FAIL_TESTVECTOR;
} }
if (XMEMCMP(tag, tests[x].tag, tests[x].taglen)) { if (XMEMCMP(tag, tests[x].tag, tests[x].taglen)) {
#if defined(LTC_CCM_TEST_DBG)
printf("\n%d: x=%lu y=%lu\n", __LINE__, x, y);
print_hex("tag is ", tag, taglen);
print_hex("tag should", tests[x].tag, taglen);
#endif
return CRYPT_FAIL_TESTVECTOR; return CRYPT_FAIL_TESTVECTOR;
} }
if ((err = ccm_memory(idx, if (y == 0) {
tests[x].key, 16, if ((err = ccm_memory(idx,
NULL, tests[x].key, 16,
tests[x].nonce, tests[x].noncelen, NULL,
tests[x].header, tests[x].headerlen, tests[x].nonce, tests[x].noncelen,
buf2, tests[x].ptlen, tests[x].header, tests[x].headerlen,
buf, buf2, tests[x].ptlen,
tag2, &taglen, 1 )) != CRYPT_OK) { buf,
return err; tag2, &taglen, 1 )) != CRYPT_OK) {
return err;
}
} else {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, 0)) != CRYPT_OK) {
return err;
}
if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
return err;
}
if ((err = ccm_process(&ccm, buf2, tests[x].ptlen, buf, CCM_DECRYPT)) != CRYPT_OK) {
return err;
}
if ((err = ccm_done(&ccm, tag2, &taglen)) != CRYPT_OK) {
return err;
}
} }
if (XMEMCMP(buf2, tests[x].pt, tests[x].ptlen)) { if (XMEMCMP(buf2, tests[x].pt, tests[x].ptlen)) {
#if defined(LTC_CCM_TEST_DBG)
printf("\n%d: x=%lu y=%lu\n", __LINE__, x, y);
print_hex("pt is ", tag, taglen);
print_hex("pt should", tests[x].tag, taglen);
#endif
return CRYPT_FAIL_TESTVECTOR; return CRYPT_FAIL_TESTVECTOR;
} }
if (XMEMCMP(tag2, tests[x].tag, tests[x].taglen)) { if (XMEMCMP(tag2, tests[x].tag, tests[x].taglen)) {
#if defined(LTC_CCM_TEST_DBG)
printf("\n%d: x=%lu y=%lu\n", __LINE__, x, y);
print_hex("tag is ", tag, taglen);
print_hex("tag should", tests[x].tag, taglen);
#endif
return CRYPT_FAIL_TESTVECTOR; return CRYPT_FAIL_TESTVECTOR;
} }
cipher_descriptor[idx].done(&skey); if (y == 0) {
cipher_descriptor[idx].done(&skey);
}
}
} }
return CRYPT_OK; return CRYPT_OK;
#endif #endif