static void test_errors(void) { cbor_failure tests[] = { {"81", CN_CBOR_ERR_OUT_OF_DATA}, {"0000", CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED}, {"bf00ff", CN_CBOR_ERR_ODD_SIZE_INDEF_MAP}, {"ff", CN_CBOR_ERR_BREAK_OUTSIDE_INDEF}, {"1f", CN_CBOR_ERR_MT_UNDEF_FOR_INDEF}, {"1c", CN_CBOR_ERR_RESERVED_AI}, {"7f4100", CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING}, }; cn_cbor inv = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL}; TEST_ASSERT_EQUAL_INT(-1, cn_cbor_encoder_write(ebuf, 0, sizeof(ebuf), &inv)); for (offs = 0; offs < sizeof(tests) / sizeof(cbor_failure); offs++) { unsigned char buf[32] = {0}; TEST_ASSERT((strlen(tests[offs].hex)/2) <= sizeof(buf)); size_t len = fmt_hex_bytes(buf, tests[offs].hex); TEST_ASSERT(len); cn_cbor *cbor = cn_cbor_decode(buf, len, &ct, &errb); TEST_ASSERT_NULL(cbor); TEST_ASSERT_EQUAL_INT(errb.err, tests[offs].err); cn_cbor_free(cbor, &ct); } }
LS_API void spud_unparse(spud_message* msg) { msg->header = NULL; if (msg->cbor) { cn_cbor_free(msg->cbor, NULL); } msg->cbor = NULL; }
bool COSE_Encrypt_encrypt(HCOSE_ENCRYPT h, const byte * pbKey, size_t cbKey, cose_errback * perr) { int alg; const cn_cbor * cn_Alg = NULL; byte * pbAuthData = NULL; cn_cbor * pAuthData = NULL; cn_cbor * ptmp = NULL; size_t cbitKey; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif COSE_Encrypt * pcose = (COSE_Encrypt *) h; CHECK_CONDITION(IsValidEncryptHandle(h), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif // USE_CBOR_CONTEXT cn_Alg = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn_Alg == NULL) goto errorReturn; CHECK_CONDITION((cn_Alg->type == CN_CBOR_UINT) || (cn_Alg->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); alg = (int) cn_Alg->v.uint; // Get the key size switch (alg) { #ifdef INCLUDE_AES_CCM case COSE_Algorithm_AES_CCM_64_64_128: case COSE_Algorithm_AES_CCM_16_128_128: case COSE_Algorithm_AES_CCM_64_128_128: case COSE_Algorithm_AES_CCM_16_64_128: cbitKey = 128; break; case COSE_Algorithm_AES_CCM_64_64_256: case COSE_Algorithm_AES_CCM_16_128_256: case COSE_Algorithm_AES_CCM_64_128_256: case COSE_Algorithm_AES_CCM_16_64_256: cbitKey = 256; break; #endif // INCLUDE_AES_CCM case COSE_Algorithm_AES_GCM_128: cbitKey = 128; break; case COSE_Algorithm_AES_GCM_192: cbitKey = 192; break; case COSE_Algorithm_AES_GCM_256: cbitKey = 256; break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Build protected headers const cn_cbor * cbProtected = _COSE_encode_protected(&pcose->m_message, perr); if (cbProtected == NULL) goto errorReturn; // Build authenticated data size_t cbAuthData = 0; if (!_COSE_Encrypt_Build_AAD(&pcose->m_message, &pbAuthData, &cbAuthData, "Encrypted", perr)) goto errorReturn; switch (alg) { #ifdef INCLUDE_AES_CCM case COSE_Algorithm_AES_CCM_16_64_128: case COSE_Algorithm_AES_CCM_16_64_256: if (!AES_CCM_Encrypt((COSE_Enveloped *)pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_AES_CCM_16_128_128: case COSE_Algorithm_AES_CCM_16_128_256: if (!AES_CCM_Encrypt((COSE_Enveloped *)pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_AES_CCM_64_64_128: case COSE_Algorithm_AES_CCM_64_64_256: if (!AES_CCM_Encrypt((COSE_Enveloped *)pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_AES_CCM_64_128_128: case COSE_Algorithm_AES_CCM_64_128_256: if (!AES_CCM_Encrypt((COSE_Enveloped *)pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; #endif case COSE_Algorithm_AES_GCM_128: case COSE_Algorithm_AES_GCM_192: case COSE_Algorithm_AES_GCM_256: if (!AES_GCM_Encrypt((COSE_Enveloped *)pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Figure out the clean up if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); return true; errorReturn: if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if (ptmp != NULL) cn_cbor_free(ptmp CBOR_CONTEXT_PARAM); return false; }
bool _COSE_Encrypt_decrypt(COSE_Encrypt * pcose, const byte * pbKey, size_t cbKey, cose_errback * perr) { int alg; const cn_cbor * cn = NULL; int cbitKey; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context; #endif byte * pbAuthData = NULL; size_t cbAuthData; cn_cbor * pAuthData = NULL; byte * pbProtected = NULL; ssize_t cbProtected; #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif cn = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn == NULL) { error: errorReturn: if (pbProtected != NULL) COSE_FREE(pbProtected, context); if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); return false; } CHECK_CONDITION((cn->type == CN_CBOR_UINT) || (cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); alg = (int) cn->v.uint; switch (alg) { #ifdef INCLUDE_AES_CCM case COSE_Algorithm_AES_CCM_16_64_128: case COSE_Algorithm_AES_CCM_16_128_128: case COSE_Algorithm_AES_CCM_64_64_128: case COSE_Algorithm_AES_CCM_64_128_128: cbitKey = 128; break; case COSE_Algorithm_AES_CCM_64_64_256: case COSE_Algorithm_AES_CCM_16_128_256: case COSE_Algorithm_AES_CCM_64_128_256: case COSE_Algorithm_AES_CCM_16_64_256: cbitKey = 256; break; #endif // INCLUDE_AES_CCM case COSE_Algorithm_AES_GCM_128: cbitKey = 128; break; case COSE_Algorithm_AES_GCM_192: cbitKey = 192; break; case COSE_Algorithm_AES_GCM_256: cbitKey = 256; break; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } CHECK_CONDITION(cbitKey / 8 == cbKey, COSE_ERR_INVALID_PARAMETER); // Build protected headers CHECK_CONDITION(pcose->m_message.m_protectedMap != NULL, COSE_ERR_INVALID_PARAMETER); if ((pcose->m_message.m_protectedMap != NULL) && (pcose->m_message.m_protectedMap->first_child != NULL)) { cbProtected = cn_cbor_encoder_write(RgbDontUse, 0, sizeof(RgbDontUse), pcose->m_message.m_protectedMap); pbProtected = (byte *)COSE_CALLOC(cbProtected, 1, context); if (pbProtected == NULL) goto error; if (cn_cbor_encoder_write(pbProtected, 0, cbProtected, pcose->m_message.m_protectedMap) != cbProtected) goto error; } else { pbProtected = NULL; cbProtected = 0; } // Build authenticated data if (!_COSE_Encrypt_Build_AAD(&pcose->m_message, &pbAuthData, &cbAuthData, "Encrypted", perr)) goto errorReturn; cn = _COSE_arrayget_int(&pcose->m_message, INDEX_BODY); CHECK_CONDITION(cn != NULL, COSE_ERR_INVALID_PARAMETER); switch (alg) { #ifdef INCLUDE_AES_CCM case COSE_Algorithm_AES_CCM_16_64_128: case COSE_Algorithm_AES_CCM_16_64_256: if (!AES_CCM_Decrypt((COSE_Enveloped *)pcose, 64, 16, pbKey, cbitKey / 8, cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) goto error; break; case COSE_Algorithm_AES_CCM_16_128_128: case COSE_Algorithm_AES_CCM_16_128_256: if (!AES_CCM_Decrypt((COSE_Enveloped *)pcose, 128, 16, pbKey, cbitKey / 8, cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) goto error; break; case COSE_Algorithm_AES_CCM_64_64_128: case COSE_Algorithm_AES_CCM_64_64_256: if (!AES_CCM_Decrypt((COSE_Enveloped *)pcose, 64, 64, pbKey, cbitKey / 8, cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) goto error; break; case COSE_Algorithm_AES_CCM_64_128_128: case COSE_Algorithm_AES_CCM_64_128_256: if (!AES_CCM_Decrypt((COSE_Enveloped *)pcose, 128, 64, pbKey, cbitKey / 8, cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) goto error; break; #endif // INCLUDE_AES_CCM case COSE_Algorithm_AES_GCM_128: case COSE_Algorithm_AES_GCM_192: case COSE_Algorithm_AES_GCM_256: if (!AES_GCM_Decrypt((COSE_Enveloped *)pcose, pbKey, cbitKey / 8, cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) goto error; break; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } if (pbProtected != NULL) COSE_FREE(pbProtected, context); if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if (perr != NULL) perr->err = COSE_ERR_NONE; return true; }
bool COSE_Mac0_validate(HCOSE_MAC0 h, const byte * pbKey, size_t cbKey, cose_errback * perr) { COSE_Mac0Message * pcose = (COSE_Mac0Message *)h; cn_cbor_errback cbor_error; byte * pbAuthData = NULL; int cbitKey = 0; int alg; const cn_cbor * cn = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif ssize_t cbAuthData; cn_cbor * pAuthData = NULL; cn_cbor * ptmp = NULL; CHECK_CONDITION(IsValidMac0Handle(h), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif cn = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn == NULL) goto errorReturn; if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); alg = (int)cn->v.uint; switch (alg) { case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_128_128: cbitKey = 128; break; case COSE_Algorithm_CBC_MAC_256_64: case COSE_Algorithm_CBC_MAC_256_128: 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; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } } // Build protected headers cn_cbor * cnProtected = _COSE_arrayget_int(&pcose->m_message, INDEX_PROTECTED); CHECK_CONDITION((cnProtected != NULL) && (cnProtected->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER); cn_cbor * cnContent = _COSE_arrayget_int(&pcose->m_message, INDEX_BODY); CHECK_CONDITION((cnContent != NULL) && (cnContent->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER); // Build authenticated data pbAuthData = NULL; pAuthData = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(pAuthData != NULL, cbor_error); ptmp = cn_cbor_string_create("MAC0", CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(cnProtected->v.bytes, (int) cnProtected->length, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(cnContent->v.bytes, (int) cnContent->length, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); cbAuthData = cn_cbor_encoder_write(RgbDontUseMac, 0, sizeof(RgbDontUseMac), pAuthData); pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context); CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION((cn_cbor_encoder_write(pbAuthData, 0, cbAuthData+1, pAuthData) == cbAuthData), COSE_ERR_CBOR); // M00HACK switch (alg) { case COSE_Algorithm_HMAC_256_256: if (!HMAC_Validate((COSE_MacMessage *)pcose, 256, 256, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_256_64: if (!HMAC_Validate((COSE_MacMessage *)pcose, 256, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_384_384: if (!HMAC_Validate((COSE_MacMessage *)pcose, 384, 384, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_512_512: if (!HMAC_Validate((COSE_MacMessage *)pcose, 512, 512, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_256_64: if (!AES_CBC_MAC_Validate((COSE_MacMessage *)pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_CBC_MAC_128_128: case COSE_Algorithm_CBC_MAC_256_128: if (!AES_CBC_MAC_Validate((COSE_MacMessage *)pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); return true; errorReturn: if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); return false; }
bool COSE_Mac0_encrypt(HCOSE_MAC0 h, const byte * pbKey, size_t cbKey, cose_errback * perr) { int alg; const cn_cbor * cn_Alg = NULL; byte * pbAuthData = NULL; cn_cbor * pAuthData = NULL; cn_cbor * ptmp = NULL; size_t cbitKey; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif COSE_Mac0Message * pcose = (COSE_Mac0Message *)h; cn_cbor_errback cbor_error; CHECK_CONDITION(IsValidMac0Handle(h), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif // USE_CBOR_CONTEXT cn_Alg = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn_Alg == NULL) goto errorReturn; CHECK_CONDITION(((cn_Alg->type == CN_CBOR_UINT || cn_Alg->type == CN_CBOR_INT)), COSE_ERR_INVALID_PARAMETER); alg = (int) cn_Alg->v.uint; // Get the key size switch (alg) { case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_128_128: cbitKey = 128; break; case COSE_Algorithm_CBC_MAC_256_64: case COSE_Algorithm_CBC_MAC_256_128: 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; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Build protected headers const cn_cbor * cbProtected = _COSE_encode_protected(&pcose->m_message, perr); if (cbProtected == NULL) goto errorReturn; // Get the body const cn_cbor * cbBody = _COSE_arrayget_int(&pcose->m_message, INDEX_BODY); CHECK_CONDITION(cbBody != NULL, COSE_ERR_INVALID_PARAMETER); // Build authenticated data // Protected headers // external data // body size_t cbAuthData = 0; pAuthData = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(pAuthData != NULL, COSE_ERR_OUT_OF_MEMORY); ptmp = cn_cbor_string_create("MAC0", CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = NULL; ptmp = cn_cbor_data_create(cbProtected->v.bytes, (int) cbProtected->length, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(ptmp != NULL, COSE_ERR_CBOR); CHECK_CONDITION(cn_cbor_array_append(pAuthData, ptmp, NULL), COSE_ERR_CBOR); ptmp = NULL; ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = NULL; ptmp = cn_cbor_data_create(cbBody->v.bytes, (int) cbBody->length, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = NULL; cbAuthData = cn_cbor_encoder_write(RgbDontUse2, 0, sizeof(RgbDontUse2), pAuthData); CHECK_CONDITION(cbAuthData > 0, COSE_ERR_CBOR); pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context); CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(cn_cbor_encoder_write(pbAuthData, 0, cbAuthData, pAuthData) == cbAuthData, COSE_ERR_CBOR); switch (alg) { case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_256_64: if (!AES_CBC_MAC_Create((COSE_MacMessage *)pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_CBC_MAC_128_128: case COSE_Algorithm_CBC_MAC_256_128: if (!AES_CBC_MAC_Create((COSE_MacMessage *)pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_256_64: if (!HMAC_Create((COSE_MacMessage *)pcose, 256, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_256_256: if (!HMAC_Create((COSE_MacMessage *)pcose, 256, 256, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_384_384: if (!HMAC_Create((COSE_MacMessage *)pcose, 384, 384, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_512_512: if (!HMAC_Create((COSE_MacMessage *)pcose, 512, 512, pbKey, cbKey, pbAuthData, cbAuthData, perr)) goto errorReturn; break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Figure out the clean up if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if (ptmp != NULL) cn_cbor_free(ptmp CBOR_CONTEXT_PARAM); return true; errorReturn: if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if (ptmp != NULL) cn_cbor_free(ptmp CBOR_CONTEXT_PARAM); return false; }
bool COSE_Mac_validate(HCOSE_MAC h, HCOSE_RECIPIENT hRecip, cose_errback * perr) { COSE_MacMessage * pcose = (COSE_MacMessage *)h; COSE_RecipientInfo * pRecip = (COSE_RecipientInfo *)hRecip; cn_cbor_errback cbor_error; byte * pbAuthData = NULL; int cbitKey = 0; int alg; const cn_cbor * cn = NULL; byte * pbKey = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif ssize_t cbAuthData; cn_cbor * pAuthData = NULL; cn_cbor * ptmp = NULL; CHECK_CONDITION(IsValidMacHandle(h) && IsValidRecipientHandle(hRecip), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif cn = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn == NULL) goto errorReturn; if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); alg = (int)cn->v.uint; switch (alg) { case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_128_128: cbitKey = 128; break; case COSE_Algorithm_CBC_MAC_256_64: case COSE_Algorithm_CBC_MAC_256_128: 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; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } } // Allocate the key if we have not already done so if (pbKey == NULL) { pbKey = COSE_CALLOC(cbitKey / 8, 1, context); CHECK_CONDITION(pbKey != NULL, COSE_ERR_OUT_OF_MEMORY); } // If there is a recipient - ask it for the key for (pRecip = pcose->m_recipientFirst; pRecip != NULL; pRecip = pRecip->m_recipientNext) { if (_COSE_Recipient_decrypt(pRecip, cbitKey, pbKey, perr)) break; } CHECK_CONDITION(pRecip != NULL, COSE_ERR_NO_RECIPIENT_FOUND); // Build protected headers cn_cbor * cnProtected = _COSE_arrayget_int(&pcose->m_message, INDEX_PROTECTED); CHECK_CONDITION((cnProtected != NULL) && (cnProtected->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER); cn_cbor * cnContent = _COSE_arrayget_int(&pcose->m_message, INDEX_BODY); CHECK_CONDITION((cnContent != NULL) && (cnContent->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER); // Build authenticated data pbAuthData = NULL; pAuthData = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(pAuthData != NULL, cbor_error); ptmp = cn_cbor_string_create("MAC", CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(cnProtected->v.bytes, (int) cnProtected->length, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); ptmp = cn_cbor_data_create(cnContent->v.bytes, (int) cnContent->length, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error); cbAuthData = cn_cbor_encoder_write(RgbDontUseMac, 0, sizeof(RgbDontUseMac), pAuthData); pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context); CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION((cn_cbor_encoder_write(pbAuthData, 0, cbAuthData+1, pAuthData) == cbAuthData), COSE_ERR_CBOR); // M00HACK switch (alg) { case COSE_Algorithm_HMAC_256_256: if (!HMAC_Validate(pcose, 256, 256, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_256_64: if (!HMAC_Validate(pcose, 256, 64, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_384_384: if (!HMAC_Validate(pcose, 384, 384, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_HMAC_512_512: if (!HMAC_Validate(pcose, 512, 512, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_CBC_MAC_128_64: case COSE_Algorithm_CBC_MAC_256_64: if (!AES_CBC_MAC_Validate(pcose, 64, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; case COSE_Algorithm_CBC_MAC_128_128: case COSE_Algorithm_CBC_MAC_256_128: if (!AES_CBC_MAC_Validate(pcose, 128, pbKey, cbitKey/8, pbAuthData, cbAuthData, perr)) goto errorReturn; break; default: FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); break; } if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if ((pbKey != NULL)) { memset(pbKey, 0xff, cbitKey / 8); COSE_FREE(pbKey, context); } return true; errorReturn: if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM); if ((pbKey != NULL)) { memset(pbKey, 0xff, cbitKey / 8); COSE_FREE(pbKey, context); } return false; }
static void test_parse(void) { char *tests[] = { "00", // 0 "01", // 1 "17", // 23 "1818", // 24 "190100", // 256 "1a00010000", // 65536 #ifndef CBOR_NO_LL "1b0000000100000000", // 4294967296 #endif /* CBOR_NO_LL */ "20", // -1 "37", // -24 "3818", // -25 "390100", // -257 "3a00010000", // -65537 #ifndef CBOR_NO_LL "3b0000000100000000", // -4294967297 #endif /* CBOR_LL */ "4161", // h"a" "6161", // "a" "80", // [] "8100", // [0] "820102", // [1,2] "818100", // [[0]] "a1616100", // {"a":0} "d8184100", // tag "f4", // false "f5", // true "f6", // null "f7", // undefined "f8ff", // simple(255) #ifndef CBOR_NO_FLOAT "f93c00", // 1.0 "f9bc00", // -1.0 "f903ff", // 6.097555160522461e-05 "f90400", // 6.103515625e-05 "f907ff", // 0.00012201070785522461 "f90800", // 0.0001220703125 "fa47800000", // 65536.0 "fb3ff199999999999a", // 1.1 "f97e00", // NaN #endif /* CBOR_NO_FLOAT */ "5f42010243030405ff", // (_ h'0102', h'030405') "7f61616161ff", // (_ "a", "a") "9fff", // [_ ] "9f9f9fffffff", // [_ [_ [_ ]]] "9f009f00ff00ff", // [_ 0, [_ 0], 0] "bf61610161629f0203ffff", // {_ "a": 1, "b": [_ 2, 3]} }; for (test = 0; test < sizeof(tests) / sizeof(char*); test++) { unsigned char buf[64] = {0}; TEST_ASSERT((strlen(tests[test])/2) <= sizeof(buf)); size_t len = fmt_hex_bytes(buf, tests[test]); TEST_ASSERT(len); errb.err = CN_CBOR_NO_ERROR; cn_cbor *cbor = cn_cbor_decode(buf, len, &ct, &errb); TEST_ASSERT_EQUAL_INT(errb.err, CN_CBOR_NO_ERROR); TEST_ASSERT_NOT_NULL(cbor); cn_cbor_encoder_write(ebuf, 0, sizeof(ebuf), cbor); for (offs = 0; offs < len; offs++) { TEST_ASSERT_EQUAL_INT(buf[offs], ebuf[offs]); } cn_cbor_free(cbor, &ct); } }
bool ECDSA_Sign(COSE * pSigner, int index, const cn_cbor * pKey, int cbitDigest, const byte * rgbToSign, size_t cbToSign, cose_errback * perr) { #if defined(MBEDTLS_ECDSA_DETERMINISTIC) byte rgbDigest[MBEDTLS_MD_MAX_SIZE]; uint8_t * pbSig = NULL; cn_cbor_errback cbor_error; int cbR; mbedtls_md_type_t mdType; const mbedtls_md_info_t *pmdInfo; mbedtls_ecp_keypair keypair; mbedtls_mpi r; mbedtls_mpi s; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pSigner->m_allocContext; #endif cn_cbor * p = NULL; bool result = false; mbedtls_ecp_keypair_init(&keypair); mbedtls_mpi_init(&r); mbedtls_mpi_init(&s); if(!ECKey_From(pKey, &keypair, perr)) goto errorReturn; CHECK_CONDITION(keypair.d.n != 0, COSE_ERR_INVALID_PARAMETER); switch(cbitDigest) { case 256: mdType = MBEDTLS_MD_SHA256; break; case 384: mdType = MBEDTLS_MD_SHA384; break; case 512: mdType = MBEDTLS_MD_SHA512; break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } pmdInfo = mbedtls_md_info_from_type(mdType); CHECK_CONDITION(pmdInfo != NULL, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(mbedtls_md(pmdInfo, rgbToSign, cbToSign, rgbDigest) == 0, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(mbedtls_ecdsa_sign_det(&keypair.grp, &r, &s, &keypair.d, rgbDigest, mbedtls_md_get_size(pmdInfo), mdType) == 0, COSE_ERR_CRYPTO_FAIL); cbR = (keypair.grp.nbits + 7) / 8; pbSig = COSE_CALLOC(cbR, 2, context); CHECK_CONDITION(pbSig != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(mbedtls_mpi_write_binary(&r, pbSig, cbR) == 0, COSE_ERR_INTERNAL); CHECK_CONDITION(mbedtls_mpi_write_binary(&s, pbSig + cbR, cbR) == 0, COSE_ERR_INTERNAL); p = cn_cbor_data_create(pbSig, cbR*2, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(p != NULL, cbor_error); CHECK_CONDITION(_COSE_array_replace(pSigner, p, index, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); p = NULL; pbSig = NULL; result = true; errorReturn: cn_cbor_free(p CBOR_CONTEXT_PARAM); COSE_FREE(pbSig, context); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s); mbedtls_ecp_keypair_free(&keypair); return result; #else return false; #endif }