Contiki-NG
Loading...
Searching...
No Matches
aes.c
Go to the documentation of this file.
1/*
2 * Original file:
3 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
4 * All rights reserved.
5 *
6 * Port to Contiki:
7 * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36/**
37 * \addtogroup cc2538-aes
38 * @{
39 *
40 * \file
41 * Implementation of the cc2538 AES driver
42 */
43#include "contiki.h"
44#include "dev/rom-util.h"
45#include "dev/nvic.h"
46#include "dev/aes.h"
47#include "reg.h"
48
49#include <stdbool.h>
50#include <stdint.h>
51/*---------------------------------------------------------------------------*/
52uint8_t
53aes_load_keys(const void *keys, uint8_t key_size, uint8_t count,
54 uint8_t start_area)
55{
56 uint32_t aes_key_store_size;
57 uint32_t areas;
58 uint64_t aligned_keys[AES_KEY_AREAS * 128 / 8 / sizeof(uint64_t)];
59 int i;
60
61 if(REG(AES_CTRL_ALG_SEL) != 0x00000000) {
62 return CRYPTO_RESOURCE_IN_USE;
63 }
64
65 /* 192-bit keys must be padded to 256 bits */
66 if(key_size == AES_KEY_STORE_SIZE_KEY_SIZE_192) {
67 for(i = 0; i < count; i++) {
68 rom_util_memcpy(&aligned_keys[i << 2], &((const uint64_t *)keys)[i * 3],
69 192 / 8);
70 aligned_keys[(i << 2) + 3] = 0;
71 }
72 }
73
74 /* Change count to the number of 128-bit key areas */
75 if(key_size != AES_KEY_STORE_SIZE_KEY_SIZE_128) {
76 count <<= 1;
77 }
78
79 /* The keys base address needs to be 4-byte aligned */
80 if(key_size != AES_KEY_STORE_SIZE_KEY_SIZE_192) {
81 rom_util_memcpy(aligned_keys, keys, count << 4);
82 }
83
84 /* Workaround for AES registers not retained after PM2 */
88
89 /* Configure master control module */
91
92 /* Clear any outstanding events */
95
96 /* Configure key store module (areas, size)
97 * Note that writing AES_KEY_STORE_SIZE deletes all stored keys */
98 aes_key_store_size = REG(AES_KEY_STORE_SIZE);
99 if((aes_key_store_size & AES_KEY_STORE_SIZE_KEY_SIZE_M) != key_size) {
100 REG(AES_KEY_STORE_SIZE) = (aes_key_store_size &
101 ~AES_KEY_STORE_SIZE_KEY_SIZE_M) | key_size;
102 }
103
104 /* Free possibly already occupied key areas */
105 areas = ((0x00000001 << count) - 1) << start_area;
106 REG(AES_KEY_STORE_WRITTEN_AREA) = areas;
107
108 /* Enable key areas to write */
109 REG(AES_KEY_STORE_WRITE_AREA) = areas;
110
111 /* Configure DMAC
112 * Enable DMA channel 0 */
114
115 /* Base address of the keys in ext. memory */
116 REG(AES_DMAC_CH0_EXTADDR) = (uint32_t)aligned_keys;
117
118 /* Total keys length in bytes (e.g. 16 for 1 x 128-bit key) */
120 ~AES_DMAC_CH_DMALENGTH_DMALEN_M) |
122
123 /* Wait for operation to complete */
125
126 /* Clean up the keys */
127 REG(AES_DMAC_CH0_EXTADDR) = 0x00000000;
128
129 /* Check for absence of errors in DMA and key store */
132 /* Disable master control / DMA clock */
133 REG(AES_CTRL_ALG_SEL) = 0x00000000;
134 return CRYPTO_DMA_BUS_ERROR;
135 }
138 /* Disable master control / DMA clock */
139 REG(AES_CTRL_ALG_SEL) = 0x00000000;
140 return AES_KEYSTORE_WRITE_ERROR;
141 }
142
143 /* Acknowledge the interrupt */
146
147 /* Disable master control / DMA clock */
148 REG(AES_CTRL_ALG_SEL) = 0x00000000;
149
150 /* Check status, if error return error code */
151 if((REG(AES_KEY_STORE_WRITTEN_AREA) & areas) != areas) {
152 return AES_KEYSTORE_WRITE_ERROR;
153 }
154
155 return CRYPTO_SUCCESS;
156}
157/*---------------------------------------------------------------------------*/
158uint8_t
159aes_auth_crypt_start(uint32_t ctrl, uint8_t key_area, const void *iv,
160 const void *adata, uint16_t adata_len,
161 const void *data_in, void *data_out, uint16_t data_len,
162 struct process *process)
163{
164 if(REG(AES_CTRL_ALG_SEL) != 0x00000000) {
165 return CRYPTO_RESOURCE_IN_USE;
166 }
167
168 /* Workaround for AES registers not retained after PM2 */
172
176
177 REG(AES_KEY_STORE_READ_AREA) = key_area;
178
179 /* Wait until key is loaded to the AES module */
181
182 /* Check for Key Store read error */
184 /* Clear the Keystore Read error bit */
186 /* Disable the master control / DMA clock */
187 REG(AES_CTRL_ALG_SEL) = 0x00000000;
188 return AES_KEYSTORE_READ_ERROR;
189 }
190
191 if(iv != NULL) {
192 /* Write initialization vector */
193 REG(AES_AES_IV_0) = ((const uint32_t *)iv)[0];
194 REG(AES_AES_IV_1) = ((const uint32_t *)iv)[1];
195 REG(AES_AES_IV_2) = ((const uint32_t *)iv)[2];
196 REG(AES_AES_IV_3) = ((const uint32_t *)iv)[3];
197 }
198
199 /* Program AES authentication/crypto operation */
200 REG(AES_AES_CTRL) = ctrl;
201
202 /* Write the length of the payload block (lo) */
203 REG(AES_AES_C_LENGTH_0) = data_len;
204 /* Write the length of the payload block (hi) */
205 REG(AES_AES_C_LENGTH_1) = 0;
206
207 /* For combined modes only (CCM or GCM) */
208 if(ctrl & (AES_AES_CTRL_CCM | AES_AES_CTRL_GCM)) {
209 /* Write the length of the AAD data block (may be non-block size-aligned) */
210 REG(AES_AES_AUTH_LENGTH) = adata_len;
211
212 if(adata_len != 0) {
213 /* Configure DMAC to fetch the AAD data
214 * Enable DMA channel 0 */
216 /* Base address of the AAD data buffer */
217 REG(AES_DMAC_CH0_EXTADDR) = (uint32_t)adata;
218 /* AAD data length in bytes */
219 REG(AES_DMAC_CH0_DMALENGTH) = adata_len;
220
221 /* Wait for completion of the AAD data transfer, DMA_IN_DONE */
223
224 /* Check for the absence of error */
226 /* Clear the DMA error */
228 /* Disable the master control / DMA clock */
229 REG(AES_CTRL_ALG_SEL) = 0x00000000;
230 return CRYPTO_DMA_BUS_ERROR;
231 }
232
233 /* Clear interrupt status */
235 }
236 }
237
238 /* Enable result available bit in interrupt enable */
240
241 if(process != NULL) {
243 NVIC_ClearPendingIRQ(AES_IRQn);
244 NVIC_EnableIRQ(AES_IRQn);
245 }
246
247 if(data_len != 0) {
248 /* Configure DMAC
249 * Enable DMA channel 0 */
251 /* Base address of the input payload data buffer */
252 REG(AES_DMAC_CH0_EXTADDR) = (uint32_t)data_in;
253 /* Input payload data length in bytes */
254 REG(AES_DMAC_CH0_DMALENGTH) = data_len;
255
256 if(data_out != NULL) {
257 /* Enable DMA channel 1 */
259 /* Base address of the output payload data buffer */
260 REG(AES_DMAC_CH1_EXTADDR) = (uint32_t)data_out;
261 /* Output payload data length in bytes */
262 REG(AES_DMAC_CH1_DMALENGTH) = data_len;
263 }
264 }
265
266 return CRYPTO_SUCCESS;
267}
268/*---------------------------------------------------------------------------*/
269uint8_t
276/*---------------------------------------------------------------------------*/
277uint8_t
278aes_auth_crypt_get_result(void *iv, void *tag)
279{
280 uint32_t aes_ctrl_int_stat;
281
282 aes_ctrl_int_stat = REG(AES_CTRL_INT_STAT);
283 /* Clear the error bits */
287
288 NVIC_DisableIRQ(AES_IRQn);
290
291 /* Disable the master control / DMA clock */
292 REG(AES_CTRL_ALG_SEL) = 0x00000000;
293
294 if(aes_ctrl_int_stat & AES_CTRL_INT_STAT_DMA_BUS_ERR) {
295 return CRYPTO_DMA_BUS_ERROR;
296 }
297 if(aes_ctrl_int_stat & AES_CTRL_INT_STAT_KEY_ST_WR_ERR) {
298 return AES_KEYSTORE_WRITE_ERROR;
299 }
300 if(aes_ctrl_int_stat & AES_CTRL_INT_STAT_KEY_ST_RD_ERR) {
301 return AES_KEYSTORE_READ_ERROR;
302 }
303
304 if(iv != NULL || tag != NULL) {
305 /* Read result
306 * Wait for the context ready bit */
308
309 if(iv != NULL) {
310 /* Read the initialization vector registers */
311 ((uint32_t *)iv)[0] = REG(AES_AES_IV_0);
312 ((uint32_t *)iv)[1] = REG(AES_AES_IV_1);
313 ((uint32_t *)iv)[2] = REG(AES_AES_IV_2);
314 ((uint32_t *)iv)[3] = REG(AES_AES_IV_3);
315 }
316
317 if(tag != NULL) {
318 /* Read the tag registers */
319 ((uint32_t *)tag)[0] = REG(AES_AES_TAG_OUT_0);
320 ((uint32_t *)tag)[1] = REG(AES_AES_TAG_OUT_1);
321 ((uint32_t *)tag)[2] = REG(AES_AES_TAG_OUT_2);
322 ((uint32_t *)tag)[3] = REG(AES_AES_TAG_OUT_3);
323 }
324 }
325
326 /* Clear the interrupt status */
329
330 return CRYPTO_SUCCESS;
331}
332
333/** @} */
Header file for the cc2538 AES driver.
@ AES_IRQn
AES Interrupt.
Definition cc2538_cm3.h:111
#define AES_CTRL_INT_CLR
Interrupt clear.
Definition aes.h:135
#define AES_AES_IV_0
AES initialization vector.
Definition aes.h:86
#define AES_AES_CTRL_SAVED_CONTEXT_READY
AES auth.
Definition aes.h:271
#define AES_KEY_STORE_SIZE_KEY_SIZE_128
Key size: 128 bits.
Definition aes.h:247
#define AES_CTRL_INT_CLR_DMA_BUS_ERR
Clear DMA bus error status.
Definition aes.h:381
#define AES_DMAC_CH1_CTRL
Channel 1 control.
Definition aes.h:65
#define AES_CTRL_INT_CFG
Interrupt configuration.
Definition aes.h:133
#define AES_AES_TAG_OUT_2
TAG.
Definition aes.h:100
#define AES_CTRL_INT_CFG_LEVEL
Level interrupt type.
Definition aes.h:366
#define AES_DMAC_CH1_DMALENGTH
Channel 1 DMA length.
Definition aes.h:67
#define AES_AES_IV_3
AES initialization vector.
Definition aes.h:89
#define AES_CTRL_INT_STAT_KEY_ST_WR_ERR
Write error detected.
Definition aes.h:407
#define AES_KEY_STORE_SIZE
Key store size.
Definition aes.h:76
#define AES_CTRL_INT_EN_DMA_IN_DONE
DMA input done interrupt enabled.
Definition aes.h:372
#define AES_AES_CTRL
AES input/output buffer control and mode.
Definition aes.h:90
#define AES_KEY_STORE_SIZE_KEY_SIZE_M
Key size mask.
Definition aes.h:250
#define AES_CTRL_INT_STAT_KEY_ST_RD_ERR
Read error detected.
Definition aes.h:409
#define AES_KEY_STORE_WRITTEN_AREA
Key store written area.
Definition aes.h:74
#define AES_CTRL_INT_STAT_DMA_BUS_ERR
DMA bus error detected.
Definition aes.h:405
uint8_t aes_load_keys(const void *keys, uint8_t key_size, uint8_t count, uint8_t start_area)
Writes keys into the Key RAM.
Definition aes.c:53
uint8_t aes_auth_crypt_check_status(void)
Checks the status of the AES authentication/crypto operation.
Definition aes.c:270
#define AES_AES_AUTH_LENGTH
Authentication length.
Definition aes.h:93
#define AES_AES_CTRL_GCM
AES-GCM mode.
Definition aes.h:280
#define AES_AES_CTRL_CCM
AES-CCM mode.
Definition aes.h:279
#define AES_DMAC_CH0_CTRL
Channel 0 control.
Definition aes.h:60
#define AES_DMAC_CH_CTRL_EN
Channel enable.
Definition aes.h:146
#define AES_KEY_STORE_READ_AREA_BUSY
Key store operation busy.
Definition aes.h:258
#define AES_AES_IV_2
AES initialization vector.
Definition aes.h:88
#define AES_AES_TAG_OUT_1
TAG.
Definition aes.h:99
#define AES_CTRL_INT_CLR_DMA_IN_DONE
Clear DMA in done interrupt.
Definition aes.h:387
#define AES_DMAC_CH0_EXTADDR
Channel 0 external address.
Definition aes.h:61
#define AES_CTRL_INT_EN_RESULT_AV
Result available interrupt enabled.
Definition aes.h:374
#define AES_KEY_STORE_READ_AREA
Key store read area.
Definition aes.h:77
uint8_t aes_auth_crypt_start(uint32_t ctrl, uint8_t key_area, const void *iv, const void *adata, uint16_t adata_len, const void *data_in, void *data_out, uint16_t data_len, struct process *process)
Starts an AES authentication/crypto operation.
Definition aes.c:159
#define AES_DMAC_CH1_EXTADDR
Channel 1 external address.
Definition aes.h:66
uint8_t aes_auth_crypt_get_result(void *iv, void *tag)
Gets the result of the AES authentication/crypto operation.
Definition aes.c:278
#define AES_AES_C_LENGTH_1
AES crypto length (MSW)
Definition aes.h:92
#define AES_AES_TAG_OUT_0
TAG.
Definition aes.h:98
#define AES_AES_TAG_OUT_3
TAG.
Definition aes.h:101
#define AES_CTRL_ALG_SEL_KEYSTORE
Select Key Store as DMA destination.
Definition aes.h:345
#define AES_AES_C_LENGTH_0
AES crypto length (LSW)
Definition aes.h:91
#define AES_CTRL_INT_STAT_DMA_IN_DONE
DMA data in done interrupt status.
Definition aes.h:411
#define AES_CTRL_INT_CLR_KEY_ST_WR_ERR
Clear key store write error status.
Definition aes.h:383
#define AES_KEY_STORE_SIZE_KEY_SIZE_192
Key size: 192 bits.
Definition aes.h:248
#define AES_CTRL_INT_CLR_RESULT_AV
Clear result available interrupt.
Definition aes.h:389
#define AES_DMAC_CH_DMALENGTH_DMALEN_S
Channel DMA length in bytes shift.
Definition aes.h:154
#define AES_CTRL_ALG_SEL
Algorithm select.
Definition aes.h:130
#define AES_CTRL_ALG_SEL_AES
Select AES engine as DMA source/destination.
Definition aes.h:344
#define AES_CTRL_INT_STAT
Interrupt status.
Definition aes.h:137
#define AES_DMAC_CH0_DMALENGTH
Channel 0 DMA length.
Definition aes.h:62
#define AES_CTRL_INT_EN
Interrupt enable.
Definition aes.h:134
#define AES_CTRL_INT_CLR_KEY_ST_RD_ERR
Clear key store read error status.
Definition aes.h:385
#define AES_KEY_STORE_WRITE_AREA
Key store write area.
Definition aes.h:72
#define AES_CTRL_INT_STAT_RESULT_AV
Result available interrupt status.
Definition aes.h:413
#define AES_AES_IV_1
AES initialization vector.
Definition aes.h:87
void crypto_register_process_notification(struct process *p)
Registers a process to be notified of the completion of a crypto operation.
Definition crypto.c:110
static volatile uint64_t count
Num.
Definition clock.c:50
Header file for the ARM Nested Vectored Interrupt Controller.
Header file with register manipulation macro definitions.
Header file for the cc2538 ROM utility function library driver.