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
  • Loading branch information
lulu254b committed Feb 14, 2025
1 parent 8aa0859 commit 2d8942c
Show file tree
Hide file tree
Showing 3 changed files with 41 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
42 changes: 36 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,
psa_status_t cryptocell_310_common_hash_update_continue(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 @@ -69,6 +64,41 @@ psa_status_t cryptocell_310_common_hash_update(CRYS_HASHUserContext_t *ctx,

offset += size;
} while ((input_length > 0) && (ret == CRYS_OK));

Check failure on line 67 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

trailing whitespace.
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)) {
DEBUG("%s : cryptocell_310 data no longer required to be in RAM.\n", RIOT_FILE_RELATIVE);

uint8_t input_buf[PSA_HASH_MAX_SIZE];
size_t buf_size;
size_t offset = 0;

Check failure on line 82 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

trailing whitespace.
do {

Check failure on line 84 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

trailing whitespace.

Check warning on line 84 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

trailing whitespace
memset(&input_buf, 0, PSA_HASH_MAX_SIZE);

Check failure on line 86 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

trailing whitespace.
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 = cryptocell_310_common_hash_update_continue(ctx, input_buf, buf_size);

offset += PSA_HASH_MAX_SIZE;

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

Check warning on line 96 in pkg/driver_cryptocell_310/psa_cryptocell_310/hashes_common.c

View workflow job for this annotation

GitHub Actions / static-tests

keyword 'while' not followed by a single space

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

if (ret != CRYS_OK) {
DEBUG("CRYS_HASH_Update failed with %s\n", cryptocell310_status_to_humanly_readable(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 2d8942c

Please sign in to comment.