blob: 20747475679f8f7512211b274ac7e38388d7a69c [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/*
Rose Zadik380d05d2018-01-25 21:52:41 +000010 * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
51 *
Rose Zadik380d05d2018-01-25 21:52:41 +000052 * This file is part of Mbed TLS (https://tls.mbed.org)
Robert Cragie3d23b1d2015-12-15 07:38:11 +000053 */
Rose Zadik380d05d2018-01-25 21:52:41 +000054
Robert Cragie3d23b1d2015-12-15 07:38:11 +000055#ifndef MBEDTLS_CMAC_H
56#define MBEDTLS_CMAC_H
57
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020058#if !defined(MBEDTLS_CONFIG_FILE)
59#include "config.h"
60#else
61#include MBEDTLS_CONFIG_FILE
62#endif
63
Ron Eldor6fd941f2017-05-14 16:17:33 +030064#include "cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000065
66#ifdef __cplusplus
67extern "C" {
68#endif
69
Ron Eldor9924bdc2018-10-04 10:59:13 +030070/* MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED is deprecated and should not be used. */
Rose Zadik380d05d2018-01-25 21:52:41 +000071#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010072
Simon Butcher69283e52016-10-06 12:49:58 +010073#define MBEDTLS_AES_BLOCK_SIZE 16
74#define MBEDTLS_DES3_BLOCK_SIZE 8
75
Simon Butcher327398a2016-10-05 14:09:11 +010076#if defined(MBEDTLS_AES_C)
Rose Zadik8c154932018-03-27 10:45:16 +010077#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010078#else
Rose Zadik8c154932018-03-27 10:45:16 +010079#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010080#endif
81
Steven Cooreman63342772017-04-04 11:47:16 +020082#if !defined(MBEDTLS_CMAC_ALT)
83
Robert Cragie3d23b1d2015-12-15 07:38:11 +000084/**
Rose Zadik380d05d2018-01-25 21:52:41 +000085 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000086 */
Simon Butcher8308a442016-10-05 15:12:59 +010087struct mbedtls_cmac_context_t
88{
Rose Zadik380d05d2018-01-25 21:52:41 +000089 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010090 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010091
Andres AGa592dcc2016-10-06 15:23:39 +010092 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000093 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010094 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010095
Rose Zadik380d05d2018-01-25 21:52:41 +000096 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010097 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010098};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000099
Ron Eldor4e6d55d2018-02-07 16:36:15 +0200100#else /* !MBEDTLS_CMAC_ALT */
101#include "cmac_alt.h"
102#endif /* !MBEDTLS_CMAC_ALT */
103
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000104/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000105 * \brief This function sets the CMAC key, and prepares to authenticate
106 * the input data.
107 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000108 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000109 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +0100110 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
111 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
112 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +0000113 * \param key The CMAC key.
114 * \param keybits The length of the CMAC key in bits.
115 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +0100116 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100117 * \return \c 0 on success.
118 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000119 */
Simon Butcher327398a2016-10-05 14:09:11 +0100120int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100121 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000122
123/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000124 * \brief This function feeds an input buffer into an ongoing CMAC
125 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000126 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000127 * It is called between mbedtls_cipher_cmac_starts() or
128 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
129 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000130 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000131 * \param ctx The cipher context used for the CMAC operation.
132 * \param input The buffer holding the input data.
133 * \param ilen The length of the input data.
134 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100135 * \return \c 0 on success.
136 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100137 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000138 */
Simon Butcher327398a2016-10-05 14:09:11 +0100139int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
140 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000141
142/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000143 * \brief This function finishes the CMAC operation, and writes
144 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000145 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000146 * It is called after mbedtls_cipher_cmac_update().
147 * It can be followed by mbedtls_cipher_cmac_reset() and
148 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100149 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000150 * \param ctx The cipher context used for the CMAC operation.
151 * \param output The output buffer for the CMAC checksum result.
152 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100153 * \return \c 0 on success.
154 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000155 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000156 */
Simon Butcher327398a2016-10-05 14:09:11 +0100157int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
158 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000159
160/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000161 * \brief This function prepares the authentication of another
162 * message with the same key as the previous CMAC
163 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000164 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000165 * It is called after mbedtls_cipher_cmac_finish()
166 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000167 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000168 * \param ctx The cipher context used for the CMAC operation.
169 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100170 * \return \c 0 on success.
171 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000172 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000173 */
Simon Butcher327398a2016-10-05 14:09:11 +0100174int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000175
176/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000177 * \brief This function calculates the full generic CMAC
178 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000179 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000180 * The function allocates the context, performs the
181 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100182 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000183 * The CMAC result is calculated as
184 * output = generic CMAC(cmac key, input buffer).
185 *
186 *
187 * \param cipher_info The cipher information.
188 * \param key The CMAC key.
189 * \param keylen The length of the CMAC key in bits.
190 * \param input The buffer holding the input data.
191 * \param ilen The length of the input data.
192 * \param output The buffer for the generic CMAC result.
193 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100194 * \return \c 0 on success.
195 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000196 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000197 */
Simon Butcher327398a2016-10-05 14:09:11 +0100198int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
199 const unsigned char *key, size_t keylen,
200 const unsigned char *input, size_t ilen,
201 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000202
Simon Butcher69283e52016-10-06 12:49:58 +0100203#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000204/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000205 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
206 * function, as defined in
207 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
208 * Message Authentication Code-Pseudo-Random Function-128
209 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
210 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000211 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000212 * \param key The key to use.
213 * \param key_len The key length in Bytes.
214 * \param input The buffer holding the input data.
215 * \param in_len The length of the input data in Bytes.
216 * \param output The buffer holding the generated 16 Bytes of
217 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000218 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000219 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000220 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700221int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000222 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100223 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700224#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000225
Brian Murrayb439d452016-05-19 16:02:42 -0700226#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000227/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000228 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000229 *
Rose Zadik8c154932018-03-27 10:45:16 +0100230 * \return \c 0 on success.
231 * \return \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000232 */
233int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700234#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000235
236#ifdef __cplusplus
237}
238#endif
239
240#endif /* MBEDTLS_CMAC_H */