Contiki-NG
Loading...
Searching...
No Matches
gcm.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-gcm
33 * @{
34 *
35 * \file
36 * Implementation of the cc2538 AES-GCM driver
37 */
38#include "contiki.h"
39#include "dev/rom-util.h"
40#include "dev/gcm.h"
41
42#include <stdbool.h>
43#include <stdint.h>
44/*---------------------------------------------------------------------------*/
45static uint8_t
46gcm_auth_crypt_start(uint8_t encrypt, uint8_t key_area, const void *iv,
47 const void *adata, uint16_t adata_len,
48 const void *data_in, void *data_out, uint16_t data_len,
49 struct process *process)
50{
51 uint32_t ctrl;
52 uint32_t aes_iv[AES_IV_LEN / sizeof(uint32_t)];
53
54 /* Program AES-GCM authentication/crypto operation */
55 ctrl = AES_AES_CTRL_SAVE_CONTEXT | /* Save context */
56 AES_AES_CTRL_GCM | /* GCM */
57 AES_AES_CTRL_CTR_WIDTH_32 | /* CTR width 32 */
58 AES_AES_CTRL_CTR | /* CTR */
59 (encrypt ? AES_AES_CTRL_DIRECTION_ENCRYPT : 0); /* En/decryption */
60
61 /* Prepare the crypto initialization vector
62 * Initialization vector */
63 rom_util_memcpy(aes_iv, iv, GCM_IV_LEN);
64 /* Initialize counter to 1 */
65 aes_iv[GCM_IV_LEN / sizeof(aes_iv[0])] = 0x01000000;
66
67 return aes_auth_crypt_start(ctrl, key_area, aes_iv, adata, adata_len,
68 data_in, data_out, data_len, process);
69}
70/*---------------------------------------------------------------------------*/
71static uint8_t
72gcm_auth_crypt_get_result(const void *tag_in, void *tag_out)
73{
74 uint32_t tag[AES_TAG_LEN / sizeof(uint32_t)];
75 uint8_t ret;
76
77 ret = aes_auth_crypt_get_result(NULL, tag);
78 if(ret != CRYPTO_SUCCESS) {
79 return ret;
80 }
81
82 if(tag_in != NULL) {
83 /* Check tag */
84 if(rom_util_memcmp(tag, tag_in, GCM_TAG_LEN)) {
85 ret = AES_AUTHENTICATION_FAILED;
86 }
87 }
88
89 if(tag_out != NULL) {
90 /* Copy tag */
91 rom_util_memcpy(tag_out, tag, GCM_TAG_LEN);
92 }
93
94 return ret;
95}
96/*---------------------------------------------------------------------------*/
97uint8_t
98gcm_auth_encrypt_start(uint8_t key_area, const void *iv, const void *adata,
99 uint16_t adata_len, const void *pdata,
100 uint16_t pdata_len, void *cdata, struct process *process)
101{
102 return gcm_auth_crypt_start(true, key_area, iv, adata, adata_len,
103 pdata, cdata, pdata_len, process);
104}
105/*---------------------------------------------------------------------------*/
106uint8_t
108{
109 return gcm_auth_crypt_get_result(NULL, tag);
110}
111/*---------------------------------------------------------------------------*/
112uint8_t
113gcm_auth_decrypt_start(uint8_t key_area, const void *iv, const void *adata,
114 uint16_t adata_len, const void *cdata,
115 uint16_t cdata_len, void *pdata, struct process *process)
116{
117 return gcm_auth_crypt_start(false, key_area, iv, adata, adata_len,
118 cdata, pdata, cdata_len, process);
119}
120/*---------------------------------------------------------------------------*/
121uint8_t
122gcm_auth_decrypt_get_result(const void *tag_in, void *tag_out)
123__attribute__ ((alias("gcm_auth_crypt_get_result")));
124
125/** @} */
Header file for the cc2538 AES-GCM driver.
#define AES_AES_CTRL_DIRECTION_ENCRYPT
Encrypt.
Definition aes.h:297
#define AES_AES_CTRL_GCM
AES-GCM mode.
Definition aes.h:280
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
#define AES_AES_CTRL_CTR_WIDTH_32
CTR counter width: 32 bits.
Definition aes.h:282
#define AES_AES_CTRL_CTR
AES-CTR mode.
Definition aes.h:290
uint8_t gcm_auth_encrypt_start(uint8_t key_area, const void *iv, const void *adata, uint16_t adata_len, const void *pdata, uint16_t pdata_len, void *cdata, struct process *process)
Starts a GCM authentication and encryption operation.
Definition gcm.c:98
uint8_t gcm_auth_decrypt_get_result(const void *tag_in, void *tag_out)
Gets the result of the GCM authentication checking and decryption operation.
uint8_t gcm_auth_encrypt_get_result(void *tag)
Gets the result of the GCM authentication and encryption operation.
Definition gcm.c:107
uint8_t gcm_auth_decrypt_start(uint8_t key_area, const void *iv, const void *adata, uint16_t adata_len, const void *cdata, uint16_t cdata_len, void *pdata, struct process *process)
Starts a GCM authentication checking and decryption operation.
Definition gcm.c:113
Header file for the cc2538 ROM utility function library driver.