blob: 8585ce5e7c9c6bb2b91bbfde804521802b041718 [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 *
Darryl Greena40a1012018-01-05 15:33:17 +000017 */
18/*
Rose Zadikeecdbea2018-01-24 12:56:53 +000019 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020020 * SPDX-License-Identifier: Apache-2.0
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020033 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000034 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020035 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#ifndef MBEDTLS_CCM_H
38#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020039
40#include "cipher.h"
41
Rose Zadikeecdbea2018-01-24 12:56:53 +000042#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
43#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
44#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020045
Steven Cooreman222e2ff2017-04-04 11:37:15 +020046
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Ron Eldor4e6d55d2018-02-07 16:36:15 +020051#if !defined(MBEDTLS_CCM_ALT)
52// Regular implementation
53//
54
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020055/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000056 * \brief The CCM context-type definition. The CCM context is passed
57 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020058 */
59typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000060 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020063
Ron Eldor4e6d55d2018-02-07 16:36:15 +020064#else /* MBEDTLS_CCM_ALT */
65#include "ccm_alt.h"
66#endif /* MBEDTLS_CCM_ALT */
67
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020068/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000069 * \brief This function initializes the specified CCM context,
70 * to make references valid, and prepare the context
71 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020072 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000073 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020074 */
75void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
76
77/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000078 * \brief This function initializes the CCM context set in the
79 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020080 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000081 * \param ctx The CCM context to initialize.
82 * \param cipher The 128-bit block cipher to use.
83 * \param key The encryption key.
84 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020085 *
Rose Zadik4ee9d242018-03-26 17:18:44 +010086 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +010087 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020088 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020089int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
90 mbedtls_cipher_id_t cipher,
91 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020092 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020093
94/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000095 * \brief This function releases and clears the specified CCM context
96 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020097 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000098 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200101
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200102/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000103 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200104 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100105 *
106 * \note The tag is written to a separate buffer. To concatenate
107 * the \p tag with the \p output, as done in <em>RFC-3610:
108 * Counter with CBC-MAC (CCM)</em>, use
109 * \p tag = \p output + \p length, and make sure that the
110 * output buffer is at least \p length + \p tag_len wide.
111 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000112 * \param ctx The CCM context to use for encryption.
113 * \param length The length of the input data in Bytes.
114 * \param iv Initialization vector (nonce).
115 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
116 * \param add The additional data field.
117 * \param add_len The length of additional data in Bytes.
118 * Must be less than 2^16 - 2^8.
119 * \param input The buffer holding the input data.
120 * \param output The buffer holding the output data.
121 * Must be at least \p length Bytes wide.
122 * \param tag The buffer holding the tag.
123 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100124 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200125 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000126 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100127 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200130 const unsigned char *iv, size_t iv_len,
131 const unsigned char *add, size_t add_len,
132 const unsigned char *input, unsigned char *output,
133 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200134
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200135/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000136 * \brief This function performs a CCM authenticated decryption of a
137 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200138 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000139 * \param ctx The CCM context to use for decryption.
140 * \param length The length of the input data in Bytes.
141 * \param iv Initialization vector.
142 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
143 * \param add The additional data field.
144 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100145 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000146 * \param input The buffer holding the input data.
147 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100148 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000149 * \param tag The buffer holding the tag.
150 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100151 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200152 *
Rose Zadik379b95c2018-04-17 16:43:00 +0100153 * \return \c 0 on success. This indicates that the message is authentic.
154 * \return #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
155 * \return A cipher-specific error code on calculation failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200158 const unsigned char *iv, size_t iv_len,
159 const unsigned char *add, size_t add_len,
160 const unsigned char *input, unsigned char *output,
161 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200162
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200165/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000166 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200167 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100168 * \return \c 0 on success.
169 * \return \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171int mbedtls_ccm_self_test( int verbose );
172#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200173
174#ifdef __cplusplus
175}
176#endif
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_CCM_H */