ccm 14.7 KB
Newer Older
ROOL's avatar
ROOL committed
1 2 3
/**
 * \file ccm.h
 *
ROOL's avatar
ROOL committed
4 5 6 7 8
 * \brief This file provides an API for the CCM authenticated encryption
 *        mode for block ciphers.
 *
 * CCM combines Counter mode encryption with CBC-MAC authentication
 * for 128-bit block ciphers.
ROOL's avatar
ROOL committed
9 10 11 12 13 14 15 16
 *
 * Input to CCM includes the following elements:
 * <ul><li>Payload - data that is both authenticated and encrypted.</li>
 * <li>Associated data (Adata) - data that is authenticated but not
 * encrypted, For example, a header.</li>
 * <li>Nonce - A unique value that is assigned to the payload and the
 * associated data.</li></ul>
 *
ROOL's avatar
ROOL committed
17 18 19 20 21 22 23 24 25 26 27 28
 * Definition of CCM:
 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
 * RFC 3610 "Counter with CBC-MAC (CCM)"
 *
 * Related:
 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
 *
 * Definition of CCM*:
 * IEEE 802.15.4 - IEEE Standard for Local and metropolitan area networks
 * Integer representation is fixed most-significant-octet-first order and
 * the representation of octets is most-significant-bit-first order. This is
 * consistent with RFC 3610.
ROOL's avatar
ROOL committed
29 30 31
 */
/*
 *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
ROOL's avatar
ROOL committed
32 33 34 35 36 37 38
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
 *
 *  This file is provided under the Apache License 2.0, or the
 *  GNU General Public License v2.0 or later.
 *
 *  **********
 *  Apache License 2.0:
ROOL's avatar
ROOL committed
39 40 41 42 43 44 45 46 47 48 49 50 51
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
ROOL's avatar
ROOL committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
 *  **********
 *
 *  **********
 *  GNU General Public License v2.0 or later:
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  **********
 *
ROOL's avatar
ROOL committed
73 74 75 76 77 78
 *  This file is part of Mbed TLS (https://tls.mbed.org)
 */

#ifndef MBEDTLS_CCM_H
#define MBEDTLS_CCM_H

ROOL's avatar
ROOL committed
79 80 81 82 83 84
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

ROOL's avatar
ROOL committed
85 86 87 88
#include "cipher.h"

#define MBEDTLS_ERR_CCM_BAD_INPUT       -0x000D /**< Bad input parameters to the function. */
#define MBEDTLS_ERR_CCM_AUTH_FAILED     -0x000F /**< Authenticated decryption failed. */
ROOL's avatar
ROOL committed
89 90

/* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */
ROOL's avatar
ROOL committed
91 92 93 94 95 96
#define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */

#ifdef __cplusplus
extern "C" {
#endif

ROOL's avatar
ROOL committed
97 98 99 100
#if !defined(MBEDTLS_CCM_ALT)
// Regular implementation
//

ROOL's avatar
ROOL committed
101 102 103 104
/**
 * \brief    The CCM context-type definition. The CCM context is passed
 *           to the APIs called.
 */
ROOL's avatar
ROOL committed
105 106
typedef struct mbedtls_ccm_context
{
ROOL's avatar
ROOL committed
107 108 109 110
    mbedtls_cipher_context_t cipher_ctx;    /*!< The cipher context used. */
}
mbedtls_ccm_context;

ROOL's avatar
ROOL committed
111 112 113 114
#else  /* MBEDTLS_CCM_ALT */
#include "ccm_alt.h"
#endif /* MBEDTLS_CCM_ALT */

ROOL's avatar
ROOL committed
115 116 117 118 119
/**
 * \brief           This function initializes the specified CCM context,
 *                  to make references valid, and prepare the context
 *                  for mbedtls_ccm_setkey() or mbedtls_ccm_free().
 *
ROOL's avatar
ROOL committed
120
 * \param ctx       The CCM context to initialize. This must not be \c NULL.
ROOL's avatar
ROOL committed
121 122 123 124 125 126 127
 */
void mbedtls_ccm_init( mbedtls_ccm_context *ctx );

/**
 * \brief           This function initializes the CCM context set in the
 *                  \p ctx parameter and sets the encryption key.
 *
ROOL's avatar
ROOL committed
128 129
 * \param ctx       The CCM context to initialize. This must be an initialized
 *                  context.
ROOL's avatar
ROOL committed
130
 * \param cipher    The 128-bit block cipher to use.
ROOL's avatar
ROOL committed
131
 * \param key       The encryption key. This must not be \c NULL.
ROOL's avatar
ROOL committed
132 133
 * \param keybits   The key size in bits. This must be acceptable by the cipher.
 *
ROOL's avatar
ROOL committed
134 135
 * \return          \c 0 on success.
 * \return          A CCM or cipher-specific error code on failure.
ROOL's avatar
ROOL committed
136 137 138 139 140 141 142 143 144 145
 */
int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
                        mbedtls_cipher_id_t cipher,
                        const unsigned char *key,
                        unsigned int keybits );

