Mbed OS Reference
Loading...
Searching...
No Matches
TARGET_NUVOTON/TARGET_M460/ccm/ccm_alt.h
1/**
2 * \file ccm.h
3 *
4 * \brief This file provides an API for the CCM authenticated encryption
5 * mode for block ciphers.
6 *
7 * CCM combines Counter mode encryption with CBC-MAC authentication
8 * for 128-bit block ciphers.
9 *
10 * Input to CCM includes the following elements:
11 * <ul><li>Payload - data that is both authenticated and encrypted.</li>
12 * <li>Associated data (Adata) - data that is authenticated but not
13 * encrypted, For example, a header.</li>
14 * <li>Nonce - A unique value that is assigned to the payload and the
15 * associated data.</li></ul>
16 *
17 * Definition of CCM:
18 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
19 * RFC 3610 "Counter with CBC-MAC (CCM)"
20 *
21 * Related:
22 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
23 *
24 * Definition of CCM*:
25 * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks
26 * Integer representation is fixed most-significant-octet-first order and
27 * the representation of octets is most-significant-bit-first order. This is
28 * consistent with RFC 3610.
29 */
30/*
31 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
32 * Copyright (c) 2022, Nuvoton Technology Corporation
33 * SPDX-License-Identifier: Apache-2.0
34 *
35 * Licensed under the Apache License, Version 2.0 (the "License"); you may
36 * not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
38 *
39 * http://www.apache.org/licenses/LICENSE-2.0
40 *
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
43 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
46 *
47 * This file is part of Mbed TLS (https://tls.mbed.org)
48 */
49
50#ifndef MBEDTLS_CCM_ALT_H
51#define MBEDTLS_CCM_ALT_H
52
53#if !defined(MBEDTLS_CONFIG_FILE)
54#include "config.h"
55#else
56#include MBEDTLS_CONFIG_FILE
57#endif
58
59#include "mbed_toolchain.h"
60#include "mbedtls/cipher.h"
61#include "NuMicro.h"
62
63#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
64#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
65
66/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
67#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73// Regular implementation
74//
75
76#define MAX_CCM_BUF 256
77#define CCM_PBLOCK_SIZE MAX_CCM_BUF
78
79/**
80 * \brief The CCM context-type definition. The CCM context is passed
81 * to the APIs called.
82 */
83typedef struct mbedtls_ccm_context
84{
85 MBED_ALIGN(4) uint8_t ccm_buf[MAX_CCM_BUF + 16]; /* 16 bytes for ctr0 in packer */
86 MBED_ALIGN(4) uint8_t out_buf[MAX_CCM_BUF + 16]; /* 16 bytes for tag */
87 MBED_ALIGN(4) uint8_t fb_buf[72]; /* feedback buffer for GCM DMA */
88 uint32_t keySize;
89 uint32_t keys[8]; /* Cipher key */
90 uint32_t encDec; /* 0: decrypt, 1: encrypt */
91 uint32_t opMode; /* AES_MODE CCM */
92
93}
95
96/**
97 * \brief This function initializes the specified CCM context,
98 * to make references valid, and prepare the context
99 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
100 *
101 * \param ctx The CCM context to initialize. This must not be \c NULL.
102 */
103void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
104
105/**
106 * \brief This function initializes the CCM context set in the
107 * \p ctx parameter and sets the encryption key.
108 *
109 * \param ctx The CCM context to initialize. This must be an initialized
110 * context.
111 * \param cipher The 128-bit block cipher to use.
112 * \param key The encryption key. This must not be \c NULL.
113 * \param keybits The key size in bits. This must be acceptable by the cipher.
114 *
115 * \return \c 0 on success.
116 * \return A CCM or cipher-specific error code on failure.
117 */
118int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
119 mbedtls_cipher_id_t cipher,
120 const unsigned char *key,
121 unsigned int keybits );
122
123/**
124 * \brief This function releases and clears the specified CCM context
125 * and underlying cipher sub-context.
126 *
127 * \param ctx The CCM context to clear. If this is \c NULL, the function
128 * has no effect. Otherwise, this must be initialized.
129 */
130void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
131
132/**
133 * \brief This function encrypts a buffer using CCM.
134 *
135 * \note The tag is written to a separate buffer. To concatenate
136 * the \p tag with the \p output, as done in <em>RFC-3610:
137 * Counter with CBC-MAC (CCM)</em>, use
138 * \p tag = \p output + \p length, and make sure that the
139 * output buffer is at least \p length + \p tag_len wide.
140 *
141 * \param ctx The CCM context to use for encryption. This must be
142 * initialized and bound to a key.
143 * \param length The length of the input data in Bytes.
144 * \param iv The initialization vector (nonce). This must be a readable
145 * buffer of at least \p iv_len Bytes.
146 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
147 * or 13. The length L of the message length field is
148 * 15 - \p iv_len.
149 * \param add The additional data field. If \p add_len is greater than
150 * zero, \p add must be a readable buffer of at least that
151 * length.
152 * \param add_len The length of additional data in Bytes.
153 * This must be less than `2^16 - 2^8`.
154 * \param input The buffer holding the input data. If \p length is greater
155 * than zero, \p input must be a readable buffer of at least
156 * that length.
157 * \param output The buffer holding the output data. If \p length is greater
158 * than zero, \p output must be a writable buffer of at least
159 * that length.
160 * \param tag The buffer holding the authentication field. This must be a
161 * readable buffer of at least \p tag_len Bytes.
162 * \param tag_len The length of the authentication field to generate in Bytes:
163 * 4, 6, 8, 10, 12, 14 or 16.
164 *
165 * \return \c 0 on success.
166 * \return A CCM or cipher-specific error code on failure.
167 */
168int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
169 const unsigned char *iv, size_t iv_len,
170 const unsigned char *add, size_t add_len,
171 const unsigned char *input, unsigned char *output,
172 unsigned char *tag, size_t tag_len );
173
174/**
175 * \brief This function encrypts a buffer using CCM*.
176 *
177 * \note The tag is written to a separate buffer. To concatenate
178 * the \p tag with the \p output, as done in <em>RFC-3610:
179 * Counter with CBC-MAC (CCM)</em>, use
180 * \p tag = \p output + \p length, and make sure that the
181 * output buffer is at least \p length + \p tag_len wide.
182 *
183 * \note When using this function in a variable tag length context,
184 * the tag length has to be encoded into the \p iv passed to
185 * this function.
186 *
187 * \param ctx The CCM context to use for encryption. This must be
188 * initialized and bound to a key.
189 * \param length The length of the input data in Bytes.
190 * \param iv The initialization vector (nonce). This must be a readable
191 * buffer of at least \p iv_len Bytes.
192 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
193 * or 13. The length L of the message length field is
194 * 15 - \p iv_len.
195 * \param add The additional data field. This must be a readable buffer of
196 * at least \p add_len Bytes.
197 * \param add_len The length of additional data in Bytes.
198 * This must be less than 2^16 - 2^8.
199 * \param input The buffer holding the input data. If \p length is greater
200 * than zero, \p input must be a readable buffer of at least
201 * that length.
202 * \param output The buffer holding the output data. If \p length is greater
203 * than zero, \p output must be a writable buffer of at least
204 * that length.
205 * \param tag The buffer holding the authentication field. This must be a
206 * readable buffer of at least \p tag_len Bytes.
207 * \param tag_len The length of the authentication field to generate in Bytes:
208 * 0, 4, 6, 8, 10, 12, 14 or 16.
209 *
210 * \warning Passing \c 0 as \p tag_len means that the message is no
211 * longer authenticated.
212 *
213 * \return \c 0 on success.
214 * \return A CCM or cipher-specific error code on failure.
215 */
216int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
217 const unsigned char *iv, size_t iv_len,
218 const unsigned char *add, size_t add_len,
219 const unsigned char *input, unsigned char *output,
220 unsigned char *tag, size_t tag_len );
221
222/**
223 * \brief This function performs a CCM authenticated decryption of a
224 * buffer.
225 *
226 * \param ctx The CCM context to use for decryption. This must be
227 * initialized and bound to a key.
228 * \param length The length of the input data in Bytes.
229 * \param iv The initialization vector (nonce). This must be a readable
230 * buffer of at least \p iv_len Bytes.
231 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
232 * or 13. The length L of the message length field is
233 * 15 - \p iv_len.
234 * \param add The additional data field. This must be a readable buffer
235 * of at least that \p add_len Bytes..
236 * \param add_len The length of additional data in Bytes.
237 * This must be less than 2^16 - 2^8.
238 * \param input The buffer holding the input data. If \p length is greater
239 * than zero, \p input must be a readable buffer of at least
240 * that length.
241 * \param output The buffer holding the output data. If \p length is greater
242 * than zero, \p output must be a writable buffer of at least
243 * that length.
244 * \param tag The buffer holding the authentication field. This must be a
245 * readable buffer of at least \p tag_len Bytes.
246 * \param tag_len The length of the authentication field to generate in Bytes:
247 * 4, 6, 8, 10, 12, 14 or 16.
248 *
249 * \return \c 0 on success. This indicates that the message is authentic.
250 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
251 * \return A cipher-specific error code on calculation failure.
252 */
253int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
254 const unsigned char *iv, size_t iv_len,
255 const unsigned char *add, size_t add_len,
256 const unsigned char *input, unsigned char *output,
257 const unsigned char *tag, size_t tag_len );
258
259/**
260 * \brief This function performs a CCM* authenticated decryption of a
261 * buffer.
262 *
263 * \note When using this function in a variable tag length context,
264 * the tag length has to be decoded from \p iv and passed to
265 * this function as \p tag_len. (\p tag needs to be adjusted
266 * accordingly.)
267 *
268 * \param ctx The CCM context to use for decryption. This must be
269 * initialized and bound to a key.
270 * \param length The length of the input data in Bytes.
271 * \param iv The initialization vector (nonce). This must be a readable
272 * buffer of at least \p iv_len Bytes.
273 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
274 * or 13. The length L of the message length field is
275 * 15 - \p iv_len.
276 * \param add The additional data field. This must be a readable buffer of
277 * at least that \p add_len Bytes.
278 * \param add_len The length of additional data in Bytes.
279 * This must be less than 2^16 - 2^8.
280 * \param input The buffer holding the input data. If \p length is greater
281 * than zero, \p input must be a readable buffer of at least
282 * that length.
283 * \param output The buffer holding the output data. If \p length is greater
284 * than zero, \p output must be a writable buffer of at least
285 * that length.
286 * \param tag The buffer holding the authentication field. This must be a
287 * readable buffer of at least \p tag_len Bytes.
288 * \param tag_len The length of the authentication field in Bytes.
289 * 0, 4, 6, 8, 10, 12, 14 or 16.
290 *
291 * \warning Passing \c 0 as \p tag_len means that the message is nos
292 * longer authenticated.
293 *
294 * \return \c 0 on success.
295 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
296 * \return A cipher-specific error code on calculation failure.
297 */
298int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
299 const unsigned char *iv, size_t iv_len,
300 const unsigned char *add, size_t add_len,
301 const unsigned char *input, unsigned char *output,
302 const unsigned char *tag, size_t tag_len );
303
304#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
305/**
306 * \brief The CCM checkup routine.
307 *
308 * \return \c 0 on success.
309 * \return \c 1 on failure.
310 */
311int mbedtls_ccm_self_test( int verbose );
312#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
313
314#ifdef __cplusplus
315}
316#endif
317
318#endif /* MBEDTLS_CCM_ALT_H */
This file contains an abstraction interface for use with the cipher primitives provided by the librar...
Configuration options (set of defines)
mbedtls_cipher_id_t
Supported cipher types.
Definition: cipher.h:89
#define MBED_ALIGN(N)
MBED_ALIGN(N) Declare a variable to be aligned on an N-byte boundary.
The CCM context-type definition.