Skip to content
Permalink
Browse files Browse the repository at this point in the history
driver: secure: move keys into static arrays
Move the keys into static arrays. This will avoid copying the keys
from the code section to the stack on the previous init_keys implementation.
Like this, the keys are hardcoded into data section at compile time,
and can be completely wiped after use.

Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
Tested-by: Nicolas Ferre <nicolas.ferre@microchip.com>
  • Loading branch information
ehristev committed Mar 30, 2020
1 parent 7753914 commit 4541949
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
101 changes: 53 additions & 48 deletions driver/secure.c
Expand Up @@ -32,63 +32,60 @@
#include "string.h"
#include "autoconf.h"


static inline void init_keys(at91_aes_key_size_t *key_size,
unsigned int *cipher_key,
unsigned int *cmac_key,
unsigned int *iv)
{
#if defined(CONFIG_AES_KEY_SIZE_128)
*key_size = AT91_AES_KEY_SIZE_128;
#elif defined(CONFIG_AES_KEY_SIZE_192)
*key_size = AT91_AES_KEY_SIZE_192;
#elif defined(CONFIG_AES_KEY_SIZE_256)
*key_size = AT91_AES_KEY_SIZE_256;
#else
#error "bad AES key size"
static unsigned int cipher_key[8] = {
CONFIG_AES_CIPHER_KEY_WORD0,
CONFIG_AES_CIPHER_KEY_WORD1,
CONFIG_AES_CIPHER_KEY_WORD2,
CONFIG_AES_CIPHER_KEY_WORD3,
#if defined(CONFIG_AES_KEY_SIZE_192) || defined(CONFIG_AES_KEY_SIZE_256)
CONFIG_AES_CIPHER_KEY_WORD4,
CONFIG_AES_CIPHER_KEY_WORD5,
#endif
#if defined(CONFIG_AES_KEY_SIZE_256)
CONFIG_AES_CIPHER_KEY_WORD6,
CONFIG_AES_CIPHER_KEY_WORD7,
#endif
};

iv[0] = CONFIG_AES_IV_WORD0;
iv[1] = CONFIG_AES_IV_WORD1;
iv[2] = CONFIG_AES_IV_WORD2;
iv[3] = CONFIG_AES_IV_WORD3;

cipher_key[0] = CONFIG_AES_CIPHER_KEY_WORD0;
cmac_key[0] = CONFIG_AES_CMAC_KEY_WORD0;
cipher_key[1] = CONFIG_AES_CIPHER_KEY_WORD1;
cmac_key[1] = CONFIG_AES_CMAC_KEY_WORD1;
cipher_key[2] = CONFIG_AES_CIPHER_KEY_WORD2;
cmac_key[2] = CONFIG_AES_CMAC_KEY_WORD2;
cipher_key[3] = CONFIG_AES_CIPHER_KEY_WORD3;
cmac_key[3] = CONFIG_AES_CMAC_KEY_WORD3;

static unsigned int cmac_key[8] = {
CONFIG_AES_CMAC_KEY_WORD0,
CONFIG_AES_CMAC_KEY_WORD1,
CONFIG_AES_CMAC_KEY_WORD2,
CONFIG_AES_CMAC_KEY_WORD3,
#if defined(CONFIG_AES_KEY_SIZE_192) || defined(CONFIG_AES_KEY_SIZE_256)
cipher_key[4] = CONFIG_AES_CIPHER_KEY_WORD4;
cmac_key[4] = CONFIG_AES_CMAC_KEY_WORD4;
cipher_key[5] = CONFIG_AES_CIPHER_KEY_WORD5;
cmac_key[5] = CONFIG_AES_CMAC_KEY_WORD5;
CONFIG_AES_CMAC_KEY_WORD4,
CONFIG_AES_CMAC_KEY_WORD5,
#endif

#if defined(CONFIG_AES_KEY_SIZE_256)
cipher_key[6] = CONFIG_AES_CIPHER_KEY_WORD6;
cmac_key[6] = CONFIG_AES_CMAC_KEY_WORD6;
cipher_key[7] = CONFIG_AES_CIPHER_KEY_WORD7;
cmac_key[7] = CONFIG_AES_CMAC_KEY_WORD7;
CONFIG_AES_CMAC_KEY_WORD6,
CONFIG_AES_CMAC_KEY_WORD7,
#endif
}
};

int secure_decrypt(void *data, unsigned int data_length, int is_signed)
static unsigned int iv[AT91_AES_IV_SIZE_WORD] = {
CONFIG_AES_IV_WORD0,
CONFIG_AES_IV_WORD1,
CONFIG_AES_IV_WORD2,
CONFIG_AES_IV_WORD3,
};

static int secure_decrypt(void *data, unsigned int data_length, int is_signed)
{
at91_aes_key_size_t key_size;
unsigned int cmac_key[8], cipher_key[8];
unsigned int iv[AT91_AES_IV_SIZE_WORD];
unsigned int computed_cmac[AT91_AES_BLOCK_SIZE_WORD];
unsigned int fixed_length;
const unsigned int *cmac;
int rc = -1;

/* Init keys */
init_keys(&key_size, cipher_key, cmac_key, iv);
#if defined(CONFIG_AES_KEY_SIZE_128)
key_size = AT91_AES_KEY_SIZE_128;
#elif defined(CONFIG_AES_KEY_SIZE_192)
key_size = AT91_AES_KEY_SIZE_192;
#elif defined(CONFIG_AES_KEY_SIZE_256)
key_size = AT91_AES_KEY_SIZE_256;
#else
#error "bad AES key size"
#endif

/* Init periph */
at91_aes_init();
Expand Down Expand Up @@ -117,26 +114,34 @@ int secure_decrypt(void *data, unsigned int data_length, int is_signed)
/* Reset periph */
at91_aes_cleanup();

return rc;
}

static void wipe_keys()
{
/* Reset keys */
memset(cmac_key, 0, sizeof(cmac_key));
memset(cipher_key, 0, sizeof(cipher_key));
memset(iv, 0, sizeof(iv));

return rc;
}

int secure_check(void *data)
{
const at91_secure_header_t *header;
void *file;
int ret = -1;

if (secure_decrypt(data, sizeof(*header), 0))
return -1;
goto secure_wipe_keys;

header = (const at91_secure_header_t *)data;
if (header->magic != AT91_SECURE_MAGIC)
return -1;
goto secure_wipe_keys;

file = (unsigned char *)data + sizeof(*header);
return secure_decrypt(file, header->file_size, 1);
ret = secure_decrypt(file, header->file_size, 1);

secure_wipe_keys:
wipe_keys();
return ret;
}
2 changes: 0 additions & 2 deletions include/secure.h
Expand Up @@ -40,8 +40,6 @@ typedef struct at91_secure_header {
unsigned int reserved[2];
} at91_secure_header_t;


int secure_decrypt(void *data, unsigned int data_length, int is_signed);
int secure_check(void *data);

#endif /* #ifdef __SECURE_H__ */

0 comments on commit 4541949

Please sign in to comment.