Updated the code fix of timer and Mbed TLS.

pull/12106/head
Andrew Chong 2020-01-07 17:55:56 +09:00
parent a26e0d1358
commit 65e9cac918
7 changed files with 76 additions and 48 deletions

View File

@ -18,6 +18,10 @@
"FVP_MPS2": {
"base-address": "0x00200000",
"size": "0x200000"
},
"S5JS100": {
"base-address": "0x40EF5000",
"size": "0x80000"
}
}
}

View File

@ -140,10 +140,10 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
if (ctx->is224) {
mbedtls_sha256_sw_update_ret(ctx, input, ilen);
} else {
if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
if (ilen > MAX_MB_HASH_BLOCK_BLEN || (ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
// H/W SHA has limitation to seperated API with oversized message.
// fall back to S/W SHA-256
if (ctx->totals == 0) {
if (ctx->totals == 0 || ctx->hw == 1) {
ctx->total[0] = 0;
ctx->total[1] = 0;
/* SHA-256 */
@ -157,11 +157,17 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *
ctx->state[7] = 0x5BE0CD19;
}
ctx->totals += ilen;
//in case, H/W -> S/W fallback case
if ((ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN && ctx->hw == 1) {
mbedtls_sha512_sw_update_ret(ctx, ctx->sbuf, ctx->pstMessage.u32DataByteLen);
}
ctx->hw = 0;
mbedtls_sha256_sw_update_ret(ctx, input, ilen);
} else {
} else { //less than MAX_MB_HASH_BLOCK_BLEN size will handle with H/W
// SHA-256 handle by SSS H/W
memcpy(ctx->sbuf + ctx->pstMessage.u32DataByteLen, input, ilen);
ctx->pstMessage.u32DataByteLen += ilen;
ctx->totals += ilen; //in case the block size increased incrementally. (3, 20, 256..)
}
}

View File

@ -61,6 +61,7 @@ typedef struct mbedtls_sha256_context_s {
/* for H/W SHA-256 */
uint32_t totals;
uint32_t hw;
unsigned char sbuf[ST_SHA256_BUF_SIZE]; /*!< ST_SHA256_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */
stOCTET_STRING pstMessage;
stOCTET_STRING pstDigest;
@ -143,7 +144,7 @@ int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned
* <li>1: Use SHA-224.</li></ul>
*/
MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
int is224);
int is224);
/**
* \brief This function feeds an input buffer into an ongoing
@ -156,8 +157,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
* \param ilen The length of the input data.
*/
MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
const unsigned char *input,
size_t ilen);
const unsigned char *input,
size_t ilen);
/**
* \brief This function finishes the SHA-256 operation, and writes
@ -169,7 +170,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
* \param output The SHA-224or SHA-256 checksum result.
*/
MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
unsigned char output[32]);
unsigned char output[32]);
/**
* \brief This function processes a single data block within
@ -182,7 +183,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
* \param data The buffer holding one block of data.
*/
MBEDTLS_DEPRECATED void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
const unsigned char data[64]);
const unsigned char data[64]);
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */

View File

@ -146,10 +146,10 @@ int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
*/
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
{
if (ilen > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
if (ilen > MAX_MB_HASH_BLOCK_BLEN || (ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN || ctx->totals > MAX_MB_HASH_BLOCK_BLEN) {
// H/W SHA has limitation to seperated API with oversized message.
// fall back to S/W SHA-512
if (ctx->totals == 0) {
// fallback to S/W from H/W pre-tested
if (ctx->totals == 0 || ctx->hw == 1) {
ctx->total[0] = 0;
ctx->total[1] = 0;
if (ctx->is384) {
@ -175,11 +175,19 @@ int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *
}
}
ctx->totals += ilen;
//in case, H/W -> S/W fallback case
if ((ctx->totals + ilen) > MAX_MB_HASH_BLOCK_BLEN && ctx->hw == 1) {
//214 + 2577
mbedtls_sha512_sw_update_ret(ctx, ctx->sbuf, ctx->pstMessage.u32DataByteLen);
}
ctx->hw = 0;
mbedtls_sha512_sw_update_ret(ctx, input, ilen);
} else {
// SHA-256 handle by SSS H/W
memcpy(ctx->sbuf + ctx->pstMessage.u32DataByteLen, input, ilen);
ctx->pstMessage.u32DataByteLen += ilen;
ctx->totals += ilen;
ctx->hw = 1;
}
return 0;

View File

@ -59,6 +59,7 @@ typedef struct mbedtls_sha512_context_s {
0: Use SHA-512, or 1: Use SHA-384. */
/* for H/W SHA-512 */
uint32_t totals;
uint32_t hw;
unsigned char sbuf[ST_SHA512_BUF_SIZE]; /*!< ST_SHA512_BLOCK_SIZE buffer to store values so that algorithm is called once the buffer is filled */
stOCTET_STRING pstMessage;
stOCTET_STRING pstDigest;
@ -141,7 +142,7 @@ int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx, const unsigned
* <li>1: Use SHA-224.</li></ul>
*/
MBEDTLS_DEPRECATED void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
int is224);
int is224);
/**
* \brief This function feeds an input buffer into an ongoing
@ -154,8 +155,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
* \param ilen The length of the input data.
*/
MBEDTLS_DEPRECATED void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen);
const unsigned char *input,
size_t ilen);
/**
* \brief This function finishes the SHA-512 operation, and writes
@ -167,7 +168,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
* \param output The SHA-224or SHA-512 checksum result.
*/
MBEDTLS_DEPRECATED void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
unsigned char output[64]);
unsigned char output[64]);
/**
* \brief This function processes a single data block within
@ -180,7 +181,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
* \param data The buffer holding one block of data.
*/
MBEDTLS_DEPRECATED void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
const unsigned char data[128]);
const unsigned char data[128]);
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */

