Contiki-NG
cbc-mac.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, 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-cbc-mac
33 * @{
34 *
35 * \file
36 * Implementation of the cc2538 AES-CBC-MAC driver
37 */
38#include "contiki.h"
39#include "dev/rom-util.h"
40#include "dev/cbc-mac.h"
41
42#include <stdbool.h>
43#include <stdint.h>
44/*---------------------------------------------------------------------------*/
45uint8_t
46cbc_mac_auth_start(uint8_t key_area, const void *mdata, uint16_t mdata_len,
47 struct process *process)
48{
49 uint32_t ctrl;
50 uint32_t iv[AES_IV_LEN / sizeof(uint32_t)];
51
52 /* Program AES-CBC-MAC authentication operation */
53 ctrl = AES_AES_CTRL_SAVE_CONTEXT | /* Save context */
54 AES_AES_CTRL_CBC_MAC | /* CBC-MAC */
55 AES_AES_CTRL_DIRECTION_ENCRYPT; /* Encryption */
56
57 /* Prepare the crypto initialization vector
58 * Set initialization vector to 0 */
59 rom_util_memset(iv, 0, AES_IV_LEN);
60
61 return aes_auth_crypt_start(ctrl, key_area, iv, NULL, 0,
62 mdata, NULL, mdata_len, process);
63}
64/*---------------------------------------------------------------------------*/
65uint8_t
66cbc_mac_auth_get_result(const void *mac_in, void *mac_out)
67{
68 uint32_t tag[AES_TAG_LEN / sizeof(uint32_t)];
69 uint8_t ret;
70
71 ret = aes_auth_crypt_get_result(NULL, tag);
72 if(ret != CRYPTO_SUCCESS) {
73 return ret;
74 }
75
76 if(mac_in != NULL) {
77 /* Check MAC */
78 if(rom_util_memcmp(tag, mac_in, CBC_MAC_MAC_LEN)) {
79 ret = AES_AUTHENTICATION_FAILED;
80 }
81 }
82
83 if(mac_out != NULL) {
84 /* Copy tag to MAC */
85 rom_util_memcpy(mac_out, tag, CBC_MAC_MAC_LEN);
86 }
87
88 return ret;
89}
90
91/** @} */
Header file for the cc2538 AES-CBC-MAC driver.
#define AES_AES_CTRL_DIRECTION_ENCRYPT
Encrypt.
Definition: aes.h:297
#define AES_AES_CTRL_CBC_MAC
AES-CBC MAC mode.
Definition: aes.h:281
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_SAVE_CONTEXT
Auth.
Definition: aes.h:273
uint8_t cbc_mac_auth_start(uint8_t key_area, const void *mdata, uint16_t mdata_len, struct process *process)
Starts a CBC-MAC authentication operation.
Definition: cbc-mac.c:46
uint8_t cbc_mac_auth_get_result(const void *mac_in, void *mac_out)
Gets the result of the CBC-MAC authentication operation.
Definition: cbc-mac.c:66
Header file for the cc2538 ROM utility function library driver.