Contiki-NG
ccm.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-ccm
38 * @{
39 *
40 * \file
41 * Implementation of the cc2538 AES-CCM driver
42 */
43#include "contiki.h"
44#include "sys/cc.h"
45#include "dev/rom-util.h"
46#include "dev/ccm.h"
47
48#include <stdbool.h>
49#include <stdint.h>
50/*---------------------------------------------------------------------------*/
51static uint8_t
52ccm_auth_crypt_start(uint8_t encrypt, uint8_t len_len, uint8_t key_area,
53 const void *nonce, const void *adata, uint16_t adata_len,
54 const void *data_in, void *data_out, uint16_t data_len,
55 uint8_t mic_len, struct process *process)
56{
57 uint32_t ctrl;
58 uint32_t iv[AES_IV_LEN / sizeof(uint32_t)];
59
60 /* Program AES-CCM authentication/crypto operation */
61 ctrl = AES_AES_CTRL_SAVE_CONTEXT | /* Save context */
62 (((MAX(mic_len, 2) - 2) >> 1) << AES_AES_CTRL_CCM_M_S) | /* M */
63 ((len_len - 1) << AES_AES_CTRL_CCM_L_S) | /* L */
64 AES_AES_CTRL_CCM | /* CCM */
65 AES_AES_CTRL_CTR_WIDTH_128 | /* CTR width 128 */
66 AES_AES_CTRL_CTR | /* CTR */
67 (encrypt ? AES_AES_CTRL_DIRECTION_ENCRYPT : 0); /* En/decryption */
68
69 /* Prepare the crypto initialization vector
70 * Flags: L' = L - 1 */
71 ((uint8_t *)iv)[0] = len_len - 1;
72 /* Nonce */
73 rom_util_memcpy(&((uint8_t *)iv)[CCM_FLAGS_LEN], nonce,
74 CCM_NONCE_LEN_LEN - len_len);
75 /* Initialize counter to 0 */
76 rom_util_memset(&((uint8_t *)iv)[AES_IV_LEN - len_len], 0, len_len);
77
78 return aes_auth_crypt_start(ctrl, key_area, iv, adata, adata_len,
79 data_in, data_out, data_len, process);
80}
81/*---------------------------------------------------------------------------*/
82static uint8_t
83ccm_auth_crypt_get_result(const void *cdata, uint16_t cdata_len,
84 void *mic, uint8_t mic_len)
85{
86 uint32_t tag[AES_TAG_LEN / sizeof(uint32_t)];
87 uint16_t data_len;
88 uint8_t ret;
89
90 ret = aes_auth_crypt_get_result(NULL, tag);
91 if(ret != CRYPTO_SUCCESS) {
92 return ret;
93 }
94
95 if(cdata != NULL) {
96 /* Check MIC */
97 data_len = cdata_len - mic_len;
98 if(rom_util_memcmp(tag, &((const uint8_t *)cdata)[data_len], mic_len)) {
99 ret = AES_AUTHENTICATION_FAILED;
100 }
101 }
102
103 /* Copy tag to MIC */
104 rom_util_memcpy(mic, tag, mic_len);
105
106 return ret;
107}
108/*---------------------------------------------------------------------------*/
109uint8_t
110ccm_auth_encrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce,
111 const void *adata, uint16_t adata_len, const void *pdata,
112 uint16_t pdata_len, void *cdata, uint8_t mic_len,
113 struct process *process)
114{
115 return ccm_auth_crypt_start(true, len_len, key_area, nonce, adata, adata_len,
116 pdata, cdata, pdata_len, mic_len, process);
117}
118/*---------------------------------------------------------------------------*/
119uint8_t
120ccm_auth_encrypt_get_result(void *mic, uint8_t mic_len)
121{
122 return ccm_auth_crypt_get_result(NULL, 0, mic, mic_len);
123}
124/*---------------------------------------------------------------------------*/
125uint8_t
126ccm_auth_decrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce,
127 const void *adata, uint16_t adata_len, const void *cdata,
128 uint16_t cdata_len, void *pdata, uint8_t mic_len,
129 struct process *process)
130{
131 uint16_t data_len = cdata_len - mic_len;
132
133 return ccm_auth_crypt_start(false, len_len, key_area, nonce, adata, adata_len,
134 cdata, pdata, data_len, mic_len, process);
135}
136/*---------------------------------------------------------------------------*/
137uint8_t
138ccm_auth_decrypt_get_result(const void *cdata, uint16_t cdata_len,
139 void *mic, uint8_t mic_len)
140__attribute__ ((alias("ccm_auth_crypt_get_result")));
141
142/** @} */
Default definitions of C compiler quirk work-arounds.
Header file for the cc2538 AES-CCM driver.
#define AES_AES_CTRL_DIRECTION_ENCRYPT
Encrypt.
Definition: aes.h:297
#define AES_AES_CTRL_CTR_WIDTH_128
CTR counter width: 128 bits.
Definition: aes.h:285
#define AES_AES_CTRL_CCM_M_S
CCM auth.
Definition: aes.h:276
#define AES_AES_CTRL_CCM_L_S
CCM length field width shift.
Definition: aes.h:278
#define AES_AES_CTRL_CCM
AES-CCM mode.
Definition: aes.h:279
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
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_CTRL_SAVE_CONTEXT
Auth.
Definition: aes.h:273
#define AES_AES_CTRL_CTR
AES-CTR mode.
Definition: aes.h:290
uint8_t ccm_auth_decrypt_get_result(const void *cdata, uint16_t cdata_len, void *mic, uint8_t mic_len)
Gets the result of the CCM authentication checking and decryption operation.
uint8_t ccm_auth_encrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce, const void *adata, uint16_t adata_len, const void *pdata, uint16_t pdata_len, void *cdata, uint8_t mic_len, struct process *process)
Starts a CCM authentication and encryption operation.
Definition: ccm.c:110
uint8_t ccm_auth_decrypt_start(uint8_t len_len, uint8_t key_area, const void *nonce, const void *adata, uint16_t adata_len, const void *cdata, uint16_t cdata_len, void *pdata, uint8_t mic_len, struct process *process)
Starts a CCM authentication checking and decryption operation.
Definition: ccm.c:126
uint8_t ccm_auth_encrypt_get_result(void *mic, uint8_t mic_len)
Gets the result of the CCM authentication and encryption operation.
Definition: ccm.c:120
Header file for the cc2538 ROM utility function library driver.