Support for Silicon Labs Gecko SDK v4. Changes SE Hash to use multipart API's. Gecko SDK v3 auto-detected or manually forced using WOLFSSL_SILABS_SE_ACCEL_3.

This commit is contained in:
David Garske
2023-06-01 13:54:36 -07:00
parent 83dca07421
commit fc153ff273
6 changed files with 142 additions and 102 deletions

View File

@ -54,6 +54,7 @@ include IDE/MCUEXPRESSO/include.am
include IDE/Espressif/include.am
include IDE/STARCORE/include.am
include IDE/MDK5-ARM/include.am
include IDE/SimplicityStudio/include.am
EXTRA_DIST+= IDE/IAR-EWARM IDE/MDK-ARM IDE/MYSQL IDE/LPCXPRESSO IDE/HEXIWEAR IDE/Espressif
EXTRA_DIST+= IDE/OPENSTM32/README.md

View File

@ -35,58 +35,52 @@
#include <wolfssl/wolfcrypt/port/silabs/silabs_hash.h>
static sl_se_hash_type_t wc_silabs_gethashtype(enum wc_HashType type)
{
/* set init state */
switch (type) {
case WC_HASH_TYPE_SHA:
return SL_SE_HASH_SHA1;
break;
case WC_HASH_TYPE_SHA224:
return SL_SE_HASH_SHA224;
break;
case WC_HASH_TYPE_SHA256:
return SL_SE_HASH_SHA256;
#ifdef WOLFSSL_SILABS_SHA384
case WC_HASH_TYPE_SHA384:
return SL_SE_HASH_SHA384;
#endif
#ifdef WOLFSSL_SILABS_SHA512
case WC_HASH_TYPE_SHA512:
return SL_SE_HASH_SHA512;
#endif
default:
break;
}
return SL_SE_HASH_NONE;
}
int wc_silabs_se_hash_init (wc_silabs_sha_t* sha, enum wc_HashType type)
{
int ret = 0;
sl_status_t rr;
sl_status_t rr;
sl_se_hash_type_t ht = wc_silabs_gethashtype(type);
if (ht == SL_SE_HASH_NONE) {
return NOT_COMPILED_IN;
}
/* set sizes and state */
XMEMSET(sha, 0, sizeof(wc_silabs_sha_t));
/* set init state */
switch(type) {
case WC_HASH_TYPE_SHA:
rr = sl_se_hash_starts(&sha->hash_ctx,
&sha->cmd_ctx,
SL_SE_HASH_SHA1,
&sha->hash_type_ctx);
break;
case WC_HASH_TYPE_SHA224:
rr = sl_se_hash_starts(&sha->hash_ctx,
&sha->cmd_ctx,
SL_SE_HASH_SHA224,
&sha->hash_type_ctx);
break;
case WC_HASH_TYPE_SHA256:
rr = sl_se_hash_starts(&sha->hash_ctx,
&sha->cmd_ctx,
SL_SE_HASH_SHA256,
&sha->hash_type_ctx);
break;
#ifdef WOLFSSL_SILABS_SHA384
case WC_HASH_TYPE_SHA384:
rr = sl_se_hash_starts(&sha->hash_ctx,
&sha->cmd_ctx,
SL_SE_HASH_SHA384,
&sha->hash_type_ctx);
break;
#ifdef WOLFSSL_SILABS_SE_ACCEL_3
rr = sl_se_hash_starts(&sha->hash_ctx, &sha->cmd_ctx, ht,
&sha->hash_type_ctx);
#else
rr = sl_se_hash_multipart_starts(&sha->hash_type_ctx, &sha->cmd_ctx, ht);
#endif
#ifdef WOLFSSL_SILABS_SHA512
case WC_HASH_TYPE_SHA512:
rr = sl_se_hash_starts(&sha->hash_ctx,
&sha->cmd_ctx,
SL_SE_HASH_SHA512,
&sha->hash_type_ctx);
break;
#endif
default:
ret = BAD_FUNC_ARG;
break;
}
if (rr != SL_STATUS_OK) {
ret = WC_HW_E;
}
@ -98,18 +92,31 @@ int wc_silabs_se_hash_update(wc_silabs_sha_t* sha, const byte* data,
word32 len)
{
int ret = 0;
sl_status_t status = sl_se_hash_update(&sha->hash_ctx, data, len);
sl_status_t status;
#ifdef WOLFSSL_SILABS_SE_ACCEL_3
status = sl_se_hash_update(&sha->hash_ctx, data, len);
#else
status = sl_se_hash_multipart_update(&sha->hash_type_ctx, &sha->cmd_ctx,
data, len);
#endif
if (status != SL_STATUS_OK) {
ret = WC_HW_E;
}
return ret;
}
int wc_silabs_se_hash_final(wc_silabs_sha_t* sha, byte* hash)
int wc_silabs_se_hash_final(wc_silabs_sha_t* sha, byte* hash, word32 len)
{
int ret = 0;
sl_status_t status = sl_se_hash_finish(&sha->hash_ctx, hash,
sha->hash_ctx.size);
sl_status_t status;
#ifdef WOLFSSL_SILABS_SE_ACCEL_3
status = sl_se_hash_finish(&sha->hash_ctx, hash, len);
#else
status = sl_se_hash_multipart_finish(&sha->hash_type_ctx, &sha->cmd_ctx,
hash, len);
#endif
if (status != SL_STATUS_OK) {
ret = WC_HW_E;
}
@ -117,7 +124,7 @@ int wc_silabs_se_hash_final(wc_silabs_sha_t* sha, byte* hash)
}
int wc_HashUpdate_ex(wc_silabs_sha_t* sha, const byte* data, word32 len)
static int wc_HashUpdate_ex(wc_silabs_sha_t* sha, const byte* data, word32 len)
{
int ret = 0;
@ -133,7 +140,7 @@ int wc_HashUpdate_ex(wc_silabs_sha_t* sha, const byte* data, word32 len)
return ret;
}
int wc_HashFinal_ex(wc_silabs_sha_t* sha, byte* hash)
static int wc_HashFinal_ex(wc_silabs_sha_t* sha, byte* hash, word32 len)
{
int ret = 0;
@ -143,7 +150,7 @@ int wc_HashFinal_ex(wc_silabs_sha_t* sha, byte* hash)
ret = wolfSSL_CryptHwMutexLock();
if (ret == 0) {
ret = wc_silabs_se_hash_final(sha, hash);
ret = wc_silabs_se_hash_final(sha, hash, len);
wolfSSL_CryptHwMutexUnLock();
}
@ -161,17 +168,17 @@ int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
(void)devId;
(void)heap;
return wc_silabs_se_hash_init(&(sha->silabsCtx), WC_HASH_TYPE_SHA);
return wc_silabs_se_hash_init(&sha->silabsCtx, WC_HASH_TYPE_SHA);
}
int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
{
return wc_HashUpdate_ex(&(sha->silabsCtx), data, len);
return wc_HashUpdate_ex(&sha->silabsCtx, data, len);
}
int wc_ShaFinal(wc_Sha* sha, byte* hash)
{
int ret = wc_HashFinal_ex(&(sha->silabsCtx), hash);
int ret = wc_HashFinal_ex(&sha->silabsCtx, hash, WC_SHA_DIGEST_SIZE);
(void)wc_InitSha(sha); /* reset state */
@ -190,18 +197,18 @@ int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId)
(void)devId;
(void)heap;
return wc_silabs_se_hash_init(&(sha->silabsCtx), WC_HASH_TYPE_SHA256);
return wc_silabs_se_hash_init(&sha->silabsCtx, WC_HASH_TYPE_SHA256);
}
int wc_Sha256Update(wc_Sha256* sha, const byte* data, word32 len)
{
return wc_HashUpdate_ex(&(sha->silabsCtx), data, len);
return wc_HashUpdate_ex(&sha->silabsCtx, data, len);
}
int wc_Sha256Final(wc_Sha256* sha, byte* hash)
{
int ret = wc_HashFinal_ex(&(sha->silabsCtx), hash);
int ret = wc_HashFinal_ex(&sha->silabsCtx, hash, WC_SHA256_DIGEST_SIZE);
(void)wc_InitSha256(sha); /* reset state */
@ -219,18 +226,18 @@ int wc_InitSha224_ex(wc_Sha224* sha, void* heap, int devId)
(void)devId;
(void)heap;
return wc_silabs_se_hash_init(&(sha->silabsCtx), WC_HASH_TYPE_SHA224);
return wc_silabs_se_hash_init(&sha->silabsCtx, WC_HASH_TYPE_SHA224);
}
int wc_Sha224Update(wc_Sha224* sha, const byte* data, word32 len)
{
return wc_HashUpdate_ex(&(sha->silabsCtx), data, len);
return wc_HashUpdate_ex(&sha->silabsCtx, data, len);
}
int wc_Sha224Final(wc_Sha224* sha, byte* hash)
{
int ret = wc_HashFinal_ex(&(sha->silabsCtx), hash);
int ret = wc_HashFinal_ex(&sha->silabsCtx, hash, WC_SHA224_DIGEST_SIZE);
(void)wc_InitSha224(sha); /* reset state */
@ -248,18 +255,18 @@ int wc_InitSha384_ex(wc_Sha384* sha, void* heap, int devId)
(void)devId;
(void)heap;
return wc_silabs_se_hash_init(&(sha->silabsCtx), WC_HASH_TYPE_SHA384);
return wc_silabs_se_hash_init(&sha->silabsCtx, WC_HASH_TYPE_SHA384);
}
int wc_Sha384Update(wc_Sha384* sha, const byte* data, word32 len)
{
return wc_HashUpdate_ex(&(sha->silabsCtx), data, len);
return wc_HashUpdate_ex(&sha->silabsCtx, data, len);
}
int wc_Sha384Final(wc_Sha384* sha, byte* hash)
{
int ret = wc_HashFinal_ex(&(sha->silabsCtx), hash);
int ret = wc_HashFinal_ex(&sha->silabsCtx, hash, WC_SHA384_DIGEST_SIZE);
(void)wc_InitSha384(sha); /* reset state */
@ -277,18 +284,18 @@ int wc_InitSha512_ex(wc_Sha512* sha, void* heap, int devId)
(void)devId;
(void)heap;
return wc_silabs_se_hash_init(&(sha->silabsCtx), WC_HASH_TYPE_SHA512);
return wc_silabs_se_hash_init(&sha->silabsCtx, WC_HASH_TYPE_SHA512);
}
int wc_Sha512Update(wc_Sha512* sha, const byte* data, word32 len)
{
return wc_HashUpdate_ex(&(sha->silabsCtx), data, len);
return wc_HashUpdate_ex(&sha->silabsCtx, data, len);
}
int wc_Sha512Final(wc_Sha512* sha, byte* hash)
{
int ret = wc_HashFinal_ex(&(sha->silabsCtx), hash);
int ret = wc_HashFinal_ex(&sha->silabsCtx, hash, WC_SHA512_DIGEST_SIZE);
(void)wc_InitSha512(sha); /* reset state */

View File

@ -995,9 +995,9 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
XMEMCPY(dst, src, sizeof(wc_Sha));
#ifdef WOLFSSL_SILABS_SE_ACCEL
dst->silabsCtx.hash_ctx.cmd_ctx = &(dst->silabsCtx.cmd_ctx);
dst->silabsCtx.hash_ctx.hash_type_ctx = &(dst->silabsCtx.hash_type_ctx);
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)