/**
 * \brief   This function releases and clears the specified CCM context
 *          and underlying cipher sub-context.
 *
ROOL's avatar
ROOL committed
146 147
 * \param ctx       The CCM context to clear. If this is \c NULL, the function
 *                  has no effect. Otherwise, this must be initialized.
ROOL's avatar
ROOL committed
148 149 150 151 152 153
 */
void mbedtls_ccm_free( mbedtls_ccm_context *ctx );

/**
 * \brief           This function encrypts a buffer using CCM.
 *
ROOL's avatar
ROOL committed
154 155 156 157 158 159
 * \note            The tag is written to a separate buffer. To concatenate
 *                  the \p tag with the \p output, as done in <em>RFC-3610:
 *                  Counter with CBC-MAC (CCM)</em>, use
 *                  \p tag = \p output + \p length, and make sure that the
 *                  output buffer is at least \p length + \p tag_len wide.
 *
ROOL's avatar
ROOL committed
160 161
 * \param ctx       The CCM context to use for encryption. This must be
 *                  initialized and bound to a key.
ROOL's avatar
ROOL committed
162
 * \param length    The length of the input data in Bytes.
ROOL's avatar
ROOL committed
163 164
 * \param iv        The initialization vector (nonce). This must be a readable
 *                  buffer of at least \p iv_len Bytes.
ROOL's avatar
ROOL committed
165 166 167
 * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
 *                  or 13. The length L of the message length field is
 *                  15 - \p iv_len.
ROOL's avatar
ROOL committed
168 169 170
 * \param add       The additional data field. If \p add_len is greater than
 *                  zero, \p add must be a readable buffer of at least that
 *                  length.
ROOL's avatar
ROOL committed
171
 * \param add_len   The length of additional data in Bytes.
ROOL's avatar
ROOL committed
172 173 174 175 176 177 178 179 180
 *                  This must be less than `2^16 - 2^8`.
 * \param input     The buffer holding the input data. If \p length is greater
 *                  than zero, \p input must be a readable buffer of at least
 *                  that length.
 * \param output    The buffer holding the output data. If \p length is greater
 *                  than zero, \p output must be a writable buffer of at least
 *                  that length.
 * \param tag       The buffer holding the authentication field. This must be a
 *                  readable buffer of at least \p tag_len Bytes.
ROOL's avatar
ROOL committed
181
 * \param tag_len   The length of the authentication field to generate in Bytes:
ROOL's avatar
ROOL committed
182 183 184
 *                  4, 6, 8, 10, 12, 14 or 16.
 *
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
185
 * \return          A CCM or cipher-specific error code on failure.
ROOL's avatar
ROOL committed
186 187 188 189 190 191 192
 */
int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
                         const unsigned char *iv, size_t iv_len,
                         const unsigned char *add, size_t add_len,
                         const unsigned char *input, unsigned char *output,
                         unsigned char *tag, size_t tag_len );

