mirror of https://github.com/ARMmbed/mbed-os.git
[NUC472/M487] Strengthen crypto DMA buffer check
1. Catch incompatible buffer range, where buffer base = 0xffffff00 and buffer size = 0x100. 2. Add buffer size alignment check.pull/4925/head
parent
ac000244f4
commit
a0a8a955a9
|
@ -134,12 +134,13 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
|
|||
|
||||
MBED_ASSERT((dataSize % 16 == 0) && (dataSize <= MAX_DMA_CHAIN_SIZE));
|
||||
|
||||
/* AES DMA buffer requires to be:
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x2xxxxxxx region
|
||||
/* AES DMA buffer has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) 16-byte aligned buffer size
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
if ((! crypto_dma_buff_compat(au8OutputData, MAX_DMA_CHAIN_SIZE)) ||
|
||||
(! crypto_dma_buff_compat(au8InputData, MAX_DMA_CHAIN_SIZE))) {
|
||||
if ((! crypto_dma_buff_compat(au8OutputData, MAX_DMA_CHAIN_SIZE, 16)) ||
|
||||
(! crypto_dma_buff_compat(au8InputData, MAX_DMA_CHAIN_SIZE, 16))) {
|
||||
error("Buffer for AES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
|
||||
}
|
||||
|
||||
|
@ -160,14 +161,14 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
|
|||
AES_SetInitVect(0, ctx->iv);
|
||||
AES_SetKey(0, ctx->keys, ctx->keySize);
|
||||
/* AES DMA buffer requirements same as above */
|
||||
if (! crypto_dma_buff_compat(input, dataSize)) {
|
||||
if (! crypto_dma_buff_compat(input, dataSize, 16)) {
|
||||
memcpy(au8InputData, input, dataSize);
|
||||
pIn = au8InputData;
|
||||
} else {
|
||||
pIn = input;
|
||||
}
|
||||
/* AES DMA buffer requirements same as above */
|
||||
if (! crypto_dma_buff_compat(output, dataSize)) {
|
||||
if (! crypto_dma_buff_compat(output, dataSize, 16)) {
|
||||
pOut = au8OutputData;
|
||||
} else {
|
||||
pOut = output;
|
||||
|
|
|
@ -315,12 +315,13 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
|
|||
return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
|
||||
}
|
||||
|
||||
/* DES DMA buffer requires to be:
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x2xxxxxxx region
|
||||
/* DES DMA buffer has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) 8-byte aligned buffer size
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
if ((! crypto_dma_buff_compat(dmabuf_in, MAXSIZE_DMABUF)) ||
|
||||
(! crypto_dma_buff_compat(dmabuf_out, MAXSIZE_DMABUF))) {
|
||||
if ((! crypto_dma_buff_compat(dmabuf_in, MAXSIZE_DMABUF, 8)) ||
|
||||
(! crypto_dma_buff_compat(dmabuf_out, MAXSIZE_DMABUF, 8))) {
|
||||
error("Buffer for DES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
|
||||
}
|
||||
|
||||
|
|
|
@ -134,12 +134,13 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
|
|||
|
||||
MBED_ASSERT((dataSize % 16 == 0) && (dataSize <= MAX_DMA_CHAIN_SIZE));
|
||||
|
||||
/* AES DMA buffer requires to be:
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x2xxxxxxx region
|
||||
/* AES DMA buffer has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) 16-byte aligned buffer size
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
if ((! crypto_dma_buff_compat(au8OutputData, MAX_DMA_CHAIN_SIZE)) ||
|
||||
(! crypto_dma_buff_compat(au8InputData, MAX_DMA_CHAIN_SIZE))) {
|
||||
if ((! crypto_dma_buff_compat(au8OutputData, MAX_DMA_CHAIN_SIZE, 16)) ||
|
||||
(! crypto_dma_buff_compat(au8InputData, MAX_DMA_CHAIN_SIZE, 16))) {
|
||||
error("Buffer for AES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
|
||||
}
|
||||
|
||||
|
@ -160,14 +161,14 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
|
|||
AES_SetInitVect(0, ctx->iv);
|
||||
AES_SetKey(0, ctx->keys, ctx->keySize);
|
||||
/* AES DMA buffer requirements same as above */
|
||||
if (! crypto_dma_buff_compat(input, dataSize)) {
|
||||
if (! crypto_dma_buff_compat(input, dataSize, 16)) {
|
||||
memcpy(au8InputData, input, dataSize);
|
||||
pIn = au8InputData;
|
||||
} else {
|
||||
pIn = input;
|
||||
}
|
||||
/* AES DMA buffer requirements same as above */
|
||||
if (! crypto_dma_buff_compat(output, dataSize)) {
|
||||
if (! crypto_dma_buff_compat(output, dataSize, 16)) {
|
||||
pOut = au8OutputData;
|
||||
} else {
|
||||
pOut = output;
|
||||
|
|
|
@ -315,12 +315,13 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
|
|||
return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
|
||||
}
|
||||
|
||||
/* DES DMA buffer requires to be:
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x2xxxxxxx region
|
||||
/* DES DMA buffer has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) 8-byte aligned buffer size
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
if ((! crypto_dma_buff_compat(dmabuf_in, MAXSIZE_DMABUF)) ||
|
||||
(! crypto_dma_buff_compat(dmabuf_out, MAXSIZE_DMABUF))) {
|
||||
if ((! crypto_dma_buff_compat(dmabuf_in, MAXSIZE_DMABUF, 8)) ||
|
||||
(! crypto_dma_buff_compat(dmabuf_out, MAXSIZE_DMABUF, 8))) {
|
||||
error("Buffer for DES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
|
||||
}
|
||||
|
||||
|
|
|
@ -125,13 +125,13 @@ void crypto_sha_release(void)
|
|||
crypto_submodule_release(&crypto_sha_avail);
|
||||
}
|
||||
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size)
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to)
|
||||
{
|
||||
uint32_t buff_ = (uint32_t) buff;
|
||||
|
||||
return (((buff_ & 0x03) == 0) && /* Word-aligned */
|
||||
(buff_ >= 0x20000000) && /* 0x20000000-0x2FFFFFFF */
|
||||
((buff_ + buff_size) <= 0x30000000));
|
||||
|
||||
return (((buff_ & 0x03) == 0) && /* Word-aligned buffer base address */
|
||||
((buff_size & (size_aligned_to - 1)) == 0) && /* Crypto submodule dependent buffer size alignment */
|
||||
(((buff_ >> 28) == 0x2) && (buff_size <= (0x30000000 - buff_)))); /* 0x20000000-0x2FFFFFFF */
|
||||
}
|
||||
|
||||
static bool crypto_submodule_acquire(uint16_t *submodule_avail)
|
||||
|
|
|
@ -52,12 +52,12 @@ void crypto_des_release(void);
|
|||
bool crypto_sha_acquire(void);
|
||||
void crypto_sha_release(void);
|
||||
|
||||
/* Check if buffer can be used for crypto DMA. It requires to be:
|
||||
*
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x20000000-0x2FFFFFFF region
|
||||
/* Check if buffer can be used for crypto DMA. It has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) Crypto submodule (AES, DES, SHA, etc.) dependent buffer size alignment. Must be 2 power.
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size);
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -125,13 +125,13 @@ void crypto_sha_release(void)
|
|||
crypto_submodule_release(&crypto_sha_avail);
|
||||
}
|
||||
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size)
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to)
|
||||
{
|
||||
uint32_t buff_ = (uint32_t) buff;
|
||||
|
||||
return (((buff_ & 0x03) == 0) && /* Word-aligned */
|
||||
(buff_ >= 0x20000000) && /* 0x20000000-0x2FFFFFFF */
|
||||
((buff_ + buff_size) <= 0x30000000));
|
||||
|
||||
return (((buff_ & 0x03) == 0) && /* Word-aligned buffer base address */
|
||||
((buff_size & (size_aligned_to - 1)) == 0) && /* Crypto submodule dependent buffer size alignment */
|
||||
(((buff_ >> 28) == 0x2) && (buff_size <= (0x30000000 - buff_)))); /* 0x20000000-0x2FFFFFFF */
|
||||
}
|
||||
|
||||
static bool crypto_submodule_acquire(uint16_t *submodule_avail)
|
||||
|
|
|
@ -52,12 +52,12 @@ void crypto_des_release(void);
|
|||
bool crypto_sha_acquire(void);
|
||||
void crypto_sha_release(void);
|
||||
|
||||
/* Check if buffer can be used for crypto DMA. It requires to be:
|
||||
*
|
||||
* 1) Word-aligned
|
||||
* 2) Located in 0x20000000-0x2FFFFFFF region
|
||||
/* Check if buffer can be used for crypto DMA. It has the following requirements:
|
||||
* (1) Word-aligned buffer base address
|
||||
* (2) Crypto submodule (AES, DES, SHA, etc.) dependent buffer size alignment. Must be 2 power.
|
||||
* (3) Located in 0x20000000-0x2FFFFFFF region
|
||||
*/
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size);
|
||||
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue