Mbed OS Reference
Loading...
Searching...
No Matches
ccm.h
Go to the documentation of this file.
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 The Mbed TLS Contributors
32 * SPDX-License-Identifier: Apache-2.0
33 *
34 * Licensed under the Apache License, Version 2.0 (the "License"); you may
35 * not use this file except in compliance with the License.
36 * You may obtain a copy of the License at
37 *
38 * http://www.apache.org/licenses/LICENSE-2.0
39 *
40 * Unless required by applicable law or agreed to in writing, software
41 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
42 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 * See the License for the specific language governing permissions and
44 * limitations under the License.
45 */
46
47#ifndef MBEDTLS_CCM_H
48#define MBEDTLS_CCM_H
49
50#if !defined(MBEDTLS_CONFIG_FILE)
51#include "mbedtls/config.h"
52#else
53#include MBEDTLS_CONFIG_FILE
54#endif
55
56#include "mbedtls/cipher.h"
57
58#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
59#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
60
61/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
62#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68/**
69 * \addtogroup mbedtls
70 * \{
71 * \defgroup mbedtls_ccm_module CCM
72 * \{
73 */
74
75#if !defined(MBEDTLS_CCM_ALT)
76// Regular implementation
77//
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 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
86}
88
89#else /* MBEDTLS_CCM_ALT */
90#include "ccm_alt.h"
91#endif /* MBEDTLS_CCM_ALT */
92
93/**
94 * \brief This function initializes the specified CCM context,
95 * to make references valid, and prepare the context
96 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
97 *
98 * \param ctx The CCM context to initialize. This must not be \c NULL.
99 */
101
102/**
103 * \brief This function initializes the CCM context set in the
104 * \p ctx parameter and sets the encryption key.
105 *
106 * \param ctx The CCM context to initialize. This must be an initialized
107 * context.
108 * \param cipher The 128-bit block cipher to use.
109 * \param key The encryption key. This must not be \c NULL.
110 * \param keybits The key size in bits. This must be acceptable by the cipher.
111 *
112 * \return \c 0 on success.
113 * \return A CCM or cipher-specific error code on failure.
114 */
116 mbedtls_cipher_id_t cipher,
117 const unsigned char *key,
118 unsigned int keybits );
119
120/**
121 * \brief This function releases and clears the specified CCM context
122 * and underlying cipher sub-context.
123 *
124 * \param ctx The CCM context to clear. If this is \c NULL, the function
125 * has no effect. Otherwise, this must be initialized.
126 */
128
129/**
130 * \brief This function encrypts a buffer using CCM.
131 *
132 * \note The tag is written to a separate buffer. To concatenate
133 * the \p tag with the \p output, as done in <em>RFC-3610:
134 * Counter with CBC-MAC (CCM)</em>, use
135 * \p tag = \p output + \p length, and make sure that the
136 * output buffer is at least \p length + \p tag_len wide.
137 *
138 * \param ctx The CCM context to use for encryption. This must be
139 * initialized and bound to a key.
140 * \param length The length of the input data in Bytes.
141 * \param iv The initialization vector (nonce). This must be a readable
142 * buffer of at least \p iv_len Bytes.
143 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
144 * or 13. The length L of the message length field is
145 * 15 - \p iv_len.
146 * \param add The additional data field. If \p add_len is greater than
147 * zero, \p add must be a readable buffer of at least that
148 * length.
149 * \param add_len The length of additional data in Bytes.
150 * This must be less than `2^16 - 2^8`.
151 * \param input The buffer holding the input data. If \p length is greater
152 * than zero, \p input must be a readable buffer of at least
153 * that length.
154 * \param output The buffer holding the output data. If \p length is greater
155 * than zero, \p output must be a writable buffer of at least
156 * that length.
157 * \param tag The buffer holding the authentication field. This must be a
158 * writable buffer of at least \p tag_len Bytes.
159 * \param tag_len The length of the authentication field to generate in Bytes:
160 * 4, 6, 8, 10, 12, 14 or 16.
161 *
162 * \return \c 0 on success.
163 * \return A CCM or cipher-specific error code on failure.
164 */
166 const unsigned char *iv, size_t iv_len,
167 const unsigned char *add, size_t add_len,
168 const unsigned char *input, unsigned char *output,
169 unsigned char *tag, size_t tag_len );
170
171/**
172 * \brief This function encrypts a buffer using CCM*.
173 *
174 * \note The tag is written to a separate buffer. To concatenate
175 * the \p tag with the \p output, as done in <em>RFC-3610:
176 * Counter with CBC-MAC (CCM)</em>, use
177 * \p tag = \p output + \p length, and make sure that the
178 * output buffer is at least \p length + \p tag_len wide.
179 *
180 * \note When using this function in a variable tag length context,
181 * the tag length has to be encoded into the \p iv passed to
182 * this function.
183 *
184 * \param ctx The CCM context to use for encryption. This must be
185 * initialized and bound to a key.
186 * \param length The length of the input data in Bytes.
187 * \param iv The initialization vector (nonce). This must be a readable
188 * buffer of at least \p iv_len Bytes.
189 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
190 * or 13. The length L of the message length field is
191 * 15 - \p iv_len.
192 * \param add The additional data field. This must be a readable buffer of
193 * at least \p add_len Bytes.
194 * \param add_len The length of additional data in Bytes.
195 * This must be less than 2^16 - 2^8.
196 * \param input The buffer holding the input data. If \p length is greater
197 * than zero, \p input must be a readable buffer of at least
198 * that length.
199 * \param output The buffer holding the output data. If \p length is greater
200 * than zero, \p output must be a writable buffer of at least
201 * that length.
202 * \param tag The buffer holding the authentication field. This must be a
203 * writable buffer of at least \p tag_len Bytes.
204 * \param tag_len The length of the authentication field to generate in Bytes:
205 * 0, 4, 6, 8, 10, 12, 14 or 16.
206 *
207 * \warning Passing \c 0 as \p tag_len means that the message is no
208 * longer authenticated.
209 *
210 * \return \c 0 on success.
211 * \return A CCM or cipher-specific error code on failure.
212 */
214 const unsigned char *iv, size_t iv_len,
215 const unsigned char *add, size_t add_len,
216 const unsigned char *input, unsigned char *output,
217 unsigned char *tag, size_t tag_len );
218
219/**
220 * \brief This function performs a CCM authenticated decryption of a
221 * buffer.
222 *
223 * \param ctx The CCM context to use for decryption. This must be
224 * initialized and bound to a key.
225 * \param length The length of the input data in Bytes.
226 * \param iv The initialization vector (nonce). This must be a readable
227 * buffer of at least \p iv_len Bytes.
228 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
229 * or 13. The length L of the message length field is
230 * 15 - \p iv_len.
231 * \param add The additional data field. This must be a readable buffer
232 * of at least that \p add_len Bytes..
233 * \param add_len The length of additional data in Bytes.
234 * This must be less than 2^16 - 2^8.
235 * \param input The buffer holding the input data. If \p length is greater
236 * than zero, \p input must be a readable buffer of at least
237 * that length.
238 * \param output The buffer holding the output data. If \p length is greater
239 * than zero, \p output must be a writable buffer of at least
240 * that length.
241 * \param tag The buffer holding the authentication field. This must be a
242 * readable buffer of at least \p tag_len Bytes.
243 * \param tag_len The length of the authentication field to generate in Bytes:
244 * 4, 6, 8, 10, 12, 14 or 16.
245 *
246 * \return \c 0 on success. This indicates that the message is authentic.
247 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
248 * \return A cipher-specific error code on calculation failure.
249 */
251 const unsigned char *iv, size_t iv_len,
252 const unsigned char *add, size_t add_len,
253 const unsigned char *input, unsigned char *output,
254 const unsigned char *tag, size_t tag_len );
255
256/**
257 * \brief This function performs a CCM* authenticated decryption of a
258 * buffer.
259 *
260 * \note When using this function in a variable tag length context,
261 * the tag length has to be decoded from \p iv and passed to
262 * this function as \p tag_len. (\p tag needs to be adjusted
263 * accordingly.)
264 *
265 * \param ctx The CCM context to use for decryption. This must be
266 * initialized and bound to a key.
267 * \param length The length of the input data in Bytes.
268 * \param iv The initialization vector (nonce). This must be a readable
269 * buffer of at least \p iv_len Bytes.
270 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
271 * or 13. The length L of the message length field is
272 * 15 - \p iv_len.
273 * \param add The additional data field. This must be a readable buffer of
274 * at least that \p add_len Bytes.
275 * \param add_len The length of additional data in Bytes.
276 * This must be less than 2^16 - 2^8.
277 * \param input The buffer holding the input data. If \p length is greater
278 * than zero, \p input must be a readable buffer of at least
279 * that length.
280 * \param output The buffer holding the output data. If \p length is greater
281 * than zero, \p output must be a writable buffer of at least
282 * that length.
283 * \param tag The buffer holding the authentication field. This must be a
284 * readable buffer of at least \p tag_len Bytes.
285 * \param tag_len The length of the authentication field in Bytes.
286 * 0, 4, 6, 8, 10, 12, 14 or 16.
287 *
288 * \warning Passing \c 0 as \p tag_len means that the message is nos
289 * longer authenticated.
290 *
291 * \return \c 0 on success.
292 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
293 * \return A cipher-specific error code on calculation failure.
294 */
296 const unsigned char *iv, size_t iv_len,
297 const unsigned char *add, size_t add_len,
298 const unsigned char *input, unsigned char *output,
299 const unsigned char *tag, size_t tag_len );
300
301#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
302/**
303 * \brief The CCM checkup routine.
304 *
305 * \return \c 0 on success.
306 * \return \c 1 on failure.
307 */
308int mbedtls_ccm_self_test( int verbose );
309#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
310
311#ifdef __cplusplus
312}
313#endif
314
315/// \}
316/// \}
317
318#endif /* MBEDTLS_CCM_H */
This file contains an abstraction interface for use with the cipher primitives provided by the librar...
Configuration options (set of defines)
void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
This function releases and clears the specified CCM context and underlying cipher sub-context.
int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, const unsigned char *tag, size_t tag_len)
This function performs a CCM* authenticated decryption of a buffer.
int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher, const unsigned char *key, unsigned int keybits)
This function initializes the CCM context set in the ctx parameter and sets the encryption key.
int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, unsigned char *tag, size_t tag_len)
This function encrypts a buffer using CCM*.
int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, unsigned char *tag, size_t tag_len)
This function encrypts a buffer using CCM.
int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len, const unsigned char *input, unsigned char *output, const unsigned char *tag, size_t tag_len)
This function performs a CCM authenticated decryption of a buffer.
void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
This function initializes the specified CCM context, to make references valid, and prepare the contex...
mbedtls_cipher_id_t
Supported cipher types.
Definition: cipher.h:89
The CCM context-type definition.
mbedtls_cipher_context_t cipher_ctx
Definition: ccm.h:85
Generic cipher context.
Definition: cipher.h:318