Commit e8a268cd authored by Benoît Thébaudeau's avatar Benoît Thébaudeau
Browse files

cc2538: aes: Add support for 192- and 256-bit keys

parent 801315e8
Loading
Loading
Loading
Loading
+23 −8
Original line number Diff line number Diff line
@@ -48,18 +48,35 @@
#include <stdint.h>
/*---------------------------------------------------------------------------*/
uint8_t
aes_load_keys(const void *keys, uint8_t count, uint8_t start_area)
aes_load_keys(const void *keys, uint8_t key_size, uint8_t count,
              uint8_t start_area)
{
  uint32_t aes_key_store_size;
  uint32_t areas;
  uint32_t aligned_keys[32];
  uint64_t aligned_keys[16];
  int i;

  if(REG(AES_CTRL_ALG_SEL) != 0x00000000) {
    return CRYPTO_RESOURCE_IN_USE;
  }

  /* 192-bit keys must be padded to 256 bits */
  if(key_size == AES_KEY_STORE_SIZE_KEY_SIZE_192) {
    for(i = 0; i < count; i++) {
      rom_util_memcpy(&aligned_keys[i << 2], &((uint64_t *)keys)[i * 3], 24);
      aligned_keys[(i << 2) + 3] = 0;
    }
  }

  /* Change count to the number of 128-bit key areas */
  if(key_size != AES_KEY_STORE_SIZE_KEY_SIZE_128) {
    count <<= 1;
  }

  /* The keys base address needs to be 4-byte aligned */
  if(key_size != AES_KEY_STORE_SIZE_KEY_SIZE_192) {
    rom_util_memcpy(aligned_keys, keys, count << 4);
  }

  /* Workaround for AES registers not retained after PM2 */
  REG(AES_CTRL_INT_CFG) = AES_CTRL_INT_CFG_LEVEL;
@@ -73,14 +90,12 @@ aes_load_keys(const void *keys, uint8_t count, uint8_t start_area)
  REG(AES_CTRL_INT_CLR) = AES_CTRL_INT_CLR_DMA_IN_DONE |
                          AES_CTRL_INT_CLR_RESULT_AV;

  /* Configure key store module (areas, size): 128-bit key size
  /* Configure key store module (areas, size)
   * Note that writing AES_KEY_STORE_SIZE deletes all stored keys */
  aes_key_store_size = REG(AES_KEY_STORE_SIZE);
  if((aes_key_store_size & AES_KEY_STORE_SIZE_KEY_SIZE_M) !=
     AES_KEY_STORE_SIZE_KEY_SIZE_128) {
  if((aes_key_store_size & AES_KEY_STORE_SIZE_KEY_SIZE_M) != key_size) {
    REG(AES_KEY_STORE_SIZE) = (aes_key_store_size &
                               ~AES_KEY_STORE_SIZE_KEY_SIZE_M) |
                              AES_KEY_STORE_SIZE_KEY_SIZE_128;
                               ~AES_KEY_STORE_SIZE_KEY_SIZE_M) | key_size;
  }

  /* Free possibly already occupied key areas */
+10 −3
Original line number Diff line number Diff line
@@ -472,11 +472,18 @@

/** \brief Writes keys into the Key RAM
 * \param keys Pointer to AES Keys
 * \param count Number of keys (1 to 8 - \p start_area)
 * \param start_area Start area in Key RAM where to store the key (0 to 7)
 * \param key_size Key size: \c AES_KEY_STORE_SIZE_KEY_SIZE_x
 * \param count Number of keys (1 to 8 - \p start_area for 128-bit keys, 1 to
 * (8 - \p start_area) / 2 for 192- and 256-bit keys)
 * \param start_area Start area in Key RAM where to store the key (0 to 7, must
 * be even for 192- and 256-bit keys)
 * \return \c CRYPTO_SUCCESS if successful, or CRYPTO/AES error code
 * \note Calling this function with a value of \p key_size different from the
 * one passed for the previous calls causes the deletion of all previously
 * stored keys.
 */
uint8_t aes_load_keys(const void *keys, uint8_t count, uint8_t start_area);
uint8_t aes_load_keys(const void *keys, uint8_t key_size, uint8_t count,
                      uint8_t start_area);

/** @} */

+3 −5
Original line number Diff line number Diff line
@@ -99,14 +99,13 @@ ccm_auth_encrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce,
  REG(AES_AES_IV_2) = iv[2];
  REG(AES_AES_IV_3) = iv[3];

  /* Program AES-CCM-128 encryption */
  /* Program AES-CCM encryption */
  REG(AES_AES_CTRL) = AES_AES_CTRL_SAVE_CONTEXT |            /* Save context */
    (((MAX(mic_len, 2) - 2) >> 1) << AES_AES_CTRL_CCM_M_S) | /* M */
    ((len_len - 1) << AES_AES_CTRL_CCM_L_S) |                /* L */
    AES_AES_CTRL_CCM |                                       /* CCM */
    AES_AES_CTRL_CTR_WIDTH_128 |                             /* CTR width 128 */
    AES_AES_CTRL_CTR |                                       /* CTR */
    AES_AES_CTRL_KEY_SIZE_128 |                              /* Key = 128 */
    AES_AES_CTRL_DIRECTION_ENCRYPT;                          /* Encryption */

  /* Write the length of the crypto block (lo) */
@@ -278,14 +277,13 @@ ccm_auth_decrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce,
  REG(AES_AES_IV_2) = iv[2];
  REG(AES_AES_IV_3) = iv[3];

  /* Program AES-CCM-128 decryption */
  /* Program AES-CCM decryption */
  REG(AES_AES_CTRL) = AES_AES_CTRL_SAVE_CONTEXT |            /* Save context */
    (((MAX(mic_len, 2) - 2) >> 1) << AES_AES_CTRL_CCM_M_S) | /* M */
    ((len_len - 1) << AES_AES_CTRL_CCM_L_S) |                /* L */
    AES_AES_CTRL_CCM |                                       /* CCM */
    AES_AES_CTRL_CTR_WIDTH_128 |                             /* CTR width 128 */
    AES_AES_CTRL_CTR |                                       /* CTR */
    AES_AES_CTRL_KEY_SIZE_128;                               /* Key = 128 */
    AES_AES_CTRL_CTR;                                        /* CTR */

  /* Write the length of the crypto block (lo) */
  REG(AES_AES_C_LENGTH_0) = pdata_len;
+413 −19

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ In terms of hardware support, the following drivers have been implemented:
    * Low Power Modes
    * General-Purpose Timers. NB: GPT0 is in use by the platform code, the remaining GPTs are available for application development.
    * ADC
    * Cryptoprocessor (AES-CCM-128, SHA-256)
    * Cryptoprocessor (AES-CCM-256, SHA-256)
  * SmartRF06 EB and BB peripherals
    * LEDs
    * Buttons
Loading