blob: 3647d5094f5415dbc746af01b194aaf176d902a8 [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/*
Rose Zadikeecdbea2018-01-24 12:56:53 +000031 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
72 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000073 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020074 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#ifndef MBEDTLS_CCM_H
77#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020078
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020079#if !defined(MBEDTLS_CONFIG_FILE)
80#include "config.h"
81#else
82#include MBEDTLS_CONFIG_FILE
83#endif
84
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020085#include "cipher.h"
86
Rose Zadikeecdbea2018-01-24 12:56:53 +000087#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
88#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030089
90/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadikeecdbea2018-01-24 12:56:53 +000091#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020092
93#ifdef __cplusplus
94extern "C" {
95#endif
96
Ron Eldor4e6d55d2018-02-07 16:36:15 +020097#if !defined(MBEDTLS_CCM_ALT)
98// Regular implementation
99//
100
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200101/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000102 * \brief The CCM context-type definition. The CCM context is passed
103 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200104 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200105typedef struct mbedtls_ccm_context
106{
Rose Zadikeecdbea2018-01-24 12:56:53 +0000107 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200110
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200111#else /* MBEDTLS_CCM_ALT */
112#include "ccm_alt.h"
113#endif /* MBEDTLS_CCM_ALT */
114
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200115/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000116 * \brief This function initializes the specified CCM context,
117 * to make references valid, and prepare the context
118 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200119 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100120 * \param ctx The CCM context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200121 */
122void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
123
124/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000125 * \brief This function initializes the CCM context set in the
126 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200127 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100128 * \param ctx The CCM context to initialize. This must be an initialized
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100129 * context.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000130 * \param cipher The 128-bit block cipher to use.
k-stachowiakb92f9332018-12-13 11:13:51 +0100131 * \param key The encryption key. This must not be \c NULL.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000132 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200133 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100134 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100135 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200136 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200137int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
138 mbedtls_cipher_id_t cipher,
139 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200140 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200141
142/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000143 * \brief This function releases and clears the specified CCM context
144 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200145 *
k-stachowiak9da5d7c2018-12-13 17:19:48 +0100146 * \param ctx The CCM context to clear. If this is \c NULL, the function
147 * has no effect. Otherwise, this must be initialized.
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 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100160 * \param ctx The CCM context to use for encryption. This must be
161 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000162 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100163 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100164 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100165 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
166 * or 13. The length L of the message length field is
167 * 15 - \p iv_len.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100168 * \param add The additional data field. If \p add_len is greater than
169 * zero, \p add must be a readable buffer of at least that
k-stachowiak247a7822018-12-19 13:36:03 +0100170 * length.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000171 * \param add_len The length of additional data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100172 * This must be less than `2^16 - 2^8`.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100173 * \param input The buffer holding the input data. If \p length is greater
174 * than zero, \p input must be a readable buffer of at least
175 * that length.
176 * \param output The buffer holding the output data. If \p length is greater
177 * than zero, \p output must be a writable buffer of at least
178 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100179 * \param tag The buffer holding the authentication field. This must be a
180 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100181 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100182 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200183 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000184 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100185 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200188 const unsigned char *iv, size_t iv_len,
189 const unsigned char *add, size_t add_len,
190 const unsigned char *input, unsigned char *output,
191 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200192
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200193/**
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100194 * \brief This function encrypts a buffer using CCM*.
195 *
196 * \note The tag is written to a separate buffer. To concatenate
197 * the \p tag with the \p output, as done in <em>RFC-3610:
198 * Counter with CBC-MAC (CCM)</em>, use
199 * \p tag = \p output + \p length, and make sure that the
200 * output buffer is at least \p length + \p tag_len wide.
201 *
202 * \note When using this function in a variable tag length context,
203 * the tag length has to be encoded into the \p iv passed to
204 * this function.
205 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100206 * \param ctx The CCM context to use for encryption. This must be
207 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100208 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100209 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100210 * 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.
k-stachowiak247a7822018-12-19 13:36:03 +0100214 * \param add The additional data field. This must be a readable buffer of
215 * at least \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100216 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100217 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100218 * \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.
k-stachowiak247a7822018-12-19 13:36:03 +0100224 * \param tag The buffer holding the authentication field. This must be a
225 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100226 * \param tag_len The length of the authentication field to generate in Bytes:
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100227 * 0, 4, 6, 8, 10, 12, 14 or 16.
228 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100229 * \warning Passing \c 0 as \p tag_len means that the message is no
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100230 * longer authenticated.
231 *
232 * \return \c 0 on success.
233 * \return A CCM or cipher-specific error code on failure.
234 */
235int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
236 const unsigned char *iv, size_t iv_len,
237 const unsigned char *add, size_t add_len,
238 const unsigned char *input, unsigned char *output,
239 unsigned char *tag, size_t tag_len );
240
241/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000242 * \brief This function performs a CCM authenticated decryption of a
243 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200244 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100245 * \param ctx The CCM context to use for decryption. This must be
246 * initialized and bound to a key.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000247 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100248 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100249 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100250 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
251 * or 13. The length L of the message length field is
252 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100253 * \param add The additional data field. This must be a readable buffer
254 * of at least that \p add_len Bytes..
Rose Zadikeecdbea2018-01-24 12:56:53 +0000255 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100256 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100257 * \param input The buffer holding the input data. If \p length is greater
258 * than zero, \p input must be a readable buffer of at least
259 * that length.
260 * \param output The buffer holding the output data. If \p length is greater
261 * than zero, \p output must be a writable buffer of at least
262 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100263 * \param tag The buffer holding the authentication field. This must be a
264 * readable buffer of at least \p tag_len Bytes.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100265 * \param tag_len The length of the authentication field to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100266 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200267 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100268 * \return \c 0 on success. This indicates that the message is authentic.
269 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
270 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200273 const unsigned char *iv, size_t iv_len,
274 const unsigned char *add, size_t add_len,
275 const unsigned char *input, unsigned char *output,
276 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200277
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100278/**
279 * \brief This function performs a CCM* authenticated decryption of a
280 * buffer.
281 *
282 * \note When using this function in a variable tag length context,
283 * the tag length has to be decoded from \p iv and passed to
284 * this function as \p tag_len. (\p tag needs to be adjusted
285 * accordingly.)
286 *
k-stachowiak6adb0572018-12-18 10:22:34 +0100287 * \param ctx The CCM context to use for decryption. This must be
288 * initialized and bound to a key.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100289 * \param length The length of the input data in Bytes.
k-stachowiak6adb0572018-12-18 10:22:34 +0100290 * \param iv The initialization vector (nonce). This must be a readable
k-stachowiakb92f9332018-12-13 11:13:51 +0100291 * buffer of at least \p iv_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100292 * \param iv_len The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
293 * or 13. The length L of the message length field is
294 * 15 - \p iv_len.
k-stachowiak247a7822018-12-19 13:36:03 +0100295 * \param add The additional data field. This must be a readable buffer of
296 * at least that \p add_len Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100297 * \param add_len The length of additional data in Bytes.
k-stachowiakb92f9332018-12-13 11:13:51 +0100298 * This must be less than 2^16 - 2^8.
k-stachowiak12f0d5c2018-12-11 16:25:08 +0100299 * \param input The buffer holding the input data. If \p length is greater
300 * than zero, \p input must be a readable buffer of at least
301 * that length.
302 * \param output The buffer holding the output data. If \p length is greater
303 * than zero, \p output must be a writable buffer of at least
304 * that length.
k-stachowiak247a7822018-12-19 13:36:03 +0100305 * \param tag The buffer holding the authentication field. This must be a
306 * readable buffer of at least \p tag_len Bytes.
Janos Follath6b4bd3d2018-05-29 11:30:26 +0100307 * \param tag_len The length of the authentication field in Bytes.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100308 * 0, 4, 6, 8, 10, 12, 14 or 16.
309 *
k-stachowiakb92f9332018-12-13 11:13:51 +0100310 * \warning Passing \c 0 as \p tag_len means that the message is nos
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100311 * longer authenticated.
312 *
Janos Follath143b3192018-05-30 13:57:29 +0100313 * \return \c 0 on success.
Janos Follath5dc8cfa2018-04-27 14:45:49 +0100314 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
315 * \return A cipher-specific error code on calculation failure.
316 */
317int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
318 const unsigned char *iv, size_t iv_len,
319 const unsigned char *add, size_t add_len,
320 const unsigned char *input, unsigned char *output,
321 const unsigned char *tag, size_t tag_len );
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200324/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000325 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200326 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100327 * \return \c 0 on success.
328 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330int mbedtls_ccm_self_test( int verbose );
331#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200332
333#ifdef __cplusplus
334}
335#endif
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_CCM_H */