blob: 3dcdc9189470e7d5c9d6d0a23b73d5bf2d6e60fb [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útia2947ac2020-08-19 16:37:36 +020031 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020032 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
33 *
34 * This file is provided under the Apache License 2.0, or the
35 * GNU General Public License v2.0 or later.
36 *
37 * **********
38 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020039 *
40 * Licensed under the Apache License, Version 2.0 (the "License"); you may
41 * not use this file except in compliance with the License.
42 * You may obtain a copy of the License at
43 *
44 * http://www.apache.org/licenses/LICENSE-2.0
45 *
46 * Unless required by applicable law or agreed to in writing, software
47 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
48 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49 * See the License for the specific language governing permissions and
50 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020051 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020052 * **********
53 *
54 * **********
55 * GNU General Public License v2.0 or later:
56 *
57 * This program is free software; you can redistribute it and/or modify
58 * it under the terms of the GNU General Public License as published by
59 * the Free Software Foundation; either version 2 of the License, or
60 * (at your option) any later version.
61 *
62 * This program is distributed in the hope that it will be useful,
63 * but WITHOUT ANY WARRANTY; without even the implied warranty of
64 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65 * GNU General Public License for more details.
66 *
67 * You should have received a copy of the GNU General Public License along
68 * with this program; if not, write to the Free Software Foundation, Inc.,
69 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
70 *
71 * **********
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020072 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#ifndef MBEDTLS_CCM_H
75#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020076
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020077#if !defined(MBEDTLS_CONFIG_FILE)
78#include "config.h"
79#else
80#include MBEDTLS_CONFIG_FILE
81#endif
82
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020083#include "cipher.h"
84
Rose Zadikeecdbea2018-01-24 12:56:53 +000085#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
86#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030087
88/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadikeecdbea2018-01-24 12:56:53 +000089#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020090
91#ifdef __cplusplus
92extern "C" {
93#endif
94
Ron Eldor4e6d55d2018-02-07 16:36:15 +020095#if !defined(MBEDTLS_CCM_ALT)
96// Regular implementation
97//
98
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020099/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000100 * \brief The CCM context-type definition. The CCM context is passed
101 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200102 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200103typedef struct mbedtls_ccm_context
104{
Rose Zadikeecdbea2018-01-24 12:56:53 +0000105 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200108
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200109#else /* MBEDTLS_CCM_ALT */
110#include "ccm_alt.h"
111#endif /* MBEDTLS_CCM_ALT */
112
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200113/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000114 * \brief This function initializes the specified CCM context,
115 * to make references valid, and prepare the context
116 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200117 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100118 * \param ctx The CCM context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200119 */
120void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
121
122/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000123 * \brief This function initializes the CCM context set in the
124 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200125 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100126 * \param ctx The CCM context to initialize. This must be an initialized
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100127 * context.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000128 * \param cipher The 128-bit block cipher to use.
k-stachowiakb92f9332018-12-13 11:13:51 +0100129 * \param key The encryption key. This must not be \c NULL.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000130 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200131 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100132 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100133 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200134 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200135int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
136 mbedtls_cipher_id_t cipher,
137 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200138 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200139
140/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000141 * \brief This function releases and clears the specified CCM context
142 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200143 *
k-stachowiak9da5d7c2018-12-13 17:19:48 +0100144 * \param ctx The CCM context to clear. If this is \c NULL, the function
145 * has no effect. Otherwise, this must be initialized.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200148
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200149/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000150 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200151 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100152 * \note The tag is written to a separate buffer. To concatenate
153 * the \p tag with the \p output, as done in <em>RFC-3610:
154 * Counter with CBC-MAC (CCM)</em>, use
155 * \p tag = \p output + \p length, and make sure that the
156 * output buffer is at least \p length + \p tag_len wide.
157 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100158 * \param ctx The CCM context to use for encryption. This must be
159 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000160 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100161 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100162 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100163 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
164 * or 13. The length L of the message length field is
165 * 15 - \p iv_len.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100166 * \param add The additional data field. If \p add_len is greater than
167 * zero, \p add must be a readable buffer of at least that
k-stachowiak247a7822018-12-19 13:36:03 +0100168 * length.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000169 * \param add_len The length of additional data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100170 * This must be less than `2^16 - 2^8`.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100171 * \param input The buffer holding the input data. If \p length is greater
172 * than zero, \p input must be a readable buffer of at least
173 * that length.
174 * \param output The buffer holding the output data. If \p length is greater
175 * than zero, \p output must be a writable buffer of at least
176 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100177 * \param tag The buffer holding the authentication field. This must be a
178 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100179 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100180 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200181 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000182 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100183 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200186 const unsigned char *iv, size_t iv_len,
187 const unsigned char *add, size_t add_len,
188 const unsigned char *input, unsigned char *output,
189 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200190
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200191/**
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100192 * \brief This function encrypts a buffer using CCM*.
193 *
194 * \note The tag is written to a separate buffer. To concatenate
195 * the \p tag with the \p output, as done in <em>RFC-3610:
196 * Counter with CBC-MAC (CCM)</em>, use
197 * \p tag = \p output + \p length, and make sure that the
198 * output buffer is at least \p length + \p tag_len wide.
199 *
200 * \note When using this function in a variable tag length context,
201 * the tag length has to be encoded into the \p iv passed to
202 * this function.
203 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100204 * \param ctx The CCM context to use for encryption. This must be
205 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100206 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100207 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100208 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100209 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
210 * or 13. The length L of the message length field is
211 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100212 * \param add The additional data field. This must be a readable buffer of
213 * at least \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100214 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100215 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100216 * \param input The buffer holding the input data. If \p length is greater
217 * than zero, \p input must be a readable buffer of at least
218 * that length.
219 * \param output The buffer holding the output data. If \p length is greater
220 * than zero, \p output must be a writable buffer of at least
221 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100222 * \param tag The buffer holding the authentication field. This must be a
223 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100224 * \param tag_len The length of the authentication field to generate in Bytes:
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100225 * 0, 4, 6, 8, 10, 12, 14 or 16.
226 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100227 * \warning Passing \c 0 as \p tag_len means that the message is no
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100228 * longer authenticated.
229 *
230 * \return \c 0 on success.
231 * \return A CCM or cipher-specific error code on failure.
232 */
233int mbedtls_ccm_star_encrypt_and_tag( 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 unsigned char *tag, size_t tag_len );
238
239/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000240 * \brief This function performs a CCM authenticated decryption of a
241 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200242 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100243 * \param ctx The CCM context to use for decryption. This must be
244 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000245 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100246 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100247 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100248 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
249 * or 13. The length L of the message length field is
250 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100251 * \param add The additional data field. This must be a readable buffer
252 * of at least that \p add_len Bytes..
Rose Zadikeecdbea2018-01-24 12:56:53 +0000253 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100254 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100255 * \param input The buffer holding the input data. If \p length is greater
256 * than zero, \p input must be a readable buffer of at least
257 * that length.
258 * \param output The buffer holding the output data. If \p length is greater
259 * than zero, \p output must be a writable buffer of at least
260 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100261 * \param tag The buffer holding the authentication field. This must be a
262 * readable buffer of at least \p tag_len Bytes.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100263 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100264 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200265 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100266 * \return \c 0 on success. This indicates that the message is authentic.
267 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
268 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200271 const unsigned char *iv, size_t iv_len,
272 const unsigned char *add, size_t add_len,
273 const unsigned char *input, unsigned char *output,
274 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200275
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100276/**
277 * \brief This function performs a CCM* authenticated decryption of a
278 * buffer.
279 *
280 * \note When using this function in a variable tag length context,
281 * the tag length has to be decoded from \p iv and passed to
282 * this function as \p tag_len. (\p tag needs to be adjusted
283 * accordingly.)
284 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100285 * \param ctx The CCM context to use for decryption. This must be
286 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100287 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100288 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100289 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100290 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
291 * or 13. The length L of the message length field is
292 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100293 * \param add The additional data field. This must be a readable buffer of
294 * at least that \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100295 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100296 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100297 * \param input The buffer holding the input data. If \p length is greater
298 * than zero, \p input must be a readable buffer of at least
299 * that length.
300 * \param output The buffer holding the output data. If \p length is greater
301 * than zero, \p output must be a writable buffer of at least
302 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100303 * \param tag The buffer holding the authentication field. This must be a
304 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100305 * \param tag_len The length of the authentication field in Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100306 * 0, 4, 6, 8, 10, 12, 14 or 16.
307 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100308 * \warning Passing \c 0 as \p tag_len means that the message is nos
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100309 * longer authenticated.
310 *
Janos Follath143b3192018-05-30 13:57:29 +0100311 * \return \c 0 on success.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100312 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
313 * \return A cipher-specific error code on calculation failure.
314 */
315int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
316 const unsigned char *iv, size_t iv_len,
317 const unsigned char *add, size_t add_len,
318 const unsigned char *input, unsigned char *output,
319 const unsigned char *tag, size_t tag_len );
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200322/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000323 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200324 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100325 * \return \c 0 on success.
326 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328int mbedtls_ccm_self_test( int verbose );
329#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200330
331#ifdef __cplusplus
332}
333#endif
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_CCM_H */