blob: 78c0ea42bc05c925ebe5ce8b3d6a2520dc77f0c7 [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
Gilles Peskine1990fab2021-07-26 18:48:10 +020085/** Bad input parameters to the function. */
86#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D
87/** Authenticated decryption failed. */
88#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F
Ron Eldor9924bdc2018-10-04 10:59:13 +030089
90/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020091/** CCM hardware accelerator failed. */
92#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020093
94#ifdef __cplusplus
95extern "C" {
96#endif
97
Ron Eldor4e6d55d2018-02-07 16:36:15 +020098#if !defined(MBEDTLS_CCM_ALT)
99// Regular implementation
100//
101
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200102/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000103 * \brief The CCM context-type definition. The CCM context is passed
104 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200105 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200106typedef struct mbedtls_ccm_context
107{
Rose Zadikeecdbea2018-01-24 12:56:53 +0000108 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200111
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200112#else /* MBEDTLS_CCM_ALT */
113#include "ccm_alt.h"
114#endif /* MBEDTLS_CCM_ALT */
115
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200116/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000117 * \brief This function initializes the specified CCM context,
118 * to make references valid, and prepare the context
119 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200120 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100121 * \param ctx The CCM context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200122 */
123void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
124
125/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000126 * \brief This function initializes the CCM context set in the
127 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200128 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100129 * \param ctx The CCM context to initialize. This must be an initialized
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100130 * context.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000131 * \param cipher The 128-bit block cipher to use.
k-stachowiakb92f9332018-12-13 11:13:51 +0100132 * \param key The encryption key. This must not be \c NULL.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000133 * \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 *
k-stachowiak9da5d7c2018-12-13 17:19:48 +0100147 * \param ctx The CCM context to clear. If this is \c NULL, the function
148 * has no effect. Otherwise, this must be initialized.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200151
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200152/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000153 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200154 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100155 * \note The tag is written to a separate buffer. To concatenate
156 * the \p tag with the \p output, as done in <em>RFC-3610:
157 * Counter with CBC-MAC (CCM)</em>, use
158 * \p tag = \p output + \p length, and make sure that the
159 * output buffer is at least \p length + \p tag_len wide.
160 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100161 * \param ctx The CCM context to use for encryption. This must be
162 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000163 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100164 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100165 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100166 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
167 * or 13. The length L of the message length field is
168 * 15 - \p iv_len.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100169 * \param add The additional data field. If \p add_len is greater than
170 * zero, \p add must be a readable buffer of at least that
k-stachowiak247a7822018-12-19 13:36:03 +0100171 * length.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000172 * \param add_len The length of additional data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100173 * This must be less than `2^16 - 2^8`.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100174 * \param input The buffer holding the input data. If \p length is greater
175 * than zero, \p input must be a readable buffer of at least
176 * that length.
177 * \param output The buffer holding the output data. If \p length is greater
178 * than zero, \p output must be a writable buffer of at least
179 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100180 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidtf9604bb2020-09-12 00:19:52 +0300181 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100182 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100183 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200184 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000185 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100186 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200189 const unsigned char *iv, size_t iv_len,
190 const unsigned char *add, size_t add_len,
191 const unsigned char *input, unsigned char *output,
192 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200193
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200194/**
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100195 * \brief This function encrypts a buffer using CCM*.
196 *
197 * \note The tag is written to a separate buffer. To concatenate
198 * the \p tag with the \p output, as done in <em>RFC-3610:
199 * Counter with CBC-MAC (CCM)</em>, use
200 * \p tag = \p output + \p length, and make sure that the
201 * output buffer is at least \p length + \p tag_len wide.
202 *
203 * \note When using this function in a variable tag length context,
204 * the tag length has to be encoded into the \p iv passed to
205 * this function.
206 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100207 * \param ctx The CCM context to use for encryption. This must be
208 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100209 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100210 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100211 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100212 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
213 * or 13. The length L of the message length field is
214 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100215 * \param add The additional data field. This must be a readable buffer of
216 * at least \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100217 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100218 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100219 * \param input The buffer holding the input data. If \p length is greater
220 * than zero, \p input must be a readable buffer of at least
221 * that length.
222 * \param output The buffer holding the output data. If \p length is greater
223 * than zero, \p output must be a writable buffer of at least
224 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100225 * \param tag The buffer holding the authentication field. This must be a
Yonatan Goldschmidtf9604bb2020-09-12 00:19:52 +0300226 * writable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100227 * \param tag_len The length of the authentication field to generate in Bytes:
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100228 * 0, 4, 6, 8, 10, 12, 14 or 16.
229 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100230 * \warning Passing \c 0 as \p tag_len means that the message is no
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100231 * longer authenticated.
232 *
233 * \return \c 0 on success.
234 * \return A CCM or cipher-specific error code on failure.
235 */
236int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
237 const unsigned char *iv, size_t iv_len,
238 const unsigned char *add, size_t add_len,
239 const unsigned char *input, unsigned char *output,
240 unsigned char *tag, size_t tag_len );
241
242/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000243 * \brief This function performs a CCM authenticated decryption of a
244 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200245 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100246 * \param ctx The CCM context to use for decryption. This must be
247 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000248 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100249 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100250 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100251 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
252 * or 13. The length L of the message length field is
253 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100254 * \param add The additional data field. This must be a readable buffer
255 * of at least that \p add_len Bytes..
Rose Zadikeecdbea2018-01-24 12:56:53 +0000256 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100257 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100258 * \param input The buffer holding the input data. If \p length is greater
259 * than zero, \p input must be a readable buffer of at least
260 * that length.
261 * \param output The buffer holding the output data. If \p length is greater
262 * than zero, \p output must be a writable buffer of at least
263 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100264 * \param tag The buffer holding the authentication field. This must be a
265 * readable buffer of at least \p tag_len Bytes.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100266 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100267 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200268 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100269 * \return \c 0 on success. This indicates that the message is authentic.
270 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
271 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200274 const unsigned char *iv, size_t iv_len,
275 const unsigned char *add, size_t add_len,
276 const unsigned char *input, unsigned char *output,
277 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200278
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100279/**
280 * \brief This function performs a CCM* authenticated decryption of a
281 * buffer.
282 *
283 * \note When using this function in a variable tag length context,
284 * the tag length has to be decoded from \p iv and passed to
285 * this function as \p tag_len. (\p tag needs to be adjusted
286 * accordingly.)
287 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100288 * \param ctx The CCM context to use for decryption. This must be
289 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100290 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100291 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100292 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100293 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
294 * or 13. The length L of the message length field is
295 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100296 * \param add The additional data field. This must be a readable buffer of
297 * at least that \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100298 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100299 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100300 * \param input The buffer holding the input data. If \p length is greater
301 * than zero, \p input must be a readable buffer of at least
302 * that length.
303 * \param output The buffer holding the output data. If \p length is greater
304 * than zero, \p output must be a writable buffer of at least
305 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100306 * \param tag The buffer holding the authentication field. This must be a
307 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100308 * \param tag_len The length of the authentication field in Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100309 * 0, 4, 6, 8, 10, 12, 14 or 16.
310 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100311 * \warning Passing \c 0 as \p tag_len means that the message is nos
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100312 * longer authenticated.
313 *
Janos Follath143b3192018-05-30 13:57:29 +0100314 * \return \c 0 on success.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100315 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
316 * \return A cipher-specific error code on calculation failure.
317 */
318int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
319 const unsigned char *iv, size_t iv_len,
320 const unsigned char *add, size_t add_len,
321 const unsigned char *input, unsigned char *output,
322 const unsigned char *tag, size_t tag_len );
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200325/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000326 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200327 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100328 * \return \c 0 on success.
329 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_ccm_self_test( int verbose );
332#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200333
334#ifdef __cplusplus
335}
336#endif
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#endif /* MBEDTLS_CCM_H */