|
@@ -17,6 +17,10 @@
|
|
|
|
|
|
#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)
|
|
|
{
|
|
|
#ifndef LTC_TEST
|
|
@@ -32,14 +36,14 @@ int ccm_test(void)
|
|
|
int ptlen;
|
|
|
unsigned char ct[64];
|
|
|
unsigned char tag[16];
|
|
|
- int taglen;
|
|
|
+ unsigned long taglen;
|
|
|
} tests[] = {
|
|
|
|
|
|
/* 13 byte nonce, 8 byte auth, 23 byte pt */
|
|
|
{
|
|
|
- { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
|
|
+ { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
|
|
0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
|
|
|
- { 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0,
|
|
|
+ { 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0,
|
|
|
0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
|
|
|
13,
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
|
|
@@ -57,20 +61,20 @@ int ccm_test(void)
|
|
|
|
|
|
/* 13 byte nonce, 12 byte header, 19 byte pt */
|
|
|
{
|
|
|
- { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
|
|
+ { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
|
|
|
0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
|
|
|
- { 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03, 0xA0,
|
|
|
+ { 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03, 0xA0,
|
|
|
0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
|
|
|
13,
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
0x08, 0x09, 0x0A, 0x0B },
|
|
|
12,
|
|
|
- { 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
|
|
|
- 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
|
|
|
+ { 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
|
|
|
+ 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
|
|
|
0x1C, 0x1D, 0x1E },
|
|
|
19,
|
|
|
- { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79,
|
|
|
- 0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91,
|
|
|
+ { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79,
|
|
|
+ 0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91,
|
|
|
0xCD, 0xAC, 0x8C },
|
|
|
{ 0x96, 0xC8, 0x61, 0xB9, 0xC9, 0xE6, 0x1E, 0xF1 },
|
|
|
8
|
|
@@ -78,7 +82,7 @@ int ccm_test(void)
|
|
|
|
|
|
/* supplied by Brian Gladman */
|
|
|
{
|
|
|
- { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
+ { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f },
|
|
|
{ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 },
|
|
|
7,
|
|
@@ -92,31 +96,32 @@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, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c,
|
|
|
0x3c, 0x04, 0xd0, 0x19 },
|
|
|
{ 0x78, 0x45, 0xce, 0x0b, 0x16, 0xf9, 0x76, 0x23 },
|
|
|
8
|
|
|
},
|
|
|
|
|
|
};
|
|
|
- unsigned long taglen, x;
|
|
|
+ unsigned long taglen, x, y;
|
|
|
unsigned char buf[64], buf2[64], tag2[16], tag[16];
|
|
|
int err, idx;
|
|
|
symmetric_key skey;
|
|
|
+ ccm_state ccm;
|
|
|
|
|
|
idx = find_cipher("aes");
|
|
|
if (idx == -1) {
|
|
@@ -127,47 +132,114 @@ int ccm_test(void)
|
|
|
}
|
|
|
|
|
|
for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
|
|
|
+ for (y = 0; y < 2; y++) {
|
|
|
taglen = tests[x].taglen;
|
|
|
- if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
|
|
|
- return err;
|
|
|
- }
|
|
|
-
|
|
|
- if ((err = ccm_memory(idx,
|
|
|
- tests[x].key, 16,
|
|
|
- &skey,
|
|
|
- tests[x].nonce, tests[x].noncelen,
|
|
|
- tests[x].header, tests[x].headerlen,
|
|
|
- (unsigned char*)tests[x].pt, tests[x].ptlen,
|
|
|
- buf,
|
|
|
- tag, &taglen, 0)) != CRYPT_OK) {
|
|
|
- return err;
|
|
|
+ if (y == 0) {
|
|
|
+ if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((err = ccm_memory(idx,
|
|
|
+ tests[x].key, 16,
|
|
|
+ &skey,
|
|
|
+ tests[x].nonce, tests[x].noncelen,
|
|
|
+ tests[x].header, tests[x].headerlen,
|
|
|
+ (unsigned char*)tests[x].pt, tests[x].ptlen,
|
|
|
+ buf,
|
|
|
+ tag, &taglen, 0)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ if ((err = ccm_add_aad(&ccm, tests[x].header, tests[x].headerlen)) != 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 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;
|
|
|
}
|
|
|
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, tests[x].taglen);
|
|
|
+ print_hex("tag should", tests[x].tag, tests[x].taglen);
|
|
|
+#endif
|
|
|
return CRYPT_FAIL_TESTVECTOR;
|
|
|
}
|
|
|
|
|
|
- if ((err = ccm_memory(idx,
|
|
|
- tests[x].key, 16,
|
|
|
- NULL,
|
|
|
- tests[x].nonce, tests[x].noncelen,
|
|
|
- tests[x].header, tests[x].headerlen,
|
|
|
- buf2, tests[x].ptlen,
|
|
|
- buf,
|
|
|
- tag2, &taglen, 1 )) != CRYPT_OK) {
|
|
|
- return err;
|
|
|
+ if (y == 0) {
|
|
|
+ if ((err = ccm_memory(idx,
|
|
|
+ tests[x].key, 16,
|
|
|
+ NULL,
|
|
|
+ tests[x].nonce, tests[x].noncelen,
|
|
|
+ tests[x].header, tests[x].headerlen,
|
|
|
+ buf2, tests[x].ptlen,
|
|
|
+ buf,
|
|
|
+ tag2, &taglen, 1 )) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
|
|
|
+ return err;
|
|
|
+ }
|
|
|
+ if ((err = ccm_add_aad(&ccm, tests[x].header, tests[x].headerlen)) != 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 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;
|
|
|
}
|
|
|
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, tests[x].taglen);
|
|
|
+ print_hex("tag should", tests[x].tag, tests[x].taglen);
|
|
|
+#endif
|
|
|
return CRYPT_FAIL_TESTVECTOR;
|
|
|
}
|
|
|
- cipher_descriptor[idx].done(&skey);
|
|
|
+ if (y == 0) {
|
|
|
+ cipher_descriptor[idx].done(&skey);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return CRYPT_OK;
|
|
|
#endif
|