blob: 80cc1f68c4f3588f3795d25134c76cae70aa2917 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/**
2 * \file ccm.h
3 *
Rose Zadikeecdbea2018-01-24 12:56:53 +00004 * \brief CCM combines Counter mode encryption with CBC-MAC authentication
5 * for 128-bit block ciphers.
6 *
7 * Input to CCM includes the following elements:
8 * <ul><li>Payload - data that is both authenticated and encrypted.</li>
9 * <li>Associated data (Adata) - data that is authenticated but not
10 * encrypted, For example, a header.</li>
11 * <li>Nonce - A unique value that is assigned to the payload and the
12 * associated data.</li></ul>
13 *
Darryl Greena40a1012018-01-05 15:33:17 +000014 */
15/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020016 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18 *
19 * This file is provided under the Apache License 2.0, or the
20 * GNU General Public License v2.0 or later.
21 *
22 * **********
23 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 *
25 * Licensed under the Apache License, Version 2.0 (the "License"); you may
26 * not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020036 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020037 * **********
38 *
39 * **********
40 * GNU General Public License v2.0 or later:
41 *
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License as published by
44 * the Free Software Foundation; either version 2 of the License, or
45 * (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License along
53 * with this program; if not, write to the Free Software Foundation, Inc.,
54 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 *
56 * **********
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020057 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#ifndef MBEDTLS_CCM_H
60#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020061
Ron Eldor0559c662018-02-14 16:02:41 +020062#if !defined(MBEDTLS_CONFIG_FILE)
63#include "config.h"
64#else
65#include MBEDTLS_CONFIG_FILE
66#endif
67
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020068#include "cipher.h"
69
Rose Zadikeecdbea2018-01-24 12:56:53 +000070#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
71#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
72#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020073
Steven Cooreman222e2ff2017-04-04 11:37:15 +020074#if !defined(MBEDTLS_CCM_ALT)
75// Regular implementation
76//
77
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020078#ifdef __cplusplus
79extern "C" {
80#endif
81
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020082/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000083 * \brief The CCM context-type definition. The CCM context is passed
84 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020085 */
86typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000087 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090
91/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000092 * \brief This function initializes the specified CCM context,
93 * to make references valid, and prepare the context
94 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020095 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000096 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020097 */
98void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
99
100/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000101 * \brief This function initializes the CCM context set in the
102 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200103 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000104 * \param ctx The CCM context to initialize.
105 * \param cipher The 128-bit block cipher to use.
106 * \param key The encryption key.
107 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200108 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000109 * \return \c 0 on success, or a cipher-specific error code.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200110 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200111int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
112 mbedtls_cipher_id_t cipher,
113 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200114 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200115
116/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000117 * \brief This function releases and clears the specified CCM context
118 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200119 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000120 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200123
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200124/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000125 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200126 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000127 * \param ctx The CCM context to use for encryption.
128 * \param length The length of the input data in Bytes.
129 * \param iv Initialization vector (nonce).
130 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
131 * \param add The additional data field.
132 * \param add_len The length of additional data in Bytes.
133 * Must be less than 2^16 - 2^8.
134 * \param input The buffer holding the input data.
135 * \param output The buffer holding the output data.
136 * Must be at least \p length Bytes wide.
137 * \param tag The buffer holding the tag.
138 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100139 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200140 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000141 * \note The tag is written to a separate buffer. To concatenate
142 * the \p tag with the \p output, as done in <em>RFC-3610:
143 * Counter with CBC-MAC (CCM)</em>, use
144 * \p tag = \p output + \p length, and make sure that the
145 * output buffer is at least \p length + \p tag_len wide.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200146 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000147 * \return \c 0 on success.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200150 const unsigned char *iv, size_t iv_len,
151 const unsigned char *add, size_t add_len,
152 const unsigned char *input, unsigned char *output,
153 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200154
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200155/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000156 * \brief This function performs a CCM authenticated decryption of a
157 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200158 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000159 * \param ctx The CCM context to use for decryption.
160 * \param length The length of the input data in Bytes.
161 * \param iv Initialization vector.
162 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
163 * \param add The additional data field.
164 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100165 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000166 * \param input The buffer holding the input data.
167 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100168 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000169 * \param tag The buffer holding the tag.
170 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100171 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200172 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000173 * \return 0 if successful and authenticated, or
174 * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200177 const unsigned char *iv, size_t iv_len,
178 const unsigned char *add, size_t add_len,
179 const unsigned char *input, unsigned char *output,
180 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200181
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200182#ifdef __cplusplus
183}
184#endif
185
Rose Zadikeecdbea2018-01-24 12:56:53 +0000186#else /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200187#include "ccm_alt.h"
Rose Zadikeecdbea2018-01-24 12:56:53 +0000188#endif /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200189
190#ifdef __cplusplus
191extern "C" {
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200195/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000196 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200197 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000198 * \return \c 0 on success, or \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ccm_self_test( int verbose );
201#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200202
203#ifdef __cplusplus
204}
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_CCM_H */