Contiki-NG
jn516x-ccm-star.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, SICS Swedish ICT.
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  * CCM* header implementation exploiting the JN516x
36  * cryptographic co-processor
37  * \author
38  * Simon Duquennoy <simonduq@sics.se>
39  */
40 
41 #include "ccm-star.h"
42 #include <AHI_AES.h>
43 #include <string.h>
44 
45 static tsReg128 current_key;
46 static int current_key_is_new = 1;
47 
48 /*---------------------------------------------------------------------------*/
49 static void
50 aead(const uint8_t *nonce,
51  uint8_t *m, uint8_t m_len,
52  const uint8_t *a, uint8_t a_len,
53  uint8_t *result, uint8_t mic_len,
54  int forward)
55 {
56  tsReg128 nonce_aligned;
57  memcpy(&nonce_aligned, nonce, sizeof(nonce_aligned));
58  if(forward) {
59  bACI_CCMstar(
60  &current_key,
61  current_key_is_new,
62  XCV_REG_AES_SET_MODE_CCM,
63  mic_len,
64  a_len,
65  m_len,
66  &nonce_aligned,
67  (uint8_t *)a,
68  (uint8_t *)m,
69  (uint8_t *)m,
70  result,
71  NULL
72  );
73  } else {
74  bool_t auth;
75  bACI_CCMstar(
76  &current_key,
77  current_key_is_new,
78  XCV_REG_AES_SET_MODE_CCM_D,
79  mic_len,
80  a_len,
81  m_len,
82  &nonce_aligned,
83  (uint8_t *)a,
84  (uint8_t *)m,
85  (uint8_t *)m,
86  (uint8_t *)a + a_len + m_len,
87  &auth
88  );
89  /* To comply with the CCM_STAR interface, copy MIC to result in case of success */
90  if(result != NULL) {
91  if(auth) {
92  memcpy(result, a + a_len + m_len, mic_len);
93  } else {
94  /* Otherwise, corrupt the result */
95  memcpy(result, a + a_len + m_len, mic_len);
96  result[0]++;
97  }
98  }
99  }
100 
101  current_key_is_new = 0;
102 }
103 /*---------------------------------------------------------------------------*/
104 static void
105 set_key(const uint8_t *key)
106 {
107  if(memcmp(&current_key, key, sizeof(current_key)) == 0) {
108  current_key_is_new = 0;
109  } else {
110  memcpy(&current_key, key, sizeof(current_key));
111  current_key_is_new = 1;
112  }
113 }
114 /*---------------------------------------------------------------------------*/
115 const struct ccm_star_driver ccm_star_driver_jn516x = {
116  set_key,
117  aead
118 };
119 /*---------------------------------------------------------------------------*/
Structure of CCM* drivers.
Definition: ccm-star.h:56
CCM* header file.
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
void(* set_key)(const uint8_t *key)
Sets the key in use.
Definition: ccm-star.h:62