Contiki-NG
Loading...
Searching...
No Matches
ccm-star.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013, Hasso-Plattner-Institut.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * AES_128-based CCM* implementation.
36 * \author
37 * Konrad Krentz <konrad.krentz@gmail.com>
38 * Justin King-Lacroix <justin.kinglacroix@gmail.com>
39 */
40
41#include "ccm-star.h"
42#include "lib/aes-128.h"
43#include <string.h>
44
45/* As per RFC 3610. L == 2 (m_len is two bytes long). */
46#define CCM_STAR_AUTH_FLAGS(a_len, mic_len) (((a_len) ? 1u << 6 : 0) \
47 | ((((mic_len) - 2u) >> 1) << 3) \
48 | 1u)
49#define CCM_STAR_ENCRYPTION_FLAGS 1
50/* Valid values are 4, 6, 8, 10, 12, 14, and 16 octets */
51#define MIC_LEN_VALID(x) ((x) >= 4 && (x) <= 16 && (x) % 2 == 0)
52
53/*---------------------------------------------------------------------------*/
54static void
55set_iv(uint8_t *iv,
56 uint8_t flags,
57 const uint8_t *nonce,
58 uint16_t counter)
59{
60 iv[0] = flags;
61 memcpy(iv + 1, nonce, CCM_STAR_NONCE_LENGTH);
62 iv[14] = counter >> 8;
63 iv[15] = counter;
64}
65/*---------------------------------------------------------------------------*/
66/* XORs the block m[pos] ... m[pos + 15] with K_{counter} */
67static void
68ctr_step(const uint8_t *nonce,
69 uint16_t pos,
70 uint8_t *m_and_result, uint16_t m_len,
71 uint16_t counter)
72{
73 uint8_t a[AES_128_BLOCK_SIZE];
74
75 set_iv(a, CCM_STAR_ENCRYPTION_FLAGS, nonce, counter);
76 AES_128.encrypt(a);
77
78 for(uint_fast8_t i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
79 m_and_result[pos + i] ^= a[i];
80 }
81}
82/*---------------------------------------------------------------------------*/
83static void
84mic(const uint8_t *nonce,
85 const uint8_t *m, uint16_t m_len,
86 const uint8_t *a, uint16_t a_len,
87 uint8_t *result, uint8_t mic_len)
88{
89 uint8_t x[AES_128_BLOCK_SIZE];
90
91 set_iv(x, CCM_STAR_AUTH_FLAGS(a_len, mic_len), nonce, m_len);
92 AES_128.encrypt(x);
93
94 if(a_len) {
95 x[0] ^= (a_len >> 8);
96 x[1] ^= a_len;
97 uint32_t pos;
98 for(pos = 0; (pos < a_len) && (pos < AES_128_BLOCK_SIZE - 2); pos++) {
99 x[2 + pos] ^= a[pos];
100 }
101
102 AES_128.encrypt(x);
103
104 /* 32-bit pos to reach the end of the loop if a_len is large */
105 for(; pos < a_len; pos += AES_128_BLOCK_SIZE) {
106 for(uint_fast8_t i = 0;
107 (pos + i < a_len) && (i < AES_128_BLOCK_SIZE);
108 i++) {
109 x[i] ^= a[pos + i];
110 }
111 AES_128.encrypt(x);
112 }
113 }
114
115 if(m_len) {
116 /* 32-bit pos to reach the end of the loop if m_len is large */
117 for(uint32_t pos = 0; pos < m_len; pos += AES_128_BLOCK_SIZE) {
118 for(uint_fast8_t i = 0;
119 (pos + i < m_len) && (i < AES_128_BLOCK_SIZE);
120 i++) {
121 x[i] ^= m[pos + i];
122 }
123 AES_128.encrypt(x);
124 }
125 }
126
127 ctr_step(nonce, 0, x, AES_128_BLOCK_SIZE, 0);
128
129 memcpy(result, x, mic_len);
130}
131/*---------------------------------------------------------------------------*/
132static void
133ctr(const uint8_t *nonce, uint8_t *m, uint16_t m_len)
134{
135 uint16_t counter = 1;
136 /* 32-bit pos to reach the end of the loop if m_len is large */
137 for(uint32_t pos = 0; pos < m_len; pos += AES_128_BLOCK_SIZE) {
138 ctr_step(nonce, pos, m, m_len, counter++);
139 }
140}
141/*---------------------------------------------------------------------------*/
142static void
143set_key(const uint8_t *key)
144{
145 AES_128.set_key(key);
146}
147/*---------------------------------------------------------------------------*/
148static void
149aead(const uint8_t* nonce,
150 uint8_t* m, uint16_t m_len,
151 const uint8_t* a, uint16_t a_len,
152 uint8_t *result, uint8_t mic_len,
153 int forward)
154{
155 if(!MIC_LEN_VALID(mic_len)) {
156 return;
157 }
158
159 if(!forward) {
160 /* decrypt */
161 ctr(nonce, m, m_len);
162 }
163
164 mic(nonce,
165 m, m_len,
166 a, a_len,
167 result,
168 mic_len);
169
170 if(forward) {
171 /* encrypt */
172 ctr(nonce, m, m_len);
173 }
174}
175/*---------------------------------------------------------------------------*/
176const struct ccm_star_driver ccm_star_driver = {
177 set_key,
178 aead
179};
180/*---------------------------------------------------------------------------*/
AES-128.
CCM* header file.
Structure of CCM* drivers.
Definition ccm-star.h:56
void(* aead)(const uint8_t *nonce, uint8_t *m, uint16_t m_len, const uint8_t *a, uint16_t a_len, uint8_t *result, uint8_t mic_len, int forward)
Combines authentication and encryption.
Definition ccm-star.h:75
void(* set_key)(const uint8_t *key)
Sets the key in use.
Definition ccm-star.h:64