View File

@ -1933,9 +1933,9 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
dst->W = NULL;
#endif
#ifdef WOLFSSL_SILABS_SE_ACCEL
dst->silabsCtx.hash_ctx.cmd_ctx = &(dst->silabsCtx.cmd_ctx);
dst->silabsCtx.hash_ctx.hash_type_ctx = &(dst->silabsCtx.hash_type_ctx);
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
@ -2068,9 +2068,9 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
dst->W = NULL;
#endif
#ifdef WOLFSSL_SILABS_SE_ACCEL
dst->silabsCtx.hash_ctx.cmd_ctx = &(dst->silabsCtx.cmd_ctx);
dst->silabsCtx.hash_ctx.hash_type_ctx = &(dst->silabsCtx.hash_type_ctx);
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)

View File

@ -1616,9 +1616,10 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
dst->W = NULL;
#endif
#ifdef WOLFSSL_SILABS_SHA512
dst->silabsCtx.hash_ctx.cmd_ctx = &(dst->silabsCtx.cmd_ctx);
dst->silabsCtx.hash_ctx.hash_type_ctx = &(dst->silabsCtx.hash_type_ctx);
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
defined(WOLFSSL_SILABS_SHA512)
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
@ -1867,9 +1868,10 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
dst->W = NULL;
#endif
#ifdef WOLFSSL_SILABS_SHA384
dst->silabsCtx.hash_ctx.cmd_ctx = &(dst->silabsCtx.cmd_ctx);
dst->silabsCtx.hash_ctx.hash_type_ctx = &(dst->silabsCtx.hash_type_ctx);
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) && \
defined(WOLFSSL_SILABS_SHA384)
dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx;
dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx;
#endif
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)

