blob: a73909cf86cccb38631f063503a8f30b168cd95f [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/**
David Horstmannf39bd972021-12-06 18:58:02 +0000104 * \brief This function starts a new CMAC computation
105 * by setting the CMAC key, and preparing to authenticate
Rose Zadik380d05d2018-01-25 21:52:41 +0000106 * the input data.
David Horstmannf39bd972021-12-06 18:58:02 +0000107 * It must be called with an initialized cipher context.
108 *
109 * Once this function has completed, data can be supplied
110 * to the CMAC computation by calling
111 * mbedtls_cipher_cmac_update().
112 *
113 * To start a CMAC computation using the same key as a previous
114 * CMAC computation, use mbedtls_cipher_cmac_finish().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000115 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000116 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +0100117 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
118 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
119 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +0000120 * \param key The CMAC key.
121 * \param keybits The length of the CMAC key in bits.
122 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100123 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100124 * \return \c 0 on success.
125 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000126 */
Simon Butcher327398a2016-10-05 14:09:11 +0100127int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100128 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000129
130/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000131 * \brief This function feeds an input buffer into an ongoing CMAC
132 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000133 *
David Horstmannf39bd972021-12-06 18:58:02 +0000134 * The CMAC computation must have previously been started
135 * by calling mbedtls_cipher_cmac_starts() or
136 * mbedtls_cipher_cmac_reset().
137 *
138 * Call this function as many times as needed to input the
139 * data to be authenticated.
140 * Once all of the required data has been input,
141 * call mbedtls_cipher_cmac_finish() to obtain the result
142 * of the CMAC operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000144 * \param ctx The cipher context used for the CMAC operation.
145 * \param input The buffer holding the input data.
146 * \param ilen The length of the input data.
147 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100148 * \return \c 0 on success.
149 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100150 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000151 */
Simon Butcher327398a2016-10-05 14:09:11 +0100152int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
153 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154
155/**
David Horstmannf39bd972021-12-06 18:58:02 +0000156 * \brief This function finishes an ongoing CMAC operation, and
157 * writes the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000158 *
David Horstmannf39bd972021-12-06 18:58:02 +0000159 * It should be followed either by
160 * mbedtls_cipher_cmac_reset(), which starts another CMAC
161 * operation with the same key, or mbedtls_cipher_free(),
162 * which clears the cipher context.
Simon Butcher327398a2016-10-05 14:09:11 +0100163 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000164 * \param ctx The cipher context used for the CMAC operation.
165 * \param output The output buffer for the CMAC checksum result.
166 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100167 * \return \c 0 on success.
168 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000169 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000170 */
Simon Butcher327398a2016-10-05 14:09:11 +0100171int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
172 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000173
174/**
David Horstmannf39bd972021-12-06 18:58:02 +0000175 * \brief This function starts a new CMAC operation with the same
176 * key as the previous one.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000177 *
David Horstmannf39bd972021-12-06 18:58:02 +0000178 * It should be called after finishing the previous CMAC
179 * operation with mbedtls_cipher_cmac_finish().
180 * After calling this function,
181 * call mbedtls_cipher_cmac_update() to supply the new
182 * CMAC operation with data.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000183 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000184 * \param ctx The cipher context used for the CMAC operation.
185 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100186 * \return \c 0 on success.
187 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000188 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000189 */
Simon Butcher327398a2016-10-05 14:09:11 +0100190int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000191
192/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000193 * \brief This function calculates the full generic CMAC
194 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000195 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000196 * The function allocates the context, performs the
197 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100198 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000199 * The CMAC result is calculated as
200 * output = generic CMAC(cmac key, input buffer).
201 *
202 *
203 * \param cipher_info The cipher information.
204 * \param key The CMAC key.
205 * \param keylen The length of the CMAC key in bits.
206 * \param input The buffer holding the input data.
207 * \param ilen The length of the input data.
208 * \param output The buffer for the generic CMAC result.
209 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100210 * \return \c 0 on success.
211 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000212 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000213 */
Simon Butcher327398a2016-10-05 14:09:11 +0100214int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
215 const unsigned char *key, size_t keylen,
216 const unsigned char *input, size_t ilen,
217 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000218
Simon Butcher69283e52016-10-06 12:49:58 +0100219#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000220/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000221 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
222 * function, as defined in
223 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
224 * Message Authentication Code-Pseudo-Random Function-128
225 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
226 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000227 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000228 * \param key The key to use.
229 * \param key_len The key length in Bytes.
230 * \param input The buffer holding the input data.
231 * \param in_len The length of the input data in Bytes.
232 * \param output The buffer holding the generated 16 Bytes of
233 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000234 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000235 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000236 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700237int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000238 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100239 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700240#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000241
Brian Murrayb439d452016-05-19 16:02:42 -0700242#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000243/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000244 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000245 *
Rose Zadik8c154932018-03-27 10:45:16 +0100246 * \return \c 0 on success.
247 * \return \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000248 */
249int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700250#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000251
252#ifdef __cplusplus
253}
254#endif
255
256#endif /* MBEDTLS_CMAC_H */