blob: 1bc79d804c0a43a4c4cc340cd19af7dfa89f8ca2 [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
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Ron Eldor4e6d55d2018-02-07 16:36:15 +020051#if !defined(MBEDTLS_CCM_ALT)
52// Regular implementation
53//
54
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020055/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000056 * \brief The CCM context-type definition. The CCM context is passed
57 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020058 */
59typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000060 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020063
Ron Eldor4e6d55d2018-02-07 16:36:15 +020064#else /* MBEDTLS_CCM_ALT */
65#include "ccm_alt.h"
66#endif /* MBEDTLS_CCM_ALT */
67
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020068/**
Janos Follathf60c8152018-04-27 14:45:49 +010069 * \brief The CCM* callback for encrypting with variable tag length.
70 * The function pointer is passed to the APIs called. This
71 * function calculates the nonce and returns it in a buffer.
72 *
73 * \warning This function must not return the same nonce more than once
74 * in the lifetime of the key!
75 *
76 * \note To prevent attacks taking advantage of the variable tag
77 * length CCM* encodes the tag length in the nonce. The method
78 * of encoding may vary. Standards might mandate encoding other
79 * information in the nonce (e.g. address and frame counter)
80 * too.
81 *
82 * \param app_ctx A pointer to structure containing the application context
83 * if it is necessary for calculating the initialisation vector
84 * (nonce).
85 * \param tag_len Length of the tag in bytes.
86 * \nonce Output variable, points to the buffer capable of holding the
87 * calculated nonce. Must be at least \p nonce_len bytes long.
88 * \nonce_len The length of the nonce in bytes.
89 *
90 * \return \c 0 on success.
91 * \return MBEDTLS_ERR_CCM_BAD_INPUT error code on failure.
92 */
93typedef int (*mbedtls_ccm_star_get_nonce_t)( void *app_ctx, size_t tag_len,
94 unsigned char *nonce,
95 size_t nonce_len );
96
97/**
98 * \brief The CCM* callback for decrypting with variable tag length.
99 * The function pointer is passed to the APIs called. This
100 * function calculates and returns the length of the tag in the
101 * output parameter.
102 *
103 * \param app_ctx A pointer to structure containing the application context
104 * if it is necessary for decoding the tag length or validating
105 * the initialisation vector (nonce).
106 * \param tag_len Output variable for holding the tag length in bytes.
107 * \nonce A buffer containing the nonce.
108 * \nonce_len The length of the nonce in bytes.
109 *
110 * \return \c 0 on success.
111 * \return MBEDTLS_ERR_CCM_BAD_INPUT error code on failure.
112 */
113typedef int (*mbedtls_ccm_star_get_tag_len_t)( void *app_ctx, size_t* tag_len,
114 const unsigned char *nonce,
115 size_t nonce_len );
116
117/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000118 * \brief This function initializes the specified CCM context,
119 * to make references valid, and prepare the context
120 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200121 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000122 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200123 */
124void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
125
126/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000127 * \brief This function initializes the CCM context set in the
128 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200129 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000130 * \param ctx The CCM context to initialize.
131 * \param cipher The 128-bit block cipher to use.
132 * \param key The encryption key.
133 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200134 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100135 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100136 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200137 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200138int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
139 mbedtls_cipher_id_t cipher,
140 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200141 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200142
143/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000144 * \brief This function releases and clears the specified CCM context
145 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200146 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000147 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200150
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200151/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000152 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200153 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100154 * \note The tag is written to a separate buffer. To concatenate
155 * the \p tag with the \p output, as done in <em>RFC-3610:
156 * Counter with CBC-MAC (CCM)</em>, use
157 * \p tag = \p output + \p length, and make sure that the
158 * output buffer is at least \p length + \p tag_len wide.
159 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000160 * \param ctx The CCM context to use for encryption.
161 * \param length The length of the input data in Bytes.
162 * \param iv Initialization vector (nonce).
163 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
164 * \param add The additional data field.
165 * \param add_len The length of additional data in Bytes.
166 * Must be less than 2^16 - 2^8.
167 * \param input The buffer holding the input data.
168 * \param output The buffer holding the output data.
169 * Must be at least \p length Bytes wide.
170 * \param tag The buffer holding the tag.
171 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100172 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200173 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000174 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100175 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200178 const unsigned char *iv, size_t iv_len,
179 const unsigned char *add, size_t add_len,
180 const unsigned char *input, unsigned char *output,
181 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200182
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200183/**
Janos Follathf60c8152018-04-27 14:45:49 +0100184 * \brief This function encrypts a buffer using CCM* with fixed tag
185 * length.
186 *
187 * \note The tag is written to a separate buffer. To concatenate
188 * the \p tag with the \p output, as done in <em>RFC-3610:
189 * Counter with CBC-MAC (CCM)</em>, use
190 * \p tag = \p output + \p length, and make sure that the
191 * output buffer is at least \p length + \p tag_len wide.
192 *
193 * \param ctx The CCM context to use for encryption.
194 * \param length The length of the input data in Bytes.
195 * \param iv Initialization vector (nonce).
196 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
197 * \param add The additional data field.
198 * \param add_len The length of additional data in Bytes.
199 * Must be less than 2^16 - 2^8.
200 * \param input The buffer holding the input data.
201 * \param output The buffer holding the output data.
202 * Must be at least \p length Bytes wide.
203 * \param tag The buffer holding the tag.
204 * \param tag_len The length of the tag to generate in Bytes:
205 * 0, 4, 6, 8, 10, 12, 14 or 16.
206 *
207 * \warning Passing 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 */
213int mbedtls_ccm_sfix_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
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 encrypts a buffer using CCM* with variable
221 * tag length.
222 *
223 * \note The tag is written to a separate buffer. To concatenate
224 * the \p tag with the \p output, as done in <em>RFC-3610:
225 * Counter with CBC-MAC (CCM)</em>, use
226 * \p tag = \p output + \p length, and make sure that the
227 * output buffer is at least \p length + \p tag_len wide.
228 *
229 * \param ctx The CCM context to use for encryption.
230 * \param length The length of the input data in Bytes.
231 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12,
232 * or 13.
233 * \param add The additional data field.
234 * \param add_len The length of additional data in Bytes.
235 * Must be less than 2^16 - 2^8.
236 * \param input The buffer holding the input data.
237 * \param output The buffer holding the output data.
238 * Must be at least \p length Bytes wide.
239 * \param tag The buffer holding the tag.
240 * \param tag_len The length of the tag to generate in Bytes:
241 * 0, 4, 6, 8, 10, 12, 14 or 16.
242 * \param get_iv A callback function returning the IV (nonce) with the
243 * tag length encoded in it.
244 * \param get_iv_ctx Context passed to the \p get_iv callback.
245 *
246 * \warning Passing 0 as \p tag_len means that the message is no
247 * longer authenticated.
248 *
249 * \return \c 0 on success.
250 * \return A CCM or cipher-specific error code on failure.
251 */
252int mbedtls_ccm_svar_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
253 size_t iv_len, const unsigned char *add,
254 size_t add_len, const unsigned char *input,
255 unsigned char *output, unsigned char *tag,
256 size_t tag_len, mbedtls_ccm_star_get_nonce_t get_iv,
257 void *get_iv_ctx );
258
259/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000260 * \brief This function performs a CCM authenticated decryption of a
261 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200262 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000263 * \param ctx The CCM context to use for decryption.
264 * \param length The length of the input data in Bytes.
265 * \param iv Initialization vector.
266 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
267 * \param add The additional data field.
268 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100269 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000270 * \param input The buffer holding the input data.
271 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100272 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000273 * \param tag The buffer holding the tag.
274 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100275 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200276 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100277 * \return \c 0 on success. This indicates that the message is authentic.
278 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
279 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200282 const unsigned char *iv, size_t iv_len,
283 const unsigned char *add, size_t add_len,
284 const unsigned char *input, unsigned char *output,
285 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286
Janos Follathf60c8152018-04-27 14:45:49 +0100287/**
288 * \brief This function performs a CCM* authenticated decryption of a
289 * buffer with fixed tag length.
290 *
291 * \param ctx The CCM context to use for decryption.
292 * \param length The length of the input data in Bytes.
293 * \param iv Initialization vector.
294 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
295 * \param add The additional data field.
296 * \param add_len The length of additional data in Bytes.
297 * Must be less than 2^16 - 2^8.
298 * \param input The buffer holding the input data.
299 * \param output The buffer holding the output data.
300 * Must be at least \p length Bytes wide.
301 * \param tag The buffer holding the tag.
302 * \param tag_len The length of the tag in Bytes.
303 * 0, 4, 6, 8, 10, 12, 14 or 16.
304 *
305 * \warning Passing 0 as \p tag_len means that the message is no
306 * longer authenticated.
307 *
308 * \return \c 0 on success. This indicates that the message is
309 * authentic.
310 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
311 * \return A cipher-specific error code on calculation failure.
312 */
313int mbedtls_ccm_sfix_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
314 const unsigned char *iv, size_t iv_len,
315 const unsigned char *add, size_t add_len,
316 const unsigned char *input, unsigned char *output,
317 const unsigned char *tag, size_t tag_len );
318
319/**
320 * \brief This function performs a CCM* authenticated decryption
321 * of a buffer with variable tag length.
322 *
323 * \param ctx The CCM context to use for decryption.
324 * \param length The length of the input data in Bytes.
325 * \param iv Initialization vector.
326 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12,
327 * or 13.
328 * \param add The additional data field.
329 * \param add_len The length of additional data in Bytes.
330 * Must be less than 2^16 - 2^8.
331 * \param input The buffer holding the input data. Unlike the \p input
332 * parameters of other Mbed TLS CCM functions, this buffer
333 * holds the concatenation of the encrypted data and the
334 * authentication tag.
335 * \param output The buffer holding the output data.
336 * Must be at least \p length Bytes wide.
337 * \param output_len The length of the decrypted data.
338 * \param get_tag_len A callback function returning the tag length.
339 * \param get_tlen_ctx Context passed to the \p get_tag_len callback.
340 *
341 *
342 * \return \c 0 on success. This indicates that the message is
343 * authentic.
344 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
345 * \return A cipher-specific error code on calculation failure.
346 */
347int mbedtls_ccm_svar_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
348 const unsigned char *iv, size_t iv_len,
349 const unsigned char *add, size_t add_len,
350 const unsigned char *input, unsigned char *output,
351 size_t* output_len,
352 mbedtls_ccm_star_get_tag_len_t get_tag_len,
353 void *get_tlen_ctx );
354
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200357/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000358 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200359 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100360 * \return \c 0 on success.
361 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363int mbedtls_ccm_self_test( int verbose );
364#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200365
366#ifdef __cplusplus
367}
368#endif
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_CCM_H */