blob: bb203cf6868a2a7a77243f48aa66b86f38312513 [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
Robert Cragie3d23b1d2015-12-15 07:38:11 +00009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
Rose Zadik380d05d2018-01-25 21:52:41 +000023 * This file is part of Mbed TLS (https://tls.mbed.org)
Robert Cragie3d23b1d2015-12-15 07:38:11 +000024 */
Rose Zadik380d05d2018-01-25 21:52:41 +000025
Robert Cragie3d23b1d2015-12-15 07:38:11 +000026#ifndef MBEDTLS_CMAC_H
27#define MBEDTLS_CMAC_H
28
Simon Butcher327398a2016-10-05 14:09:11 +010029#include "mbedtls/cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Rose Zadik380d05d2018-01-25 21:52:41 +000035#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010036
Simon Butcher69283e52016-10-06 12:49:58 +010037#define MBEDTLS_AES_BLOCK_SIZE 16
38#define MBEDTLS_DES3_BLOCK_SIZE 8
39
Simon Butcher327398a2016-10-05 14:09:11 +010040#if defined(MBEDTLS_AES_C)
Rose Zadik380d05d2018-01-25 21:52:41 +000041#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010042#else
Rose Zadik380d05d2018-01-25 21:52:41 +000043#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010044#endif
45
Steven Cooreman63342772017-04-04 11:47:16 +020046#if !defined(MBEDTLS_CMAC_ALT)
47
Robert Cragie3d23b1d2015-12-15 07:38:11 +000048/**
Rose Zadik380d05d2018-01-25 21:52:41 +000049 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000050 */
Simon Butcher8308a442016-10-05 15:12:59 +010051struct mbedtls_cmac_context_t
52{
Rose Zadik380d05d2018-01-25 21:52:41 +000053 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010054 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010055
Andres AGa592dcc2016-10-06 15:23:39 +010056 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000057 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010058 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010059
Rose Zadik380d05d2018-01-25 21:52:41 +000060 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010061 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010062};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000063
Ron Eldor4e6d55d2018-02-07 16:36:15 +020064#else /* !MBEDTLS_CMAC_ALT */
65#include "cmac_alt.h"
66#endif /* !MBEDTLS_CMAC_ALT */
67
Robert Cragie3d23b1d2015-12-15 07:38:11 +000068/**
Rose Zadik380d05d2018-01-25 21:52:41 +000069 * \brief This function sets the CMAC key, and prepares to authenticate
70 * the input data.
71 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000072 *
Rose Zadik380d05d2018-01-25 21:52:41 +000073 * \param ctx The cipher context used for the CMAC operation, initialized
74 * as one of the following types:<ul>
75 * <li>MBEDTLS_CIPHER_AES_128_ECB</li>
76 * <li>MBEDTLS_CIPHER_AES_192_ECB</li>
77 * <li>MBEDTLS_CIPHER_AES_256_ECB</li>
78 * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul>
79 * \param key The CMAC key.
80 * \param keybits The length of the CMAC key in bits.
81 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +010082 *
Rose Zadik380d05d2018-01-25 21:52:41 +000083 * \return \c 0 on success, or a cipher-specific error code.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000084 */
Simon Butcher327398a2016-10-05 14:09:11 +010085int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010086 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000087
88/**
Rose Zadik380d05d2018-01-25 21:52:41 +000089 * \brief This function feeds an input buffer into an ongoing CMAC
90 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000091 *
Rose Zadik380d05d2018-01-25 21:52:41 +000092 * It is called between mbedtls_cipher_cmac_starts() or
93 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
94 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000095 *
Rose Zadik380d05d2018-01-25 21:52:41 +000096 * \param ctx The cipher context used for the CMAC operation.
97 * \param input The buffer holding the input data.
98 * \param ilen The length of the input data.
99 *
100 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
101 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000102 */
Simon Butcher327398a2016-10-05 14:09:11 +0100103int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
104 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000105
106/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000107 * \brief This function finishes the CMAC operation, and writes
108 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000109 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000110 * It is called after mbedtls_cipher_cmac_update().
111 * It can be followed by mbedtls_cipher_cmac_reset() and
112 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100113 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000114 * \param ctx The cipher context used for the CMAC operation.
115 * \param output The output buffer for the CMAC checksum result.
116 *
117 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
118 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000119 */
Simon Butcher327398a2016-10-05 14:09:11 +0100120int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
121 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000122
123/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000124 * \brief This function prepares the authentication of another
125 * message with the same key as the previous CMAC
126 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000127 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000128 * It is called after mbedtls_cipher_cmac_finish()
129 * and before mbedtls_cipher_cmac_update().
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 *
133 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
134 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000135 */
Simon Butcher327398a2016-10-05 14:09:11 +0100136int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000137
138/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000139 * \brief This function calculates the full generic CMAC
140 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000141 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000142 * The function allocates the context, performs the
143 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100144 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000145 * The CMAC result is calculated as
146 * output = generic CMAC(cmac key, input buffer).
147 *
148 *
149 * \param cipher_info The cipher information.
150 * \param key The CMAC key.
151 * \param keylen The length of the CMAC key in bits.
152 * \param input The buffer holding the input data.
153 * \param ilen The length of the input data.
154 * \param output The buffer for the generic CMAC result.
155 *
156 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
157 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000158 */
Simon Butcher327398a2016-10-05 14:09:11 +0100159int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
160 const unsigned char *key, size_t keylen,
161 const unsigned char *input, size_t ilen,
162 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000163
Simon Butcher69283e52016-10-06 12:49:58 +0100164#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000165/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000166 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
167 * function, as defined in
168 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
169 * Message Authentication Code-Pseudo-Random Function-128
170 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
171 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000172 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000173 * \param key The key to use.
174 * \param key_len The key length in Bytes.
175 * \param input The buffer holding the input data.
176 * \param in_len The length of the input data in Bytes.
177 * \param output The buffer holding the generated 16 Bytes of
178 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000179 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000180 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000181 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700182int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000183 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100184 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700185#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000186
Brian Murrayb439d452016-05-19 16:02:42 -0700187#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000188/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000189 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000190 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000191 * \return \c 0 on success, or \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000192 */
193int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700194#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000195
196#ifdef __cplusplus
197}
198#endif
199
200#endif /* MBEDTLS_CMAC_H */