Skip to content

Commit

Permalink
Supoort HAVE_AES_ECB, Sha1, and Sha224
Browse files Browse the repository at this point in the history
  • Loading branch information
night1rider committed Aug 2, 2024
1 parent 03cc389 commit ebf7e55
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 5 deletions.
42 changes: 42 additions & 0 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -11506,6 +11506,48 @@ int wc_AesGetKeySize(Aes* aes, word32* keySize)
#elif defined(WOLFSSL_RISCV_ASM)
/* implemented in wolfcrypt/src/port/riscv/riscv-64-aes.c */

#elif defined(MAX3266X_AES)

int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
int status;
word32 keySize;

if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG;

status = wc_AesGetKeySize(aes, &keySize);
if (status != 0) {
return status;
}

status = wc_MXC_TPU_AesEncrypt(in, aes->reg, aes->key, MXC_TPU_MODE_ECB,
sz, out, keySize);

return status;
}

#ifdef HAVE_AES_DECRYPT
int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
int status;
word32 keySize;

if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG;

status = wc_AesGetKeySize(aes, &keySize);
if (status != 0) {
return status;
}

status = wc_MXC_TPU_AesDecrypt(in, aes->reg, aes->key, MXC_TPU_MODE_ECB,
sz, out, keySize);

return status;
}
#endif /* HAVE_AES_DECRYPT */

#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)

/* Software AES - ECB */
Expand Down
109 changes: 105 additions & 4 deletions wolfcrypt/src/port/maxim/max3266x.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int wc_MXC_TPU_AesEncrypt(const unsigned char* in, const unsigned char* iv,
}
return 0;
}

#ifdef HAVE_AES_DECRYPT
int wc_MXC_TPU_AesDecrypt(const unsigned char* in, const unsigned char* iv,
const unsigned char* dec_key,
MXC_TPU_MODE_TYPE mode, unsigned int data_size,
Expand Down Expand Up @@ -227,7 +227,7 @@ int wc_MXC_TPU_AesDecrypt(const unsigned char* in, const unsigned char* iv,
}
return 0;
}

#endif /* HAVE_AES_DECRYPT */
#endif /* MAX3266X_AES */

#if defined(MAX3266X_SHA)
Expand Down Expand Up @@ -355,11 +355,21 @@ int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest,
{
if (hash->msg == 0 && hash->size == 0 && digest != NULL) {
switch(algo) {
#ifndef NO_SHA
case MXC_TPU_HASH_SHA1:
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA1, WC_SHA_DIGEST_SIZE);
break;
#endif /* NO_SHA */
#ifndef NO_SHA256
case MXC_TPU_HASH_SHA256:
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, WC_SHA256_DIGEST_SIZE);
break;
#endif
#endif /* NO_SHA256 */
#ifdef WOLFSSL_SHA224
case MXC_TPU_HASH_SHA224:
XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE);
break;
#endif /* WOLFSSL_SHA224 */
default:
return BAD_FUNC_ARG;
}
Expand All @@ -368,6 +378,97 @@ int wc_MXC_TPU_SHA_GetDigest(wc_MXC_Sha *hash, unsigned char* digest,
return 0; /* False */
}

#if !defined(NO_SHA)

WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
{
if (sha == NULL) {
return BAD_FUNC_ARG;
}
(void)heap;
(void)devId;
return wc_MXC_TPU_SHA_Init((wc_MXC_Sha *)sha);
}

WOLFSSL_API int wc_ShaUpdate(wc_Sha* sha, const unsigned char* data,
unsigned int len)
{
return wc_MXC_TPU_SHA_Update(sha, data, len);
}

WOLFSSL_API int wc_ShaFinal(wc_Sha* sha, unsigned char* hash)
{
return wc_MXC_TPU_SHA_Final((wc_MXC_Sha *)sha, hash,
MXC_TPU_HASH_SHA1);
}

WOLFSSL_API int wc_ShaGetHash(wc_Sha* sha, unsigned char* hash)
{
return wc_MXC_TPU_SHA_GetHash((wc_MXC_Sha *)sha, hash,
MXC_TPU_HASH_SHA1);
}

WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
{
return wc_MXC_TPU_SHA_Copy((wc_MXC_Sha *)src, (wc_MXC_Sha *)dst);
}

WOLFSSL_API void wc_ShaFree(wc_Sha* sha)
{
wc_MXC_TPU_SHA_Free((wc_MXC_Sha *)sha);
return;
}

#endif /* NO_SHA */

#if defined(WOLFSSL_SHA224)

WOLFSSL_API int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
{
if (sha224 == NULL) {
return BAD_FUNC_ARG;
}
(void)heap;
(void)devId;
return wc_MXC_TPU_SHA_Init((wc_MXC_Sha *)sha224);
}

WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224)
{
return wc_InitSha224_ex(sha224, NULL, INVALID_DEVID);
}

