blob: 8f252c4bd0f0c5551f8ae82d74e5f4e7c390cd64 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/**
2 * \file ccm.h
3 *
Rose Zadik4ee9d242018-03-26 17:18:44 +01004 * \brief This file contains CCM definitions and functions.
5 *
6 * CCM combines Counter mode encryption with CBC-MAC authentication
7 * for 128-bit block ciphers.
Rose Zadikeecdbea2018-01-24 12:56:53 +00008 *
9 * Input to CCM includes the following elements:
10 * <ul><li>Payload - data that is both authenticated and encrypted.</li>
11 * <li>Associated data (Adata) - data that is authenticated but not
12 * encrypted, For example, a header.</li>
13 * <li>Nonce - A unique value that is assigned to the payload and the
14 * associated data.</li></ul>
15 *
Darryl Greena40a1012018-01-05 15:33:17 +000016 */
17/*
Rose Zadikeecdbea2018-01-24 12:56:53 +000018 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020032 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000033 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020034 */
Rose Zadikeecdbea2018-01-24 12:56:53 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#ifndef MBEDTLS_CCM_H
37#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020038
39#include "cipher.h"
40
Rose Zadikeecdbea2018-01-24 12:56:53 +000041#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
42#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
43#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020044
Steven Cooreman222e2ff2017-04-04 11:37:15 +020045#if !defined(MBEDTLS_CCM_ALT)
46// Regular implementation
47//
48
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020053/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000054 * \brief The CCM context-type definition. The CCM context is passed
55 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020056 */
57typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000058 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020061
62/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000063 * \brief This function initializes the specified CCM context,
64 * to make references valid, and prepare the context
65 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020066 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000067 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020068 */
69void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
70
71/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000072 * \brief This function initializes the CCM context set in the
73 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020074 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000075 * \param ctx The CCM context to initialize.
76 * \param cipher The 128-bit block cipher to use.
77 * \param key The encryption key.
78 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020079 *
Rose Zadik4ee9d242018-03-26 17:18:44 +010080 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +010081 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020082 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020083int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
84 mbedtls_cipher_id_t cipher,
85 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020086 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020087
88/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000089 * \brief This function releases and clears the specified CCM context
90 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020091 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000092 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020095
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020096/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000097 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020098 *
Rose Zadik4ee9d242018-03-26 17:18:44 +010099 *
100 * \note The tag is written to a separate buffer. To concatenate
101 * the \p tag with the \p output, as done in <em>RFC-3610:
102 * Counter with CBC-MAC (CCM)</em>, use
103 * \p tag = \p output + \p length, and make sure that the
104 * output buffer is at least \p length + \p tag_len wide.
105 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000106 * \param ctx The CCM context to use for encryption.
107 * \param length The length of the input data in Bytes.
108 * \param iv Initialization vector (nonce).
109 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
110 * \param add The additional data field.
111 * \param add_len The length of additional data in Bytes.
112 * Must be less than 2^16 - 2^8.
113 * \param input The buffer holding the input data.
114 * \param output The buffer holding the output data.
115 * Must be at least \p length Bytes wide.
116 * \param tag The buffer holding the tag.
117 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100118 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200119 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000120 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100121 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200124 const unsigned char *iv, size_t iv_len,
125 const unsigned char *add, size_t add_len,
126 const unsigned char *input, unsigned char *output,
127 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200128
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200129/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000130 * \brief This function performs a CCM authenticated decryption of a
131 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200132 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000133 * \param ctx The CCM context to use for decryption.
134 * \param length The length of the input data in Bytes.
135 * \param iv Initialization vector.
136 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
137 * \param add The additional data field.
138 * \param add_len The length of additional data in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100139 * Must be less than 2^16 - 2^8.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000140 * \param input The buffer holding the input data.
141 * \param output The buffer holding the output data.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100142 * Must be at least \p length Bytes wide.
Rose Zadikeecdbea2018-01-24 12:56:53 +0000143 * \param tag The buffer holding the tag.
144 * \param tag_len The length of the tag in Bytes.
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100145 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200146 *
Rose Zadikbd9571a2018-04-16 09:45:12 +0100147 * \return \c 0 on success.
Rose Zadikef871792018-04-17 10:41:48 +0100148 * \return A CCM or cipher-specific error code on failure.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200151 const unsigned char *iv, size_t iv_len,
152 const unsigned char *add, size_t add_len,
153 const unsigned char *input, unsigned char *output,
154 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200155
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200156#ifdef __cplusplus
157}
158#endif
159
Rose Zadikeecdbea2018-01-24 12:56:53 +0000160#else /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200161#include "ccm_alt.h"
Rose Zadikeecdbea2018-01-24 12:56:53 +0000162#endif /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200163
164#ifdef __cplusplus
165extern "C" {
166#endif
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200169/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000170 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200171 *
Rose Zadik4ee9d242018-03-26 17:18:44 +0100172 * \return \c 0 on success.
173 * \return \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 */