blob: 6b3414144dd066a8f7b7ea1caab17f890c61b9dd [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/*
Rose Zadik380d05d2018-01-25 21:52:41 +00008 * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
49 *
Rose Zadik380d05d2018-01-25 21:52:41 +000050 * This file is part of Mbed TLS (https://tls.mbed.org)
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 Eldor0559c662018-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 Eldordf9b93e2017-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
Rose Zadik380d05d2018-01-25 21:52:41 +000068#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010069
Simon Butcher69283e52016-10-06 12:49:58 +010070#define MBEDTLS_AES_BLOCK_SIZE 16
71#define MBEDTLS_DES3_BLOCK_SIZE 8
72
Simon Butcher327398a2016-10-05 14:09:11 +010073#if defined(MBEDTLS_AES_C)
Rose Zadik380d05d2018-01-25 21:52:41 +000074#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010075#else
Rose Zadik380d05d2018-01-25 21:52:41 +000076#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010077#endif
78
Steven Cooreman63342772017-04-04 11:47:16 +020079#if !defined(MBEDTLS_CMAC_ALT)
80
Robert Cragie3d23b1d2015-12-15 07:38:11 +000081/**
Rose Zadik380d05d2018-01-25 21:52:41 +000082 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000083 */
Simon Butcher8308a442016-10-05 15:12:59 +010084struct mbedtls_cmac_context_t
85{
Rose Zadik380d05d2018-01-25 21:52:41 +000086 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010087 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010088
Andres AGa592dcc2016-10-06 15:23:39 +010089 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000090 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010091 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010092
Rose Zadik380d05d2018-01-25 21:52:41 +000093 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010094 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010095};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000096
97/**
Rose Zadik380d05d2018-01-25 21:52:41 +000098 * \brief This function sets the CMAC key, and prepares to authenticate
99 * the input data.
100 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000101 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000102 * \param ctx The cipher context used for the CMAC operation, initialized
103 * as one of the following types:<ul>
104 * <li>MBEDTLS_CIPHER_AES_128_ECB</li>
105 * <li>MBEDTLS_CIPHER_AES_192_ECB</li>
106 * <li>MBEDTLS_CIPHER_AES_256_ECB</li>
107 * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul>
108 * \param key The CMAC key.
109 * \param keybits The length of the CMAC key in bits.
110 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100111 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000112 * \return \c 0 on success, or a cipher-specific error code.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000113 */
Simon Butcher327398a2016-10-05 14:09:11 +0100114int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100115 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000116
117/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000118 * \brief This function feeds an input buffer into an ongoing CMAC
119 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000121 * It is called between mbedtls_cipher_cmac_starts() or
122 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
123 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000124 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000125 * \param ctx The cipher context used for the CMAC operation.
126 * \param input The buffer holding the input data.
127 * \param ilen The length of the input data.
128 *
129 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
130 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000131 */
Simon Butcher327398a2016-10-05 14:09:11 +0100132int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
133 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000134
135/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000136 * \brief This function finishes the CMAC operation, and writes
137 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000138 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000139 * It is called after mbedtls_cipher_cmac_update().
140 * It can be followed by mbedtls_cipher_cmac_reset() and
141 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100142 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000143 * \param ctx The cipher context used for the CMAC operation.
144 * \param output The output buffer for the CMAC checksum result.
145 *
146 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
147 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000148 */
Simon Butcher327398a2016-10-05 14:09:11 +0100149int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
150 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000151
152/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000153 * \brief This function prepares the authentication of another
154 * message with the same key as the previous CMAC
155 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000156 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000157 * It is called after mbedtls_cipher_cmac_finish()
158 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000159 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000160 * \param ctx The cipher context used for the CMAC operation.
161 *
162 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
163 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000164 */
Simon Butcher327398a2016-10-05 14:09:11 +0100165int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000166
167/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000168 * \brief This function calculates the full generic CMAC
169 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000170 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000171 * The function allocates the context, performs the
172 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100173 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000174 * The CMAC result is calculated as
175 * output = generic CMAC(cmac key, input buffer).
176 *
177 *
178 * \param cipher_info The cipher information.
179 * \param key The CMAC key.
180 * \param keylen The length of the CMAC key in bits.
181 * \param input The buffer holding the input data.
182 * \param ilen The length of the input data.
183 * \param output The buffer for the generic CMAC result.
184 *
185 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
186 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000187 */
Simon Butcher327398a2016-10-05 14:09:11 +0100188int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
189 const unsigned char *key, size_t keylen,
190 const unsigned char *input, size_t ilen,
191 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000192
Simon Butcher69283e52016-10-06 12:49:58 +0100193#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000194/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000195 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
196 * function, as defined in
197 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
198 * Message Authentication Code-Pseudo-Random Function-128
199 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
200 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000201 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000202 * \param key The key to use.
203 * \param key_len The key length in Bytes.
204 * \param input The buffer holding the input data.
205 * \param in_len The length of the input data in Bytes.
206 * \param output The buffer holding the generated 16 Bytes of
207 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000208 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000209 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000210 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700211int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000212 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100213 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700214#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000215
Steven Cooreman63342772017-04-04 11:47:16 +0200216#ifdef __cplusplus
217}
218#endif
219
220#else /* !MBEDTLS_CMAC_ALT */
221#include "cmac_alt.h"
222#endif /* !MBEDTLS_CMAC_ALT */
223
224#ifdef __cplusplus
225extern "C" {
226#endif
227
Brian Murrayb439d452016-05-19 16:02:42 -0700228#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000229/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000230 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000231 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000232 * \return \c 0 on success, or \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000233 */
234int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700235#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000236
237#ifdef __cplusplus
238}
239#endif
240
241#endif /* MBEDTLS_CMAC_H */