Contiki 3.x
cc2538-aes-128.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Hasso-Plattner-Institut.
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  * \addtogroup cc2538-aes-128
33  * @{
34  *
35  * \file
36  * Implementation of the AES-128 driver for the CC2538 SoC
37  * \author
38  * Konrad Krentz <konrad.krentz@gmail.com>
39  */
40 #include "contiki.h"
41 #include "dev/ecb.h"
42 #include "dev/cc2538-aes-128.h"
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 /*---------------------------------------------------------------------------*/
47 #define MODULE_NAME "cc2538-aes-128"
48 
49 #define DEBUG 0
50 #if DEBUG
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #else
53 #define PRINTF(...)
54 #endif
55 /*---------------------------------------------------------------------------*/
56 static uint8_t
57 enable_crypto(void)
58 {
59  uint8_t enabled = CRYPTO_IS_ENABLED();
60  if(!enabled) {
61  crypto_enable();
62  }
63  return enabled;
64 }
65 /*---------------------------------------------------------------------------*/
66 static void
67 restore_crypto(uint8_t enabled)
68 {
69  if(!enabled) {
71  }
72 }
73 /*---------------------------------------------------------------------------*/
74 static void
75 set_key(const uint8_t *key)
76 {
77  uint8_t crypto_enabled, ret;
78 
79  crypto_enabled = enable_crypto();
80 
82  CC2538_AES_128_KEY_AREA);
83  if(ret != CRYPTO_SUCCESS) {
84  PRINTF("%s: aes_load_keys() error %u\n", MODULE_NAME, ret);
85  }
86 
87  restore_crypto(crypto_enabled);
88 }
89 /*---------------------------------------------------------------------------*/
90 static void
91 encrypt(uint8_t *plaintext_and_result)
92 {
93  uint8_t crypto_enabled, ret;
94  int8_t res;
95 
96  crypto_enabled = enable_crypto();
97 
98  ret = ecb_crypt_start(true, CC2538_AES_128_KEY_AREA, plaintext_and_result,
99  plaintext_and_result, AES_128_BLOCK_SIZE, NULL);
100  if(ret != CRYPTO_SUCCESS) {
101  PRINTF("%s: ecb_crypt_start() error %u\n", MODULE_NAME, ret);
102  restore_crypto(crypto_enabled);
103  return;
104  }
105 
106  while((res = ecb_crypt_check_status()) == CRYPTO_PENDING);
107  if(res != CRYPTO_SUCCESS) {
108  PRINTF("%s: ecb_crypt_check_status() error %d\n", MODULE_NAME, res);
109  }
110 
111  restore_crypto(crypto_enabled);
112 }
113 /*---------------------------------------------------------------------------*/
114 const struct aes_128_driver cc2538_aes_128_driver = {
115  set_key,
116  encrypt
117 };
118 
119 /** @} */
int8_t ecb_crypt_check_status(void)
Checks the status of the ECB crypto operation.
Definition: ecb.c:58
Header file of the AES-128 driver for the CC2538 SoC.
Header file for the cc2538 AES-ECB driver.
void(* set_key)(const uint8_t *key)
Sets the current key.
Definition: aes-128.h:62
void crypto_enable(void)
Enables the AES/SHA cryptoprocessor.
Definition: crypto.c:97
Structure of AES drivers.
Definition: aes-128.h:57
#define AES_KEY_STORE_SIZE_KEY_SIZE_128
Key size: 128 bits.
Definition: aes.h:247
void(* encrypt)(uint8_t *plaintext_and_result)
Encrypts.
Definition: aes-128.h:67
void crypto_disable(void)
Disables the AES/SHA cryptoprocessor.
Definition: crypto.c:106
#define NULL
The null pointer.
uint8_t aes_load_keys(const void *keys, uint8_t key_size, uint8_t count, uint8_t start_area)
Writes keys into the Key RAM.
Definition: aes.c:53
#define CRYPTO_IS_ENABLED()
Indicates whether the AES/SHA cryptoprocessor is enabled.
Definition: crypto.h:69
uint8_t ecb_crypt_start(uint8_t encrypt, uint8_t key_area, const void *mdata_in, void *mdata_out, uint16_t mdata_len, struct process *process)
Starts an ECB crypto operation.
Definition: ecb.c:45