Add more HMAC examples
diff --git a/src/MacMessage.c b/src/MacMessage.c
index 1c86a84..83e6a0e 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.c
@@ -448,10 +448,19 @@
alg = (int)cn->v.uint;
switch (alg) {
+ case COSE_Algorithm_HMAC_256_64:
case COSE_Algorithm_HMAC_256_256:
cbitKey = 256;
break;
+ case COSE_Algorithm_HMAC_384_384:
+ cbitKey = 384;
+ break;
+
+ case COSE_Algorithm_HMAC_512_512:
+ cbitKey = 512;
+ break;
+
case COSE_Int_Alg_AES_CBC_MAC_256_64:
default:
FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM);
@@ -514,6 +523,18 @@
if (!HMAC_Validate(pcose, 256, 256, pbAuthData, cbAuthData, perr)) goto errorReturn;
break;
+ case COSE_Algorithm_HMAC_256_64:
+ if (!HMAC_Validate(pcose, 256, 64, pbAuthData, cbAuthData, perr)) goto errorReturn;
+ break;
+
+ case COSE_Algorithm_HMAC_384_384:
+ if (!HMAC_Validate(pcose, 384, 384, pbAuthData, cbAuthData, perr)) goto errorReturn;
+ break;
+
+ case COSE_Algorithm_HMAC_512_512:
+ if (!HMAC_Validate(pcose, 512, 512, pbAuthData, cbAuthData, perr)) goto errorReturn;
+ break;
+
case COSE_Int_Alg_AES_CBC_MAC_256_64:
if (!AES_CBC_MAC_Validate(pcose, 256, 64, pbAuthData, cbAuthData, perr)) goto errorReturn;
break;
diff --git a/src/cose.h b/src/cose.h
index aedd499..3259957 100644
--- a/src/cose.h
+++ b/src/cose.h
@@ -76,6 +76,9 @@
typedef enum {
COSE_Algorithm_HMAC_256_256 = 4,
+ COSE_Algorithm_HMAC_384_384 = 5,
+ COSE_Algorithm_HMAC_512_512 = 6,
+ COSE_Algorithm_HMAC_256_64 = 7,
COSE_Algorithm_AES_CCM_16_64_128 = 10,
COSE_Algorithm_AES_CCM_16_64_256 = 11,
diff --git a/src/openssl.c b/src/openssl.c
index 29610b3..da69d08 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -315,6 +315,8 @@
switch (HSize) {
case 256: pmd = EVP_sha256(); break;
+ case 384: pmd = EVP_sha384(); break;
+ case 512: pmd = EVP_sha512(); break;
default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); break;
}
@@ -331,7 +333,7 @@
#else
#endif
- if (cn->length != (int) cbOut) return false;
+ if (cn->length > (int) cbOut) return false;
for (i = 0; i < (unsigned int) TSize/8; i++) f |= (cn->v.bytes[i] != rgbOut[i]);
HMAC_cleanup(&ctx);
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 2cb21c4..431c334 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -32,5 +32,9 @@
add_test ( NAME mac-04 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/spec-examples/Mac-04.json )
add_test ( NAME hmac-01 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/hmac-examples/HMac-01.json )
+add_test ( NAME hmac-02 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/hmac-examples/HMac-02.json )
+add_test ( NAME hmac-03 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/hmac-examples/HMac-03.json )
+add_test ( NAME hmac-04 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/hmac-examples/HMac-04.json )
+add_test ( NAME hmac-05 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test Examples/hmac-examples/HMac-05.json )
diff --git a/test/json.c b/test/json.c
index db1d023..996e0c6 100644
--- a/test/json.c
+++ b/test/json.c
@@ -60,7 +60,22 @@
ib = ib2;
break;
+ case't':
+ if (strncmp(&rgch[ib], "true", 4) != 0) goto error;
+ node = cn_cbor_data_create(NULL, 0, NULL, NULL);
+ node->type = CN_CBOR_TRUE;
+ ib += 3;
+ break;
+
+ case'f':
+ if (strncmp(&rgch[ib], "false", 5) != 0) goto error;
+ node = cn_cbor_data_create(NULL, 0, NULL, NULL);
+ node->type = CN_CBOR_FALSE;
+ ib += 4;
+ break;
+
default:
+ error:
fprintf(stderr, "Parse failure @ '%s'\n", &rgch[ib]);
break;
}
diff --git a/test/test.c b/test/test.c
index fd3dd55..c6175b0 100644
--- a/test/test.c
+++ b/test/test.c
@@ -160,11 +160,19 @@
int cbEncoded;
byte * pbEncoded = GetCBOREncoding(pControl, &cbEncoded);
const cn_cbor * pInput = cn_cbor_mapget_string(pControl, "input");
+ const cn_cbor * pFail;
const cn_cbor * pMac;
const cn_cbor * pRecipients;
HCOSE_MAC hMAC;
int type;
int iRecipient;
+ bool fFail = false;
+ bool fFailBody = false;
+
+ pFail = cn_cbor_mapget_string(pControl, "fail");
+ if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
+ fFailBody = true;
+ }
hMAC = (HCOSE_MAC) COSE_Decode(pbEncoded, cbEncoded, &type, COSE_mac_object, NULL, NULL);
if (hMAC == NULL) exit(1);
@@ -179,12 +187,37 @@
pRecipients = pRecipients->first_child;
for (iRecipient = 0; pRecipients != NULL; iRecipient++,pRecipients=pRecipients->next) {
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"));
+ if (pkey == NULL) {
+ fFail = true;
+ continue;
+ }
HCOSE_RECIPIENT hRecip = COSE_Mac_GetRecipient(hMAC, iRecipient, NULL);
- COSE_Recipient_SetKey(hRecip, pkey, NULL);
- if (!COSE_Mac_validate(hMAC, hRecip, NULL)) CFails += 1;
+ if (hRecip == NULL) {
+ fFail = true;
+ continue;
+ }
+
+ if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) {
+ fFail = true;
+ continue;
+ }
+
+ pFail = cn_cbor_mapget_string(pRecipients, "fail");
+ if (COSE_Mac_validate(hMAC, hRecip, NULL)) {
+ if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) fFail = true;
+ }
+ else {
+ if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) fFail = true;
+ }
}
+ if (fFailBody) {
+ if (!fFail) fFail = true;
+ else fFail = false;
+ }
+
+ if (fFail) CFails += 1;
return 0;
}