Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 95 additions & 1 deletion wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4871,7 +4871,10 @@ static WARN_UNUSED_RESULT int wc_AesDecrypt(Aes* aes, const byte* inBlock,
int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
const byte* iv, int dir)
{
return wc_Psoc6_Aes_SetKey(aes, userKey, keylen, iv, dir);
int ret = wc_Psoc6_Aes_SetKey(aes, userKey, keylen, iv, dir);
if (ret == 0 && aes != NULL)
aes->keySet = 1;
return ret;
}

#if defined(WOLFSSL_AES_DIRECT)
Expand Down Expand Up @@ -5188,6 +5191,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
if (ret == 0) {
/* Callback succeeded - SE owns the key */
aes->keylen = (int)keylen;
aes->keySet = 1;
if (iv != NULL)
XMEMCPY(aes->reg, iv, WC_AES_BLOCK_SIZE);
else
Expand Down Expand Up @@ -5304,6 +5308,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
* reads it as the source of truth for the configured key size. */
aes->keylen = (int)keylen;
aes->rounds = (keylen / 4) + 6;
aes->keySet = 1;
#if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \
defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_XTS) || \
defined(WOLFSSL_AES_CTS)
Expand Down Expand Up @@ -5333,6 +5338,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)

aes->keylen = (int)keylen;
aes->rounds = (keylen/4) + 6;
aes->keySet = 1;
ret = wc_AesSetIV(aes, iv);
if (ret != 0)
return ret;
Expand Down Expand Up @@ -6699,12 +6705,20 @@ int wc_AesSetIV(Aes* aes, const byte* iv)

int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if (aes == NULL || aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}
return wc_Psoc6_Aes_CbcEncrypt(aes, out, in, sz);
}

#if defined(HAVE_AES_DECRYPT)
int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if (aes == NULL || aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}
return wc_Psoc6_Aes_CbcDecrypt(aes, out, in, sz);
}
#endif /* HAVE_AES_DECRYPT */
Expand Down Expand Up @@ -6760,6 +6774,13 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
/* fall-through when unavailable */
}
#endif

/* Software/HW key schedule required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}
Comment thread
julek-wolfssl marked this conversation as resolved.
Comment on lines +6778 to +6782

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
/* if async and byte count above threshold */
if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
Expand Down Expand Up @@ -6970,6 +6991,13 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
/* fall-through when unavailable */
}
#endif

/* Software/HW key schedule required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
/* if async and byte count above threshold */
if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
Expand Down Expand Up @@ -7425,6 +7453,12 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
}
#endif

/* Software/HW key schedule required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

/* consume any unused bytes left in aes->tmp */
processed = min(aes->left, sz);
xorbufout(out, in, (byte*)aes->tmp + WC_AES_BLOCK_SIZE - aes->left,
Expand Down Expand Up @@ -10423,6 +10457,12 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
}
#endif

/* Software/HW key schedule (and hash subkey H) required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
/* if async and byte count above threshold */
/* only 12-byte IV is supported in HW */
Expand Down Expand Up @@ -11173,6 +11213,12 @@ int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
}
#endif

/* Software/HW key schedule (and hash subkey H) required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
/* if async and byte count above threshold */
/* only 12-byte IV is supported in HW */
Expand Down Expand Up @@ -14449,6 +14495,10 @@ int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG;
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

return wc_Psoc6_Aes_EcbEncrypt(aes, out, in, sz);
}
Expand All @@ -14460,6 +14510,10 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG;
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

return wc_Psoc6_Aes_EcbDecrypt(aes, out, in, sz);
}
Expand Down Expand Up @@ -14497,6 +14551,12 @@ static WARN_UNUSED_RESULT int _AesEcbEncrypt(
return DCPAesEcbEncrypt(aes, out, in, sz);
#endif

/* Software key schedule required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

VECTOR_REGISTERS_PUSH;

#if !defined(__aarch64__) && defined(WOLFSSL_ARMASM)
Expand Down Expand Up @@ -14592,6 +14652,12 @@ static WARN_UNUSED_RESULT int _AesEcbDecrypt(
return DCPAesEcbDecrypt(aes, out, in, sz);
#endif

/* Software key schedule required from here on. */
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

VECTOR_REGISTERS_PUSH;

#if !defined(__aarch64__) && defined(WOLFSSL_ARMASM)
Expand Down Expand Up @@ -14694,12 +14760,20 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)

int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if (aes == NULL || aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}
return wc_Psoc6_Aes_CfbEncrypt(aes, out, in, sz);
}

#ifdef HAVE_AES_DECRYPT
int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
if (aes == NULL || aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}
return wc_Psoc6_Aes_CfbDecrypt(aes, out, in, sz);
}
#endif /* HAVE_AES_DECRYPT */
Expand Down Expand Up @@ -14732,6 +14806,10 @@ static WARN_UNUSED_RESULT int AesCfbEncrypt_C(Aes* aes, byte* out,
if (sz == 0) {
return 0;
}
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

if (aes->left > 0) {
/* consume any unused bytes left in aes->tmp */
Expand Down Expand Up @@ -14807,6 +14885,10 @@ static WARN_UNUSED_RESULT int AesCfbDecrypt_C(Aes* aes, byte* out,
if (sz == 0) {
return 0;
}
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

if (aes->left > 0) {
/* consume any unused bytes left in aes->tmp */
Expand Down Expand Up @@ -14951,6 +15033,10 @@ static WARN_UNUSED_RESULT int wc_AesFeedbackCFB8(
if (sz == 0) {
return 0;
}
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

VECTOR_REGISTERS_PUSH;

Expand Down Expand Up @@ -15011,6 +15097,10 @@ static WARN_UNUSED_RESULT int wc_AesFeedbackCFB1(
if (sz == 0) {
return 0;
}
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

VECTOR_REGISTERS_PUSH;

Expand Down Expand Up @@ -15169,6 +15259,10 @@ static WARN_UNUSED_RESULT int AesOfbCrypt_C(Aes* aes, byte* out, const byte* in,
if (sz == 0) {
return 0;
}
if (aes->keySet == 0) {
WOLFSSL_MSG("AES key not set");
return BAD_FUNC_ARG;
}

if (aes->left > 0) {
/* consume any unused bytes left in aes->tmp */
Expand Down
Loading