Mbed OS Reference
Loading...
Searching...
No Matches
cmac.h
Go to the documentation of this file.
1/**
2 * \file cmac.h
3 *
4 * \brief This file contains CMAC definitions and functions.
5 *
6 * The Cipher-based Message Authentication Code (CMAC) Mode for
7 * Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>.
8 */
9/*
10 * Copyright The Mbed TLS Contributors
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 */
25
26#ifndef MBEDTLS_CMAC_H
27#define MBEDTLS_CMAC_H
28
29#if !defined(MBEDTLS_CONFIG_FILE)
30#include "mbedtls/config.h"
31#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
35#include "mbedtls/cipher.h"
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42* \addtogroup mbedtls
43* \{
44* \defgroup mbedtls_cmac_module CMAC
45* \{
46*/
47
48/* MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED is deprecated and should not be used. */
49#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
50
51#define MBEDTLS_AES_BLOCK_SIZE 16
52#define MBEDTLS_DES3_BLOCK_SIZE 8
53
54#if defined(MBEDTLS_AES_C)
55#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
56#else
57#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
58#endif
59
60#if !defined(MBEDTLS_CMAC_ALT)
61
62/**
63 * The CMAC context structure.
64 */
66{
67 /** The internal state of the CMAC algorithm. */
69
70 /** Unprocessed data - either data that was not block aligned and is still
71 * pending processing, or the final block. */
73
74 /** The length of data pending processing. */
76};
77
78#else /* !MBEDTLS_CMAC_ALT */
79#include "cmac_alt.h"
80#endif /* !MBEDTLS_CMAC_ALT */
81
82/**
83 * \brief This function sets the CMAC key, and prepares to authenticate
84 * the input data.
85 * Must be called with an initialized cipher context.
86 *
87 * \param ctx The cipher context used for the CMAC operation, initialized
88 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
89 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
90 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
91 * \param key The CMAC key.
92 * \param keybits The length of the CMAC key in bits.
93 * Must be supported by the cipher.
94 *
95 * \return \c 0 on success.
96 * \return A cipher-specific error code on failure.
97 */
99 const unsigned char *key, size_t keybits );
100
101/**
102 * \brief This function feeds an input buffer into an ongoing CMAC
103 * computation.
104 *
105 * It is called between mbedtls_cipher_cmac_starts() or
106 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
107 * Can be called repeatedly.
108 *
109 * \param ctx The cipher context used for the CMAC operation.
110 * \param input The buffer holding the input data.
111 * \param ilen The length of the input data.
112 *
113 * \return \c 0 on success.
114 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
115 * if parameter verification fails.
116 */
118 const unsigned char *input, size_t ilen );
119
120/**
121 * \brief This function finishes the CMAC operation, and writes
122 * the result to the output buffer.
123 *
124 * It is called after mbedtls_cipher_cmac_update().
125 * It can be followed by mbedtls_cipher_cmac_reset() and
126 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
127 *
128 * \param ctx The cipher context used for the CMAC operation.
129 * \param output The output buffer for the CMAC checksum result.
130 *
131 * \return \c 0 on success.
132 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
133 * if parameter verification fails.
134 */
136 unsigned char *output );
137
138/**
139 * \brief This function prepares the authentication of another
140 * message with the same key as the previous CMAC
141 * operation.
142 *
143 * It is called after mbedtls_cipher_cmac_finish()
144 * and before mbedtls_cipher_cmac_update().
145 *
146 * \param ctx The cipher context used for the CMAC operation.
147 *
148 * \return \c 0 on success.
149 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
150 * if parameter verification fails.
151 */
153
154/**
155 * \brief This function calculates the full generic CMAC
156 * on the input buffer with the provided key.
157 *
158 * The function allocates the context, performs the
159 * calculation, and frees the context.
160 *
161 * The CMAC result is calculated as
162 * output = generic CMAC(cmac key, input buffer).
163 *
164 *
165 * \param cipher_info The cipher information.
166 * \param key The CMAC key.
167 * \param keylen The length of the CMAC key in bits.
168 * \param input The buffer holding the input data.
169 * \param ilen The length of the input data.
170 * \param output The buffer for the generic CMAC result.
171 *
172 * \return \c 0 on success.
173 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
174 * if parameter verification fails.
175 */
177 const unsigned char *key, size_t keylen,
178 const unsigned char *input, size_t ilen,
179 unsigned char *output );
180
181#if defined(MBEDTLS_AES_C)
182/**
183 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
184 * function, as defined in
185 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
186 * Message Authentication Code-Pseudo-Random Function-128
187 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
188 * Exchange Protocol (IKE).</em>
189 *
190 * \param key The key to use.
191 * \param key_len The key length in Bytes.
192 * \param input The buffer holding the input data.
193 * \param in_len The length of the input data in Bytes.
194 * \param output The buffer holding the generated 16 Bytes of
195 * pseudorandom output.
196 *
197 * \return \c 0 on success.
198 */
199int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
200 const unsigned char *input, size_t in_len,
201 unsigned char output[16] );
202#endif /* MBEDTLS_AES_C */
203
204#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
205/**
206 * \brief The CMAC checkup routine.
207 *
208 * \return \c 0 on success.
209 * \return \c 1 on failure.
210 */
211int mbedtls_cmac_self_test( int verbose );
212#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
213
214/// \}
215/// \}
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif /* MBEDTLS_CMAC_H */
This file contains an abstraction interface for use with the cipher primitives provided by the librar...
Configuration options (set of defines)
int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing CMAC computation.
int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
This function prepares the authentication of another message with the same key as the previous CMAC o...
int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx, unsigned char *output)
This function finishes the CMAC operation, and writes the result to the output buffer.
#define MBEDTLS_CIPHER_BLKSIZE_MAX
The longest block used by CMAC is that of 3DES.
Definition: cmac.h:57
int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx, const unsigned char *key, size_t keybits)
This function sets the CMAC key, and prepares to authenticate the input data.
int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the full generic CMAC on the input buffer with the provided key.
Generic cipher context.
Definition: cipher.h:318
Cipher information.
Definition: cipher.h:276
The CMAC context structure.
Definition: cmac.h:66
size_t unprocessed_len
The length of data pending processing.
Definition: cmac.h:75
unsigned char state[8]
The internal state of the CMAC algorithm.
Definition: cmac.h:68
unsigned char unprocessed_block[8]
Unprocessed data - either data that was not block aligned and is still pending processing,...
Definition: cmac.h:72