blob: adfe1c3e01928507c410d0a7fb0c7d003961f304 [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 Eldor0559c662018-02-14 16:02:41 +020029#if !defined(MBEDTLS_CONFIG_FILE)
30#include "config.h"
31#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
Ron Eldordf9b93e2017-05-14 16:17:33 +030035#include "cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
Rose Zadik380d05d2018-01-25 21:52:41 +000041#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010042
Simon Butcher69283e52016-10-06 12:49:58 +010043#define MBEDTLS_AES_BLOCK_SIZE 16
44#define MBEDTLS_DES3_BLOCK_SIZE 8
45
Simon Butcher327398a2016-10-05 14:09:11 +010046#if defined(MBEDTLS_AES_C)
Rose Zadik380d05d2018-01-25 21:52:41 +000047#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010048#else
Rose Zadik380d05d2018-01-25 21:52:41 +000049#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010050#endif
51
Steven Cooreman63342772017-04-04 11:47:16 +020052#if !defined(MBEDTLS_CMAC_ALT)
53
Robert Cragie3d23b1d2015-12-15 07:38:11 +000054/**
Rose Zadik380d05d2018-01-25 21:52:41 +000055 * The CMAC context structure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000056 */
Simon Butcher8308a442016-10-05 15:12:59 +010057struct mbedtls_cmac_context_t
58{
Rose Zadik380d05d2018-01-25 21:52:41 +000059 /** The internal state of the CMAC algorithm. */
Simon Butcher69283e52016-10-06 12:49:58 +010060 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010061
Andres AGa592dcc2016-10-06 15:23:39 +010062 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000063 * pending processing, or the final block. */
Simon Butcher69283e52016-10-06 12:49:58 +010064 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010065
Rose Zadik380d05d2018-01-25 21:52:41 +000066 /** The length of data pending processing. */
Simon Butcher327398a2016-10-05 14:09:11 +010067 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010068};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000069
70/**
Rose Zadik380d05d2018-01-25 21:52:41 +000071 * \brief This function sets the CMAC key, and prepares to authenticate
72 * the input data.
73 * Must be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000074 *
Rose Zadik380d05d2018-01-25 21:52:41 +000075 * \param ctx The cipher context used for the CMAC operation, initialized
76 * as one of the following types:<ul>
77 * <li>MBEDTLS_CIPHER_AES_128_ECB</li>
78 * <li>MBEDTLS_CIPHER_AES_192_ECB</li>
79 * <li>MBEDTLS_CIPHER_AES_256_ECB</li>
80 * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul>
81 * \param key The CMAC key.
82 * \param keybits The length of the CMAC key in bits.
83 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +010084 *
Rose Zadik380d05d2018-01-25 21:52:41 +000085 * \return \c 0 on success, or a cipher-specific error code.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000086 */
Simon Butcher327398a2016-10-05 14:09:11 +010087int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010088 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000089
90/**
Rose Zadik380d05d2018-01-25 21:52:41 +000091 * \brief This function feeds an input buffer into an ongoing CMAC
92 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000093 *
Rose Zadik380d05d2018-01-25 21:52:41 +000094 * It is called between mbedtls_cipher_cmac_starts() or
95 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish().
96 * Can be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000097 *
Rose Zadik380d05d2018-01-25 21:52:41 +000098 * \param ctx The cipher context used for the CMAC operation.
99 * \param input The buffer holding the input data.
100 * \param ilen The length of the input data.
101 *
102 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
103 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000104 */
Simon Butcher327398a2016-10-05 14:09:11 +0100105int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
106 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000107
108/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000109 * \brief This function finishes the CMAC operation, and writes
110 * the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000111 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000112 * It is called after mbedtls_cipher_cmac_update().
113 * It can be followed by mbedtls_cipher_cmac_reset() and
114 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free().
Simon Butcher327398a2016-10-05 14:09:11 +0100115 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000116 * \param ctx The cipher context used for the CMAC operation.
117 * \param output The output buffer for the CMAC checksum result.
118 *
119 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
120 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000121 */
Simon Butcher327398a2016-10-05 14:09:11 +0100122int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
123 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000124
125/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000126 * \brief This function prepares the authentication of another
127 * message with the same key as the previous CMAC
128 * operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000129 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000130 * It is called after mbedtls_cipher_cmac_finish()
131 * and before mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000132 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000133 * \param ctx The cipher context used for the CMAC operation.
134 *
135 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
136 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000137 */
Simon Butcher327398a2016-10-05 14:09:11 +0100138int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000139
140/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000141 * \brief This function calculates the full generic CMAC
142 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000144 * The function allocates the context, performs the
145 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100146 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000147 * The CMAC result is calculated as
148 * output = generic CMAC(cmac key, input buffer).
149 *
150 *
151 * \param cipher_info The cipher information.
152 * \param key The CMAC key.
153 * \param keylen The length of the CMAC key in bits.
154 * \param input The buffer holding the input data.
155 * \param ilen The length of the input data.
156 * \param output The buffer for the generic CMAC result.
157 *
158 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA
159 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000160 */
Simon Butcher327398a2016-10-05 14:09:11 +0100161int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
162 const unsigned char *key, size_t keylen,
163 const unsigned char *input, size_t ilen,
164 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000165
Simon Butcher69283e52016-10-06 12:49:58 +0100166#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000167/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000168 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
169 * function, as defined in
170 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
171 * Message Authentication Code-Pseudo-Random Function-128
172 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
173 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000174 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000175 * \param key The key to use.
176 * \param key_len The key length in Bytes.
177 * \param input The buffer holding the input data.
178 * \param in_len The length of the input data in Bytes.
179 * \param output The buffer holding the generated 16 Bytes of
180 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000181 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000182 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000183 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700184int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000185 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100186 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700187#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000188
Steven Cooreman63342772017-04-04 11:47:16 +0200189#ifdef __cplusplus
190}
191#endif
192
193#else /* !MBEDTLS_CMAC_ALT */
194#include "cmac_alt.h"
195#endif /* !MBEDTLS_CMAC_ALT */
196
197#ifdef __cplusplus
198extern "C" {
199#endif
200
Brian Murrayb439d452016-05-19 16:02:42 -0700201#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000202/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000203 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000204 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000205 * \return \c 0 on success, or \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000206 */
207int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700208#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000209
210#ifdef __cplusplus
211}
212#endif
213
214#endif /* MBEDTLS_CMAC_H */