Microchip® Advanced Software Framework

sal.c File Reference

Low-level crypto API for an AES unit implemented in AT86RFA1.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

#include "sal_types.h"
#include "sal.h"
#include <string.h>

Macros

#define _SR_MASK(addr, mask, pos, val)   (((uint8_t)val << pos) & mask)
 
#define _trx_bit_read(addr, mask, pos)   (((*(volatile uint8_t *)(addr)) & (mask)) >> (pos))
 
#define AES_COMPLETED   (1)
 Constant AES_COMPLETED for sub-register SR_AES_DONE in register AES_STATUS. More...
 
#define AES_DIR_DECRYPT   (1)
 Constant AES_DIR_DECRYPT for sub-register SR_AES_DIR in register AES_CTRL. More...
 
#define AES_DIR_ENCRYPT   (0)
 Constant AES_DIR_ENCRYPT for sub-register SR_AES_DIR in register AES_CTRL. More...
 
#define AES_DIR_VOID   (AES_DIR_ENCRYPT + AES_DIR_DECRYPT + 1)
 
#define AES_NOT_COMPLETED   (0)
 Constant AES_NOT_COMPLETED for sub-register SR_AES_DONE in register AES_STATUS. More...
 
#define AES_START   (1)
 Constant AES_START for sub-register SR_AES_REQUEST in register AES_CTRL. More...
 
#define AES_START   (1)
 Constant AES_START for sub-register SR_AES_REQUEST in register AES_CTRL. More...
 
#define RG_AES_CTRL   (0x13C)
 Offset for register AES_CTRL. More...
 
#define RG_AES_KEY   (0x13F)
 Offset for register AES_KEY. More...
 
#define RG_AES_STATE   (0x13E)
 Offset for register AES_STATE. More...
 
#define RG_AES_STATUS   (0x13D)
 Offset for register AES_STATUS. More...
 
#define RG_TRX_STATUS   (0x141)
 Offset for register TRX_STATUS. More...
 
#define SR_AES_DIR   0x13C, 0x08, 3
 Access parameters for sub-register AES_DIR in register RG_AES_CTRL. More...
 
#define SR_AES_DONE   0x13D, 0x01, 0
 Sub-registers of Register RG_AES_STATUS. More...
 
#define SR_AES_ER   0x13D, 0x80, 7
 Access parameters for sub-register AES_ER in register RG_AES_STATUS. More...
 
#define SR_AES_IM   0x13C, 0x04, 2
 Sub-registers of Register RG_AES_CTRL. More...
 
#define SR_AES_MODE   0x13C, 0x20, 5
 Access parameters for sub-register AES_MODE in register RG_AES_CTRL. More...
 
#define SR_AES_REQUEST   0x13C, 0x80, 7
 Access parameters for sub-register AES_REQUEST in register RG_AES_CTRL. More...
 
#define SR_MASK(SR, val)   _SR_MASK(SR, val)
 
#define trx_bit_read(arg)   _trx_bit_read(arg)
 Subregister read. More...
 
#define trx_reg_read(addr)   (*(volatile uint8_t *)(addr))
 Reads current value from a transceiver register. More...
 
#define trx_reg_write(addr, data)   (*(volatile uint8_t *)(addr)) = (data)
 Writes data into a transceiver register. More...
 

Functions

void _sal_aes_clean_up (void)
 Cleans up the SAL/AES after STB has been finished. More...
 
void sal_aes_exec (uint8_t *data)
 En/decrypt one AES block. More...
 
void sal_aes_read (uint8_t *data)
 Reads the result of previous AES en/decryption. More...
 
void sal_aes_restart (void)
 Re-inits key and state after a sleep or TRX reset. More...
 
bool sal_aes_setup (uint8_t *key, uint8_t enc_mode, uint8_t dir)
 Setup AES unit. More...
 
void sal_init (void)
 Initialization of SAL. More...
 

Variables

static bool dec_initialized = false
 
static uint8_t dec_key [AES_KEYSIZE]
 
