Addressing PR comments

This commit is contained in:
Andras Fekete
2023-07-06 16:33:32 -04:00
parent 266307da6c
commit ef9206f73f
2 changed files with 738 additions and 682 deletions

View File

@ -37,6 +37,7 @@ size and a key size of 128, 192, or 256 bits.
#include <wolfssl/wolfcrypt/port/aria/aria-crypt.h>
#ifdef HAVE_ARIA
/* return 0 on success or WC_INIT_E on failure */
int wc_AriaInitCrypt(wc_Aria* aria, MC_ALGID algo)
{
@ -46,17 +47,23 @@ size and a key size of 128, 192, or 256 bits.
MC_ALGMODE algMode = MC_ALGMODE_GCM;
MC_PADTYPE algPad = MC_PADTYPE_NONE;
if (aria == NULL) return BAD_FUNC_ARG;
if (aria == NULL)
return BAD_FUNC_ARG;
if (rv == MC_OK) rv = MC_Initialize(NULL);
if (rv == MC_OK)
rv = MC_Initialize(NULL);
if (rv == MC_OK) rv = wc_AriaFreeCrypt(aria);
if (rv == MC_OK)
rv = wc_AriaFreeCrypt(aria);
if (rv == MC_OK) rv = MC_OpenSession(&(aria->hSession));
if (rv == MC_OK)
rv = MC_OpenSession(&(aria->hSession));
if (rv == MC_OK) rv = MC_SetApiMode(aria->hSession, gApimode);
if (rv == MC_OK)
rv = MC_SetApiMode(aria->hSession, gApimode);
if (rv == MC_OK) rv = MC_SetOption(aria->hSession, algMode, algPad);
if (rv == MC_OK)
rv = MC_SetOption(aria->hSession, algMode, algPad);
if (rv == MC_OK) {
aria->algo = algo;
@ -74,9 +81,11 @@ size and a key size of 128, 192, or 256 bits.
/* return 0 on success or BAD_STATE_E on failure */
int wc_AriaFreeCrypt(wc_Aria* aria)
{
if (aria == NULL) return 0;
MC_RV rv = MC_OK;
if (aria == NULL)
return 0;
if (aria->hKey != NULL) {
if (rv == MC_OK) rv = MC_DestroyObject(aria->hSession, aria->hKey);
if (rv == MC_OK) aria->hKey = NULL;
@ -110,10 +119,13 @@ size and a key size of 128, 192, or 256 bits.
}
if (aria->hKey != NULL) {
if (rv == MC_OK) rv = MC_DestroyObject(aria->hSession, aria->hKey);
if (rv == MC_OK) aria->hKey = NULL;
if (rv == MC_OK)
rv = MC_DestroyObject(aria->hSession, aria->hKey);
if (rv == MC_OK)
aria->hKey = NULL;
}
if (rv == MC_OK) rv = MC_CreateObject(aria->hSession, (MC_UCHAR*)key, keylen, &(aria->hKey));
if (rv == MC_OK)
rv = MC_CreateObject(aria->hSession, (MC_UCHAR*)key, keylen, &(aria->hKey));
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));
@ -190,10 +202,10 @@ size and a key size of 128, 192, or 256 bits.
.nTLen = authTagSz,
.nDataLen = inSz
};
MC_UINT outSz = inSz + authTagSz;
MC_ALGORITHM mcAlg = {aria->algo, NULL, 0};
mcAlg.pParam = (MC_UCHAR*)&param;
mcAlg.nParam = sizeof(param);
MC_UINT outSz = inSz + authTagSz;
if (authTag == NULL || iv == NULL || authTagSz > ARIA_BLOCK_SIZE ||
authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ ||
@ -207,11 +219,14 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = MC_EncryptInit(aria->hSession, &mcAlg, aria->hKey);
if (rv == MC_OK)
rv = MC_EncryptInit(aria->hSession, &mcAlg, aria->hKey);
if (rv == MC_OK) rv = MC_Encrypt(aria->hSession, in, inSz, out, &outSz);
if (rv == MC_OK)
rv = MC_Encrypt(aria->hSession, in, inSz, out, &outSz);
if (rv == MC_OK) XMEMCPY(authTag, out + inSz, authTagSz);
if (rv == MC_OK)
XMEMCPY(authTag, out + inSz, authTagSz);
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));
@ -236,10 +251,10 @@ size and a key size of 128, 192, or 256 bits.
.nTLen = authTagSz,
.nDataLen = inSz
};
MC_UINT outSz = inSz;
MC_ALGORITHM mcAlg = {aria->algo, NULL, 0};
mcAlg.pParam = (MC_UCHAR*)&param;
mcAlg.nParam = sizeof(param);
MC_UINT outSz = inSz;
if (authTag == NULL || iv == NULL || authTagSz > ARIA_BLOCK_SIZE ||
authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ ||
@ -253,13 +268,15 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = MC_DecryptInit(aria->hSession, &mcAlg, aria->hKey);
if (rv == MC_OK)
rv = MC_DecryptInit(aria->hSession, &mcAlg, aria->hKey);
if (rv == MC_OK) {
XMEMCPY((byte*)in + inSz, authTag, authTagSz);
inSz += authTagSz;
}
if (rv == MC_OK) rv = MC_Decrypt(aria->hSession, in, inSz, out, &outSz);
if (rv == MC_OK)
rv = MC_Decrypt(aria->hSession, in, inSz, out, &outSz);
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));

