bool _COSE_Encrypt_Build_AAD(COSE * pMessage, byte ** ppbAAD, size_t * pcbAAD, const char * szContext, cose_errback * perr) { #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pMessage->m_allocContext; #endif cn_cbor_errback cbor_error; byte * pbAuthData; size_t cbAuthData; cn_cbor * pAuthData; cn_cbor * pItem; cn_cbor * ptmp = NULL; // 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(szContext, 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; pItem = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); CHECK_CONDITION(pItem != NULL, COSE_ERR_INVALID_PARAMETER); if ((pItem->length == 1) && (pItem->v.bytes[0] == 0xa0)) { ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA &cbor_error); } else { ptmp = cn_cbor_data_create(pItem->v.bytes, (int)pItem->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; ptmp = cn_cbor_data_create(pMessage->m_pbExternal, (int) pMessage->m_cbExternal, 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(RgbDontUse, 0, sizeof(RgbDontUse), pAuthData); pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context); CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION((size_t)cn_cbor_encoder_write(pbAuthData, 0, cbAuthData, pAuthData) == cbAuthData, COSE_ERR_CBOR); *ppbAAD = pbAuthData; *pcbAAD = cbAuthData; return true; errorReturn: if (pbAuthData != NULL) COSE_FREE(pbAuthData, context); if (ptmp != NULL) CN_CBOR_FREE(ptmp, NULL); if (pAuthData != NULL) CN_CBOR_FREE(pAuthData, context); return false; }
bool AES_KW_Encrypt(COSE_RecipientInfo * pcose, const byte * pbKeyIn, int cbitKey, const byte * pbContent, int cbContent, cose_errback * perr) { byte *pbOut = NULL; AES_KEY key; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_encrypt.m_message.m_allocContext; #endif cn_cbor * cnTmp = NULL; pbOut = COSE_CALLOC(cbContent + 8, 1, context); CHECK_CONDITION(AES_set_encrypt_key(pbKeyIn, cbitKey, &key) == 0, COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(AES_wrap_key(&key, NULL, pbOut, pbContent, cbContent), COSE_ERR_CRYPTO_FAIL); cnTmp = cn_cbor_data_create(pbOut, (int)cbContent + 8, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(cnTmp != NULL, COSE_ERR_CBOR); pbOut = NULL; CHECK_CONDITION(_COSE_array_replace(&pcose->m_encrypt.m_message, cnTmp, INDEX_BODY, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); cnTmp = NULL; return true; errorReturn: COSE_FREE(cnTmp, context); if (pbOut != NULL) COSE_FREE(pbOut, context); return false; }
LS_API bool tube_data(tube *t, uint8_t *data, size_t len, ls_err *err) { // max size for CBOR preamble 19 bytes: // 1(map|27) 8(length) 1(key:0) 1(bstr|27) 8(length) //uint8_t preamble[19]; cn_cbor *map; cn_cbor *cdata; bool ret = false; cn_cbor_context ctx; assert(t); if (len == 0) { return tube_send(t, SPUD_DATA, false, false, NULL, 0, 0, err); } if (!_map_create(&ctx, &map, err)) { return false; } // TODO: the whole point of the iov system is so that we don't have to copy // the data here. Which we just did. Please fix. if (!(cdata = cn_cbor_data_create(data, len, &ctx, NULL)) || !cn_cbor_mapput_int(map, 0, cdata, &ctx, NULL)) { LS_ERROR(err, LS_ERR_NO_MEMORY); goto cleanup; } ret = tube_send_cbor(t, SPUD_DATA, false, false, map, err); cleanup: ls_pool_destroy((ls_pool*)ctx.context); return ret; }
bool AES_CBC_MAC_Create(COSE_MacMessage * pcose, int TSize, const byte * pbKey, size_t cbKey, const byte * pbAuthData, size_t cbAuthData, cose_errback * perr) { const EVP_CIPHER * pcipher = NULL; EVP_CIPHER_CTX ctx; int cbOut; byte rgbIV[16] = { 0 }; byte * rgbOut = NULL; bool f = false; unsigned int i; cn_cbor * cn = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_message.m_allocContext; #endif rgbOut = COSE_CALLOC(16, 1, context); CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY); switch (cbKey*8) { case 128: pcipher = EVP_aes_128_cbc(); break; case 256: pcipher = EVP_aes_256_cbc(); break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Setup and run the OpenSSL code EVP_CIPHER_CTX_init(&ctx); CHECK_CONDITION(EVP_EncryptInit_ex(&ctx, pcipher, NULL, pbKey, rgbIV), COSE_ERR_CRYPTO_FAIL); for (i = 0; i < (unsigned int)cbAuthData / 16; i++) { CHECK_CONDITION(EVP_EncryptUpdate(&ctx, rgbOut, &cbOut, pbAuthData + (i * 16), 16), COSE_ERR_CRYPTO_FAIL); } if (cbAuthData % 16 != 0) { CHECK_CONDITION(EVP_EncryptUpdate(&ctx, rgbOut, &cbOut, pbAuthData + (i * 16), cbAuthData % 16), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_EncryptUpdate(&ctx, rgbOut, &cbOut, rgbIV, 16 - (cbAuthData % 16)), COSE_ERR_CRYPTO_FAIL); } cn = cn_cbor_data_create(rgbOut, TSize / 8, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(cn != NULL, COSE_ERR_OUT_OF_MEMORY); rgbOut = NULL; CHECK_CONDITION(_COSE_array_replace(&pcose->m_message, cn, INDEX_MAC_TAG, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); cn = NULL; EVP_CIPHER_CTX_cleanup(&ctx); return !f; errorReturn: if (rgbOut != NULL) COSE_FREE(rgbOut, context); if (cn != NULL) CN_CBOR_FREE(cn, context); EVP_CIPHER_CTX_cleanup(&ctx); return false; }
bool ECDSA_Sign(COSE * pSigner, int index, const cn_cbor * pKey, int cbitDigest, const byte * rgbToSign, size_t cbToSign, cose_errback * perr) { EC_KEY * eckey = NULL; byte rgbDigest[EVP_MAX_MD_SIZE]; unsigned int cbDigest = sizeof(rgbDigest); byte * pbSig = NULL; const EVP_MD * digest; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pSigner->m_allocContext; #endif cn_cbor * p = NULL; ECDSA_SIG * psig = NULL; cn_cbor_errback cbor_error; int cbR; byte rgbSig[66]; int cb; eckey = ECKey_From(pKey, &cbR, perr); if (eckey == NULL) { errorReturn: if (p != NULL) CN_CBOR_FREE(p, context); if (eckey != NULL) EC_KEY_free(eckey); return false; } switch (cbitDigest) { case 256: digest = EVP_sha256(); break; case 512: digest = EVP_sha512(); break; case 384: digest = EVP_sha384(); break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } EVP_Digest(rgbToSign, cbToSign, rgbDigest, &cbDigest, digest, NULL); psig = ECDSA_do_sign(rgbDigest, cbDigest, eckey); CHECK_CONDITION(psig != NULL, COSE_ERR_CRYPTO_FAIL); pbSig = COSE_CALLOC(cbR, 2, context); CHECK_CONDITION(pbSig != NULL, COSE_ERR_OUT_OF_MEMORY); cb = BN_bn2bin(psig->r, rgbSig); CHECK_CONDITION(cb <= cbR, COSE_ERR_INVALID_PARAMETER); memcpy(pbSig + cbR - cb, rgbSig, cb); cb = BN_bn2bin(psig->s, rgbSig); CHECK_CONDITION(cb <= cbR, COSE_ERR_INVALID_PARAMETER); memcpy(pbSig + 2*cbR - cb, rgbSig, cb); 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); return true; }
static bool _mapput_string_data(cn_cbor *map, const char *key, uint8_t *data, size_t sz, cn_cbor_context *ctx, ls_err *err) { cn_cbor *cdata = cn_cbor_data_create(data, sz, ctx, NULL); if (!cdata || !cn_cbor_mapput_string(map, key, cdata, ctx, NULL)) { LS_ERROR(err, LS_ERR_NO_MEMORY); return false; } return true; }
bool HMAC_Create(COSE_MacMessage * pcose, int HSize, int TSize, const byte * pbKey, size_t cbKey, const byte * pbAuthData, size_t cbAuthData, cose_errback * perr) { byte * rgbOut = NULL; // unsigned int cbOut; mbedtls_md_context_t contx; const char* md_name; const struct mbedtls_md_info_t * info; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_message.m_allocContext; #endif switch (HSize) { case 256: md_name = "SHA256"; break; case 384: md_name = "SHA384"; break; case 512: md_name = "SHA512"; break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); break; } if (0) { errorReturn: COSE_FREE(rgbOut, context); mbedtls_md_free(&contx); return false; } mbedtls_md_init(&contx); info = mbedtls_md_info_from_string (md_name); mbedtls_md_setup( &contx, info, 1 ); rgbOut = COSE_CALLOC(mbedtls_md_get_size(info), 1, context); CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(!(mbedtls_md_hmac_starts (&contx, (char*)pbKey, cbKey)), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(!(mbedtls_md_hmac_update (&contx, pbAuthData, cbAuthData)), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(!(mbedtls_md_hmac_finish (&contx, rgbOut)), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(_COSE_array_replace(&pcose->m_message, cn_cbor_data_create(rgbOut, TSize / 8, CBOR_CONTEXT_PARAM_COMMA NULL), INDEX_MAC_TAG, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); mbedtls_md_free(&contx); return true; }
cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr) { cn_cbor * pProtected; int cbProtected; byte * pbProtected = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pMessage->m_allocContext; #endif // USE_CBOR_CONTEXT #ifdef TAG_IN_ARRAY pProtected = cn_cbor_index(pMessage->m_cbor, INDEX_PROTECTED + (pMessage->m_msgType == 0 ? 0 : 1)); #else pProtected = cn_cbor_index(pMessage->m_cbor, INDEX_PROTECTED); #endif if ((pProtected != NULL) &&(pProtected->type != CN_CBOR_INVALID)) { errorReturn: if (pbProtected != NULL) COSE_FREE(pbProtected, context); return pProtected; } if (pMessage->m_protectedMap->length > 0) { cbProtected = cn_cbor_encoder_write(RgbDontUse3, 0, sizeof(RgbDontUse3), pMessage->m_protectedMap); pbProtected = (byte *)COSE_CALLOC(cbProtected, 1, context); CHECK_CONDITION(pbProtected != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(cn_cbor_encoder_write(pbProtected, 0, cbProtected, pMessage->m_protectedMap) == cbProtected, COSE_ERR_CBOR); } else { cbProtected = 0; } pProtected = cn_cbor_data_create(pbProtected, cbProtected, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(pProtected != NULL, COSE_ERR_OUT_OF_MEMORY); pbProtected = NULL; CHECK_CONDITION(_COSE_array_replace(pMessage, pProtected, INDEX_PROTECTED, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); return pProtected; }
bool HMAC_Create(COSE_MacMessage * pcose, int HSize, int TSize, const byte * pbKey, size_t cbKey, const byte * pbAuthData, size_t cbAuthData, cose_errback * perr) { HMAC_CTX ctx; const EVP_MD * pmd = NULL; byte * rgbOut = NULL; unsigned int cbOut; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_message.m_allocContext; #endif HMAC_CTX_init(&ctx); if (0) { errorReturn: COSE_FREE(rgbOut, context); HMAC_cleanup(&ctx); return false; } 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; } rgbOut = COSE_CALLOC(EVP_MAX_MD_SIZE, 1, context); CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(HMAC_Init(&ctx, pbKey, (int) cbKey, pmd), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(HMAC_Update(&ctx, pbAuthData, cbAuthData), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(HMAC_Final(&ctx, rgbOut, &cbOut), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(_COSE_array_replace(&pcose->m_message, cn_cbor_data_create(rgbOut, TSize / 8, CBOR_CONTEXT_PARAM_COMMA NULL), INDEX_MAC_TAG, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); HMAC_cleanup(&ctx); return true; }
bool COSE_Mac0_SetContent(HCOSE_MAC0 cose, const byte * rgbContent, size_t cbContent, cose_errback * perr) { COSE_Mac0Message * p = (COSE_Mac0Message *)cose; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &p->m_message.m_allocContext; #endif cn_cbor * ptmp = NULL; cn_cbor_errback cbor_error; CHECK_CONDITION(IsValidMac0Handle(cose), COSE_ERR_INVALID_PARAMETER); ptmp = cn_cbor_data_create(rgbContent, (int) cbContent, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(ptmp != NULL, cbor_error); CHECK_CONDITION_CBOR(_COSE_array_replace(&p->m_message, ptmp, INDEX_BODY, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); ptmp = NULL; return true; errorReturn: if (ptmp != NULL) CN_CBOR_FREE(ptmp, context); return false; }
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 AES_CCM_Encrypt(COSE_Enveloped * pcose, int TSize, int LSize, const byte * pbKey, size_t cbKey, const byte * pbAuthData, size_t cbAuthData, cose_errback * perr) { EVP_CIPHER_CTX ctx; int cbOut; byte * rgbOut = NULL; int NSize = 15 - (LSize/8); int outl = 0; const cn_cbor * cbor_iv = NULL; cn_cbor * cbor_iv_t = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_message.m_allocContext; #endif cn_cbor * cnTmp = NULL; const EVP_CIPHER * cipher; byte rgbIV[16]; byte * pbIV = NULL; cn_cbor_errback cbor_error; switch (cbKey*8) { case 128: cipher = EVP_aes_128_ccm(); break; case 192: cipher = EVP_aes_192_ccm(); break; case 256: cipher = EVP_aes_256_ccm(); break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } // Setup the IV/Nonce and put it into the message cbor_iv = _COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr); if (cbor_iv == NULL) { pbIV = COSE_CALLOC(NSize, 1, context); CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY); rand_bytes(pbIV, NSize); memcpy(rgbIV, pbIV, NSize); cbor_iv_t = cn_cbor_data_create(pbIV, NSize, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cbor_iv_t != NULL, cbor_error); pbIV = NULL; if (!_COSE_map_put(&pcose->m_message, COSE_Header_IV, cbor_iv_t, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; cbor_iv_t = NULL; } else { CHECK_CONDITION(cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == NSize, COSE_ERR_INVALID_PARAMETER); memcpy(rgbIV, cbor_iv->v.str, cbor_iv->length); } // Setup and run the OpenSSL code EVP_CIPHER_CTX_init(&ctx); CHECK_CONDITION(EVP_EncryptInit_ex(&ctx, cipher, NULL, NULL, NULL), COSE_ERR_CRYPTO_FAIL); TSize /= 8; // Comes in in bits not bytes. CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_L, (LSize/8), 0), COSE_ERR_CRYPTO_FAIL); // CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, NSize, 0), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, TSize, NULL), COSE_ERR_CRYPTO_FAIL); // Say we are doing an 8 byte tag CHECK_CONDITION(EVP_EncryptInit(&ctx, 0, pbKey, rgbIV), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_EncryptUpdate(&ctx, 0, &cbOut, 0, (int) pcose->cbContent), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_EncryptUpdate(&ctx, NULL, &outl, pbAuthData, (int) cbAuthData), COSE_ERR_CRYPTO_FAIL); rgbOut = (byte *)COSE_CALLOC(cbOut+TSize, 1, context); CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(EVP_EncryptUpdate(&ctx, rgbOut, &cbOut, pcose->pbContent, (int) pcose->cbContent), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_EncryptFinal_ex(&ctx, &rgbOut[cbOut], &cbOut), COSE_ERR_CRYPTO_FAIL); CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_GET_TAG, TSize, &rgbOut[pcose->cbContent]), COSE_ERR_CRYPTO_FAIL); cnTmp = cn_cbor_data_create(rgbOut, (int)pcose->cbContent + TSize, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(cnTmp != NULL, COSE_ERR_CBOR); rgbOut = NULL; CHECK_CONDITION(_COSE_array_replace(&pcose->m_message, cnTmp, INDEX_BODY, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); cnTmp = NULL; EVP_CIPHER_CTX_cleanup(&ctx); return true; errorReturn: if (pbIV != NULL) COSE_FREE(pbIV, context); if (cbor_iv_t != NULL) COSE_FREE(cbor_iv_t, context); if (rgbOut != NULL) COSE_FREE(rgbOut, context); if (cnTmp != NULL) COSE_FREE(cnTmp, context); EVP_CIPHER_CTX_cleanup(&ctx); return false; }
cn_cbor * BuildKey(const cn_cbor * pKeyIn, bool fPublicKey) { cn_cbor * pKeyOut = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA NULL); cn_cbor * pKty = cn_cbor_mapget_string(pKeyIn, "kty"); cn_cbor * p; cn_cbor * pKey; cn_cbor * pValue; int i; int kty; unsigned char * pb; size_t cb; if (pKeyOut == NULL) return NULL; if ((pKty == NULL) || (pKty->type != CN_CBOR_TEXT)) return NULL; if (pKty->length == 2) { if (strncmp(pKty->v.str, "EC", 2) == 0) kty = 2; else return NULL; } else if (pKty->length == 3) { if (strncmp(pKty->v.str, "oct", 3) == 0) kty = 4; else return NULL; } else return NULL; p = cn_cbor_int_create(kty, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, 1, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; for (pKey = pKeyIn->first_child; pKey != NULL; pKey = pKey->next->next) { pValue = pKey->next; if (pKey->type == CN_CBOR_TEXT) { for (i = 0; i < 7; i++) { if ((pKey->length == strlen(RgStringKeys[i].szKey)) && (strncmp(pKey->v.str, RgStringKeys[i].szKey, strlen(RgStringKeys[i].szKey)) == 0) && ((RgStringKeys[i].kty == 0) || (RgStringKeys[i].kty == kty))) { switch (RgStringKeys[i].operation) { case OPERATION_NONE: p = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; case OPERATION_BASE64: if ((strcmp(pKey->v.str, "d") == 0) && fPublicKey) continue; pb = base64_decode(pValue->v.str, pValue->length, &cb); p = cn_cbor_data_create(pb, (int)cb, CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; case OPERATION_STRING: p = cn_cbor_int_create(MapName(pValue, RgCurveNames, _countof(RgCurveNames)), CBOR_CONTEXT_PARAM_COMMA NULL); if (p == NULL) return NULL; if (!cn_cbor_mapput_int(pKeyOut, RgStringKeys[i].keyNew, p, CBOR_CONTEXT_PARAM_COMMA NULL)) return NULL; break; } i = 99; } } } } return pKeyOut; }
bool SetAttributes(HCOSE hHandle, const cn_cbor * pAttributes, int which, int msgType, bool fPublicKey) { const cn_cbor * pKey; const cn_cbor * pValue; int keyNew; cn_cbor * pValueNew; bool f = false; if (pAttributes == NULL) return true; if (pAttributes->type != CN_CBOR_MAP) return false; for (pKey = pAttributes->first_child; pKey != NULL; pKey = pKey->next->next) { pValue = pKey->next; if (pKey->type != CN_CBOR_TEXT) return false; if (strcmp(pKey->v.str, "alg") == 0) { keyNew = COSE_Header_Algorithm; pValueNew = cn_cbor_int_create(MapAlgorithmName(pValue), CBOR_CONTEXT_PARAM_COMMA NULL); } else if (strcmp(pKey->v.str, "ctyp") == 0) { keyNew = COSE_Header_Content_Type; pValueNew = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "IV_hex") == 0) { keyNew = COSE_Header_IV; pValueNew = cn_cbor_data_create(FromHex(pValue->v.str, (int) pValue->length), (int) pValue->length / 2, CBOR_CONTEXT_PARAM_COMMA NULL); } else if (strcmp(pKey->v.str, "apu_id") == 0) { keyNew = COSE_Header_KDF_U_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "apv_id") == 0) { keyNew = COSE_Header_KDF_V_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "pub_other") == 0) { keyNew = COSE_Header_KDF_PUB_other; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "priv_other") == 0) { keyNew = COSE_Header_KDF_PRIV; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "spk") == 0) { keyNew = COSE_Header_ECDH_STATIC; pValueNew = BuildKey(pValue, fPublicKey); if (pValueNew == NULL) return false; } else { continue; } switch (msgType) { #if INCLUDE_MAC case Attributes_MAC_protected: f = COSE_Mac_map_put_int((HCOSE_MAC)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_MAC0 case Attributes_MAC0_protected: f = COSE_Mac0_map_put_int((HCOSE_MAC0)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT || INCLUDE_MAC case Attributes_Recipient_protected: f = COSE_Recipient_map_put_int((HCOSE_RECIPIENT)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT case Attributes_Enveloped_protected: f = COSE_Enveloped_map_put_int((HCOSE_ENVELOPED)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: f = COSE_Encrypt_map_put_int((HCOSE_ENCRYPT)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN case Attributes_Sign_protected: f = COSE_Sign_map_put_int((HCOSE_SIGN)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN case Attributes_Signer_protected: f = COSE_Signer_map_put_int((HCOSE_SIGNER)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN0 case Attributes_Sign0_protected: f = COSE_Sign0_map_put_int((HCOSE_SIGN0)hHandle, keyNew, pValueNew, which, NULL); break; #endif } // assert(f); } return true; }
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; }
HCOSE_RECIPIENT COSE_Mac_add_shared_secret(HCOSE_MAC hcose, COSE_Algorithms alg, byte * rgbKey, int cbKey, byte * rgbKid, int cbKid, cose_errback * perr) { #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = NULL; #endif // USE_CBOR_CONTEXT COSE_RecipientInfo * pobj; COSE_MacMessage * pcose = (COSE_MacMessage *)hcose; cn_cbor * cn_Temp = NULL; cn_cbor * pRecipients = NULL; cn_cbor * pRecipientsNew = NULL; byte * pbKey = NULL; byte * pbTemp = NULL; cn_cbor * cnTemp = NULL; cn_cbor_errback cbor_error; CHECK_CONDITION(IsValidMacHandle(hcose) && (rgbKey != NULL), COSE_ERR_INVALID_PARAMETER); #ifdef USE_CBOR_CONTEXT context = &pcose->m_message.m_allocContext; #endif // USE_CBOR_CONTEXT switch (alg) { case COSE_Algorithm_Direct: break; default: FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } pobj = (COSE_RecipientInfo *)COSE_CALLOC(1, sizeof(COSE_RecipientInfo), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init(&pobj->m_encrypt.m_message, COSE_unknown_object, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } cn_Temp = cn_cbor_int_create(alg, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cn_Temp != NULL, cbor_error); CHECK_CONDITION_CBOR(cn_cbor_mapput_int(pobj->m_encrypt.m_message.m_unprotectMap, COSE_Header_Algorithm, cn_Temp, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); cn_Temp = NULL; if (cbKid > 0) { pbTemp = (byte *)COSE_CALLOC(cbKid, 1, context); CHECK_CONDITION(pbTemp != NULL, COSE_ERR_OUT_OF_MEMORY); memcpy(pbTemp, rgbKid, cbKid); cnTemp = cn_cbor_data_create(pbTemp, cbKid, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cnTemp != NULL, cbor_error); pbTemp = NULL; CHECK_CONDITION_CBOR(cn_cbor_mapput_int(pobj->m_encrypt.m_message.m_unprotectMap, COSE_Header_KID, cnTemp, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); } pobj->m_encrypt.pbKey = pbKey = (byte *)COSE_CALLOC(cbKey, 1, context); CHECK_CONDITION(pobj->m_encrypt.pbKey != NULL, COSE_ERR_OUT_OF_MEMORY); memcpy(pbKey, rgbKey, cbKey); pobj->m_encrypt.cbKey = cbKey; pobj->m_recipientNext = pcose->m_recipientFirst; pcose->m_recipientFirst = pobj; pRecipients = _COSE_arrayget_int(&pcose->m_message, INDEX_MAC_RECIPIENTS); if (pRecipients == NULL) { pRecipients = pRecipientsNew = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(pRecipients != NULL, cbor_error); pRecipientsNew = NULL; CHECK_CONDITION_CBOR(_COSE_array_replace(&pcose->m_message, pRecipients, INDEX_MAC_RECIPIENTS, CBOR_CONTEXT_PARAM_COMMA &cbor_error), cbor_error); } CHECK_CONDITION_CBOR(cn_cbor_array_append(pRecipients, pobj->m_encrypt.m_message.m_cbor, &cbor_error), cbor_error); pobj->m_encrypt.m_message.m_flags |= 1; return (HCOSE_RECIPIENT)pobj; errorReturn: if (cn_Temp != NULL) CN_CBOR_FREE(cn_Temp, context); if (pRecipientsNew != NULL) CN_CBOR_FREE(pRecipientsNew, context); // if (pobj != NULL) COSE_Recipient_Free(pobj); return NULL; }
bool AES_CCM_Encrypt(COSE_Enveloped * pcose, int TSize, int LSize, const byte * pbKey, size_t cbKey, const byte * pbAuthData, size_t cbAuthData, cose_errback * perr) { mbedtls_ccm_context ctx; int cbOut; byte * rgbOut = NULL; int NSize = 15 - (LSize/8); const cn_cbor * cbor_iv = NULL; cn_cbor * cbor_iv_t = NULL; #ifdef USE_CBOR_CONTEXT cn_cbor_context * context = &pcose->m_message.m_allocContext; #endif cn_cbor * cnTmp = NULL; mbedtls_cipher_id_t cipher; byte rgbIV[16]; byte * pbIV = NULL; cn_cbor_errback cbor_error; mbedtls_ccm_init(&ctx); cipher = MBEDTLS_CIPHER_ID_AES; // Setup the IV/Nonce and put it into the message cbor_iv = _COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr); if (cbor_iv == NULL) { pbIV = COSE_CALLOC(NSize, 1, context); CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY); rand_bytes(pbIV, NSize); memcpy(rgbIV, pbIV, NSize); cbor_iv_t = cn_cbor_data_create(pbIV, NSize, CBOR_CONTEXT_PARAM_COMMA &cbor_error); CHECK_CONDITION_CBOR(cbor_iv_t != NULL, cbor_error); pbIV = NULL; if (!_COSE_map_put(&pcose->m_message, COSE_Header_IV, cbor_iv_t, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; cbor_iv_t = NULL; } else { CHECK_CONDITION(cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == NSize, COSE_ERR_INVALID_PARAMETER); memcpy(rgbIV, cbor_iv->v.str, cbor_iv->length); } // Setup and run the mbedTLS code //cbKey comes in bytes not bits CHECK_CONDITION(!mbedtls_ccm_setkey(&ctx, cipher, pbKey, cbKey*8), COSE_ERR_CRYPTO_FAIL); TSize /= 8; // Comes in in bits not bytes. cbOut = pcose->cbContent; // M00BUG - This is a missing call? rgbOut = (byte *)COSE_CALLOC(cbOut+TSize, 1, context); CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY); CHECK_CONDITION(!mbedtls_ccm_encrypt_and_tag(&ctx, pcose->cbContent, rgbIV, NSize, pbAuthData, cbAuthData, pcose->pbContent, rgbOut, &rgbOut[pcose->cbContent], TSize), COSE_ERR_CRYPTO_FAIL); cnTmp = cn_cbor_data_create(rgbOut, (int)pcose->cbContent + TSize, CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(cnTmp != NULL, COSE_ERR_CBOR); rgbOut = NULL; CHECK_CONDITION(_COSE_array_replace(&pcose->m_message, cnTmp, INDEX_BODY, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); cnTmp = NULL; mbedtls_ccm_free(&ctx); return true; errorReturn: if (pbIV != NULL) COSE_FREE(pbIV, context); if (cbor_iv_t != NULL) COSE_FREE(cbor_iv_t, context); if (rgbOut != NULL) COSE_FREE(rgbOut, context); if (cnTmp != NULL) COSE_FREE(cnTmp, context); printf("errorReturn from OPENSSL\n"); mbedtls_ccm_free(&ctx); return false; }
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 }