View File

@ -22,45 +22,75 @@
#ifndef _SILABS_HASH_H_
#define _SILABS_HASH_H_
#include <em_device.h>
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_SILABS_SE_ACCEL)
#include <em_device.h>
#include <wolfssl/wolfcrypt/types.h>
#include <sl_se_manager.h>
#include <sl_se_manager_hash.h>
#if defined(SL_SE_HASH_SHA384) && !defined(NO_SHA384)
#define WOLFSSL_SILABS_SHA384
/* workaround to detect older Gecko SDK version 3 */
#if !defined(WOLFSSL_SILABS_SE_ACCEL_3) && !defined(SL_SE_PRF_HMAC_SHA1)
/* Use streaming instead of new multipart */
#define WOLFSSL_SILABS_SE_ACCEL_3
#endif
#if defined(SL_SE_HASH_SHA512) && !defined(NO_SHA384)
#define WOLFSSL_SILABS_SHA512
/* Enable SHA2-2384 and SHA2-512 if HW supports and enabled */
#if (_SILICON_LABS_SECURITY_FEATURE == _SILICON_LABS_SECURITY_FEATURE_VAULT)
#ifdef WOLFSSL_SHA384
#define WOLFSSL_SILABS_SHA384
#endif
#ifdef WOLFSSL_SHA512
#define WOLFSSL_SILABS_SHA512
#endif
#endif
#ifdef WOLFSSL_SILABS_SE_ACCEL_3
/* Gecko SDK v3 uses "streaming" interface */
typedef struct {
sl_se_hash_streaming_context_t hash_ctx;
sl_se_command_context_t cmd_ctx;
union hash_type_ctx_u {
sl_se_sha1_streaming_context_t sha1_ctx;
sl_se_sha224_streaming_context_t sha224_ctx;
sl_se_sha256_streaming_context_t sha256_ctx;
#ifdef WOLFSSL_SILABS_SHA384
sl_se_sha384_streaming_context_t sha384_ctx;
#endif
#ifdef WOLFSSL_SILABS_SHA512
sl_se_sha512_streaming_context_t sha512_ctx;
#endif
} hash_type_ctx;
sl_se_hash_streaming_context_t hash_ctx;
sl_se_command_context_t cmd_ctx;
union hash_type_ctx_u {
sl_se_sha1_streaming_context_t sha1_ctx;
sl_se_sha224_streaming_context_t sha224_ctx;
sl_se_sha256_streaming_context_t sha256_ctx;
#ifdef WOLFSSL_SILABS_SHA384
sl_se_sha384_streaming_context_t sha384_ctx;
#endif
#ifdef WOLFSSL_SILABS_SHA512
sl_se_sha512_streaming_context_t sha512_ctx;
#endif
} hash_type_ctx;
} wc_silabs_sha_t;
#else
/* Gecko SDK v4 or later uses "multipart" interface */
typedef struct {
sl_se_command_context_t cmd_ctx;
union hash_type_ctx_u {
sl_se_sha1_multipart_context_t sha1_ctx;
sl_se_sha224_multipart_context_t sha224_ctx;
sl_se_sha256_multipart_context_t sha256_ctx;
#ifdef WOLFSSL_SILABS_SHA384
sl_se_sha384_multipart_context_t sha384_ctx;
#endif
#ifdef WOLFSSL_SILABS_SHA512
sl_se_sha512_multipart_context_t sha512_ctx;
#endif
} hash_type_ctx;
} wc_silabs_sha_t;
#endif
int wc_silabs_se_hash_init (wc_silabs_sha_t* sha, enum wc_HashType type);
int wc_silabs_se_hash_update (wc_silabs_sha_t* sha, const byte* data, word32 len);
int wc_silabs_se_hash_final (wc_silabs_sha_t* sha, byte* hash);
int wc_silabs_se_hash_init(wc_silabs_sha_t* sha, enum wc_HashType type);
int wc_silabs_se_hash_update(wc_silabs_sha_t* sha, const byte* data,
word32 len);
int wc_silabs_se_hash_final(wc_silabs_sha_t* sha, byte* hash, word32 len);
#endif /* defined(WOLFSSL_SILABS_SE_ACCEL) */
#endif /* WOLFSSL_SILABS_SE_ACCEL */
#endif /* _SILABS_HASH_H_ */