blob: 7d117d9a94f04e95998d9b346c1a1356aa7df6a8 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/**
2 * \file ccm.h
3 *
Rose Zadik477dce12018-04-17 16:31:22 +01004 * \brief This file provides an API for the CCM authenticated encryption
5 * mode for block ciphers.
Rose Zadik4ee9d242018-03-26 17:18:44 +01006 *
7 * CCM combines Counter mode encryption with CBC-MAC authentication
8 * for 128-bit block ciphers.
Rose Zadikeecdbea2018-01-24 12:56:53 +00009 *
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 *
Darryl Greena40a1012018-01-05 15:33:17 +000017 */
18/*
Rose Zadikeecdbea2018-01-24 12:56:53 +000019 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020020 * SPDX-License-Identifier: Apache-2.0
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020033 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000034 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020035 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#ifndef MBEDTLS_CCM_H
38#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020039
40#include "cipher.h"
41
Rose Zadikeecdbea2018-01-24 12:56:53 +000042#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
43#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
44#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020045
Steven Cooreman222e2ff2017-04-04 11:37:15 +020046#if !defined(MBEDTLS_CCM_ALT)
47// Regular implementation
48//
49
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020054/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000055 * \brief The CCM context-type definition. The CCM context is passed
56 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020057 */
58typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000059 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020062
63/**
Janos Follathf60c8152018-04-27 14:45:49 +010064 * \brief The CCM* callback for encrypting with variable tag length.
65 * The function pointer is passed to the APIs called. This
66 * function calculates the nonce and returns it in a buffer.
67 *
68 * \warning This function must not return the same nonce more than once
69 * in the lifetime of the key!
70 *
71 * \note To prevent attacks taking advantage of the variable tag
72 * length CCM* encodes the tag length in the nonce. The method
73 * of encoding may vary. Standards might mandate encoding other
74 * information in the nonce (e.g. address and frame counter)
75 * too.
76 *
77 * \param app_ctx A pointer to structure containing the application context
78 * if it is necessary for calculating the initialisation vector
79 * (nonce).
80 * \param tag_len Length of the tag in bytes.
81 * \nonce Output variable, points to the buffer capable of holding the
82 * calculated nonce. Must be at least \p nonce_len bytes long.
83 * \nonce_len The length of the nonce in bytes.
84 *
85 * \return \c 0 on success.
86 * \return MBEDTLS_ERR_CCM_BAD_INPUT error code on failure.
87 */
88typedef int (*mbedtls_ccm_star_get_nonce_t)( void *app_ctx, size_t tag_len,
89 unsigned char *nonce,
90 size_t nonce_len );
91
92/**
93 * \brief The CCM* callback for decrypting with variable tag length.
94 * The function pointer is passed to the APIs called. This
95 * function calculates and returns the length of the tag in the
96 * output parameter.
97 *
98 * \param app_ctx A pointer to structure containing the application context
99 * if it is necessary for decoding the tag length or validating
100 * the initialisation vector (nonce).
101 * \param tag_len Output variable for holding the tag length in bytes.
102 * \nonce A buffer containing the nonce.
103 * \nonce_len The length of the nonce in bytes.
104 *
105 * \return \c 0 on success.
106 * \return MBEDTLS_ERR_CCM_BAD_INPUT error code on failure.
107 */
108typedef int (*mbedtls_ccm_star_get_tag_len_t)( void *app_ctx, size_t* tag_len,
109 const unsigned char *nonce,
110 size_t nonce_len );
111
112/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000113 * \brief This function initializes the specified CCM context,
114 * to make references valid, and prepare the context
115 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200116 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000117 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200118 */
119void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
120
121/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000122 * \brief This function initializes the CCM context set in the
123 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200124 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000125 * \param ctx The CCM context to initialize.
126 * \param cipher The 128-bit block cipher to use.
127 * \param key The encryption key.
128 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200129 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100130 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100131 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200132 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200133int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
134 mbedtls_cipher_id_t cipher,
135 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200136 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200137
138/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000139 * \brief This function releases and clears the specified CCM context
140 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200141 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000142 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200145
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200146/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000147 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200148 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100149 * \note The tag is written to a separate buffer. To concatenate
150 * the \p tag with the \p output, as done in <em>RFC-3610:
151 * Counter with CBC-MAC (CCM)</em>, use
152 * \p tag = \p output + \p length, and make sure that the
153 * output buffer is at least \p length + \p tag_len wide.
154 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000155 * \param ctx The CCM context to use for encryption.
156 * \param length The length of the input data in Bytes.
157 * \param iv Initialization vector (nonce).
158 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
159 * \param add The additional data field.
160 * \param add_len The length of additional data in Bytes.
161 * Must be less than 2^16 - 2^8.
162 * \param input The buffer holding the input data.
163 * \param output The buffer holding the output data.
164 * Must be at least \p length Bytes wide.
165 * \param tag The buffer holding the tag.
166 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100167 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200168 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000169 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100170 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200173 const unsigned char *iv, size_t iv_len,
174 const unsigned char *add, size_t add_len,
175 const unsigned char *input, unsigned char *output,
176 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200177
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200178/**
Janos Follathf60c8152018-04-27 14:45:49 +0100179 * \brief This function encrypts a buffer using CCM* with fixed tag
180 * length.
181 *
182 * \note The tag is written to a separate buffer. To concatenate
183 * the \p tag with the \p output, as done in <em>RFC-3610:
184 * Counter with CBC-MAC (CCM)</em>, use
185 * \p tag = \p output + \p length, and make sure that the
186 * output buffer is at least \p length + \p tag_len wide.
187 *
188 * \param ctx The CCM context to use for encryption.
189 * \param length The length of the input data in Bytes.
190 * \param iv Initialization vector (nonce).
191 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
192 * \param add The additional data field.
193 * \param add_len The length of additional data in Bytes.
194 * Must be less than 2^16 - 2^8.
195 * \param input The buffer holding the input data.
196 * \param output The buffer holding the output data.
197 * Must be at least \p length Bytes wide.
198 * \param tag The buffer holding the tag.
199 * \param tag_len The length of the tag to generate in Bytes:
200 * 0, 4, 6, 8, 10, 12, 14 or 16.
201 *
202 * \warning Passing 0 as \p tag_len means that the message is no
203 * longer authenticated.
204 *
205 * \return \c 0 on success.
206 * \return A CCM or cipher-specific error code on failure.
207 */
208int mbedtls_ccm_sfix_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
209 const unsigned char *iv, size_t iv_len,
210 const unsigned char *add, size_t add_len,
211 const unsigned char *input, unsigned char *output,
212 unsigned char *tag, size_t tag_len );
213
214/**
215 * \brief This function encrypts a buffer using CCM* with variable
216 * tag length.
217 *
218 * \note The tag is written to a separate buffer. To concatenate
219 * the \p tag with the \p output, as done in <em>RFC-3610:
220 * Counter with CBC-MAC (CCM)</em>, use
221 * \p tag = \p output + \p length, and make sure that the
222 * output buffer is at least \p length + \p tag_len wide.
223 *
224 * \param ctx The CCM context to use for encryption.
225 * \param length The length of the input data in Bytes.
226 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12,
227 * or 13.
228 * \param add The additional data field.
229 * \param add_len The length of additional data in Bytes.
230 * Must be less than 2^16 - 2^8.
231 * \param input The buffer holding the input data.
232 * \param output The buffer holding the output data.
233 * Must be at least \p length Bytes wide.
234 * \param tag The buffer holding the tag.
235 * \param tag_len The length of the tag to generate in Bytes:
236 * 0, 4, 6, 8, 10, 12, 14 or 16.
237 * \param get_iv A callback function returning the IV (nonce) with the
238 * tag length encoded in it.
239 * \param get_iv_ctx Context passed to the \p get_iv callback.
240 *
241 * \warning Passing 0 as \p tag_len means that the message is no
242 * longer authenticated.
243 *
244 * \return \c 0 on success.
245 * \return A CCM or cipher-specific error code on failure.
246 */
247int mbedtls_ccm_svar_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
248 size_t iv_len, const unsigned char *add,
249 size_t add_len, const unsigned char *input,
250 unsigned char *output, unsigned char *tag,
251 size_t tag_len, mbedtls_ccm_star_get_nonce_t get_iv,
252 void *get_iv_ctx );
253
254/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000255 * \brief This function performs a CCM authenticated decryption of a
256 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200257 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000258 * \param ctx The CCM context to use for decryption.
259 * \param length The length of the input data in Bytes.
260 * \param iv Initialization vector.
261 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
262 * \param add The additional data field.
263 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100264 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000265 * \param input The buffer holding the input data.
266 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100267 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000268 * \param tag The buffer holding the tag.
269 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100270 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200271 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100272 * \return \c 0 on success. This indicates that the message is authentic.
273 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
274 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200277 const unsigned char *iv, size_t iv_len,
278 const unsigned char *add, size_t add_len,
279 const unsigned char *input, unsigned char *output,
280 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200281
Janos Follathf60c8152018-04-27 14:45:49 +0100282/**
283 * \brief This function performs a CCM* authenticated decryption of a
284 * buffer with fixed tag length.
285 *
286 * \param ctx The CCM context to use for decryption.
287 * \param length The length of the input data in Bytes.
288 * \param iv Initialization vector.
289 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
290 * \param add The additional data field.
291 * \param add_len The length of additional data in Bytes.
292 * Must be less than 2^16 - 2^8.
293 * \param input The buffer holding the input data.
294 * \param output The buffer holding the output data.
295 * Must be at least \p length Bytes wide.
296 * \param tag The buffer holding the tag.
297 * \param tag_len The length of the tag in Bytes.
298 * 0, 4, 6, 8, 10, 12, 14 or 16.
299 *
300 * \warning Passing 0 as \p tag_len means that the message is no
301 * longer authenticated.
302 *
303 * \return \c 0 on success. This indicates that the message is
304 * authentic.
305 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
306 * \return A cipher-specific error code on calculation failure.
307 */
308int mbedtls_ccm_sfix_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
309 const unsigned char *iv, size_t iv_len,
310 const unsigned char *add, size_t add_len,
311 const unsigned char *input, unsigned char *output,
312 const unsigned char *tag, size_t tag_len );
313
314/**
315 * \brief This function performs a CCM* authenticated decryption
316 * of a buffer with variable tag length.
317 *
318 * \param ctx The CCM context to use for decryption.
319 * \param length The length of the input data in Bytes.
320 * \param iv Initialization vector.
321 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12,
322 * or 13.
323 * \param add The additional data field.
324 * \param add_len The length of additional data in Bytes.
325 * Must be less than 2^16 - 2^8.
326 * \param input The buffer holding the input data. Unlike the \p input
327 * parameters of other Mbed TLS CCM functions, this buffer
328 * holds the concatenation of the encrypted data and the
329 * authentication tag.
330 * \param output The buffer holding the output data.
331 * Must be at least \p length Bytes wide.
332 * \param output_len The length of the decrypted data.
333 * \param get_tag_len A callback function returning the tag length.
334 * \param get_tlen_ctx Context passed to the \p get_tag_len callback.
335 *
336 *
337 * \return \c 0 on success. This indicates that the message is
338 * authentic.
339 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
340 * \return A cipher-specific error code on calculation failure.
341 */
342int mbedtls_ccm_svar_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
343 const unsigned char *iv, size_t iv_len,
344 const unsigned char *add, size_t add_len,
345 const unsigned char *input, unsigned char *output,
346 size_t* output_len,
347 mbedtls_ccm_star_get_tag_len_t get_tag_len,
348 void *get_tlen_ctx );
349
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200350#ifdef __cplusplus
351}
352#endif
353
Rose Zadikeecdbea2018-01-24 12:56:53 +0000354#else /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200355#include "ccm_alt.h"
Rose Zadikeecdbea2018-01-24 12:56:53 +0000356#endif /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200357
358#ifdef __cplusplus
359extern "C" {
360#endif
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200363/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000364 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200365 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100366 * \return \c 0 on success.
367 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369int mbedtls_ccm_self_test( int verbose );
370#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200371
372#ifdef __cplusplus
373}
374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* MBEDTLS_CCM_H */