blob: e311e751d53e4f333e7ddb33dad97a7ea6328e68 [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/*
Rose Zadikeecdbea2018-01-24 12:56:53 +000016 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020030 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000031 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020032 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#ifndef MBEDTLS_CCM_H
35#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020036
Ron Eldor0559c662018-02-14 16:02:41 +020037#if !defined(MBEDTLS_CONFIG_FILE)
38#include "config.h"
39#else
40#include MBEDTLS_CONFIG_FILE
41#endif
42
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020043#include "cipher.h"
44
Rose Zadikeecdbea2018-01-24 12:56:53 +000045#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
46#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
47#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020048
Steven Cooreman222e2ff2017-04-04 11:37:15 +020049#if !defined(MBEDTLS_CCM_ALT)
50// Regular implementation
51//
52
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020057/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000058 * \brief The CCM context-type definition. The CCM context is passed
59 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020060 */
61typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000062 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020065
66/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000067 * \brief This function initializes the specified CCM context,
68 * to make references valid, and prepare the context
69 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020070 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000071 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020072 */
73void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
74
75/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000076 * \brief This function initializes the CCM context set in the
77 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020078 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000079 * \param ctx The CCM context to initialize.
80 * \param cipher The 128-bit block cipher to use.
81 * \param key The encryption key.
82 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020083 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000084 * \return \c 0 on success, or a cipher-specific error code.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020085 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020086int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
87 mbedtls_cipher_id_t cipher,
88 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020089 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090
91/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000092 * \brief This function releases and clears the specified CCM context
93 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020094 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000095 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020098
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020099/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000100 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200101 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000102 * \param ctx The CCM context to use for encryption.
103 * \param length The length of the input data in Bytes.
104 * \param iv Initialization vector (nonce).
105 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
106 * \param add The additional data field.
107 * \param add_len The length of additional data in Bytes.
108 * Must be less than 2^16 - 2^8.
109 * \param input The buffer holding the input data.
110 * \param output The buffer holding the output data.
111 * Must be at least \p length Bytes wide.
112 * \param tag The buffer holding the tag.
113 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100114 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200115 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000116 * \note The tag is written to a separate buffer. To concatenate
117 * the \p tag with the \p output, as done in <em>RFC-3610:
118 * Counter with CBC-MAC (CCM)</em>, use
119 * \p tag = \p output + \p length, and make sure that the
120 * output buffer is at least \p length + \p tag_len wide.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200121 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000122 * \return \c 0 on success.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200125 const unsigned char *iv, size_t iv_len,
126 const unsigned char *add, size_t add_len,
127 const unsigned char *input, unsigned char *output,
128 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200129
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200130/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000131 * \brief This function performs a CCM authenticated decryption of a
132 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200133 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000134 * \param ctx The CCM context to use for decryption.
135 * \param length The length of the input data in Bytes.
136 * \param iv Initialization vector.
137 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
138 * \param add The additional data field.
139 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100140 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000141 * \param input The buffer holding the input data.
142 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100143 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000144 * \param tag The buffer holding the tag.
145 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100146 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200147 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000148 * \return 0 if successful and authenticated, or
149 * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200152 const unsigned char *iv, size_t iv_len,
153 const unsigned char *add, size_t add_len,
154 const unsigned char *input, unsigned char *output,
155 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200156
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200157#ifdef __cplusplus
158}
159#endif
160
Rose Zadikeecdbea2018-01-24 12:56:53 +0000161#else /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200162#include "ccm_alt.h"
Rose Zadikeecdbea2018-01-24 12:56:53 +0000163#endif /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200164
165#ifdef __cplusplus
166extern "C" {
167#endif
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200170/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000171 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200172 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000173 * \return \c 0 on success, or \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175int mbedtls_ccm_self_test( int verbose );
176#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200177
178#ifdef __cplusplus
179}
180#endif
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#endif /* MBEDTLS_CCM_H */