CCM* module now accepts fixed-size 13-byte nonces

also adjusted variable naming conventions in CCM* module
to use 'nonce' and 'iv' in line with RFC 3610 terminology
This commit is contained in:
Justin King-Lacroix 2015-06-29 22:51:00 +02:00
parent 3ce8f26eea
commit 30704e5afc
3 changed files with 39 additions and 48 deletions

View file

@ -45,30 +45,26 @@
/* see RFC 3610 */
#define CCM_STAR_AUTH_FLAGS(Adata, M) ((Adata ? (1u << 6) : 0) | (((M - 2u) >> 1) << 3) | 1u)
#define CCM_STAR_ENCRYPTION_FLAGS 1
#define CCM_STAR_NONCE_MAX_IV_LENGTH 13
/*---------------------------------------------------------------------------*/
static void
set_nonce(uint8_t *nonce,
set_nonce(uint8_t *iv,
uint8_t flags,
const uint8_t *iv, uint8_t iv_len,
const uint8_t *nonce,
uint8_t counter)
{
/* 1 byte|| 8 bytes || 4 bytes || 1 byte || 2 bytes */
/* flags || extended_source_address || frame_counter || sec_lvl || counter */
if(iv_len > CCM_STAR_NONCE_MAX_IV_LENGTH)
iv_len = CCM_STAR_NONCE_MAX_IV_LENGTH;
nonce[0] = flags;
memcpy(nonce + 1, iv, iv_len);
memset(nonce + iv_len + 1, 0, 16 - (1 + 1 + iv_len));
nonce[15] = counter;
iv[0] = flags;
memcpy(iv + 1, nonce, CCM_STAR_NONCE_LENGTH);
iv[14] = 0;
iv[15] = counter;
}
/*---------------------------------------------------------------------------*/
/* XORs the block m[pos] ... m[pos + 15] with K_{counter} */
static void
ctr_step(const uint8_t *iv, uint8_t iv_len,
ctr_step(const uint8_t *nonce,
uint8_t pos,
uint8_t *m_and_result,
uint8_t m_len,
@ -77,7 +73,7 @@ ctr_step(const uint8_t *iv, uint8_t iv_len,
uint8_t a[AES_128_BLOCK_SIZE];
uint8_t i;
set_nonce(a, CCM_STAR_ENCRYPTION_FLAGS, iv, iv_len, counter);
set_nonce(a, CCM_STAR_ENCRYPTION_FLAGS, nonce, counter);
AES_128.encrypt(a);
for(i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
@ -86,9 +82,9 @@ ctr_step(const uint8_t *iv, uint8_t iv_len,
}
/*---------------------------------------------------------------------------*/
static void
mic(const uint8_t* m, uint8_t m_len,
const uint8_t* iv, uint8_t iv_len,
const uint8_t* a, uint8_t a_len,
mic(const uint8_t *m, uint8_t m_len,
const uint8_t *nonce,
const uint8_t *a, uint8_t a_len,
uint8_t *result,
uint8_t mic_len)
{
@ -96,10 +92,7 @@ mic(const uint8_t* m, uint8_t m_len,
uint8_t pos;
uint8_t i;
set_nonce(x,
CCM_STAR_AUTH_FLAGS(a_len, mic_len),
iv, iv_len,
m_len);
set_nonce(x, CCM_STAR_AUTH_FLAGS(a_len, mic_len), nonce, m_len);
AES_128.encrypt(x);
if(a_len > 0) {
@ -132,13 +125,13 @@ mic(const uint8_t* m, uint8_t m_len,
}
}
ctr_step(iv, iv_len, 0, x, AES_128_BLOCK_SIZE, 0);
ctr_step(nonce, 0, x, AES_128_BLOCK_SIZE, 0);
memcpy(result, x, mic_len);
}
/*---------------------------------------------------------------------------*/
static void
ctr(uint8_t* m, uint8_t m_len, const uint8_t* iv, uint8_t iv_len)
ctr(uint8_t *m, uint8_t m_len, const uint8_t* nonce)
{
uint8_t pos;
uint8_t counter;
@ -146,12 +139,12 @@ ctr(uint8_t* m, uint8_t m_len, const uint8_t* iv, uint8_t iv_len)
pos = 0;
counter = 1;
while(pos < m_len) {
ctr_step(iv, iv_len, pos, m, m_len, counter++);
ctr_step(nonce, pos, m, m_len, counter++);
pos += AES_128_BLOCK_SIZE;
}
}
/*---------------------------------------------------------------------------*/
static void set_key(const uint8_t* key) {
static void set_key(const uint8_t *key) {
AES_128.set_key((uint8_t*)key);
}
/*---------------------------------------------------------------------------*/