blob: ef75839baa84d8317e34b2175d64bdc467191b37 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/**
2 * \file ccm.h
3 *
4 * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_CCM_H
24#define MBEDTLS_CCM_H
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020025
26#include "cipher.h"
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */
29#define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020035/**
36 * \brief CCM context structure
37 */
38typedef struct {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041mbedtls_ccm_context;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020042
43/**
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020044 * \brief Initialize CCM context (just makes references valid)
45 * Makes the context ready for mbedtls_ccm_setkey() or
46 * mbedtls_ccm_free().
47 *
48 * \param ctx CCM context to initialize
49 */
50void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
51
52/**
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020053 * \brief CCM initialization (encryption and decryption)
54 *
55 * \param ctx CCM context to be initialized
56 * \param cipher cipher to use (a 128-bit block cipher)
57 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020058 * \param keybits key size in bits (must be acceptable by the cipher)
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020059 *
60 * \return 0 if successful, or a cipher specific error code
61 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020062int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
63 mbedtls_cipher_id_t cipher,
64 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020065 unsigned int keybits );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020066
67/**
68 * \brief Free a CCM context and underlying cipher sub-context
69 *
70 * \param ctx CCM context to free
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_ccm_free( mbedtls_ccm_context *ctx );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020073
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020074/**
75 * \brief CCM buffer encryption
76 *
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020077 * \param ctx CCM context
78 * \param length length of the input data in bytes
79 * \param iv nonce (initialization vector)
80 * \param iv_len length of IV in bytes
81 * must be 2, 3, 4, 5, 6, 7 or 8
82 * \param add additional data
83 * \param add_len length of additional data in bytes
84 * must be less than 2^16 - 2^8
85 * \param input buffer holding the input data
86 * \param output buffer for holding the output data
87 * must be at least 'length' bytes wide
88 * \param tag buffer for holding the tag
89 * \param tag_len length of the tag to generate in bytes
90 * must be 4, 6, 8, 10, 14 or 16
91 *
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020092 * \note The tag is written to a separate buffer. To get the tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020093 * concatenated with the output as in the CCM spec, use
94 * tag = output + length and make sure the output buffer is
95 * at least length + tag_len wide.
96 *
97 * \return 0 if successful
98 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200100 const unsigned char *iv, size_t iv_len,
101 const unsigned char *add, size_t add_len,
102 const unsigned char *input, unsigned char *output,
103 unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200104
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200105/**
106 * \brief CCM buffer authenticated decryption
107 *
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200108 * \param ctx CCM context
109 * \param length length of the input data
110 * \param iv initialization vector
111 * \param iv_len length of IV
112 * \param add additional data
113 * \param add_len length of additional data
114 * \param input buffer holding the input data
115 * \param output buffer for holding the output data
116 * \param tag buffer holding the tag
117 * \param tag_len length of the tag
118 *
119 * \return 0 if successful and authenticated,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200123 const unsigned char *iv, size_t iv_len,
124 const unsigned char *add, size_t add_len,
125 const unsigned char *input, unsigned char *output,
126 const unsigned char *tag, size_t tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200129/**
130 * \brief Checkup routine
131 *
132 * \return 0 if successful, or 1 if the test failed
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_ccm_self_test( int verbose );
135#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200136
137#ifdef __cplusplus
138}
139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#endif /* MBEDTLS_CCM_H */