blob: 5a7c9b246f0b701f469b08e5642a42fcff5eabc2 [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. */
Rose Zadik380d05d2018-01-25 21:52:41 +000069#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010070
Simon Butcher69283e52016-10-06 12:49:58 +010071#define MBEDTLS_AES_BLOCK_SIZE 16
72#define MBEDTLS_DES3_BLOCK_SIZE 8
73
Simon Butcher327398a2016-10-05 14:09:11 +010074#if defined(MBEDTLS_AES_C)
Rose Zadik8c154932018-03-27 10:45:16 +010075#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010076#else
Rose Zadik8c154932018-03-27 10:45:16 +010077#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010078#endif
79
Steven Cooreman63342772017-04-04 11:47:16 +020080#if !defined(MBEDTLS_CMAC_ALT)
81
Robert Cragie3d23b1d2015-12-15 07:38:11 +000082/**
Rose Zadik380d05d2018-01-25 21:52:41 +000083 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000084 */
Simon Butcher8308a442016-10-05 15:12:59 +010085struct mbedtls_cmac_context_t
86{
Rose Zadik380d05d2018-01-25 21:52:41 +000087 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010088 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010089
Andres AGa592dcc2016-10-06 15:23:39 +010090 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000091 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010092 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010093
Rose Zadik380d05d2018-01-25 21:52:41 +000094 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010095 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010096};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000097
Ron Eldor4e6d55d2018-02-07 16:36:15 +020098#else /* !MBEDTLS_CMAC_ALT */
99#include "cmac_alt.h"
100#endif /* !MBEDTLS_CMAC_ALT */
101
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000102/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000103 * \brief This function sets the CMAC key, and prepares to authenticate
104 * the input data.
105 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000106 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000107 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +0100108 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
109 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
110 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +0000111 * \param key The CMAC key.
112 * \param keybits The length of the CMAC key in bits.
113 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100114 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100115 * \return \c 0 on success.
116 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000117 */
Simon Butcher327398a2016-10-05 14:09:11 +0100118int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100119 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120
121/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000122 * \brief This function feeds an input buffer into an ongoing CMAC
123 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000124 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000125 * It is called between mbedtls_cipher_cmac_starts() or
126 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
127 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000128 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000129 * \param ctx The cipher context used for the CMAC operation.
130 * \param input The buffer holding the input data.
131 * \param ilen The length of the input data.
132 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100133 * \return \c 0 on success.
134 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100135 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000136 */
Simon Butcher327398a2016-10-05 14:09:11 +0100137int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
138 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000139
140/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000141 * \brief This function finishes the CMAC operation, and writes
142 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000144 * It is called after mbedtls_cipher_cmac_update().
145 * It can be followed by mbedtls_cipher_cmac_reset() and
146 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100147 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000148 * \param ctx The cipher context used for the CMAC operation.
149 * \param output The output buffer for the CMAC checksum result.
150 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100151 * \return \c 0 on success.
152 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000153 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154 */
Simon Butcher327398a2016-10-05 14:09:11 +0100155int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
156 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000157
158/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000159 * \brief This function prepares the authentication of another
160 * message with the same key as the previous CMAC
161 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000162 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000163 * It is called after mbedtls_cipher_cmac_finish()
164 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000165 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000166 * \param ctx The cipher context used for the CMAC operation.
167 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100168 * \return \c 0 on success.
169 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000170 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000171 */
Simon Butcher327398a2016-10-05 14:09:11 +0100172int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000173
174/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000175 * \brief This function calculates the full generic CMAC
176 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000177 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000178 * The function allocates the context, performs the
179 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100180 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000181 * The CMAC result is calculated as
182 * output = generic CMAC(cmac key, input buffer).
183 *
184 *
185 * \param cipher_info The cipher information.
186 * \param key The CMAC key.
187 * \param keylen The length of the CMAC key in bits.
188 * \param input The buffer holding the input data.
189 * \param ilen The length of the input data.
190 * \param output The buffer for the generic CMAC result.
191 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100192 * \return \c 0 on success.
193 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000194 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000195 */
Simon Butcher327398a2016-10-05 14:09:11 +0100196int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
197 const unsigned char *key, size_t keylen,
198 const unsigned char *input, size_t ilen,
199 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000200
Simon Butcher69283e52016-10-06 12:49:58 +0100201#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000202/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000203 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
204 * function, as defined in
205 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
206 * Message Authentication Code-Pseudo-Random Function-128
207 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
208 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000209 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000210 * \param key The key to use.
211 * \param key_len The key length in Bytes.
212 * \param input The buffer holding the input data.
213 * \param in_len The length of the input data in Bytes.
214 * \param output The buffer holding the generated 16 Bytes of
215 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000216 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000217 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000218 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700219int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000220 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100221 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700222#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000223
Brian Murrayb439d452016-05-19 16:02:42 -0700224#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000225/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000226 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000227 *
Rose Zadik8c154932018-03-27 10:45:16 +0100228 * \return \c 0 on success.
229 * \return \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000230 */
231int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700232#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000233
234#ifdef __cplusplus
235}
236#endif
237
238#endif /* MBEDTLS_CMAC_H */