static uint8_t enc_key [AES_KEYSIZE]
 
static uint8_t last_dir = AES_DIR_VOID
 
static uint8_t mode_byte
 

#define _SR_MASK (   addr,
  mask,
  pos,
  val 
)    (((uint8_t)val << pos) & mask)
#define _trx_bit_read (   addr,
  mask,
  pos 
)    (((*(volatile uint8_t *)(addr)) & (mask)) >> (pos))
#define AES_COMPLETED   (1)

Constant AES_COMPLETED for sub-register SR_AES_DONE in register AES_STATUS.

#define AES_DIR_DECRYPT   (1)

Constant AES_DIR_DECRYPT for sub-register SR_AES_DIR in register AES_CTRL.

Referenced by sal_aes_setup().

#define AES_DIR_ENCRYPT   (0)

Constant AES_DIR_ENCRYPT for sub-register SR_AES_DIR in register AES_CTRL.

Referenced by sal_aes_restart(), and sal_aes_setup().

#define AES_DIR_VOID   (AES_DIR_ENCRYPT + AES_DIR_DECRYPT + 1)
Author
Microchip Technology Inc: http://www.microchip.com Support: https://www.microchip.com/support/

Referenced by sal_aes_setup().

#define AES_NOT_COMPLETED   (0)

Constant AES_NOT_COMPLETED for sub-register SR_AES_DONE in register AES_STATUS.

#define AES_START   (1)

Constant AES_START for sub-register SR_AES_REQUEST in register AES_CTRL.

#define AES_START   (1)

Constant AES_START for sub-register SR_AES_REQUEST in register AES_CTRL.

#define RG_AES_CTRL   (0x13C)

Offset for register AES_CTRL.

Referenced by sal_aes_exec(), sal_aes_restart(), and sal_aes_setup().

#define RG_AES_KEY   (0x13F)

Offset for register AES_KEY.

Referenced by sal_aes_restart(), and sal_aes_setup().

#define RG_AES_STATE   (0x13E)

Offset for register AES_STATE.

Referenced by sal_aes_exec(), and sal_aes_read().

#define RG_AES_STATUS   (0x13D)

Offset for register AES_STATUS.

#define RG_TRX_STATUS   (0x141)

Offset for register TRX_STATUS.

#define SR_AES_DIR   0x13C, 0x08, 3

Access parameters for sub-register AES_DIR in register RG_AES_CTRL.

Referenced by sal_aes_setup().

#define SR_AES_DONE   0x13D, 0x01, 0

Sub-registers of Register RG_AES_STATUS.

Access parameters for sub-register AES_DONE in register RG_AES_STATUS

Referenced by sal_aes_exec().

#define SR_AES_ER   0x13D, 0x80, 7

Access parameters for sub-register AES_ER in register RG_AES_STATUS.

#define SR_AES_IM   0x13C, 0x04, 2

Sub-registers of Register RG_AES_CTRL.

Access parameters for sub-register AES_IM in register RG_AES_CTRL

#define SR_AES_MODE   0x13C, 0x20, 5

Access parameters for sub-register AES_MODE in register RG_AES_CTRL.

Referenced by sal_aes_setup().

#define SR_AES_REQUEST   0x13C, 0x80, 7

Access parameters for sub-register AES_REQUEST in register RG_AES_CTRL.

Referenced by sal_aes_exec().

#define SR_MASK (   SR,
  val 
)    _SR_MASK(SR, val)

Referenced by sal_aes_exec(), and sal_aes_setup().

#define trx_bit_read (   arg)    _trx_bit_read(arg)

Subregister read.

Parameters
argSubregister
Returns
Value of the read subregister

Referenced by sal_aes_exec().

#define trx_reg_read (   addr)    (*(volatile uint8_t *)(addr))

Reads current value from a transceiver register.

This macro reads the current value from a transceiver register.

Parameters
addrSpecifies the address of the trx register from which the data shall be read
Returns
value of the register read

