blob: 24839a20eb4bee2366ddef22a0040d44323fb5d5 [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
Ron Eldordf9b93e2017-05-14 16:17:33 +030029#include "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
64/**
Rose Zadik380d05d2018-01-25 21:52:41 +000065 * \brief This function sets the CMAC key, and prepares to authenticate
66 * the input data.
67 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000068 *
Rose Zadik380d05d2018-01-25 21:52:41 +000069 * \param ctx The cipher context used for the CMAC operation, initialized
70 * as one of the following types:<ul>
71 * <li>MBEDTLS_CIPHER_AES_128_ECB</li>
72 * <li>MBEDTLS_CIPHER_AES_192_ECB</li>
73 * <li>MBEDTLS_CIPHER_AES_256_ECB</li>
74 * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul>
75 * \param key The CMAC key.
76 * \param keybits The length of the CMAC key in bits.
77 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +010078 *
Rose Zadik380d05d2018-01-25 21:52:41 +000079 * \return \c 0 on success, or a cipher-specific error code.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000080 */
Simon Butcher327398a2016-10-05 14:09:11 +010081int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010082 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000083
84/**
Rose Zadik380d05d2018-01-25 21:52:41 +000085 * \brief This function feeds an input buffer into an ongoing CMAC
86 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000087 *
Rose Zadik380d05d2018-01-25 21:52:41 +000088 * It is called between mbedtls_cipher_cmac_starts() or
89 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
90 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000091 *
Rose Zadik380d05d2018-01-25 21:52:41 +000092 * \param ctx The cipher context used for the CMAC operation.
93 * \param input The buffer holding the input data.
94 * \param ilen The length of the input data.
95 *
96 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
97 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000098 */
Simon Butcher327398a2016-10-05 14:09:11 +010099int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
100 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000101
102/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000103 * \brief This function finishes the CMAC operation, and writes
104 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000105 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000106 * It is called after mbedtls_cipher_cmac_update().
107 * It can be followed by mbedtls_cipher_cmac_reset() and
108 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100109 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000110 * \param ctx The cipher context used for the CMAC operation.
111 * \param output The output buffer for the CMAC checksum result.
112 *
113 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
114 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000115 */
Simon Butcher327398a2016-10-05 14:09:11 +0100116int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
117 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000118
119/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000120 * \brief This function prepares the authentication of another
121 * message with the same key as the previous CMAC
122 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000123 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000124 * It is called after mbedtls_cipher_cmac_finish()
125 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000126 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000127 * \param ctx The cipher context used for the CMAC operation.
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_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000133
134/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000135 * \brief This function calculates the full generic CMAC
136 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000137 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000138 * The function allocates the context, performs the
139 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100140 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000141 * The CMAC result is calculated as
142 * output = generic CMAC(cmac key, input buffer).
143 *
144 *
145 * \param cipher_info The cipher information.
146 * \param key The CMAC key.
147 * \param keylen The length of the CMAC key in bits.
148 * \param input The buffer holding the input data.
149 * \param ilen The length of the input data.
150 * \param output The buffer for the generic CMAC result.
151 *
152 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
153 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154 */
Simon Butcher327398a2016-10-05 14:09:11 +0100155int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
156 const unsigned char *key, size_t keylen,
157 const unsigned char *input, size_t ilen,
158 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000159
Simon Butcher69283e52016-10-06 12:49:58 +0100160#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000161/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000162 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
163 * function, as defined in
164 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
165 * Message Authentication Code-Pseudo-Random Function-128
166 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
167 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000168 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000169 * \param key The key to use.
170 * \param key_len The key length in Bytes.
171 * \param input The buffer holding the input data.
172 * \param in_len The length of the input data in Bytes.
173 * \param output The buffer holding the generated 16 Bytes of
174 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000175 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000176 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000177 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700178int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000179 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100180 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700181#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000182
Steven Cooreman63342772017-04-04 11:47:16 +0200183#ifdef __cplusplus
184}
185#endif
186
187#else /* !MBEDTLS_CMAC_ALT */
188#include "cmac_alt.h"
189#endif /* !MBEDTLS_CMAC_ALT */
190
191#ifdef __cplusplus
192extern "C" {
193#endif
194
Brian Murrayb439d452016-05-19 16:02:42 -0700195#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000196/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000197 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000198 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000199 * \return \c 0 on success, or \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000200 */
201int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700202#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000203
204#ifdef __cplusplus
205}
206#endif
207
208#endif /* MBEDTLS_CMAC_H */