blob: 48f73edda7abfaad7349decb9d7a78f63e4cee10 [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
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Ron Eldor4e6d55d2018-02-07 16:36:15 +020048#if !defined(MBEDTLS_CCM_ALT)
49// Regular implementation
50//
51
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020052/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000053 * \brief The CCM context-type definition. The CCM context is passed
54 * to the APIs called.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020055 */
56typedef struct {
Rose Zadikeecdbea2018-01-24 12:56:53 +000057 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020060
Ron Eldor4e6d55d2018-02-07 16:36:15 +020061#else /* MBEDTLS_CCM_ALT */
62#include "ccm_alt.h"
63#endif /* MBEDTLS_CCM_ALT */
64
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020065/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000066 * \brief This function initializes the specified CCM context,
67 * to make references valid, and prepare the context
68 * for mbedtls_ccm_setkey() or mbedtls_ccm_free().
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020069 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000070 * \param ctx The CCM context to initialize.
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020071 */
72void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
73
74/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000075 * \brief This function initializes the CCM context set in the
76 * \p ctx parameter and sets the encryption key.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020077 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000078 * \param ctx The CCM context to initialize.
79 * \param cipher The 128-bit block cipher to use.
80 * \param key The encryption key.
81 * \param keybits The key size in bits. This must be acceptable by the cipher.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020082 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000083 * \return \c 0 on success, or a cipher-specific error code.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020084 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020085int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
86 mbedtls_cipher_id_t cipher,
87 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020088 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020089
90/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000091 * \brief This function releases and clears the specified CCM context
92 * and underlying cipher sub-context.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020093 *
Rose Zadikeecdbea2018-01-24 12:56:53 +000094 * \param ctx The CCM context to clear.
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020097
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020098/**
Rose Zadikeecdbea2018-01-24 12:56:53 +000099 * \brief This function encrypts a buffer using CCM.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200100 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000101 * \param ctx The CCM context to use for encryption.
102 * \param length The length of the input data in Bytes.
103 * \param iv Initialization vector (nonce).
104 * \param iv_len The length of the IV in Bytes: 7, 8, 9, 10, 11, 12, or 13.
105 * \param add The additional data field.
106 * \param add_len The length of additional data in Bytes.
107 * Must be less than 2^16 - 2^8.
108 * \param input The buffer holding the input data.
109 * \param output The buffer holding the output data.
110 * Must be at least \p length Bytes wide.
111 * \param tag The buffer holding the tag.
112 * \param tag_len The length of the tag to generate in Bytes:
Mathieu Briandffb6efd2018-02-07 10:29:27 +0100113 * 4, 6, 8, 10, 12, 14 or 16.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200114 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000115 * \note The tag is written to a separate buffer. To concatenate
116 * the \p tag with the \p output, as done in <em>RFC-3610:
117 * Counter with CBC-MAC (CCM)</em>, use
118 * \p tag = \p output + \p length, and make sure that the
119 * output buffer is at least \p length + \p tag_len wide.
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200120 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000121 * \return \c 0 on success.
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 Zadikeecdbea2018-01-24 12:56:53 +0000147 * \return 0 if successful and authenticated, or
148 * #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200158/**
Rose Zadikeecdbea2018-01-24 12:56:53 +0000159 * \brief The CCM checkup routine.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200160 *
Rose Zadikeecdbea2018-01-24 12:56:53 +0000161 * \return \c 0 on success, or \c 1 on failure.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_ccm_self_test( int verbose );
164#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200165
166#ifdef __cplusplus
167}
168#endif
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_CCM_H */