blob: 5a9ee4a1cdad914eaf8bf83515e1a6b2fba17a6a [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
37#include "cipher.h"
38
Rose Zadikeecdbea2018-01-24 12:56:53 +000039#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to the function. */
40#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
41#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020042
Steven Cooreman222e2ff2017-04-04 11:37:15 +020043#if !defined(MBEDTLS_CCM_ALT)
44// Regular implementation
45//
46
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020051/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000052 * \brief The CCM context-type definition. The CCM context is passed
53 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020054 */
55typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000056 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020059
60/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000061 * \brief This function initializes the specified CCM context,
62 * to make references valid, and prepare the context
63 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020064 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000065 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020066 */
67void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
68
69/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000070 * \brief This function initializes the CCM context set in the
71 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020072 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000073 * \param ctx The CCM context to initialize.
74 * \param cipher The 128-bit block cipher to use.
75 * \param key The encryption key.
76 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020077 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000078 * \return \c 0 on success, or a cipher-specific error code.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020079 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020080int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
81 mbedtls_cipher_id_t cipher,
82 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020083 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020084
85/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000086 * \brief This function releases and clears the specified CCM context
87 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020088 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000089 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020092
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020093/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000094 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020095 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000096 * \param ctx The CCM context to use for encryption.
97 * \param length The length of the input data in Bytes.
98 * \param iv Initialization vector (nonce).
99 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
100 * \param add The additional data field.
101 * \param add_len The length of additional data in Bytes.
102 * Must be less than 2^16 - 2^8.
103 * \param input The buffer holding the input data.
104 * \param output The buffer holding the output data.
105 * Must be at least \p length Bytes wide.
106 * \param tag The buffer holding the tag.
107 * \param tag_len The length of the tag to generate in Bytes:
108 * 4, 6, 8, 10, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200109 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000110 * \note The tag is written to a separate buffer. To concatenate
111 * the \p tag with the \p output, as done in <em>RFC-3610:
112 * Counter with CBC-MAC (CCM)</em>, use
113 * \p tag = \p output + \p length, and make sure that the
114 * output buffer is at least \p length + \p tag_len wide.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200115 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000116 * \return \c 0 on success.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200119 const unsigned char *iv, size_t iv_len,
120 const unsigned char *add, size_t add_len,
121 const unsigned char *input, unsigned char *output,
122 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200123
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200124/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000125 * \brief This function performs a CCM authenticated decryption of a
126 * buffer.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200127 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000128 * \param ctx The CCM context to use for decryption.
129 * \param length The length of the input data in Bytes.
130 * \param iv Initialization vector.
131 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
132 * \param add The additional data field.
133 * \param add_len The length of additional data in Bytes.
134 * \param input The buffer holding the input data.
135 * \param output The buffer holding the output data.
136 * \param tag The buffer holding the tag.
137 * \param tag_len The length of the tag in Bytes.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200138 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000139 * \return 0 if successful and authenticated, or
140 * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200143 const unsigned char *iv, size_t iv_len,
144 const unsigned char *add, size_t add_len,
145 const unsigned char *input, unsigned char *output,
146 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200147
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200148#ifdef __cplusplus
149}
150#endif
151
Rose Zadikeecdbea2018-01-24 12:56:53 +0000152#else /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200153#include "ccm_alt.h"
Rose Zadikeecdbea2018-01-24 12:56:53 +0000154#endif /* MBEDTLS_CCM_ALT */
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200155
156#ifdef __cplusplus
157extern "C" {
158#endif
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200161/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000162 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200163 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000164 * \return \c 0 on success, or \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166int mbedtls_ccm_self_test( int verbose );
167#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200168
169#ifdef __cplusplus
170}
171#endif
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_CCM_H */