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 /*---------------------------------------------------------------------------*/
57 static uint8_t
58 enable_crypto(void)
59 {
60  uint8_t enabled = CRYPTO_IS_ENABLED();
61  if(!enabled) {
62  crypto_enable();
63  }
64  return enabled;
65 }
66 /*---------------------------------------------------------------------------*/
67 static void
68 restore_crypto(uint8_t enabled)
69 {
70  if(!enabled) {
72  }
73 }
74 /*---------------------------------------------------------------------------*/
75 static void
76 set_key(const uint8_t *key)
77 {
78  cc2538_aes_128_driver.set_key(key);
79 }
80 /*---------------------------------------------------------------------------*/
81 static void
82 aead(const uint8_t *nonce, uint8_t *m, uint8_t m_len, const uint8_t *a,
83  uint8_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  cdata_len = m_len + mic_len;
106  ret = ccm_auth_decrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA,
107  nonce, a, a_len, m, cdata_len, m, mic_len,
108  NULL);
109  if(ret != CRYPTO_SUCCESS) {
110  PRINTF("%s: ccm_auth_decrypt_start() error %u\n", MODULE_NAME, ret);
111  restore_crypto(crypto_enabled);
112  return;
113  }
114 
116  ret = ccm_auth_decrypt_get_result(m, cdata_len, result, mic_len);
117  if(ret != CRYPTO_SUCCESS) {
118  PRINTF("%s: ccm_auth_decrypt_get_result() error %u\n", MODULE_NAME, ret);
119  }
120  }
121 
122  restore_crypto(crypto_enabled);
123 }
124 /*---------------------------------------------------------------------------*/
125 const struct ccm_star_driver cc2538_ccm_star_driver = {
126  set_key,
127  aead
128 };
129 
130 /** @} */
Structure of CCM* drivers.
Definition: ccm-star.h:56
#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_get_result(void *mic, uint8_t mic_len)
Gets the result of the CCM authentication and encryption operation.
Definition: ccm.c:120
Header file of the AES-128 driver for the CC2538 SoC.
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_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
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
Header file of the AES-CCM* driver for the CC2538 SoC.
#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(* aead)(const uint8_t *nonce, uint8_t *m, uint8_t m_len, const uint8_t *a, uint8_t a_len, uint8_t *result, uint8_t mic_len, int forward)
Combines authentication and encryption.
Definition: ccm-star.h:73
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
void(* set_key)(const uint8_t *key)
Sets the key in use.
Definition: ccm-star.h:62
Header file for the cc2538 AES-CCM driver.
void(* set_key)(const uint8_t *key)
Sets the current key.
Definition: aes-128.h:62