blob: 65c831c8707d580ff1038c404facb48ccfc4baf3 [file] [log] [blame]
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001/**
2 * \file cmac.h
3 *
Rose Zadik8c154932018-03-27 10:45:16 +01004 * \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>.
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Robert Cragie3d23b1d2015-12-15 07:38:11 +000011 * 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.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000024 */
Rose Zadik380d05d2018-01-25 21:52:41 +000025
Robert Cragie3d23b1d2015-12-15 07:38:11 +000026#ifndef MBEDTLS_CMAC_H
27#define MBEDTLS_CMAC_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000029
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050032#else
33#include MBEDTLS_CONFIG_FILE
34#endif
35
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Simon Butcher69283e52016-10-06 12:49:58 +010042#define MBEDTLS_AES_BLOCK_SIZE 16
43#define MBEDTLS_DES3_BLOCK_SIZE 8
44
Simon Butcher327398a2016-10-05 14:09:11 +010045#if defined(MBEDTLS_AES_C)
Rose Zadik8c154932018-03-27 10:45:16 +010046#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010047#else
Rose Zadik8c154932018-03-27 10:45:16 +010048#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010049#endif
50
Steven Cooreman63342772017-04-04 11:47:16 +020051#if !defined(MBEDTLS_CMAC_ALT)
52
Robert Cragie3d23b1d2015-12-15 07:38:11 +000053/**
Rose Zadik380d05d2018-01-25 21:52:41 +000054 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000055 */
Simon Butcher8308a442016-10-05 15:12:59 +010056struct mbedtls_cmac_context_t
57{
Rose Zadik380d05d2018-01-25 21:52:41 +000058 /** The internal state of the CMAC algorithm. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020059 unsigned char MBEDTLS_PRIVATE(state)[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010060
Andres AGa592dcc2016-10-06 15:23:39 +010061 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000062 * pending processing, or the final block. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020063 unsigned char MBEDTLS_PRIVATE(unprocessed_block)[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010064
Rose Zadik380d05d2018-01-25 21:52:41 +000065 /** The length of data pending processing. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020066 size_t MBEDTLS_PRIVATE(unprocessed_len);
Simon Butcher8308a442016-10-05 15:12:59 +010067};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000068
Ron Eldor4e6d55d2018-02-07 16:36:15 +020069#else /* !MBEDTLS_CMAC_ALT */
70#include "cmac_alt.h"
71#endif /* !MBEDTLS_CMAC_ALT */
72
Robert Cragie3d23b1d2015-12-15 07:38:11 +000073/**
Rose Zadik380d05d2018-01-25 21:52:41 +000074 * \brief This function sets the CMAC key, and prepares to authenticate
75 * the input data.
76 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000077 *
Steven Cooremanc338cef2021-04-26 11:24:44 +020078 * \note When the CMAC implementation is supplied by an alternate
79 * implementation (through #MBEDTLS_CMAC_ALT), some ciphers
80 * may not be supported by that implementation, and thus
81 * return an error. Alternate implementations must support
82 * AES-128 and AES-256, and may support AES-192 and 3DES.
83 *
Rose Zadik380d05d2018-01-25 21:52:41 +000084 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +010085 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
86 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
87 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +000088 * \param key The CMAC key.
89 * \param keybits The length of the CMAC key in bits.
90 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +010091 *
Rose Zadikc138bb72018-04-16 11:11:25 +010092 * \return \c 0 on success.
93 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000094 */
Simon Butcher327398a2016-10-05 14:09:11 +010095int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010096 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000097
98/**
Rose Zadik380d05d2018-01-25 21:52:41 +000099 * \brief This function feeds an input buffer into an ongoing CMAC
100 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000101 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000102 * It is called between mbedtls_cipher_cmac_starts() or
103 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
104 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000105 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000106 * \param ctx The cipher context used for the CMAC operation.
107 * \param input The buffer holding the input data.
108 * \param ilen The length of the input data.
109 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100110 * \return \c 0 on success.
111 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100112 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000113 */
Simon Butcher327398a2016-10-05 14:09:11 +0100114int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
115 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000116
117/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000118 * \brief This function finishes the CMAC operation, and writes
119 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000121 * It is called after mbedtls_cipher_cmac_update().
122 * It can be followed by mbedtls_cipher_cmac_reset() and
123 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100124 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000125 * \param ctx The cipher context used for the CMAC operation.
126 * \param output The output buffer for the CMAC checksum result.
127 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100128 * \return \c 0 on success.
129 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000130 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000131 */
Simon Butcher327398a2016-10-05 14:09:11 +0100132int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
133 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000134
135/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000136 * \brief This function prepares the authentication of another
137 * message with the same key as the previous CMAC
138 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000139 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000140 * It is called after mbedtls_cipher_cmac_finish()
141 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000142 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000143 * \param ctx The cipher context used for the CMAC operation.
144 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100145 * \return \c 0 on success.
146 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000147 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000148 */
Simon Butcher327398a2016-10-05 14:09:11 +0100149int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000150
151/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000152 * \brief This function calculates the full generic CMAC
153 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000155 * The function allocates the context, performs the
156 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100157 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000158 * The CMAC result is calculated as
159 * output = generic CMAC(cmac key, input buffer).
160 *
Steven Cooremanc338cef2021-04-26 11:24:44 +0200161 * \note When the CMAC implementation is supplied by an alternate
162 * implementation (through #MBEDTLS_CMAC_ALT), some ciphers
163 * may not be supported by that implementation, and thus
164 * return an error. Alternate implementations must support
165 * AES-128 and AES-256, and may support AES-192 and 3DES.
Rose Zadik380d05d2018-01-25 21:52:41 +0000166 *
167 * \param cipher_info The cipher information.
168 * \param key The CMAC key.
169 * \param keylen The length of the CMAC key in bits.
170 * \param input The buffer holding the input data.
171 * \param ilen The length of the input data.
172 * \param output The buffer for the generic CMAC result.
173 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100174 * \return \c 0 on success.
175 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000176 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000177 */
Simon Butcher327398a2016-10-05 14:09:11 +0100178int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
179 const unsigned char *key, size_t keylen,
180 const unsigned char *input, size_t ilen,
181 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000182
Simon Butcher69283e52016-10-06 12:49:58 +0100183#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000184/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000185 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
186 * function, as defined in
187 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
188 * Message Authentication Code-Pseudo-Random Function-128
189 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
190 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000191 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000192 * \param key The key to use.
193 * \param key_len The key length in Bytes.
194 * \param input The buffer holding the input data.
195 * \param in_len The length of the input data in Bytes.
196 * \param output The buffer holding the generated 16 Bytes of
197 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000198 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000199 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000200 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700201int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000202 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100203 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700204#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000205
Brian Murrayb439d452016-05-19 16:02:42 -0700206#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000207/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000208 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000209 *
Steven Cooreman894b9c42021-04-23 08:19:43 +0200210 * \note In case the CMAC routines are provided by an alternative
211 * implementation (i.e. #MBEDTLS_CMAC_ALT is defined), the
212 * checkup routine will succeed even if the implementation does
213 * not support the less widely used AES-192 or 3DES primitives.
214 * The self-test requires at least AES-128 and AES-256 to be
215 * supported by the underlying implementation.
216 *
Rose Zadik8c154932018-03-27 10:45:16 +0100217 * \return \c 0 on success.
218 * \return \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000219 */
220int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700221#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000222
223#ifdef __cplusplus
224}
225#endif
226
227#endif /* MBEDTLS_CMAC_H */