ROOL's avatar
ROOL committed
193 194 195 196 197 198 199 200 201 202 203 204 205
/**
 * \brief           This function encrypts a buffer using CCM*.
 *
 * \note            The tag is written to a separate buffer. To concatenate
 *                  the \p tag with the \p output, as done in <em>RFC-3610:
 *                  Counter with CBC-MAC (CCM)</em>, use
 *                  \p tag = \p output + \p length, and make sure that the
 *                  output buffer is at least \p length + \p tag_len wide.
 *
 * \note            When using this function in a variable tag length context,
 *                  the tag length has to be encoded into the \p iv passed to
 *                  this function.
 *
ROOL's avatar
ROOL committed
206 207
 * \param ctx       The CCM context to use for encryption. This must be
 *                  initialized and bound to a key.
ROOL's avatar
ROOL committed
208
 * \param length    The length of the input data in Bytes.
ROOL's avatar
ROOL committed
209 210
 * \param iv        The initialization vector (nonce). This must be a readable
 *                  buffer of at least \p iv_len Bytes.
ROOL's avatar
ROOL committed
211 212 213
 * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
 *                  or 13. The length L of the message length field is
 *                  15 - \p iv_len.
ROOL's avatar
ROOL committed
214 215
 * \param add       The additional data field. This must be a readable buffer of
 *                  at least \p add_len Bytes.
ROOL's avatar
ROOL committed
216
 * \param add_len   The length of additional data in Bytes.
ROOL's avatar
ROOL committed
217 218 219 220 221 222 223 224 225
 *                  This must be less than 2^16 - 2^8.
 * \param input     The buffer holding the input data. If \p length is greater
 *                  than zero, \p input must be a readable buffer of at least
 *                  that length.
 * \param output    The buffer holding the output data. If \p length is greater
 *                  than zero, \p output must be a writable buffer of at least
 *                  that length.
 * \param tag       The buffer holding the authentication field. This must be a
 *                  readable buffer of at least \p tag_len Bytes.
ROOL's avatar
ROOL committed
226 227 228
 * \param tag_len   The length of the authentication field to generate in Bytes:
 *                  0, 4, 6, 8, 10, 12, 14 or 16.
 *
ROOL's avatar
ROOL committed
229
 * \warning         Passing \c 0 as \p tag_len means that the message is no
ROOL's avatar
ROOL committed
230 231 232 233 234 235 236 237 238 239 240
 *                  longer authenticated.
 *
 * \return          \c 0 on success.
 * \return          A CCM or cipher-specific error code on failure.
 */
int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
                         const unsigned char *iv, size_t iv_len,
                         const unsigned char *add, size_t add_len,
                         const unsigned char *input, unsigned char *output,
                         unsigned char *tag, size_t tag_len );

ROOL's avatar
ROOL committed
241 242 243 244
/**
 * \brief           This function performs a CCM authenticated decryption of a
 *                  buffer.
 *
ROOL's avatar
ROOL committed
245 246
 * \param ctx       The CCM context to use for decryption. This must be
 *                  initialized and bound to a key.
ROOL's avatar
ROOL committed
247
 * \param length    The length of the input data in Bytes.
ROOL's avatar
ROOL committed
248 249
 * \param iv        The initialization vector (nonce). This must be a readable
 *                  buffer of at least \p iv_len Bytes.
ROOL's avatar
ROOL committed
250 251 252
 * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
 *                  or 13. The length L of the message length field is
 *                  15 - \p iv_len.
ROOL's avatar
ROOL committed
253 254
 * \param add       The additional data field. This must be a readable buffer
 *                  of at least that \p add_len Bytes..
ROOL's avatar
ROOL committed
255
 * \param add_len   The length of additional data in Bytes.
ROOL's avatar
ROOL committed
256 257 258 259 260 261 262 263 264 265
 *                  This must be less than 2^16 - 2^8.
 * \param input     The buffer holding the input data. If \p length is greater
 *                  than zero, \p input must be a readable buffer of at least
 *                  that length.
 * \param output    The buffer holding the output data. If \p length is greater
 *                  than zero, \p output must be a writable buffer of at least
 *                  that length.
 * \param tag       The buffer holding the authentication field. This must be a
 *                  readable buffer of at least \p tag_len Bytes.
 * \param tag_len   The length of the authentication field to generate in Bytes:
ROOL's avatar
ROOL committed
266 267
 *                  4, 6, 8, 10, 12, 14 or 16.
 *
ROOL's avatar
ROOL committed
268 269 270
 * \return          \c 0 on success. This indicates that the message is authentic.
 * \return          #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
 * \return          A cipher-specific error code on calculation failure.
ROOL's avatar
ROOL committed
271 272 273 274 275 276 277
 */
