blob: 2b9909e1a670157765112d1f34c198c286096511 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020032 * 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.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020045 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#ifndef MBEDTLS_CCM_H
48#define MBEDTLS_CCM_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020049#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020050
Bence Szépkútic662b362021-05-27 11:25:03 +020051#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010053#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020054
Ronald Cron266694e2021-05-20 09:02:37 +020055#define MBEDTLS_CCM_DECRYPT 0
56#define MBEDTLS_CCM_ENCRYPT 1
57#define MBEDTLS_CCM_STAR_DECRYPT 2
58#define MBEDTLS_CCM_STAR_ENCRYPT 3
59
Gilles Peskined2971572021-07-26 18:48:10 +020060/** Bad input parameters to the function. */
61#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D
62/** Authenticated decryption failed. */
63#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F
Ron Eldor9924bdc2018-10-04 10:59:13 +030064
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020065#ifdef __cplusplus
66extern "C" {
67#endif
68
Ron Eldor4e6d55d2018-02-07 16:36:15 +020069#if !defined(MBEDTLS_CCM_ALT)
70// Regular implementation
71//
72
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020073/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000074 * \brief The CCM context-type definition. The CCM context is passed
75 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020076 */
Dawid Drozd428cc522018-07-24 10:02:47 +020077typedef struct mbedtls_ccm_context
78{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020079 mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx); /*!< The cipher context used. */
Mateusz Starzyk89d469c2021-06-22 16:24:28 +020080 unsigned char MBEDTLS_PRIVATE(b)[16]; /*!< The B working buffer */
81 unsigned char MBEDTLS_PRIVATE(y)[16]; /*!< The Y working buffer */
82 unsigned char MBEDTLS_PRIVATE(ctr)[16]; /*!< The counter buffer */
83 unsigned char MBEDTLS_PRIVATE(q); /*!< The Q working value */
84 size_t MBEDTLS_PRIVATE(plaintext_len); /*!< The counter buffer */
85 int MBEDTLS_PRIVATE(mode); /*!< The operation to perform:
86 #MBEDTLS_CCM_ENCRYPT or
87 #MBEDTLS_CCM_DECRYPT or
88 #MBEDTLS_CCM_STAR_ENCRYPT or
89 #MBEDTLS_CCM_STAR_DECRYPT. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092
Ron Eldor4e6d55d2018-02-07 16:36:15 +020093#else /* MBEDTLS_CCM_ALT */
94#include "ccm_alt.h"
95#endif /* MBEDTLS_CCM_ALT */
96
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020097/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000098 * \brief This function initializes the specified CCM context,
99 * to make references valid, and prepare the context
100 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200101 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 * \param ctx The CCM context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200103 */
104void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
105
106/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000107 * \brief This function initializes the CCM context set in the
108 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200109 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * \param ctx The CCM context to initialize. This must be an initialized
111 * context.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000112 * \param cipher The 128-bit block cipher to use.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param key The encryption key. This must not be \c NULL.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000114 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200115 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100116 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100117 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200118 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200119int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
120 mbedtls_cipher_id_t cipher,
121 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200122 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200123
124/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000125 * \brief This function releases and clears the specified CCM context
126 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200127 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * \param ctx The CCM context to clear. If this is \c NULL, the function
129 * has no effect. Otherwise, this must be initialized.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200132
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200133/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000134 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200135 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100136 * \note The tag is written to a separate buffer. To concatenate
137 * the \p tag with the \p output, as done in <em>RFC-3610:
138 * Counter with CBC-MAC (CCM)</em>, use
139 * \p tag = \p output + \p length, and make sure that the
140 * output buffer is at least \p length + \p tag_len wide.
141 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \param ctx The CCM context to use for encryption. This must be
143 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000144 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500145 * \param iv The initialization vector (nonce). This must be a readable
146 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100147 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
148 * or 13. The length L of the message length field is
149 * 15 - \p iv_len.
Ronald Cron51584c62021-05-27 09:47:15 +0200150 * \param ad The additional data field. If \p ad_len is greater than
151 * zero, \p ad must be a readable buffer of at least that
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * length.
Ronald Cron51584c62021-05-27 09:47:15 +0200153 * \param ad_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * This must be less than `2^16 - 2^8`.
155 * \param input The buffer holding the input data. If \p length is greater
156 * than zero, \p input must be a readable buffer of at least
157 * that length.
158 * \param output The buffer holding the output data. If \p length is greater
159 * than zero, \p output must be a writable buffer of at least
160 * that length.
161 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidt6e2af092020-09-12 00:19:52 +0300162 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100163 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100164 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200165 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000166 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100167 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200170 const unsigned char *iv, size_t iv_len,
Ronald Cron51584c62021-05-27 09:47:15 +0200171 const unsigned char *ad, size_t ad_len,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200172 const unsigned char *input, unsigned char *output,
173 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200174
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200175/**
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100176 * \brief This function encrypts a buffer using CCM*.
177 *
178 * \note The tag is written to a separate buffer. To concatenate
179 * the \p tag with the \p output, as done in <em>RFC-3610:
180 * Counter with CBC-MAC (CCM)</em>, use
181 * \p tag = \p output + \p length, and make sure that the
182 * output buffer is at least \p length + \p tag_len wide.
183 *
184 * \note When using this function in a variable tag length context,
185 * the tag length has to be encoded into the \p iv passed to
186 * this function.
187 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * \param ctx The CCM context to use for encryption. This must be
189 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100190 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * \param iv The initialization vector (nonce). This must be a readable
192 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100193 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
194 * or 13. The length L of the message length field is
195 * 15 - \p iv_len.
Ronald Cron51584c62021-05-27 09:47:15 +0200196 * \param ad The additional data field. This must be a readable buffer of
197 * at least \p ad_len Bytes.
198 * \param ad_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 * This must be less than 2^16 - 2^8.
200 * \param input The buffer holding the input data. If \p length is greater
201 * than zero, \p input must be a readable buffer of at least
202 * that length.
203 * \param output The buffer holding the output data. If \p length is greater
204 * than zero, \p output must be a writable buffer of at least
205 * that length.
206 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidt6e2af092020-09-12 00:19:52 +0300207 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100208 * \param tag_len The length of the authentication field to generate in Bytes:
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100209 * 0, 4, 6, 8, 10, 12, 14 or 16.
210 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500211 * \warning Passing \c 0 as \p tag_len means that the message is no
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100212 * longer authenticated.
213 *
214 * \return \c 0 on success.
215 * \return A CCM or cipher-specific error code on failure.
216 */
217int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
218 const unsigned char *iv, size_t iv_len,
Ronald Cron51584c62021-05-27 09:47:15 +0200219 const unsigned char *ad, size_t ad_len,
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100220 const unsigned char *input, unsigned char *output,
221 unsigned char *tag, size_t tag_len );
222
223/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000224 * \brief This function performs a CCM authenticated decryption of a
225 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200226 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 * \param ctx The CCM context to use for decryption. This must be
228 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000229 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 * \param iv The initialization vector (nonce). This must be a readable
231 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100232 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
233 * or 13. The length L of the message length field is
234 * 15 - \p iv_len.
Ronald Cron51584c62021-05-27 09:47:15 +0200235 * \param ad The additional data field. This must be a readable buffer
236 * of at least that \p ad_len Bytes..
237 * \param ad_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 * This must be less than 2^16 - 2^8.
239 * \param input The buffer holding the input data. If \p length is greater
240 * than zero, \p input must be a readable buffer of at least
241 * that length.
242 * \param output The buffer holding the output data. If \p length is greater
243 * than zero, \p output must be a writable buffer of at least
244 * that length.
245 * \param tag The buffer holding the authentication field. This must be a
246 * readable buffer of at least \p tag_len Bytes.
247 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100248 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200249 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100250 * \return \c 0 on success. This indicates that the message is authentic.
251 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
252 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200255 const unsigned char *iv, size_t iv_len,
Ronald Cron51584c62021-05-27 09:47:15 +0200256 const unsigned char *ad, size_t ad_len,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200257 const unsigned char *input, unsigned char *output,
258 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200259
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100260/**
261 * \brief This function performs a CCM* authenticated decryption of a
262 * buffer.
263 *
264 * \note When using this function in a variable tag length context,
265 * the tag length has to be decoded from \p iv and passed to
266 * this function as \p tag_len. (\p tag needs to be adjusted
267 * accordingly.)
268 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 * \param ctx The CCM context to use for decryption. This must be
270 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100271 * \param length The length of the input data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * \param iv The initialization vector (nonce). This must be a readable
273 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100274 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
275 * or 13. The length L of the message length field is
276 * 15 - \p iv_len.
Ronald Cron51584c62021-05-27 09:47:15 +0200277 * \param ad The additional data field. This must be a readable buffer of
278 * at least that \p ad_len Bytes.
279 * \param ad_len The length of additional data in Bytes.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * This must be less than 2^16 - 2^8.
281 * \param input The buffer holding the input data. If \p length is greater
282 * than zero, \p input must be a readable buffer of at least
283 * that length.
284 * \param output The buffer holding the output data. If \p length is greater
285 * than zero, \p output must be a writable buffer of at least
286 * that length.
287 * \param tag The buffer holding the authentication field. This must be a
288 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100289 * \param tag_len The length of the authentication field in Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100290 * 0, 4, 6, 8, 10, 12, 14 or 16.
291 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 * \warning Passing \c 0 as \p tag_len means that the message is nos
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100293 * longer authenticated.
294 *
Janos Follath143b3192018-05-30 13:57:29 +0100295 * \return \c 0 on success.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100296 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
297 * \return A cipher-specific error code on calculation failure.
298 */
299int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
300 const unsigned char *iv, size_t iv_len,
Ronald Cron51584c62021-05-27 09:47:15 +0200301 const unsigned char *ad, size_t ad_len,
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100302 const unsigned char *input, unsigned char *output,
303 const unsigned char *tag, size_t tag_len );
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200304
Ronald Cron266694e2021-05-20 09:02:37 +0200305/**
306 * \brief This function starts a CCM encryption or decryption
307 * operation.
308 *
Ronald Cron542957d2021-06-01 09:22:05 +0200309 * This function and mbedtls_ccm_set_lengths() must be called
310 * before calling mbedtls_ccm_update_ad() or
311 * mbedtls_ccm_update(). This function can be called before
312 * or after mbedtls_ccm_set_lengths().
313 *
Ronald Cron4c2a3792021-05-26 10:37:45 +0200314 * \note This function is not implemented in Mbed TLS yet.
315 *
Ronald Cron266694e2021-05-20 09:02:37 +0200316 * \param ctx The CCM context. This must be initialized.
317 * \param mode The operation to perform: #MBEDTLS_CCM_ENCRYPT or
318 * #MBEDTLS_CCM_DECRYPT or #MBEDTLS_CCM_STAR_ENCRYPT or
319 * #MBEDTLS_CCM_STAR_DECRYPT.
Ronald Cron5905f912021-05-26 09:46:09 +0200320 * \param iv The initialization vector. This must be a readable buffer
321 * of at least \p iv_len Bytes.
Ronald Cron0cc60f92021-05-27 09:01:25 +0200322 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
323 * or 13. The length L of the message length field is
324 * 15 - \p iv_len.
Ronald Cron7c41cd22021-05-29 17:22:10 +0200325 *
326 * \return \c 0 on success.
Ronald Crone13d3082021-06-01 13:35:40 +0200327 * \return #MBEDTLS_ERR_CCM_BAD_INPUT on failure:
328 * \p ctx is in an invalid state,
Ronald Cron7c41cd22021-05-29 17:22:10 +0200329 * \p mode is invalid,
330 * \p iv_len is invalid (lower than \c 7 or greater than
331 * \c 13).
332 */
333int mbedtls_ccm_starts( mbedtls_ccm_context *ctx,
334 int mode,
335 const unsigned char *iv,
336 size_t iv_len );
337
338/**
339 * \brief This function declares the lengths of the message
340 * and additional data for a CCM encryption or decryption
341 * operation.
342 *
Ronald Cron542957d2021-06-01 09:22:05 +0200343 * This function and mbedtls_ccm_starts() must be called
344 * before calling mbedtls_ccm_update_ad() or
345 * mbedtls_ccm_update(). This function can be called before
346 * or after mbedtls_ccm_starts().
347 *
Ronald Cron7c41cd22021-05-29 17:22:10 +0200348 * \note This function is not implemented in Mbed TLS yet.
349 *
350 * \param ctx The CCM context. This must be initialized.
Ronald Cron51584c62021-05-27 09:47:15 +0200351 * \param total_ad_len The total length of additional data in bytes.
Ronald Cron0cc60f92021-05-27 09:01:25 +0200352 * This must be less than `2^16 - 2^8`.
Ronald Cronc0cc7ba2021-05-27 08:47:09 +0200353 * \param plaintext_len The length in bytes of the plaintext to encrypt or
354 * result of the decryption (thus not encompassing the
355 * additional data that are not encrypted).
Mateusz Starzyk98d45b92021-06-23 10:45:14 +0200356 * \param tag_len The length of the tag to generate in Bytes:
357 * 4, 6, 8, 10, 12, 14 or 16.
358 * For CCM*, zero is also valid.
Ronald Cron266694e2021-05-20 09:02:37 +0200359 *
360 * \return \c 0 on success.
Ronald Crone13d3082021-06-01 13:35:40 +0200361 * \return #MBEDTLS_ERR_CCM_BAD_INPUT on failure:
362 * \p ctx is in an invalid state,
Ronald Cron51584c62021-05-27 09:47:15 +0200363 * \p total_ad_len is greater than \c 0xFF00.
Ronald Cron266694e2021-05-20 09:02:37 +0200364 */
Ronald Cron7c41cd22021-05-29 17:22:10 +0200365int mbedtls_ccm_set_lengths( mbedtls_ccm_context *ctx,
366 size_t total_ad_len,
Mateusz Starzyk98d45b92021-06-23 10:45:14 +0200367 size_t plaintext_len,
368 size_t tag_len );
Ronald Cron266694e2021-05-20 09:02:37 +0200369
370/**
371 * \brief This function feeds an input buffer as associated data
372 * (authenticated but not encrypted data) in a CCM
373 * encryption or decryption operation.
374 *
Ronald Crond1a29a92021-05-26 09:49:11 +0200375 * You may call this function zero, one or more times
376 * to pass successive parts of the additional data. The
Ronald Cron51584c62021-05-27 09:47:15 +0200377 * lengths \p ad_len of the data parts should eventually add
Ronald Crond1a29a92021-05-26 09:49:11 +0200378 * up exactly to the total length of additional data
Ronald Cron7c41cd22021-05-29 17:22:10 +0200379 * \c total_ad_len passed to mbedtls_ccm_set_lengths(). You
380 * may not call this function after calling
381 * mbedtls_ccm_update().
Ronald Cron266694e2021-05-20 09:02:37 +0200382 *
Ronald Cron4c2a3792021-05-26 10:37:45 +0200383 * \note This function is not implemented in Mbed TLS yet.
384 *
Ronald Cron266694e2021-05-20 09:02:37 +0200385 * \param ctx The CCM context. This must have been started with
Ronald Cron7c41cd22021-05-29 17:22:10 +0200386 * mbedtls_ccm_starts(), the lengths of the message and
387 * additional data must have been declared with
388 * mbedtls_ccm_set_lengths() and this must not have yet
389 * received any input with mbedtls_ccm_update().
Ronald Cron51584c62021-05-27 09:47:15 +0200390 * \param ad The buffer holding the additional data, or \c NULL
391 * if \p ad_len is \c 0.
392 * \param ad_len The length of the additional data. If \c 0,
393 * \p ad may be \c NULL.
Ronald Cron266694e2021-05-20 09:02:37 +0200394 *
395 * \return \c 0 on success.
Ronald Crone13d3082021-06-01 13:35:40 +0200396 * \return #MBEDTLS_ERR_CCM_BAD_INPUT on failure:
397 * \p ctx is in an invalid state,
Ronald Cron266694e2021-05-20 09:02:37 +0200398 * total input length too long.
399 */
400int mbedtls_ccm_update_ad( mbedtls_ccm_context *ctx,
Ronald Cron51584c62021-05-27 09:47:15 +0200401 const unsigned char *ad,
402 size_t ad_len );
Ronald Cron266694e2021-05-20 09:02:37 +0200403
404/**
405 * \brief This function feeds an input buffer into an ongoing CCM
406 * encryption or decryption operation.
407 *
408 * You may call this function zero, one or more times
409 * to pass successive parts of the input: the plaintext to
410 * encrypt, or the ciphertext (not including the tag) to
411 * decrypt. After the last part of the input, call
Ronald Croneabc3af2021-05-26 10:45:30 +0200412 * mbedtls_ccm_finish(). The lengths \p input_len of the
Ronald Cronc0cc7ba2021-05-27 08:47:09 +0200413 * data parts should eventually add up exactly to the
414 * plaintext length \c plaintext_len passed to
Ronald Cron7c41cd22021-05-29 17:22:10 +0200415 * mbedtls_ccm_set_lengths().
Ronald Cron266694e2021-05-20 09:02:37 +0200416 *
417 * This function may produce output in one of the following
418 * ways:
419 * - Immediate output: the output length is always equal
420 * to the input length.
Ronald Cronff924792021-05-27 09:51:30 +0200421 * - Buffered output: except for the last part of input data,
Ronald Cron266694e2021-05-20 09:02:37 +0200422 * the output consists of a whole number of 16-byte blocks.
423 * If the total input length so far (not including
424 * associated data) is 16 \* *B* + *A* with *A* < 16 then
425 * the total output length is 16 \* *B*.
Ronald Cron2d40b102021-05-26 10:11:06 +0200426 * For the last part of input data, the output length is
427 * equal to the input length plus the number of bytes (*A*)
428 * buffered in the previous call to the function (if any).
Ronald Cronc0cc7ba2021-05-27 08:47:09 +0200429 * The function uses the plaintext length
Ronald Cron7c41cd22021-05-29 17:22:10 +0200430 * \c plaintext_len passed to mbedtls_ccm_set_lengths()
Ronald Cron2d40b102021-05-26 10:11:06 +0200431 * to detect the last part of input data.
Ronald Cron266694e2021-05-20 09:02:37 +0200432 *
433 * In particular:
434 * - It is always correct to call this function with
Ronald Croneabc3af2021-05-26 10:45:30 +0200435 * \p output_size >= \p input_len + 15.
436 * - If \p input_len is a multiple of 16 for all the calls
Ronald Cron266694e2021-05-20 09:02:37 +0200437 * to this function during an operation (not necessary for
438 * the last one) then it is correct to use \p output_size
Ronald Croneabc3af2021-05-26 10:45:30 +0200439 * =\p input_len.
Ronald Cron266694e2021-05-20 09:02:37 +0200440 *
Ronald Cron4c2a3792021-05-26 10:37:45 +0200441 * \note This function is not implemented in Mbed TLS yet.
442 *
Ronald Cron86e6c9f2021-05-27 09:30:59 +0200443 * \param ctx The CCM context. This must have been started with
Ronald Cron7c41cd22021-05-29 17:22:10 +0200444 * mbedtls_ccm_starts() and the lengths of the message and
445 * additional data must have been declared with
446 * mbedtls_ccm_set_lengths().
Ronald Croneabc3af2021-05-26 10:45:30 +0200447 * \param input The buffer holding the input data. If \p input_len
Ronald Cron266694e2021-05-20 09:02:37 +0200448 * is greater than zero, this must be a readable buffer
Ronald Croneabc3af2021-05-26 10:45:30 +0200449 * of at least \p input_len bytes.
450 * \param input_len The length of the input data in bytes.
Ronald Cron266694e2021-05-20 09:02:37 +0200451 * \param output The buffer for the output data. If \p output_size
452 * is greater than zero, this must be a writable buffer of
453 * at least \p output_size bytes.
454 * \param output_size The size of the output buffer in bytes.
455 * See the function description regarding the output size.
Ronald Croneabc3af2021-05-26 10:45:30 +0200456 * \param output_len On success, \p *output_len contains the actual
Ronald Cron266694e2021-05-20 09:02:37 +0200457 * length of the output written in \p output.
Ronald Croneabc3af2021-05-26 10:45:30 +0200458 * On failure, the content of \p *output_len is
Ronald Cron266694e2021-05-20 09:02:37 +0200459 * unspecified.
460 *
461 * \return \c 0 on success.
462 * \return #MBEDTLS_ERR_CCM_BAD_INPUT on failure:
Ronald Crone13d3082021-06-01 13:35:40 +0200463 * \p ctx is in an invalid state,
Ronald Cron266694e2021-05-20 09:02:37 +0200464 * total input length too long,
Ronald Cron266694e2021-05-20 09:02:37 +0200465 * or \p output_size too small.
466 */
467int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
Ronald Croneabc3af2021-05-26 10:45:30 +0200468 const unsigned char *input, size_t input_len,
Ronald Cron266694e2021-05-20 09:02:37 +0200469 unsigned char *output, size_t output_size,
Ronald Croneabc3af2021-05-26 10:45:30 +0200470 size_t *output_len );
Ronald Cron266694e2021-05-20 09:02:37 +0200471
472/**
473 * \brief This function finishes the CCM operation and generates
474 * the authentication tag.
475 *
476 * It wraps up the CCM stream, and generates the
477 * tag. The tag can have a maximum length of 16 Bytes.
478 *
Ronald Cron4c2a3792021-05-26 10:37:45 +0200479 * \note This function is not implemented in Mbed TLS yet.
480 *
Ronald Cron86e6c9f2021-05-27 09:30:59 +0200481 * \param ctx The CCM context. This must have been started with
Ronald Cron7c41cd22021-05-29 17:22:10 +0200482 * mbedtls_ccm_starts() and the lengths of the message and
483 * additional data must have been declared with
484 * mbedtls_ccm_set_lengths().
Ronald Cron266694e2021-05-20 09:02:37 +0200485 * \param tag The buffer for holding the tag. If \p tag_len is greater
486 * than zero, this must be a writable buffer of at least \p
487 * tag_len Bytes.
Mateusz Starzyk82c48c92021-06-23 12:39:40 +0200488 * \param tag_len The length of the tag. Must match the tag length passed to
489 * mbedtls_ccm_set_lengths() function.
Ronald Cron266694e2021-05-20 09:02:37 +0200490 *
491 * \return \c 0 on success.
492 * \return #MBEDTLS_ERR_CCM_BAD_INPUT on failure:
Ronald Crone13d3082021-06-01 13:35:40 +0200493 * \p ctx is in an invalid state,
Ronald Cron9ca25502021-05-26 10:22:06 +0200494 * invalid value of \p tag_len,
495 * the total amount of additional data passed to
496 * mbedtls_ccm_update_ad() was lower than the total length of
Ronald Cron51584c62021-05-27 09:47:15 +0200497 * additional data \c total_ad_len passed to
Ronald Cron7c41cd22021-05-29 17:22:10 +0200498 * mbedtls_ccm_set_lengths(),
Ronald Cron9ca25502021-05-26 10:22:06 +0200499 * the total amount of input data passed to
Ronald Cronc0cc7ba2021-05-27 08:47:09 +0200500 * mbedtls_ccm_update() was lower than the plaintext length
Ronald Cron7c41cd22021-05-29 17:22:10 +0200501 * \c plaintext_len passed to mbedtls_ccm_set_lengths().
Ronald Cron266694e2021-05-20 09:02:37 +0200502 */
503int mbedtls_ccm_finish( mbedtls_ccm_context *ctx,
504 unsigned char *tag, size_t tag_len );
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200507/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000508 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200509 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100510 * \return \c 0 on success.
511 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513int mbedtls_ccm_self_test( int verbose );
514#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200515
516#ifdef __cplusplus
517}
518#endif
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_CCM_H */