blob: dbfc1bbf9c2863316de22bf7710fe00e7fcdba34 [file] [log] [blame]
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001/**
2 * \file cmac.h
3 *
Rose Zadik380d05d2018-01-25 21:52:41 +00004 * \brief The Cipher-based Message Authentication Code (CMAC) Mode for
5 * Authentication.
Darryl Greena40a1012018-01-05 15:33:17 +00006 */
7/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Robert Cragie3d23b1d2015-12-15 07:38:11 +000016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Robert Cragie3d23b1d2015-12-15 07:38:11 +000049 */
Rose Zadik380d05d2018-01-25 21:52:41 +000050
Robert Cragie3d23b1d2015-12-15 07:38:11 +000051#ifndef MBEDTLS_CMAC_H
52#define MBEDTLS_CMAC_H
53
Ron Eldor0559c662018-02-14 16:02:41 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Ron Eldordf9b93e2017-05-14 16:17:33 +030060#include "cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000061
62#ifdef __cplusplus
63extern "C" {
64#endif
65
Rose Zadik380d05d2018-01-25 21:52:41 +000066#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010067
Simon Butcher69283e52016-10-06 12:49:58 +010068#define MBEDTLS_AES_BLOCK_SIZE 16
69#define MBEDTLS_DES3_BLOCK_SIZE 8
70
Simon Butcher327398a2016-10-05 14:09:11 +010071#if defined(MBEDTLS_AES_C)
Rose Zadik380d05d2018-01-25 21:52:41 +000072#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010073#else
Rose Zadik380d05d2018-01-25 21:52:41 +000074#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010075#endif
76
Steven Cooreman63342772017-04-04 11:47:16 +020077#if !defined(MBEDTLS_CMAC_ALT)
78
Robert Cragie3d23b1d2015-12-15 07:38:11 +000079/**
Rose Zadik380d05d2018-01-25 21:52:41 +000080 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000081 */
Simon Butcher8308a442016-10-05 15:12:59 +010082struct mbedtls_cmac_context_t
83{
Rose Zadik380d05d2018-01-25 21:52:41 +000084 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010085 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010086
Andres AGa592dcc2016-10-06 15:23:39 +010087 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000088 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010089 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010090
Rose Zadik380d05d2018-01-25 21:52:41 +000091 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010092 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010093};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000094
95/**
Rose Zadik380d05d2018-01-25 21:52:41 +000096 * \brief This function sets the CMAC key, and prepares to authenticate
97 * the input data.
98 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000099 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000100 * \param ctx The cipher context used for the CMAC operation, initialized
101 * as one of the following types:<ul>
102 * <li>MBEDTLS_CIPHER_AES_128_ECB</li>
103 * <li>MBEDTLS_CIPHER_AES_192_ECB</li>
104 * <li>MBEDTLS_CIPHER_AES_256_ECB</li>
105 * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul>
106 * \param key The CMAC key.
107 * \param keybits The length of the CMAC key in bits.
108 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100109 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000110 * \return \c 0 on success, or a cipher-specific error code.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000111 */
Simon Butcher327398a2016-10-05 14:09:11 +0100112int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100113 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000114
115/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000116 * \brief This function feeds an input buffer into an ongoing CMAC
117 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000118 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000119 * It is called between mbedtls_cipher_cmac_starts() or
120 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
121 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000122 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000123 * \param ctx The cipher context used for the CMAC operation.
124 * \param input The buffer holding the input data.
125 * \param ilen The length of the input data.
126 *
127 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
128 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000129 */
Simon Butcher327398a2016-10-05 14:09:11 +0100130int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
131 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000132
133/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000134 * \brief This function finishes the CMAC operation, and writes
135 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000136 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000137 * It is called after mbedtls_cipher_cmac_update().
138 * It can be followed by mbedtls_cipher_cmac_reset() and
139 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100140 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000141 * \param ctx The cipher context used for the CMAC operation.
142 * \param output The output buffer for the CMAC checksum result.
143 *
144 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
145 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000146 */
Simon Butcher327398a2016-10-05 14:09:11 +0100147int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
148 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000149
150/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000151 * \brief This function prepares the authentication of another
152 * message with the same key as the previous CMAC
153 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000155 * It is called after mbedtls_cipher_cmac_finish()
156 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000157 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000158 * \param ctx The cipher context used for the CMAC operation.
159 *
160 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
161 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000162 */
Simon Butcher327398a2016-10-05 14:09:11 +0100163int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000164
165/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000166 * \brief This function calculates the full generic CMAC
167 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000168 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000169 * The function allocates the context, performs the
170 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100171 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000172 * The CMAC result is calculated as
173 * output = generic CMAC(cmac key, input buffer).
174 *
175 *
176 * \param cipher_info The cipher information.
177 * \param key The CMAC key.
178 * \param keylen The length of the CMAC key in bits.
179 * \param input The buffer holding the input data.
180 * \param ilen The length of the input data.
181 * \param output The buffer for the generic CMAC result.
182 *
183 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
184 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000185 */
Simon Butcher327398a2016-10-05 14:09:11 +0100186int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
187 const unsigned char *key, size_t keylen,
188 const unsigned char *input, size_t ilen,
189 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000190
Simon Butcher69283e52016-10-06 12:49:58 +0100191#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000192/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000193 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
194 * function, as defined in
195 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
196 * Message Authentication Code-Pseudo-Random Function-128
197 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
198 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000199 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000200 * \param key The key to use.
201 * \param key_len The key length in Bytes.
202 * \param input The buffer holding the input data.
203 * \param in_len The length of the input data in Bytes.
204 * \param output The buffer holding the generated 16 Bytes of
205 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000206 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000207 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000208 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700209int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000210 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100211 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700212#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000213
Steven Cooreman63342772017-04-04 11:47:16 +0200214#ifdef __cplusplus
215}
216#endif
217
218#else /* !MBEDTLS_CMAC_ALT */
219#include "cmac_alt.h"
220#endif /* !MBEDTLS_CMAC_ALT */
221
222#ifdef __cplusplus
223extern "C" {
224#endif
225
Brian Murrayb439d452016-05-19 16:02:42 -0700226#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000227/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000228 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000229 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000230 * \return \c 0 on success, or \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000231 */
232int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700233#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000234
235#ifdef __cplusplus
236}
237#endif
238
239#endif /* MBEDTLS_CMAC_H */