Referenced by sal_aes_read(), and sal_aes_setup().

#define trx_reg_write (   addr,
  data 
)    (*(volatile uint8_t *)(addr)) = (data)

Writes data into a transceiver register.

This macro writes a value into transceiver register.

Parameters
addrAddress of the trx register
dataData to be written to trx register

Referenced by sal_aes_exec(), sal_aes_restart(), and sal_aes_setup().

void _sal_aes_clean_up ( void  )

Cleans up the SAL/AES after STB has been finished.

Cleans up the SAL/AES after STB has been completed.

void sal_aes_exec ( uint8_t *  data)

En/decrypt one AES block.

The function returns after the AES operation is finished.

Parameters
[in]dataAES block to be en/decrypted

References AES_BLOCKSIZE, AES_START, mode_byte, RG_AES_CTRL, RG_AES_STATE, SR_AES_DONE, SR_AES_REQUEST, SR_MASK, trx_bit_read, and trx_reg_write.

Referenced by compute_mic(), encrypt_with_padding(), PHY_EncryptReq(), and sal_aes_setup().

void sal_aes_read ( uint8_t *  data)

Reads the result of previous AES en/decryption.

This function returns the result of the previous AES operation, so this function is needed in order to get the last result of a series of sal_aes_wrrd() calls.

Parameters
[out]data- result of previous operation

References AES_BLOCKSIZE, RG_AES_STATE, and trx_reg_read.

Referenced by compute_mic(), encrypt_pldmic(), and PHY_EncryptReq().

void sal_aes_restart ( void  )

Re-inits key and state after a sleep or TRX reset.

This function re-initializes the AES key and the state of the AES engine after TRX sleep or reset. The contents of AES register AES_CON is restored, the next AES operation started with sal_aes_exec() will be executed correctly.

References AES_BLOCKSIZE, AES_DIR_ENCRYPT, dec_key, enc_key, last_dir, mode_byte, RG_AES_CTRL, RG_AES_KEY, and trx_reg_write.

Referenced by stb_ccm_secure().

bool sal_aes_setup ( uint8_t *  key,
uint8_t  enc_mode,
uint8_t  dir 
)

Setup AES unit.

This function perform the following tasks as part of the setup of the AES unit: key initialization, set encryption direction and encryption mode.

In general, the contents of SRAM buffer is destroyed. When using sal_aes_wrrd(), sal_aes_read() needs to be called in order to get the result of the last AES operation before you may call sal_aes_setup() again.

Parameters
[in]keyAES key or NULL (NULL: use last key)
[in]enc_modeAES_MODE_ECB or AES_MODE_CBC
[in]dirAES_DIR_ENCRYPT or AES_DIR_DECRYPT
Returns
False if some parameter was illegal, true else

References AES_BLOCKSIZE, AES_DIR_DECRYPT, AES_DIR_ENCRYPT, AES_DIR_VOID, AES_KEYSIZE, AES_MODE_CBC, AES_MODE_ECB, dec_initialized, dec_key, enc_key, last_dir, mode_byte, NULL, RG_AES_CTRL, RG_AES_KEY, sal_aes_exec(), SR_AES_DIR, SR_AES_MODE, SR_MASK, trx_reg_read, and trx_reg_write.

Referenced by compute_mic(), encrypt_pldmic(), PHY_EncryptReq(), and stb_ccm_secure().

void sal_init ( void  )

Initialization of SAL.

This functions initializes the SAL.

Referenced by stb_init(), and SYS_Init().

bool dec_initialized = false
static

Referenced by sal_aes_setup().

uint8_t dec_key[AES_KEYSIZE]
static

Referenced by sal_aes_restart(), and sal_aes_setup().

uint8_t enc_key[AES_KEYSIZE]
static

Referenced by sal_aes_restart(), and sal_aes_setup().

uint8_t last_dir = AES_DIR_VOID
static

Referenced by sal_aes_restart(), and sal_aes_setup().

uint8_t mode_byte
static