Skip to content

Commit

Permalink
allow data to be in ROM on hash update
Browse files Browse the repository at this point in the history
removed whitespaces

Update pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>

Update pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>

Update pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>

Update pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

Co-authored-by: mguetschow <mikolai.guetschow@tu-dresden.de>

added comment and updated function calls
  • Loading branch information
lulu254b committed Feb 21, 2025
1 parent 8aa0859 commit 0ff9fd5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
6 changes: 2 additions & 4 deletions examples/advanced_examples/psa_crypto/example_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@

#include "psa/crypto.h"

/* certain PSA backends require the data to be in RAM rather than ROM
* so these values cannot be `const` */
static uint8_t msg[] = "Hello World!";
static size_t msg_len = sizeof(msg)-1; // exclude NULL-byte
static const uint8_t msg[] = "Hello World!";
static const size_t msg_len = sizeof(msg)-1; // exclude NULL-byte

static const uint8_t hash_sha224[] = {
0x45, 0x75, 0xbb, 0x4e, 0xc1, 0x29, 0xdf, 0x63, 0x80, 0xce, 0xdd, 0xe6, 0xd7,
Expand Down
41 changes: 35 additions & 6 deletions pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,14 @@ psa_status_t cryptocell_310_common_hash_setup(CRYS_HASHUserContext_t *ctx,
return PSA_SUCCESS;
}

psa_status_t cryptocell_310_common_hash_update(CRYS_HASHUserContext_t *ctx,
static psa_status_t _hash_update_from_ram(CRYS_HASHUserContext_t *ctx,
const uint8_t *input,
size_t input_length)
{
CRYSError_t ret = 0;
size_t offset = 0;
size_t size;

if (!cryptocell_310_data_within_ram(input)) {
DEBUG("%s : cryptocell_310 data required to be in RAM.\n", RIOT_FILE_RELATIVE);
return PSA_ERROR_DATA_INVALID;
}

do {
if (input_length > CC310_MAX_HASH_INPUT_BLOCK) {
size = CC310_MAX_HASH_INPUT_BLOCK;
Expand All @@ -70,6 +65,40 @@ psa_status_t cryptocell_310_common_hash_update(CRYS_HASHUserContext_t *ctx,
offset += size;
} while ((input_length > 0) && (ret == CRYS_OK));

return ret;
}

psa_status_t cryptocell_310_common_hash_update(CRYS_HASHUserContext_t *ctx,
const uint8_t *input,
size_t input_length)
{
CRYSError_t ret = 0;
if (!cryptocell_310_data_within_ram(input)) {
/* copy input chuckwise into RAM until the end of input is reached */
uint8_t input_buf[PSA_HASH_MAX_SIZE];
size_t buf_size;
size_t offset = 0;

do {

memset(&input_buf, 0, PSA_HASH_MAX_SIZE);

buf_size = input_length - offset;
buf_size = buf_size < PSA_HASH_MAX_SIZE ? buf_size : PSA_HASH_MAX_SIZE;

memcpy(&input_buf, input + offset, buf_size);

ret = _hash_update_from_ram(ctx, input_buf, buf_size);

offset += PSA_HASH_MAX_SIZE;

} while ((offset < input_length) && (ret == CRYS_OK));

}
else {
ret = _hash_update_from_ram(ctx, input, input_length);
}

if (ret != CRYS_OK) {
DEBUG("CRYS_HASH_Update failed with %s\n", cryptocell310_status_to_humanly_readable(ret));
return CRYS_to_psa_error(ret);
Expand Down
8 changes: 3 additions & 5 deletions tests/sys/psa_crypto_hashes/example_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

#include "psa/crypto.h"

/* certain PSA backends require the data to be in RAM rather than ROM
* so these values cannot be `const` */
static uint8_t msg[] = "Hello World!";
static size_t msg_len = sizeof(msg)-1; // exclude NULL-byte
static const uint8_t msg[] = "Hello World!";
static const size_t msg_len = sizeof(msg)-1; // exclude NULL-byte

static const uint8_t hash_sha224[] = {
0x45, 0x75, 0xbb, 0x4e, 0xc1, 0x29, 0xdf, 0x63, 0x80, 0xce, 0xdd, 0xe6, 0xd7,
Expand Down Expand Up @@ -63,7 +61,7 @@ static const uint8_t hash_sha512_256[] = {
0x72, 0x3a, 0x26, 0x71, 0x0e, 0x46, 0x76, 0x13, 0x01, 0xc8, 0xb5, 0x4c, 0x56,
0xfa, 0x72, 0x22, 0x67, 0x58, 0x1a};

static uint8_t msg_long[] = {
static const uint8_t msg_long[] = {
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Expand Down

0 comments on commit 0ff9fd5

Please sign in to comment.