int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
                      const unsigned char *iv, size_t iv_len,
                      const unsigned char *add, size_t add_len,
                      const unsigned char *input, unsigned char *output,
                      const unsigned char *tag, size_t tag_len );

ROOL's avatar
ROOL committed
278 279 280 281 282 283 284 285 286
/**
 * \brief           This function performs a CCM* authenticated decryption of a
 *                  buffer.
 *
 * \note            When using this function in a variable tag length context,
 *                  the tag length has to be decoded from \p iv and passed to
 *                  this function as \p tag_len. (\p tag needs to be adjusted
 *                  accordingly.)
 *
ROOL's avatar
ROOL committed
287 288
 * \param ctx       The CCM context to use for decryption. This must be
 *                  initialized and bound to a key.
ROOL's avatar
ROOL committed
289
 * \param length    The length of the input data in Bytes.
ROOL's avatar
ROOL committed
290 291
 * \param iv        The initialization vector (nonce). This must be a readable
 *                  buffer of at least \p iv_len Bytes.
ROOL's avatar
ROOL committed
292 293 294
 * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12,
 *                  or 13. The length L of the message length field is
 *                  15 - \p iv_len.
ROOL's avatar
ROOL committed
295 296
 * \param add       The additional data field. This must be a readable buffer of
 *                  at least that \p add_len Bytes.
ROOL's avatar
ROOL committed
297
 * \param add_len   The length of additional data in Bytes.
ROOL's avatar
ROOL committed
298 299 300 301 302 303 304 305 306
 *                  This must be less than 2^16 - 2^8.
 * \param input     The buffer holding the input data. If \p length is greater
 *                  than zero, \p input must be a readable buffer of at least
 *                  that length.
 * \param output    The buffer holding the output data. If \p length is greater
 *                  than zero, \p output must be a writable buffer of at least
 *                  that length.
 * \param tag       The buffer holding the authentication field. This must be a
 *                  readable buffer of at least \p tag_len Bytes.
ROOL's avatar
ROOL committed
307 308 309
 * \param tag_len   The length of the authentication field in Bytes.
 *                  0, 4, 6, 8, 10, 12, 14 or 16.
 *
ROOL's avatar
ROOL committed
310
 * \warning         Passing \c 0 as \p tag_len means that the message is nos
ROOL's avatar
ROOL committed
311 312 313 314 315 316 317 318 319 320 321
 *                  longer authenticated.
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_CCM_AUTH_FAILED if the tag does not match.
 * \return          A cipher-specific error code on calculation failure.
 */
int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
                      const unsigned char *iv, size_t iv_len,
                      const unsigned char *add, size_t add_len,
                      const unsigned char *input, unsigned char *output,
                      const unsigned char *tag, size_t tag_len );
ROOL's avatar
ROOL committed
322 323 324 325 326

#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
/**
 * \brief          The CCM checkup routine.
 *
ROOL's avatar
ROOL committed
327 328
 * \return         \c 0 on success.
 * \return         \c 1 on failure.
ROOL's avatar
ROOL committed
329 330 331 332 333 334 335 336 337
 */
int mbedtls_ccm_self_test( int verbose );
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */

#ifdef __cplusplus
}
#endif

#endif /* MBEDTLS_CCM_H */