blob: adb14cc636f9163ed6b357bb5a082d1598ee485f [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 *
Janos Follath6b4bd3d2018-05-29 11:30:26 +010017 * 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.
Darryl Greena40a1012018-01-05 15:33:17 +000029 */
30/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020031 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000032 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020033 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#ifndef MBEDTLS_CCM_H
36#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020037
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050038#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010039#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#else
41#include MBEDTLS_CONFIG_FILE
42#endif
43
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010044#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020045
Gilles Peskinea3974432021-07-26 18:48:10 +020046/** Bad input parameters to the function. */
47#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D
48/** Authenticated decryption failed. */
49#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F
Ron Eldor9924bdc2018-10-04 10:59:13 +030050
51/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020052/** CCM hardware accelerator failed. */
53#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020054
55#ifdef __cplusplus
56extern "C" {
57#endif
58
Ron Eldor4e6d55d2018-02-07 16:36:15 +020059#if !defined(MBEDTLS_CCM_ALT)
60// Regular implementation
61//
62
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020063/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000064 * \brief The CCM context-type definition. The CCM context is passed
65 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067typedef struct mbedtls_ccm_context {
Rose Zadikeecdbea2018-01-24 12:56:53 +000068 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020071
Ron Eldor4e6d55d2018-02-07 16:36:15 +020072#else /* MBEDTLS_CCM_ALT */
73#include "ccm_alt.h"
74#endif /* MBEDTLS_CCM_ALT */
75
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020076/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000077 * \brief This function initializes the specified CCM context,
78 * to make references valid, and prepare the context
79 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020080 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081 * \param ctx The CCM context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020082 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083void mbedtls_ccm_init(mbedtls_ccm_context *ctx);
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020084
85/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000086 * \brief This function initializes the CCM context set in the
87 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020088 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089 * \param ctx The CCM context to initialize. This must be an initialized
90 * context.
Rose Zadikeecdbea2018-01-24 12:56:53 +000091 * \param cipher The 128-bit block cipher to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * \param key The encryption key. This must not be \c NULL.
Rose Zadikeecdbea2018-01-24 12:56:53 +000093 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020094 *
Rose Zadik4ee9d242018-03-26 17:18:44 +010095 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +010096 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020097 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
99 mbedtls_cipher_id_t cipher,
100 const unsigned char *key,
101 unsigned int keybits);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200102
103/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000104 * \brief This function releases and clears the specified CCM context
105 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200106 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * \param ctx The CCM context to clear. If this is \c NULL, the function
108 * has no effect. Otherwise, this must be initialized.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110void mbedtls_ccm_free(mbedtls_ccm_context *ctx);
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200111
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200112/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000113 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200114 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100115 * \note The tag is written to a separate buffer. To concatenate
116 * the \p tag with the \p output, as done in <em>RFC-3610:
117 * Counter with CBC-MAC (CCM)</em>, use
118 * \p tag = \p output + \p length, and make sure that the
119 * output buffer is at least \p length + \p tag_len wide.
120 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \param ctx The CCM context to use for encryption. This must be
122 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000123 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \param iv The initialization vector (nonce). This must be a readable
125 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100126 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
127 * or 13. The length L of the message length field is
128 * 15 - \p iv_len.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129 * \param add The additional data field. If \p add_len is greater than
130 * zero, \p add must be a readable buffer of at least that
131 * length.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000132 * \param add_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * This must be less than `2^16 - 2^8`.
134 * \param input The buffer holding the input data. If \p length is greater
135 * than zero, \p input must be a readable buffer of at least
136 * that length.
137 * \param output The buffer holding the output data. If \p length is greater
138 * than zero, \p output must be a writable buffer of at least
139 * that length.
140 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidt6e2af092020-09-12 00:19:52 +0300141 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100142 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100143 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200144 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000145 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100146 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
149 const unsigned char *iv, size_t iv_len,
150 const unsigned char *add, size_t add_len,
151 const unsigned char *input, unsigned char *output,
152 unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200153
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200154/**
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100155 * \brief This function encrypts a buffer using CCM*.
156 *
157 * \note The tag is written to a separate buffer. To concatenate
158 * the \p tag with the \p output, as done in <em>RFC-3610:
159 * Counter with CBC-MAC (CCM)</em>, use
160 * \p tag = \p output + \p length, and make sure that the
161 * output buffer is at least \p length + \p tag_len wide.
162 *
163 * \note When using this function in a variable tag length context,
164 * the tag length has to be encoded into the \p iv passed to
165 * this function.
166 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * \param ctx The CCM context to use for encryption. This must be
168 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100169 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * \param iv The initialization vector (nonce). This must be a readable
171 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100172 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
173 * or 13. The length L of the message length field is
174 * 15 - \p iv_len.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 * \param add The additional data field. This must be a readable buffer of
176 * at least \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100177 * \param add_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 * This must be less than 2^16 - 2^8.
179 * \param input The buffer holding the input data. If \p length is greater
180 * than zero, \p input must be a readable buffer of at least
181 * that length.
182 * \param output The buffer holding the output data. If \p length is greater
183 * than zero, \p output must be a writable buffer of at least
184 * that length.
185 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidt6e2af092020-09-12 00:19:52 +0300186 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100187 * \param tag_len The length of the authentication field to generate in Bytes:
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100188 * 0, 4, 6, 8, 10, 12, 14 or 16.
189 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 * \warning Passing \c 0 as \p tag_len means that the message is no
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100191 * longer authenticated.
192 *
193 * \return \c 0 on success.
194 * \return A CCM or cipher-specific error code on failure.
195 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
197 const unsigned char *iv, size_t iv_len,
198 const unsigned char *add, size_t add_len,
199 const unsigned char *input, unsigned char *output,
200 unsigned char *tag, size_t tag_len);
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100201
202/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000203 * \brief This function performs a CCM authenticated decryption of a
204 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200205 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * \param ctx The CCM context to use for decryption. This must be
207 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000208 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 * \param iv The initialization vector (nonce). This must be a readable
210 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100211 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
212 * or 13. The length L of the message length field is
213 * 15 - \p iv_len.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * \param add The additional data field. This must be a readable buffer
215 * of at least that \p add_len Bytes..
Rose Zadikeecdbea2018-01-24 12:56:53 +0000216 * \param add_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 * This must be less than 2^16 - 2^8.
218 * \param input The buffer holding the input data. If \p length is greater
219 * than zero, \p input must be a readable buffer of at least
220 * that length.
221 * \param output The buffer holding the output data. If \p length is greater
222 * than zero, \p output must be a writable buffer of at least
223 * that length.
224 * \param tag The buffer holding the authentication field. This must be a
225 * readable buffer of at least \p tag_len Bytes.
226 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100227 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200228 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100229 * \return \c 0 on success. This indicates that the message is authentic.
230 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
231 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
234 const unsigned char *iv, size_t iv_len,
235 const unsigned char *add, size_t add_len,
236 const unsigned char *input, unsigned char *output,
237 const unsigned char *tag, size_t tag_len);
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200238
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100239/**
240 * \brief This function performs a CCM* authenticated decryption of a
241 * buffer.
242 *
243 * \note When using this function in a variable tag length context,
244 * the tag length has to be decoded from \p iv and passed to
245 * this function as \p tag_len. (\p tag needs to be adjusted
246 * accordingly.)
247 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 * \param ctx The CCM context to use for decryption. This must be
249 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100250 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 * \param iv The initialization vector (nonce). This must be a readable
252 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100253 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
254 * or 13. The length L of the message length field is
255 * 15 - \p iv_len.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 * \param add The additional data field. This must be a readable buffer of
257 * at least that \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100258 * \param add_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 * This must be less than 2^16 - 2^8.
260 * \param input The buffer holding the input data. If \p length is greater
261 * than zero, \p input must be a readable buffer of at least
262 * that length.
263 * \param output The buffer holding the output data. If \p length is greater
264 * than zero, \p output must be a writable buffer of at least
265 * that length.
266 * \param tag The buffer holding the authentication field. This must be a
267 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100268 * \param tag_len The length of the authentication field in Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100269 * 0, 4, 6, 8, 10, 12, 14 or 16.
270 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 * \warning Passing \c 0 as \p tag_len means that the message is nos
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100272 * longer authenticated.
273 *
Janos Follath143b3192018-05-30 13:57:29 +0100274 * \return \c 0 on success.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100275 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
276 * \return A cipher-specific error code on calculation failure.
277 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
279 const unsigned char *iv, size_t iv_len,
280 const unsigned char *add, size_t add_len,
281 const unsigned char *input, unsigned char *output,
282 const unsigned char *tag, size_t tag_len);
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200285/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000286 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200287 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100288 * \return \c 0 on success.
289 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_ccm_self_test(int verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200293
294#ifdef __cplusplus
295}
296#endif
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_CCM_H */