View File

@ -1,4 +1,4 @@
/* aria.c
/* aria-cryptocb.c
*
* Copyright (C) 2006-2023 wolfSSL Inc.
*
@ -43,8 +43,10 @@ size and a key size of 128, 192, or 256 bits.
static char isInit = 0;
if (isInit == 0) {
if (rv == MC_OK) rv = MC_Initialize(NULL);
if (rv == MC_OK) isInit = 1;
if (rv == MC_OK)
rv = MC_Initialize(NULL);
if (rv == MC_OK)
isInit = 1;
#ifdef WOLF_CRYPTO_CB
if (rv == MC_OK) {
@ -74,13 +76,17 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = wc_AriaInit();
if (rv == MC_OK)
rv = wc_AriaInit();
if (rv == MC_OK) rv = MC_OpenSession(hSession);
if (rv == MC_OK)
rv = MC_OpenSession(hSession);
if (rv == MC_OK) rv = MC_SetApiMode(*hSession, gApimode);
if (rv == MC_OK)
rv = MC_SetApiMode(*hSession, gApimode);
if (rv == MC_OK) rv = MC_DigestInit(*hSession, &mcAlg);
if (rv == MC_OK)
rv = MC_DigestInit(*hSession, &mcAlg);
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));
@ -99,7 +105,8 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = MC_DigestUpdate(hSession, data, len);
if (rv == MC_OK)
rv = MC_DigestUpdate(hSession, data, len);
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));
@ -119,9 +126,11 @@ size and a key size of 128, 192, or 256 bits.
}
/* Do an extra DigestUpdate noop just in case it is never explicitly called. */
if (rv == MC_OK) rv = MC_DigestUpdate(hSession, NULL, 0);
if (rv == MC_OK)
rv = MC_DigestUpdate(hSession, NULL, 0);
if (rv == MC_OK) rv = MC_DigestFinal(hSession, out, len);
if (rv == MC_OK)
rv = MC_DigestFinal(hSession, out, len);
if (rv != MC_OK) {
WOLFSSL_MSG(MC_GetErrorString(rv));
@ -142,13 +151,17 @@ size and a key size of 128, 192, or 256 bits.
}
if (obj1 != NULL) {
if (rv == MC_OK) rv = MC_DestroyObject(*hSession, *obj1);
if (rv == MC_OK) *obj1 = NULL;
if (rv == MC_OK)
rv = MC_DestroyObject(*hSession, *obj1);
if (rv == MC_OK)
*obj1 = NULL;
}
if (hSession != NULL) {
if (rv == MC_OK) rv = MC_CloseSession(*hSession);
if (rv == MC_OK) *hSession = NULL;
if (rv == MC_OK)
rv = MC_CloseSession(*hSession);
if (rv == MC_OK)
*hSession = NULL;
}
if (rv != MC_OK) {
@ -177,11 +190,14 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = wc_AriaInit();
if (rv == MC_OK)
rv = wc_AriaInit();
if (rv == MC_OK) rv = MC_OpenSession(&hSession);
if (rv == MC_OK)
rv = MC_OpenSession(&hSession);
if (rv == MC_OK) rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK)
rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK) {
int ret = wc_EccPrivateKeyToDerNoCurve(key,keyAsn1,keyAsn1Sz);
@ -209,13 +225,16 @@ size and a key size of 128, 192, or 256 bits.
rv = MC_ERR_UNSUPPORTED_ALGORITHM;
}
if (rv == MC_OK) rv = MC_CreateObject(hSession, keyAsn1, keyAsn1Sz, &hPrikey);
if (rv == MC_OK)
rv = MC_CreateObject(hSession, keyAsn1, keyAsn1Sz, &hPrikey);
WOLFSSL_MSG_EX("AriaSign CreateObject rv=%d",rv);
if (rv == MC_OK) rv = MC_SignInit(hSession, &mcAlg, hPrikey);
if (rv == MC_OK)
rv = MC_SignInit(hSession, &mcAlg, hPrikey);
WOLFSSL_MSG_EX("AriaSign SignInit rv=%d",rv);
if (rv == MC_OK) rv = MC_Sign(hSession, in, inSz, out, outSz);
if (rv == MC_OK)
rv = MC_Sign(hSession, in, inSz, out, outSz);
WOLFSSL_MSG_EX("AriaSign Sign rv=%d",rv);
wc_AriaFree(&hSession, &hPrikey);
@ -247,11 +266,14 @@ size and a key size of 128, 192, or 256 bits.
*res = 0; /* Default to invalid signature */
if (rv == MC_OK) rv = wc_AriaInit();
if (rv == MC_OK)
rv = wc_AriaInit();
if (rv == MC_OK) rv = MC_OpenSession(&hSession);
if (rv == MC_OK)
rv = MC_OpenSession(&hSession);
if (rv == MC_OK) rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK)
rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK) {
int ret = wc_EccPublicKeyToDer(key,keyarr,keySz,0);
@ -279,13 +301,16 @@ size and a key size of 128, 192, or 256 bits.
rv = MC_ERR_UNSUPPORTED_ALGORITHM;
}
if (rv == MC_OK) rv = MC_CreateObject(hSession, keyarr, keySz, &hPubkey);
if (rv == MC_OK)
rv = MC_CreateObject(hSession, keyarr, keySz, &hPubkey);
WOLFSSL_MSG_EX("AriaVerify CreateObject rv=%d",rv);
if (rv == MC_OK) rv = MC_VerifyInit(hSession, &mcAlg, hPubkey);
if (rv == MC_OK)
rv = MC_VerifyInit(hSession, &mcAlg, hPubkey);
WOLFSSL_MSG_EX("AriaVerify VerifyInit rv=%d",rv);
if (rv == MC_OK) rv = MC_Verify(hSession, hash, hashSz, sig, sigSz);
if (rv == MC_OK)
rv = MC_Verify(hSession, hash, hashSz, sig, sigSz);
WOLFSSL_MSG_EX("AriaVerify Verify rv=%d",rv);
wc_AriaFree(&hSession, &hPubkey);
@ -318,16 +343,22 @@ size and a key size of 128, 192, or 256 bits.
return BAD_FUNC_ARG;
}
if (rv == MC_OK) rv = wc_AriaInit();
if (rv == MC_OK)
rv = wc_AriaInit();
if (rv == MC_OK) rv = MC_OpenSession(&hSession);
if (rv == MC_OK)
rv = MC_OpenSession(&hSession);
if (rv == MC_OK) rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK)
rv = MC_SetApiMode(hSession, gApimode);
if (rv == MC_OK) {
int ret = wc_EccPublicKeyToDer(public_key,pubAsn1,pubAsn1Sz,0);
if (ret < 0) { rv = ret; }
else { pubAsn1Sz = ret; }
if (ret < 0) {
rv = ret;
} else {
pubAsn1Sz = ret;
}
WOLFSSL_MSG_EX("AriaDerive PublicKeyToDer ret=%d",ret);
}
WOLFSSL_MSG_EX("AriaVerify pubAsn1(%d):",pubAsn1Sz);
@ -337,8 +368,11 @@ size and a key size of 128, 192, or 256 bits.
if (rv == MC_OK) {
int ret = wc_EccPrivateKeyToDerNoCurve(private_key,privAsn1,privAsn1Sz);
if (ret < 0) { rv = ret; }
else { privAsn1Sz = ret; }
if (ret < 0) {
rv = ret;
} else {
privAsn1Sz = ret;
}
WOLFSSL_MSG_EX("AriaDerive PrivateKeyToDer ret=%d",ret);
}
WOLFSSL_MSG_EX("AriaVerify privAsn1(%d):",privAsn1Sz);
@ -359,10 +393,12 @@ size and a key size of 128, 192, or 256 bits.
rv = MC_ERR_UNSUPPORTED_ALGORITHM;
}
if (rv == MC_OK) rv = MC_CreateObject(hSession, privAsn1, privAsn1Sz, &hPrikey);
if (rv == MC_OK)
rv = MC_CreateObject(hSession, privAsn1, privAsn1Sz, &hPrikey);
WOLFSSL_MSG_EX("AriaDerive CreateObject rv=%d",rv);
if (rv == MC_OK) rv = MC_DeriveKey(hSession, &mcAlg, hPrikey, out, outSz);
if (rv == MC_OK)
rv = MC_DeriveKey(hSession, &mcAlg, hPrikey, out, outSz);
WOLFSSL_MSG_EX("AriaDerive DeriveKey rv=%d",rv);
wc_AriaFree(&hSession, &hPrikey);
@ -469,7 +505,8 @@ int wc_AriaCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
printOutput((char *)"eccverify.key (after)",
(byte *)info->pk.eccverify.key,sizeof(info->pk.eccverify.key));
if (ret != 0) ret = CRYPTOCB_UNAVAILABLE;
if (ret != 0)
ret = CRYPTOCB_UNAVAILABLE;
/* reset devId */
info->pk.eccverify.key->devId = devIdArg;
}
@ -481,7 +518,8 @@ int wc_AriaCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
info->pk.ecdh.private_key, info->pk.ecdh.public_key,
info->pk.ecdh.out, info->pk.ecdh.outlen);
if (ret != 0) ret = CRYPTOCB_UNAVAILABLE;
if (ret != 0)
ret = CRYPTOCB_UNAVAILABLE;
/* reset devId */
info->pk.ecdh.private_key->devId = devIdArg;
}
@ -511,7 +549,8 @@ int wc_AriaCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
if ((ret == 0) || (ret == CRYPTOCB_UNAVAILABLE))
ret = wc_AriaFree(&(info->hash.sha256->hSession),NULL);
}
if (ret != 0) ret = CRYPTOCB_UNAVAILABLE;
if (ret != 0)
ret = CRYPTOCB_UNAVAILABLE;
/* reset devId */
info->hash.sha256->devId = devIdArg;
}