blob: 7152dac5ee1dea30cc7e77d8dd0e95453fe3ab5e [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útia2947ac2020-08-19 16:37:36 +020010 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12 *
13 * This file is provided under the Apache License 2.0, or the
14 * GNU General Public License v2.0 or later.
15 *
16 * **********
17 * Apache License 2.0:
Robert Cragie3d23b1d2015-12-15 07:38:11 +000018 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020031 * **********
32 *
33 * **********
34 * GNU General Public License v2.0 or later:
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License along
47 * with this program; if not, write to the Free Software Foundation, Inc.,
48 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 *
50 * **********
Robert Cragie3d23b1d2015-12-15 07:38:11 +000051 */
Rose Zadik380d05d2018-01-25 21:52:41 +000052
Robert Cragie3d23b1d2015-12-15 07:38:11 +000053#ifndef MBEDTLS_CMAC_H
54#define MBEDTLS_CMAC_H
55
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020056#if !defined(MBEDTLS_CONFIG_FILE)
57#include "config.h"
58#else
59#include MBEDTLS_CONFIG_FILE
60#endif
61
Ron Eldor6fd941f2017-05-14 16:17:33 +030062#include "cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000063
64#ifdef __cplusplus
65extern "C" {
66#endif
67
Ron Eldor9924bdc2018-10-04 10:59:13 +030068/* MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020069/** CMAC hardware accelerator failed. */
70#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010071
Simon Butcher69283e52016-10-06 12:49:58 +010072#define MBEDTLS_AES_BLOCK_SIZE 16
73#define MBEDTLS_DES3_BLOCK_SIZE 8
74
Simon Butcher327398a2016-10-05 14:09:11 +010075#if defined(MBEDTLS_AES_C)
Rose Zadik8c154932018-03-27 10:45:16 +010076#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010077#else
Rose Zadik8c154932018-03-27 10:45:16 +010078#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010079#endif
80
Steven Cooreman63342772017-04-04 11:47:16 +020081#if !defined(MBEDTLS_CMAC_ALT)
82
Robert Cragie3d23b1d2015-12-15 07:38:11 +000083/**
Rose Zadik380d05d2018-01-25 21:52:41 +000084 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000085 */
Simon Butcher8308a442016-10-05 15:12:59 +010086struct mbedtls_cmac_context_t
87{
Rose Zadik380d05d2018-01-25 21:52:41 +000088 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010089 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010090
Andres AGa592dcc2016-10-06 15:23:39 +010091 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000092 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010093 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010094
Rose Zadik380d05d2018-01-25 21:52:41 +000095 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010096 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010097};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000098
Ron Eldor4e6d55d2018-02-07 16:36:15 +020099#else /* !MBEDTLS_CMAC_ALT */
100#include "cmac_alt.h"
101#endif /* !MBEDTLS_CMAC_ALT */
102
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000103/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000104 * \brief This function sets the CMAC key, and prepares to authenticate
105 * the input data.
106 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000107 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000108 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +0100109 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
110 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
111 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +0000112 * \param key The CMAC key.
113 * \param keybits The length of the CMAC key in bits.
114 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100115 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100116 * \return \c 0 on success.
117 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000118 */
Simon Butcher327398a2016-10-05 14:09:11 +0100119int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100120 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000121
122/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000123 * \brief This function feeds an input buffer into an ongoing CMAC
124 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000125 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000126 * It is called between mbedtls_cipher_cmac_starts() or
127 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
128 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000129 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000130 * \param ctx The cipher context used for the CMAC operation.
131 * \param input The buffer holding the input data.
132 * \param ilen The length of the input data.
133 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100134 * \return \c 0 on success.
135 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100136 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000137 */
Simon Butcher327398a2016-10-05 14:09:11 +0100138int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
139 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000140
141/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000142 * \brief This function finishes the CMAC operation, and writes
143 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000144 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000145 * It is called after mbedtls_cipher_cmac_update().
146 * It can be followed by mbedtls_cipher_cmac_reset() and
147 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100148 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000149 * \param ctx The cipher context used for the CMAC operation.
150 * \param output The output buffer for the CMAC checksum result.
151 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100152 * \return \c 0 on success.
153 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000154 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000155 */
Simon Butcher327398a2016-10-05 14:09:11 +0100156int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
157 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000158
159/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000160 * \brief This function prepares the authentication of another
161 * message with the same key as the previous CMAC
162 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000163 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000164 * It is called after mbedtls_cipher_cmac_finish()
165 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000166 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000167 * \param ctx The cipher context used for the CMAC operation.
168 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100169 * \return \c 0 on success.
170 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000171 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000172 */
Simon Butcher327398a2016-10-05 14:09:11 +0100173int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000174
175/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000176 * \brief This function calculates the full generic CMAC
177 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000178 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000179 * The function allocates the context, performs the
180 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100181 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000182 * The CMAC result is calculated as
183 * output = generic CMAC(cmac key, input buffer).
184 *
185 *
186 * \param cipher_info The cipher information.
187 * \param key The CMAC key.
188 * \param keylen The length of the CMAC key in bits.
189 * \param input The buffer holding the input data.
190 * \param ilen The length of the input data.
191 * \param output The buffer for the generic CMAC result.
192 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100193 * \return \c 0 on success.
194 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000195 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000196 */
Simon Butcher327398a2016-10-05 14:09:11 +0100197int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
198 const unsigned char *key, size_t keylen,
199 const unsigned char *input, size_t ilen,
200 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000201
Simon Butcher69283e52016-10-06 12:49:58 +0100202#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000203/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000204 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
205 * function, as defined in
206 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
207 * Message Authentication Code-Pseudo-Random Function-128
208 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
209 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000210 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000211 * \param key The key to use.
212 * \param key_len The key length in Bytes.
213 * \param input The buffer holding the input data.
214 * \param in_len The length of the input data in Bytes.
215 * \param output The buffer holding the generated 16 Bytes of
216 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000217 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000218 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000219 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700220int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000221 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100222 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700223#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000224
Brian Murrayb439d452016-05-19 16:02:42 -0700225#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000226/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000227 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000228 *
Rose Zadik8c154932018-03-27 10:45:16 +0100229 * \return \c 0 on success.
230 * \return \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 */