Contiki 3.x
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  */
39 
40 /**
41  * \addtogroup llsec802154
42  * @{
43  */
44 
45 #include "net/llsec/ccm-star.h"
46 #include "net/llsec/llsec802154.h"
47 #include "net/packetbuf.h"
48 #include "lib/aes-128.h"
49 #include <string.h>
50 
51 /*---------------------------------------------------------------------------*/
52 static void
53 set_nonce(uint8_t *nonce,
54  uint8_t flags,
55  const uint8_t *extended_source_address,
56  uint8_t counter)
57 {
58  /* 1 byte|| 8 bytes || 4 bytes || 1 byte || 2 bytes */
59  /* flags || extended_source_address || frame_counter || sec_lvl || counter */
60 
61  nonce[0] = flags;
62  memcpy(nonce + 1, extended_source_address, 8);
63  nonce[9] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3) >> 8;
64  nonce[10] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_2_3) & 0xff;
65  nonce[11] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1) >> 8;
66  nonce[12] = packetbuf_attr(PACKETBUF_ATTR_FRAME_COUNTER_BYTES_0_1) & 0xff;
67  nonce[13] = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL);
68  nonce[14] = 0;
69  nonce[15] = counter;
70 }
71 /*---------------------------------------------------------------------------*/
72 /* XORs the block m[pos] ... m[pos + 15] with K_{counter} */
73 static void
74 ctr_step(const uint8_t *extended_source_address,
75  uint8_t pos,
76  uint8_t *m_and_result,
77  uint8_t m_len,
78  uint8_t counter)
79 {
80  uint8_t a[AES_128_BLOCK_SIZE];
81  uint8_t i;
82 
83  set_nonce(a, CCM_STAR_ENCRYPTION_FLAGS, extended_source_address, counter);
84  AES_128.encrypt(a);
85 
86  for(i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
87  m_and_result[pos + i] ^= a[i];
88  }
89 }
90 /*---------------------------------------------------------------------------*/
91 static void
92 mic(const uint8_t *extended_source_address,
93  uint8_t *result,
94  uint8_t mic_len)
95 {
96  uint8_t x[AES_128_BLOCK_SIZE];
97  uint8_t pos;
98  uint8_t i;
99  uint8_t a_len;
100  uint8_t *a;
101 #if LLSEC802154_USES_ENCRYPTION
102  uint8_t shall_encrypt;
103  uint8_t m_len;
104  uint8_t *m;
105 
106  shall_encrypt = packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & (1 << 2);
107  if(shall_encrypt) {
108  a_len = packetbuf_hdrlen();
109  m_len = packetbuf_datalen();
110  } else {
111  a_len = packetbuf_totlen();
112  m_len = 0;
113  }
114  set_nonce(x,
115  CCM_STAR_AUTH_FLAGS(a_len, mic_len),
116  extended_source_address,
117  m_len);
118 #else /* LLSEC802154_USES_ENCRYPTION */
119  a_len = packetbuf_totlen();
120  set_nonce(x,
121  CCM_STAR_AUTH_FLAGS(a_len, mic_len),
122  extended_source_address,
123  0);
124 #endif /* LLSEC802154_USES_ENCRYPTION */
125  AES_128.encrypt(x);
126 
127  a = packetbuf_hdrptr();
128  if(a_len) {
129  x[1] = x[1] ^ a_len;
130  for(i = 2; (i - 2 < a_len) && (i < AES_128_BLOCK_SIZE); i++) {
131  x[i] ^= a[i - 2];
132  }
133 
134  AES_128.encrypt(x);
135 
136  pos = 14;
137  while(pos < a_len) {
138  for(i = 0; (pos + i < a_len) && (i < AES_128_BLOCK_SIZE); i++) {
139  x[i] ^= a[pos + i];
140  }
141  pos += AES_128_BLOCK_SIZE;
142  AES_128.encrypt(x);
143  }
144  }
145 
146 #if LLSEC802154_USES_ENCRYPTION
147  if(shall_encrypt) {
148  m = a + a_len;
149  pos = 0;
150  while(pos < m_len) {
151  for(i = 0; (pos + i < m_len) && (i < AES_128_BLOCK_SIZE); i++) {
152  x[i] ^= m[pos + i];
153  }
154  pos += AES_128_BLOCK_SIZE;
155  AES_128.encrypt(x);
156  }
157  }
158 #endif /* LLSEC802154_USES_ENCRYPTION */
159 
160  ctr_step(extended_source_address, 0, x, AES_128_BLOCK_SIZE, 0);
161 
162  memcpy(result, x, mic_len);
163 }
164 /*---------------------------------------------------------------------------*/
165 static void
166 ctr(const uint8_t *extended_source_address)
167 {
168  uint8_t m_len;
169  uint8_t *m;
170  uint8_t pos;
171  uint8_t counter;
172 
173  m_len = packetbuf_datalen();
174  m = (uint8_t *) packetbuf_dataptr();
175 
176  pos = 0;
177  counter = 1;
178  while(pos < m_len) {
179  ctr_step(extended_source_address, pos, m, m_len, counter++);
180  pos += AES_128_BLOCK_SIZE;
181  }
182 }
183 /*---------------------------------------------------------------------------*/
184 const struct ccm_star_driver ccm_star_driver = {
185  mic,
186  ctr
187 };
188 /*---------------------------------------------------------------------------*/
189 
190 /** @} */
void(* ctr)(const uint8_t *extended_source_address)
XORs the frame in the packetbuf with the key stream.
Definition: ccm-star.h:78
Header file for the Rime buffer (packetbuf) management
Structure of CCM* drivers.
Definition: ccm-star.h:64
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition: packetbuf.c:245
CCM* header file.
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
AES-128.
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
Common functionality of 802.15.4-compliant llsec_drivers.
void(* mic)(const uint8_t *extended_source_address, uint8_t *result, uint8_t mic_len)
Generates a MIC over the frame in the packetbuf.
Definition: ccm-star.h:71
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207