Contiki-NG
cc2538-ccm-star.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Benoît Thébaudeau <benoit.thebaudeau.dev@gmail.com>
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 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/**
32 * \addtogroup cc2538-ccm-star
33 * @{
34 *
35 * \file
36 * Implementation of the AES-CCM* driver for the CC2538 SoC
37 */
38#include "contiki.h"
39#include "dev/ccm.h"
40#include "dev/cc2538-aes-128.h"
41#include "dev/cc2538-ccm-star.h"
42
43#include <stdint.h>
44#include <stdio.h>
45/*---------------------------------------------------------------------------*/
46#define MODULE_NAME "cc2538-ccm-star"
47
48#define CCM_STAR_LEN_LEN (CCM_NONCE_LEN_LEN - CCM_STAR_NONCE_LENGTH)
49
50#define DEBUG 0
51#if DEBUG
52#define PRINTF(...) printf(__VA_ARGS__)
53#else
54#define PRINTF(...)
55#endif
56/*---------------------------------------------------------------------------*/
57static uint8_t
58enable_crypto(void)
59{
60 uint8_t enabled = CRYPTO_IS_ENABLED();
61 if(!enabled) {
63 }
64 return enabled;
65}
66/*---------------------------------------------------------------------------*/
67static void
68restore_crypto(uint8_t enabled)
69{
70 if(!enabled) {
72 }
73}
74/*---------------------------------------------------------------------------*/
75static void
76set_key(const uint8_t *key)
77{
78 cc2538_aes_128_driver.set_key(key);
79}
80/*---------------------------------------------------------------------------*/
81static void
82aead(const uint8_t *nonce, uint8_t *m, uint16_t m_len, const uint8_t *a,
83 uint16_t a_len, uint8_t *result, uint8_t mic_len, int forward)
84{
85 uint16_t cdata_len;
86 uint8_t crypto_enabled, ret;
87
88 crypto_enabled = enable_crypto();
89
90 if(forward) {
91 ret = ccm_auth_encrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA,
92 nonce, a, a_len, m, m_len, m, mic_len, NULL);
93 if(ret != CRYPTO_SUCCESS) {
94 PRINTF("%s: ccm_auth_encrypt_start() error %u\n", MODULE_NAME, ret);
95 restore_crypto(crypto_enabled);
96 return;
97 }
98
100 ret = ccm_auth_encrypt_get_result(result, mic_len);
101 if(ret != CRYPTO_SUCCESS) {
102 PRINTF("%s: ccm_auth_encrypt_get_result() error %u\n", MODULE_NAME, ret);
103 }
104 } else {
105 if(m_len + mic_len > 0xffff) {
106 /* This implementation passes cdata_len as 16bit param to ccm_auth_* functions.
107 * Abort if m_len exceeds 0xffff - mic_len */
108 return;
109 }
110 cdata_len = m_len + mic_len;
111 ret = ccm_auth_decrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA,
112 nonce, a, a_len, m, cdata_len, m, mic_len,
113 NULL);
114 if(ret != CRYPTO_SUCCESS) {
115 PRINTF("%s: ccm_auth_decrypt_start() error %u\n", MODULE_NAME, ret);
116 restore_crypto(crypto_enabled);
117 return;
118 }
119
121 ret = ccm_auth_decrypt_get_result(m, cdata_len, result, mic_len);
122 if(ret != CRYPTO_SUCCESS) {
123 PRINTF("%s: ccm_auth_decrypt_get_result() error %u\n", MODULE_NAME, ret);
124 }
125 }
126
127 restore_crypto(crypto_enabled);
128}
129/*---------------------------------------------------------------------------*/
130const struct ccm_star_driver cc2538_ccm_star_driver = {
131 set_key,
132 aead
133};
134
135/** @} */
Header file of the AES-128 driver for the CC2538 SoC.
Header file of the AES-CCM* driver for the CC2538 SoC.
Header file for the cc2538 AES-CCM driver.
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.
#define ccm_auth_decrypt_check_status
Checks the status of the CCM authentication checking and decryption operation.
Definition: ccm.h:137
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
#define ccm_auth_encrypt_check_status
Checks the status of the CCM authentication and encryption operation.
Definition: ccm.h:98
void crypto_enable(void)
Enables the AES/SHA cryptoprocessor.
Definition: crypto.c:92
#define CRYPTO_IS_ENABLED()
Indicates whether the AES/SHA cryptoprocessor is enabled.
Definition: crypto.h:69
void crypto_disable(void)
Disables the AES/SHA cryptoprocessor.
Definition: crypto.c:101
void(* set_key)(const uint8_t *key)
Sets the current key.
Definition: aes-128.h:62
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:73
void(* set_key)(const uint8_t *key)
Sets the key in use.
Definition: ccm-star.h:62