mirror of https://github.com/ARMmbed/mbed-os.git
Check HASH is not busy before save/resteore context
parent
8ea9ca0af9
commit
a27498c44a
|
@ -27,26 +27,44 @@ static void mbedtls_zeroize( void *v, size_t n ) {
|
|||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
static void st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
|
||||
static int st_sha256_restore_hw_context(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t tickstart;
|
||||
/* allow multi-instance of HASH use: save context for HASH HW module CR */
|
||||
/* Check that there is no HASH activity on going */
|
||||
tickstart = HAL_GetTick();
|
||||
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
|
||||
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
|
||||
return 0; // timeout: HASH processor is busy
|
||||
}
|
||||
}
|
||||
HASH->STR = ctx->ctx_save_str;
|
||||
HASH->CR = (ctx->ctx_save_cr|HASH_CR_INIT);
|
||||
HASH->CR = (ctx->ctx_save_cr | HASH_CR_INIT);
|
||||
for (i=0;i<38;i++) {
|
||||
HASH->CSR[i] = ctx->ctx_save_csr[i];
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
|
||||
static int st_sha256_save_hw_context(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t tickstart;
|
||||
/* Check that there is no HASH activity on going */
|
||||
tickstart = HAL_GetTick();
|
||||
while ((HASH->SR & (HASH_FLAG_BUSY | HASH_FLAG_DMAS)) != 0) {
|
||||
if ((HAL_GetTick() - tickstart) > ST_SHA256_TIMEOUT) {
|
||||
return 0; // timeout: HASH processor is busy
|
||||
}
|
||||
}
|
||||
/* allow multi-instance of HASH use: restore context for HASH HW module CR */
|
||||
ctx->ctx_save_cr = HASH->CR;
|
||||
ctx->ctx_save_str = HASH->STR;
|
||||
for (i=0;i<38;i++) {
|
||||
ctx->ctx_save_csr[i] = HASH->CSR[i];
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
|
||||
|
@ -85,12 +103,16 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 )
|
|||
// error found to be returned
|
||||
return;
|
||||
}
|
||||
st_sha256_save_hw_context(ctx);
|
||||
if (st_sha256_save_hw_context(ctx) != 1) {
|
||||
return; // return HASH_BUSY timeout Error here
|
||||
}
|
||||
}
|
||||
|
||||
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[ST_SHA256_BLOCK_SIZE] )
|
||||
{
|
||||
st_sha256_restore_hw_context(ctx);
|
||||
if (st_sha256_restore_hw_context(ctx) != 1) {
|
||||
return; // Return HASH_BUSY timout error here
|
||||
}
|
||||
if (ctx->is224 == 0) {
|
||||
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, (uint8_t *) data, ST_SHA256_BLOCK_SIZE) != 0) {
|
||||
return; // Return error code
|
||||
|
@ -101,16 +123,20 @@ void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char da
|
|||
}
|
||||
}
|
||||
|
||||
st_sha256_save_hw_context(ctx);
|
||||
if (st_sha256_save_hw_context(ctx) != 1) {
|
||||
return; // return HASH_BUSY timeout Error here
|
||||
}
|
||||
}
|
||||
|
||||
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen )
|
||||
{
|
||||
size_t currentlen = ilen;
|
||||
st_sha256_restore_hw_context(ctx);
|
||||
if (st_sha256_restore_hw_context(ctx) != 1) {
|
||||
return; // Return HASH_BUSY timout error here
|
||||
}
|
||||
|
||||
// store mechanism to accumulate ST_SHA256_BLOCK_SIZE bytes (512 bits) in the HW
|
||||
if (currentlen == 0){ // only change HW status is size if 0
|
||||
if (currentlen == 0) { // only change HW status is size if 0
|
||||
if(ctx->hhash_sha256.Phase == HAL_HASH_PHASE_READY) {
|
||||
/* Select the SHA256 or SHA224 mode and reset the HASH processor core, so that the HASH will be ready to compute
|
||||
the message digest of a new message */
|
||||
|
@ -149,12 +175,16 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
|
|||
memcpy(ctx->sbuf, input + ilen - ctx->sbuf_len, ctx->sbuf_len);
|
||||
}
|
||||
}
|
||||
st_sha256_save_hw_context(ctx);
|
||||
if (st_sha256_save_hw_context(ctx) != 1) {
|
||||
return; // return HASH_BUSY timeout Error here
|
||||
}
|
||||
}
|
||||
|
||||
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] )
|
||||
{
|
||||
st_sha256_restore_hw_context(ctx);
|
||||
if (st_sha256_restore_hw_context(ctx) != 1) {
|
||||
return; // Return HASH_BUSY timout error here
|
||||
}
|
||||
if (ctx->sbuf_len > 0) {
|
||||
if (ctx->is224 == 0) {
|
||||
if (HAL_HASHEx_SHA256_Accumulate(&ctx->hhash_sha256, ctx->sbuf, ctx->sbuf_len) != 0) {
|
||||
|
@ -179,7 +209,9 @@ void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32
|
|||
return; // Return error code here
|
||||
}
|
||||
}
|
||||
st_sha256_save_hw_context(ctx);
|
||||
if (st_sha256_save_hw_context(ctx) != 1) {
|
||||
return; // return HASH_BUSY timeout Error here
|
||||
}
|
||||
}
|
||||
|
||||
#endif /*MBEDTLS_SHA256_ALT*/
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ST_SHA256_BLOCK_SIZE ((size_t)(64)) // HW handles 512 bits, ie 64 bytes
|
||||
#define ST_SHA256_BLOCK_SIZE ((size_t) 64) // HW handles 512 bits, ie 64 bytes
|
||||
#define ST_SHA256_TIMEOUT ((uint32_t) 3)
|
||||
/**
|
||||
* \brief SHA-256 context structure
|
||||
* \note HAL_HASH_SHA256_Accumulate will accumulate 512 bits packets, unless it is the last call to the function
|
||||
|
|
Loading…
Reference in New Issue