Coverity cleanup + start of ECDH-SS
diff --git a/src/Encrypt0.c b/src/Encrypt0.c
index f305b78..11b3241 100644
--- a/src/Encrypt0.c
+++ b/src/Encrypt0.c
@@ -129,7 +129,6 @@
#endif
byte * pbAuthData = NULL;
size_t cbAuthData;
- cn_cbor * pAuthData = NULL;
byte * pbProtected = NULL;
ssize_t cbProtected;
@@ -143,7 +142,6 @@
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);
@@ -241,7 +239,6 @@
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;
@@ -252,13 +249,12 @@
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;
+ bool fRet = false;
CHECK_CONDITION(IsValidEncryptHandle(h), COSE_ERR_INVALID_PARAMETER);
@@ -344,16 +340,11 @@
// Figure out the clean up
- if (pbAuthData != NULL) COSE_FREE(pbAuthData, context);
- if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM);
-
- return true;
+ fRet = 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;
+ return fRet;
}
bool COSE_Encrypt_SetContent(HCOSE_ENCRYPT h, const byte * rgb, size_t cb, cose_errback * perror)
diff --git a/src/MacMessage0.c b/src/MacMessage0.c
index 1d04e93..a31feb4 100644
--- a/src/MacMessage0.c
+++ b/src/MacMessage0.c
@@ -159,14 +159,15 @@
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;
+ bool fRet = false;
+ cn_cbor * pAuthData = NULL;
+ cn_cbor * ptmp = NULL;
CHECK_CONDITION(IsValidMac0Handle(h), COSE_ERR_INVALID_PARAMETER);
@@ -285,16 +286,13 @@
// 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;
+ fRet = 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;
+ return fRet;
}
byte RgbDontUseMac[1024];
diff --git a/src/Recipient.c b/src/Recipient.c
index 71e594e..e9ffc80 100644
--- a/src/Recipient.c
+++ b/src/Recipient.c
@@ -125,17 +125,18 @@
cn_cbor_context * context;
#endif
byte * pbAuthData = NULL;
- cn_cbor * pAuthData = NULL;
byte * pbProtected = NULL;
COSE_Enveloped * pcose = &pRecip->m_encrypt;
cn_cbor * cnBody = NULL;
byte * pbContext = NULL;
size_t cbContext;
+ byte rgbKey[256 / 8];
byte rgbDigest[512 / 8];
size_t cbDigest;
cn_cbor * pkey = NULL;
byte * pbSecret = NULL;
size_t cbSecret;
+ int cbKey2;
#ifdef USE_CBOR_CONTEXT
context = &pcose->m_message.m_allocContext;
@@ -147,7 +148,6 @@
if (pbContext != NULL) COSE_FREE(pbContext, context);
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 (pbSecret != NULL) COSE_FREE(pbSecret, context);
return false;
}
@@ -184,6 +184,18 @@
case COSE_Algorithm_ECDH_ES_HKDF_256:
case COSE_Algorithm_ECDH_ES_HKDF_512:
+ case COSE_Algorithm_ECDH_SS_HKDF_256:
+ case COSE_Algorithm_ECDH_SS_HKDF_512:
+ break;
+
+ case COSE_Algorithm_ECDH_ES_A128KW:
+ case COSE_Algorithm_ECDH_ES_A192KW:
+ case COSE_Algorithm_ECDH_ES_A256KW:
+ break;
+
+ case COSE_Algorithm_ECDH_SS_A128KW:
+ case COSE_Algorithm_ECDH_SS_A192KW:
+ case COSE_Algorithm_ECDH_SS_A256KW:
break;
default:
@@ -290,6 +302,40 @@
break;
+ case COSE_Algorithm_ECDH_ES_A128KW:
+ if (!BuildContextBytes(&pcose->m_message, alg, 128, &pbContext, &cbContext, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ pkey = _COSE_map_get_int(&pcose->m_message, COSE_Header_ECDH_EPHEMERAL, COSE_BOTH, perr);
+ if (pkey == NULL) goto errorReturn;
+
+ CHECK_CONDITION(pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER);
+ if (!ECDH_ComputeSecret(&pcose->m_message, (cn_cbor **)&pRecip->m_pkey, pkey, &pbSecret, &cbSecret, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ if (!HKDF_Extract(&pcose->m_message, pbSecret, cbSecret, 256, rgbDigest, &cbDigest, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ if (!HKDF_Expand(&pcose->m_message, 256, rgbDigest, cbDigest, pbContext, cbContext, rgbKey, 128 / 8, perr)) goto errorReturn;
+
+ if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, cnBody->v.bytes, cnBody->length, pbKey, &cbKey2, perr)) goto errorReturn;
+
+ break;
+
+ case COSE_Algorithm_ECDH_SS_A128KW:
+ if (!BuildContextBytes(&pcose->m_message, alg, 128, &pbContext, &cbContext, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ pkey = _COSE_map_get_int(&pcose->m_message, COSE_Header_ECDH_STATIC, COSE_BOTH, perr);
+ if (pkey == NULL) goto errorReturn;
+
+ CHECK_CONDITION(pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER);
+ if (!ECDH_ComputeSecret(&pcose->m_message, (cn_cbor **)&pRecip->m_pkey, pkey, &pbSecret, &cbSecret, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ if (!HKDF_Extract(&pcose->m_message, pbSecret, cbSecret, 256, rgbDigest, &cbDigest, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+
+ if (!HKDF_Expand(&pcose->m_message, 256, rgbDigest, cbDigest, pbContext, cbContext, rgbKey, 128 / 8, perr)) goto errorReturn;
+
+ if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, cnBody->v.bytes, cnBody->length, pbKey, &cbKey2, perr)) goto errorReturn;
+
+ break;
+
default:
FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM);
break;