WOLFSSL_API int wc_Sha224Update(wc_Sha224* sha224, const unsigned char* data,
unsigned int len)
{
return wc_MXC_TPU_SHA_Update(sha224, data, len);
}

WOLFSSL_API int wc_Sha224Final(wc_Sha224* sha224, unsigned char* hash)
{
return wc_MXC_TPU_SHA_Final((wc_MXC_Sha *)sha224, hash,
MXC_TPU_HASH_SHA224);
}

WOLFSSL_API int wc_Sha224GetHash(wc_Sha224* sha224, unsigned char* hash)
{
return wc_MXC_TPU_SHA_GetHash((wc_MXC_Sha *)sha224, hash,
MXC_TPU_HASH_SHA224);
}

WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
{
return wc_MXC_TPU_SHA_Copy((wc_MXC_Sha *)src, (wc_MXC_Sha *)dst);
}

WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224)
{
wc_MXC_TPU_SHA_Free((wc_MXC_Sha *)sha224);
return;
}

#endif /* WOLFSSL_SHA224 */

#if !defined(NO_SHA256)

WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
Expand Down Expand Up @@ -414,7 +515,7 @@ WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256)
return;
}

#endif
#endif /* NO_SHA256 */

#endif /* MAX3266X_SHA */

Expand Down
10 changes: 10 additions & 0 deletions wolfcrypt/src/sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@
!defined(WOLFSSL_QNX_CAAM)
/* wolfcrypt/src/port/caam/caam_sha.c */

#elif defined(MAX3266X_SHA)
/* Already brought in by sha.h */
/* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */

#elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \
defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW)

Expand Down Expand Up @@ -1035,6 +1039,8 @@ int wc_InitSha(wc_Sha* sha)

#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)

#ifndef MAX3266X_SHA

void wc_ShaFree(wc_Sha* sha)
{
if (sha == NULL)
Expand Down Expand Up @@ -1068,6 +1074,7 @@ void wc_ShaFree(wc_Sha* sha)
#endif
}

#endif /* !MAX3266X_SHA */
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
#endif /* !WOLFSSL_TI_HASH */

Expand All @@ -1082,6 +1089,8 @@ void wc_ShaFree(wc_Sha* sha)

#if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH)

#ifndef MAX3266X_SHA

/* wc_ShaGetHash get hash value */
int wc_ShaGetHash(wc_Sha* sha, byte* hash)
{
Expand Down Expand Up @@ -1152,6 +1161,7 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
return ret;
}
#endif /* WOLFSSL_RENESAS_RX64_HASH */
#endif /* !MAX3266X_SHA */
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
#endif /* !defined(WOLFSSL_RENESAS_TSIP_TLS) && \
!defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) ||
Expand Down
6 changes: 6 additions & 0 deletions wolfcrypt/src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,9 @@ static int InitSha256(wc_Sha256* sha256)
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */

#elif defined(MAX3266X_SHA)
/* implemented in wolfcrypt/src/port/maxim/max3266x.c */

#elif defined(WOLFSSL_RENESAS_RX64_HASH)

/* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */
Expand Down Expand Up @@ -2337,6 +2340,9 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH)
/* implemented in wolfcrypt/src/port/psa/psa_hash.c */

#elif defined(MAX3266X_SHA)
/* implemented in wolfcrypt/src/port/maxim/max3266x.c */

#else

int wc_Sha224GetHash(wc_Sha224* sha224, byte* hash)
Expand Down
29 changes: 28 additions & 1 deletion wolfssl/wolfcrypt/port/maxim/max3266x.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@
unsigned char hash[WOLFSSL_MAX_HASH_SIZE];
} wc_MXC_Sha;

#if !defined(NO_SHA)
typedef wc_MXC_Sha wc_Sha;
#define WC_SHA_TYPE_DEFINED

/* Define the SHA digest for an empty string */
/* as a constant byte array */
static const unsigned char MXC_EMPTY_DIGEST_SHA1[20] = {
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
0xaf, 0xd8, 0x07, 0x09};

#endif /* NO_SHA */

#if defined(WOLFSSL_SHA224)
typedef wc_MXC_Sha wc_Sha224;
#define WC_SHA224_TYPE_DEFINED

/* Define the SHA-224 digest for an empty string */
/* as a constant byte array */
static const unsigned char MXC_EMPTY_DIGEST_SHA224[28] = {
0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9,
0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a,
0xc5, 0xb3, 0xe4, 0x2f};

#endif /* WOLFSSL_SHA224 */

#if !defined(NO_SHA256)
typedef wc_MXC_Sha wc_Sha256;
#define WC_SHA256_TYPE_DEFINED
Expand All @@ -236,7 +263,7 @@
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};

#endif
#endif /* NO_SHA256 */


WOLFSSL_LOCAL int wc_MXC_TPU_SHA_Init(wc_MXC_Sha *hash);
Expand Down

0 comments on commit ebf7e55

Please sign in to comment.