Contiki-NG
cc26xx-aes.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk
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 /**
30  * \addtogroup cc26xx-aes
31  * @{
32  *
33  * \file
34  * Implementation of the AES driver for the CC26x0/CC13x0 SoC
35  * \author
36  * Atis Elsts <atis.elsts@gmail.com>
37  */
38 #include "contiki.h"
39 #include "dev/cc26xx-aes.h"
40 #include "ti-lib.h"
41 /*---------------------------------------------------------------------------*/
42 #include "sys/log.h"
43 #define LOG_MODULE "cc26xx-aes"
44 #define LOG_LEVEL LOG_LEVEL_MAIN
45 /*---------------------------------------------------------------------------*/
46 static uint32_t skey[AES_128_KEY_LENGTH / sizeof(uint32_t)];
47 
48 /*---------------------------------------------------------------------------*/
49 void
50 cc26xx_aes_set_key(const uint8_t *key)
51 {
52  memcpy(skey, key, AES_128_KEY_LENGTH);
53 }
54 /*---------------------------------------------------------------------------*/
55 static void
56 encrypt_decrypt(uint8_t *plaintext_and_result, bool do_encrypt)
57 {
58  uint32_t result[AES_128_BLOCK_SIZE / sizeof(uint32_t)];
59  unsigned status;
60  int i;
61 
62  /* First, make sure the PERIPH PD is on */
63  ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH);
64  while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH)
65  != PRCM_DOMAIN_POWER_ON));
66 
67  /* Enable CRYPTO peripheral */
68  ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_CRYPTO);
69  ti_lib_prcm_load_set();
70  while(!ti_lib_prcm_load_get());
71 
72  status = ti_lib_crypto_aes_load_key(skey, CRYPTO_KEY_AREA_0);
73  if(status != AES_SUCCESS) {
74  LOG_WARN("load key failed: %u\n", status);
75  } else {
76 
77  status = ti_lib_crypto_aes_ecb((uint32_t *)plaintext_and_result, result, CRYPTO_KEY_AREA_0, do_encrypt, false);
78  if(status != AES_SUCCESS) {
79  LOG_WARN("ecb failed: %u\n", status);
80  } else {
81 
82  for(i = 0; i < 100; ++i) {
83  ti_lib_cpu_delay(10);
84  status = ti_lib_crypto_aes_ecb_status();
85  if(status != AES_DMA_BSY) {
86  break;
87  }
88  }
89 
90  ti_lib_crypto_aes_ecb_finish();
91 
92  if(status != AES_SUCCESS) {
93  LOG_WARN("ecb get result failed: %u\n", status);
94  }
95  }
96  }
97 
98  ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_CRYPTO);
99  ti_lib_prcm_load_set();
100  while(!ti_lib_prcm_load_get());
101 
102  if(status == AES_SUCCESS) {
103  memcpy(plaintext_and_result, result, AES_128_BLOCK_SIZE);
104  } else {
105  /* corrupt the result */
106  plaintext_and_result[0] ^= 1;
107  }
108 }
109 /*---------------------------------------------------------------------------*/
110 void
111 cc26xx_aes_encrypt(uint8_t *plaintext_and_result)
112 {
113  encrypt_decrypt(plaintext_and_result, true);
114 }
115 /*---------------------------------------------------------------------------*/
116 void
117 cc26xx_aes_decrypt(uint8_t *cyphertext_and_result)
118 {
119  encrypt_decrypt(cyphertext_and_result, false);
120 }
121 /*---------------------------------------------------------------------------*/
122 const struct aes_128_driver cc26xx_aes_128_driver = {
125 };
126 
127 /** @} */
Header file with macros which rename TI CC26xxware functions.
void cc26xx_aes_encrypt(uint8_t *plaintext_and_result)
Encrypt a message using the SoC AES-128 hardware implementation.
Definition: cc26xx-aes.c:111
Structure of AES drivers.
Definition: aes-128.h:57
void cc26xx_aes_set_key(const uint8_t *key)
Set a key to use in subsequent encryption & decryption operations.
Definition: cc26xx-aes.c:50
void cc26xx_aes_decrypt(uint8_t *cyphertext_and_result)
Decrypt a message using the SoC AES-128 hardware implementation.
Definition: cc26xx-aes.c:117
Header file for the logging system
Header file of the AES-128 driver for the CC26xx SoC.