Skip to content

Commit

Permalink
fixup! crypto: stm32: use HASH IP for HASH and HMAC algorithm
Browse files Browse the repository at this point in the history
hash.c & hmac.c:
 - Initial variables with errors where possible,
 - Remove dynamic allocation in do_hmac_final() and do_hash_final()
stm32_hash.c:
 - Fix documentation
 - Remove unnecessary "U"
 - Add space before }
 - Remove unnecessary parenthesis

Signed-off-by: Thomas Bourgoin <[email protected]>
  • Loading branch information
tbourgoi committed Feb 6, 2025
1 parent f44378c commit d69755b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
28 changes: 10 additions & 18 deletions core/drivers/crypto/stm32/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,17 @@ static TEE_Result do_hash_final(struct crypto_hash_ctx *ctx, uint8_t *digest,
size_t len)
{
struct stm32_hash_ctx *c = to_stm32_hash_ctx(ctx);
TEE_Result res = TEE_SUCCESS;
uint8_t *full_digest = NULL;
bool alloc = false;

if (len < stm32_hash_digest_size(&c->hash)) {
full_digest = malloc(stm32_hash_digest_size(&c->hash));
if (!full_digest)
return TEE_ERROR_OUT_OF_MEMORY;
alloc = true;
} else {
full_digest = digest;
}
TEE_Result res = TEE_ERROR_GENERIC;
uint8_t block_digest[STM32_HASH_MAX_DIGEST_SIZE] = { 0 };
uint8_t *tmp_digest = digest;

res = stm32_hash_final(&c->hash, full_digest, NULL, 0);
if (len < stm32_hash_digest_size(&c->hash))
tmp_digest = block_digest;

if (alloc) {
memcpy(digest, full_digest, len);
free(full_digest);
}
res = stm32_hash_final(&c->hash, tmp_digest, NULL, 0);

if (res == TEE_SUCCESS && len < stm32_hash_digest_size(&c->hash))
memcpy(digest, tmp_digest, len);

return res;
}
Expand Down Expand Up @@ -138,7 +130,7 @@ static const struct crypto_hash_ops hash_ops = {
static TEE_Result stm32_hash_allocate(struct crypto_hash_ctx **ctx,
uint32_t algo)
{
TEE_Result res = TEE_SUCCESS;
TEE_Result res = TEE_ERROR_GENERIC;
struct stm32_hash_ctx *c = NULL;
enum stm32_hash_algo stm32_algo = STM32_HASH_SHA256;

Expand Down
40 changes: 22 additions & 18 deletions core/drivers/crypto/stm32/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static struct stm32_hmac_ctx *to_stm32_hmac_ctx(struct crypto_mac_ctx *ctx)
* Initialization of the hmac operation
*
* @ctx Operation software context
* @key Key used for hmac operation
* @len Length of @key in bytes
*/
static TEE_Result do_hmac_init(struct crypto_mac_ctx *ctx, const uint8_t *key,
size_t len)
Expand Down Expand Up @@ -70,6 +72,11 @@ static TEE_Result do_hmac_update(struct crypto_mac_ctx *ctx,
{
struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx);

if (!c->key) {
EMSG("NULL key pointer");
return TEE_ERROR_OUT_OF_MEMORY;
}

return stm32_hash_update(&c->hash, data, len);
}

Expand All @@ -84,25 +91,22 @@ static TEE_Result do_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest,
size_t len)
{
struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx);
TEE_Result res = TEE_SUCCESS;
uint8_t *full_digest = NULL;
bool alloc = false;

if (len < stm32_hash_digest_size(&c->hash)) {
full_digest = malloc(stm32_hash_digest_size(&c->hash));
if (!full_digest)
return TEE_ERROR_OUT_OF_MEMORY;
alloc = true;
} else {
full_digest = digest;
TEE_Result res = TEE_ERROR_GENERIC;
uint8_t block_digest[STM32_HASH_MAX_DIGEST_SIZE] = { 0 };
uint8_t *tmp_digest = digest;

if (!c->key) {
EMSG("NULL key pointer");
return TEE_ERROR_OUT_OF_MEMORY;
}

res = stm32_hash_final(&c->hash, full_digest, c->key, c->key_len);
if (len < stm32_hash_digest_size(&c->hash))
tmp_digest = block_digest;

if (alloc) {
memcpy(digest, full_digest, len);
free(full_digest);
}
res = stm32_hash_final(&c->hash, tmp_digest, c->key, c->key_len);

if (res == TEE_SUCCESS && len < stm32_hash_digest_size(&c->hash))
memcpy(digest, tmp_digest, len);

return res;
}
Expand Down Expand Up @@ -167,8 +171,8 @@ static const struct crypto_mac_ops hmac_ops = {
static TEE_Result stm32_hmac_allocate(struct crypto_mac_ctx **ctx,
uint32_t algo)
{
TEE_Result res = TEE_SUCCESS;
enum stm32_hash_algo stm32_algo;
TEE_Result res = TEE_ERROR_GENERIC;
enum stm32_hash_algo stm32_algo = STM32_HASH_MD5;
struct stm32_hmac_ctx *c = NULL;

switch (TEE_ALG_GET_MAIN_ALG(algo)) {
Expand Down
16 changes: 8 additions & 8 deletions core/drivers/crypto/stm32/stm32_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,14 @@ static TEE_Result hash_get_digest(struct stm32_hash_context *c, uint8_t *digest)
{
TEE_Result res = TEE_ERROR_GENERIC;
vaddr_t base = c->dev->pdata.base;
uint32_t i = 0U;
uint32_t dsg = 0U;
uint32_t i = 0;
uint32_t dsg = 0;

res = wait_digest_ready(base);
if (res)
return res;

for (i = 0U; i < c->digest_u32; i++) {
for (i = 0; i < c->digest_u32; i++) {
dsg = TEE_U32_FROM_BIG_ENDIAN(io_read32(base + _HASH_HR(i)));
memcpy(digest + (i * sizeof(uint32_t)), &dsg, sizeof(uint32_t));
}
Expand Down Expand Up @@ -853,7 +853,7 @@ static TEE_Result stm32_hash_probe(const void *fdt, int node,
}

rev = io_read32(stm32_hash->pdata.base + _HASH_VERR);
FMSG("STM32 HASH V%u/%u", (rev & _HASH_VERR_MAJREV) >> 4,
FMSG("STM32 HASH v%"PRIu32".%"PRIu32, (rev & _HASH_VERR_MAJREV) >> 4,
rev & _HASH_VERR_MINREV);

if (stm32_hash->pdata.reset &&
Expand Down Expand Up @@ -888,17 +888,17 @@ static TEE_Result stm32_hash_probe(const void *fdt, int node,
}

static const struct stm32_hash_compat mp13_compat = {
.caps = (CAPS_SHA1 | CAPS_SHA2_224 | CAPS_SHA2_256 | CAPS_SHA2_384 |
CAPS_SHA2_512 | CAPS_SHA3),
.caps = CAPS_SHA1 | CAPS_SHA2_224 | CAPS_SHA2_256 | CAPS_SHA2_384 |
CAPS_SHA2_512 | CAPS_SHA3,
};

static const struct stm32_hash_compat mp15_compat = {
.caps = CAPS_MD5 | CAPS_SHA1 | CAPS_SHA2_224 | CAPS_SHA2_256,
};

static const struct dt_device_match hash_match_table[] = {
{ .compatible = "st,stm32mp13-hash", .compat_data = &mp13_compat},
{ .compatible = "st,stm32f756-hash", .compat_data = &mp15_compat},
{ .compatible = "st,stm32mp13-hash", .compat_data = &mp13_compat },
{ .compatible = "st,stm32f756-hash", .compat_data = &mp15_compat },
{ }
};

Expand Down
3 changes: 3 additions & 0 deletions core/drivers/crypto/stm32/stm32_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <mm/core_memprot.h>
#include <stdint.h>

/* Max size supported is SHA512 */
#define STM32_HASH_MAX_DIGEST_SIZE U(64)

struct stm32_hash_compat {
uint32_t caps;
};
Expand Down

0 comments on commit d69755b

Please sign in to comment.