Contiki-NG
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#include "dev/sys-ctrl.h"
44
45#include <stdint.h>
46#include <stdio.h>
47/*---------------------------------------------------------------------------*/
48#define MODULE_NAME "cc2538-aes-128"
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 uint8_t crypto_enabled, ret;
79
80 crypto_enabled = enable_crypto();
81
83 CC2538_AES_128_KEY_AREA);
84 if(ret != CRYPTO_SUCCESS) {
85 PRINTF("%s: aes_load_keys() error %u\n", MODULE_NAME, ret);
87 }
88
89 restore_crypto(crypto_enabled);
90}
91/*---------------------------------------------------------------------------*/
92static void
93encrypt(uint8_t *plaintext_and_result)
94{
95 uint8_t crypto_enabled, ret;
96 int8_t res;
97
98 crypto_enabled = enable_crypto();
99
100 ret = ecb_crypt_start(true, CC2538_AES_128_KEY_AREA, plaintext_and_result,
101 plaintext_and_result, AES_128_BLOCK_SIZE, NULL);
102 if(ret != CRYPTO_SUCCESS) {
103 PRINTF("%s: ecb_crypt_start() error %u\n", MODULE_NAME, ret);
105 }
106
107 while((res = ecb_crypt_check_status()) == CRYPTO_PENDING);
108 if(res != CRYPTO_SUCCESS) {
109 PRINTF("%s: ecb_crypt_check_status() error %d\n", MODULE_NAME, res);
111 }
112
113 restore_crypto(crypto_enabled);
114}
115/*---------------------------------------------------------------------------*/
116const struct aes_128_driver cc2538_aes_128_driver = {
117 set_key,
118 encrypt
119};
120
121/** @} */
Header file of the AES-128 driver for the CC2538 SoC.
Header file for the cc2538 AES-ECB driver.
#define AES_KEY_STORE_SIZE_KEY_SIZE_128
Key size: 128 bits.
Definition: aes.h:247
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
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
int8_t ecb_crypt_check_status(void)
Checks the status of the ECB crypto operation.
Definition: ecb.c:58
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
void sys_ctrl_reset()
Generates a warm reset through the SYS_CTRL_PWRDBG register.
Definition: sys-ctrl.c:114
Structure of AES drivers.
Definition: aes-128.h:57
void(* set_key)(const uint8_t *key)
Sets the current key.
Definition: aes-128.h:62
void(* encrypt)(uint8_t *plaintext_and_result)
Encrypts.
Definition: aes-128.h:67
Header file for the cc2538 System Control driver.