Contiki-NG
ctr.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-ctr
33 * @{
34 *
35 * \file
36 * Implementation of the cc2538 AES-CTR driver
37 */
38#include "contiki.h"
39#include "dev/rom-util.h"
40#include "dev/ctr.h"
41
42#include <stdbool.h>
43#include <stdint.h>
44/*---------------------------------------------------------------------------*/
45uint8_t
46ctr_crypt_start(uint8_t encrypt, uint8_t key_area, const void *nonce,
47 const void *ictr, uint8_t ctr_len, const void *mdata_in,
48 void *mdata_out, uint16_t mdata_len, struct process *process)
49{
50 uint32_t ctrl;
51 uint32_t iv[AES_IV_LEN / sizeof(uint32_t)];
52 uint8_t nonce_len;
53
54 /* Program AES-CTR crypto operation */
55 ctrl = (((ctr_len >> 2) - 1) << AES_AES_CTRL_CTR_WIDTH_S) | /* CTR width */
56 AES_AES_CTRL_CTR | /* CTR */
57 (encrypt ? AES_AES_CTRL_DIRECTION_ENCRYPT : 0); /* En/decryption */
58
59 /* Prepare the crypto initialization vector */
60 nonce_len = AES_IV_LEN - ctr_len;
61 /* Nonce */
62 rom_util_memcpy(&((uint8_t *)iv)[0], nonce, nonce_len);
63 /* Initial counter */
64 rom_util_memcpy(&((uint8_t *)iv)[nonce_len], ictr, ctr_len);
65
66 return aes_auth_crypt_start(ctrl, key_area, iv, NULL, 0,
67 mdata_in, mdata_out, mdata_len, process);
68}
69/*---------------------------------------------------------------------------*/
70int8_t
72{
74 CRYPTO_PENDING;
75}
76
77/** @} */
Header file for the cc2538 AES-CTR driver.
#define AES_AES_CTRL_DIRECTION_ENCRYPT
Encrypt.
Definition: aes.h:297
#define AES_AES_CTRL_CTR_WIDTH_S
CTR counter width shift.
Definition: aes.h:289
uint8_t aes_auth_crypt_check_status(void)
Checks the status of the AES authentication/crypto operation.
Definition: aes.c:270
uint8_t aes_auth_crypt_start(uint32_t ctrl, uint8_t key_area, const void *iv, const void *adata, uint16_t adata_len, const void *data_in, void *data_out, uint16_t data_len, struct process *process)
Starts an AES authentication/crypto operation.
Definition: aes.c:159
uint8_t aes_auth_crypt_get_result(void *iv, void *tag)
Gets the result of the AES authentication/crypto operation.
Definition: aes.c:278
#define AES_AES_CTRL_CTR
AES-CTR mode.
Definition: aes.h:290
uint8_t ctr_crypt_start(uint8_t encrypt, uint8_t key_area, const void *nonce, const void *ictr, uint8_t ctr_len, const void *mdata_in, void *mdata_out, uint16_t mdata_len, struct process *process)
Starts a CTR crypto operation.
Definition: ctr.c:46
int8_t ctr_crypt_check_status(void)
Checks the status of the CTR crypto operation.
Definition: ctr.c:71
Header file for the cc2538 ROM utility function library driver.