View File

@ -1,3 +1,27 @@
/*
* Copyright (c) 2019 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* @file : cmsis_nvic_virtual.h
* @brief : NVIC functions list
* @date : December 2019
* @note : List of NVIC macro and customize TFM interrupt
*/
#include "cmsis.h"
#ifndef NVIC_VIRTUAL_H

View File

@ -27,9 +27,8 @@
#define TIMER_TARGET_COUNT_DEFAULT 0xFFFFFFFF
volatile uint32_t us_ticker_initialized = 0;
volatile uint32_t us_user_intset;
volatile uint32_t g_us_last_return = 0;
uint32_t us_ticker_initialized = 0;
uint32_t g_us_last_return = 0;
void us_ticker_enable_interrupt(void);
void us_ticker_disable_interrupt(void);
@ -37,7 +36,7 @@ void us_ticker_disable_interrupt(void);
const ticker_info_t *us_ticker_get_info()
{
static const ticker_info_t info = {
1000000, //1Mhz
26000000, //26Mhz
32 //32bit counter
};
return &info;
@ -45,11 +44,11 @@ const ticker_info_t *us_ticker_get_info()
static void enable_timer0(void)
{
putreg32(1, S5JS100_TIMER0_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
putreg32(0x0, S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(0x1, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_ENABLE);
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_SEL);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_SEL);
putreg32(0, S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_ENABLE);
putreg32(3, S5JS100_TIMER0_BASE + S5JS100_TIMER_CONTROL);
}
@ -67,9 +66,9 @@ static void disable_timer0(void)
static void enable_timer1(void)
{
putreg32(1, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
putreg32(0x0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL); // Set Up count
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_ENABLE);
putreg32(3, S5JS100_TIMER1_BASE + S5JS100_TIMER_CONTROL);
}
@ -77,8 +76,8 @@ static void enable_timer1(void)
static void disable_timer1(void)
{
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_UP_DOWN_SEL);
putreg32(0xFFFFFFFF, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_VALUE);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_CONTROL);
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_ENABLE);
}
@ -118,31 +117,16 @@ uint32_t us_ticker_read()
if (!us_ticker_initialized) {
us_ticker_init();
}
volatile uint32_t current_count = getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE) / 26;
uint32_t current_count = TIMER_TARGET_COUNT_DEFAULT - getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE);
g_us_last_return = current_count;
return current_count;
}
void us_ticker_set_interrupt(timestamp_t timestamp)
{
if (timestamp < 0x70000000 && timestamp != 0) {
uint32_t temp = g_us_last_return;
us_user_intset = timestamp - g_us_last_return;
/* keep to check
if (us_user_intset < 0) {
us_ticker_irq_handler();
return;
}*/
us_user_intset = us_user_intset * 26;
uint32_t past_tick = us_ticker_read() - temp;
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
putreg32(us_user_intset - past_tick * 26, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
us_ticker_enable_interrupt();
} else {
putreg32(0, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
putreg32(TIMER_TARGET_COUNT_DEFAULT, S5JS100_TIMER1_BASE + S5JS100_TIMER_INT_SEL);
us_ticker_enable_interrupt();
}
uint32_t past_tick = TIMER_TARGET_COUNT_DEFAULT - getreg32(S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE) - g_us_last_return;
putreg32(timestamp - g_us_last_return - past_tick, S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE);
us_ticker_enable_interrupt();
}
void us_ticker_fire_interrupt(void)