blob: 1cac948968ff7f78a08e1fc5027610b87ba86753 [file] [log] [blame]
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001/**
2 * \file cmac.h
3 *
Simon Butcher327398a2016-10-05 14:09:11 +01004 * \brief Cipher-based Message Authentication Code (CMAC) Mode for
5 * Authentication
Darryl Greena40a1012018-01-05 15:33:17 +00006 */
7/*
Simon Butcher327398a2016-10-05 14:09:11 +01008 * Copyright (C) 2015-2016, ARM Limited, 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 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25#ifndef MBEDTLS_CMAC_H
26#define MBEDTLS_CMAC_H
27
Simon Butcher327398a2016-10-05 14:09:11 +010028#include "mbedtls/cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000029
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010034#define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */
35
Simon Butcher69283e52016-10-06 12:49:58 +010036#define MBEDTLS_AES_BLOCK_SIZE 16
37#define MBEDTLS_DES3_BLOCK_SIZE 8
38
Simon Butcher327398a2016-10-05 14:09:11 +010039#if defined(MBEDTLS_AES_C)
Simon Butcher69283e52016-10-06 12:49:58 +010040#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
Simon Butcher327398a2016-10-05 14:09:11 +010041#else
Simon Butcher69283e52016-10-06 12:49:58 +010042#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
Simon Butcher327398a2016-10-05 14:09:11 +010043#endif
44
Steven Cooreman63342772017-04-04 11:47:16 +020045#if !defined(MBEDTLS_CMAC_ALT)
46
Robert Cragie3d23b1d2015-12-15 07:38:11 +000047/**
Simon Butcher8308a442016-10-05 15:12:59 +010048 * CMAC context structure - Contains internal state information only
Robert Cragie3d23b1d2015-12-15 07:38:11 +000049 */
Simon Butcher8308a442016-10-05 15:12:59 +010050struct mbedtls_cmac_context_t
51{
Simon Butcher327398a2016-10-05 14:09:11 +010052 /** Internal state of the CMAC algorithm */
Simon Butcher69283e52016-10-06 12:49:58 +010053 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010054
Andres AGa592dcc2016-10-06 15:23:39 +010055 /** Unprocessed data - either data that was not block aligned and is still
Simon Butcher327398a2016-10-05 14:09:11 +010056 * pending to be processed, or the final block */
Simon Butcher69283e52016-10-06 12:49:58 +010057 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010058
59 /** Length of data pending to be processed */
60 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010061};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000062
63/**
Simon Butcher327398a2016-10-05 14:09:11 +010064 * \brief Set the CMAC key and prepare to authenticate the input
65 * data.
Simon Butcherf4957a82016-12-14 15:27:22 +000066 * Should be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000067 *
Simon Butcherf4957a82016-12-14 15:27:22 +000068 * \param ctx Cipher context. This should be a cipher context,
69 * initialized to be one of the following types:
70 * MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB,
71 * MBEDTLS_CIPHER_AES_256_ECB or
72 * MBEDTLS_CIPHER_DES_EDE3_ECB.
Simon Butcher327398a2016-10-05 14:09:11 +010073 * \param key CMAC key
74 * \param keybits length of the CMAC key in bits
75 * (must be acceptable by the cipher)
76 *
77 * \return 0 if successful, or a cipher specific error code
Robert Cragie3d23b1d2015-12-15 07:38:11 +000078 */
Simon Butcher327398a2016-10-05 14:09:11 +010079int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010080 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000081
82/**
Simon Butcher327398a2016-10-05 14:09:11 +010083 * \brief Generic CMAC process buffer.
84 * Called between mbedtls_cipher_cmac_starts() or
85 * mbedtls_cipher_cmac_reset() and
86 * mbedtls_cipher_cmac_finish().
87 * May be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000088 *
Simon Butcher327398a2016-10-05 14:09:11 +010089 * \param ctx CMAC context
90 * \param input buffer holding the data
91 * \param ilen length of the input data
Robert Cragie3d23b1d2015-12-15 07:38:11 +000092 *
Simon Butcher327398a2016-10-05 14:09:11 +010093 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
94 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000095 */
Simon Butcher327398a2016-10-05 14:09:11 +010096int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
97 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000098
99/**
Simon Butcher327398a2016-10-05 14:09:11 +0100100 * \brief Output CMAC.
101 * Called after mbedtls_cipher_cmac_update().
102 * Usually followed by mbedtls_cipher_cmac_reset(), then
103 * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000104 *
Simon Butcher327398a2016-10-05 14:09:11 +0100105 * \param ctx CMAC context
106 * \param output Generic CMAC checksum result
107 *
108 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
109 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000110 */
Simon Butcher327398a2016-10-05 14:09:11 +0100111int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
112 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000113
114/**
Simon Butcher327398a2016-10-05 14:09:11 +0100115 * \brief Prepare to authenticate a new message with the same key.
116 * Called after mbedtls_cipher_cmac_finish() and before
117 * mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000118 *
Simon Butcher327398a2016-10-05 14:09:11 +0100119 * \param ctx CMAC context to be reset
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120 *
Simon Butcher327398a2016-10-05 14:09:11 +0100121 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
122 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000123 */
Simon Butcher327398a2016-10-05 14:09:11 +0100124int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000125
126/**
Simon Butcherf4957a82016-12-14 15:27:22 +0000127 * \brief Output = Generic_CMAC( cmac key, input buffer )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000128 *
Simon Butcher327398a2016-10-05 14:09:11 +0100129 * \param cipher_info message digest info
130 * \param key CMAC key
131 * \param keylen length of the CMAC key in bits
132 * \param input buffer holding the data
133 * \param ilen length of the input data
134 * \param output Generic CMAC-result
135 *
136 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
137 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000138 */
Simon Butcher327398a2016-10-05 14:09:11 +0100139int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
140 const unsigned char *key, size_t keylen,
141 const unsigned char *input, size_t ilen,
142 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143
Simon Butcher69283e52016-10-06 12:49:58 +0100144#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000145/**
146 * \brief AES-CMAC-128-PRF
Simon Butcher327398a2016-10-05 14:09:11 +0100147 * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000148 *
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000149 * \param key PRF key
Simon Butcher327398a2016-10-05 14:09:11 +0100150 * \param key_len PRF key length in bytes
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000151 * \param input buffer holding the input data
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000152 * \param in_len length of the input data in bytes
Simon Butcher327398a2016-10-05 14:09:11 +0100153 * \param output buffer holding the generated pseudorandom output (16 bytes)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000154 *
155 * \return 0 if successful
156 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700157int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000158 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100159 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700160#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000161
Steven Cooreman63342772017-04-04 11:47:16 +0200162#ifdef __cplusplus
163}
164#endif
165
166#else /* !MBEDTLS_CMAC_ALT */
167#include "cmac_alt.h"
168#endif /* !MBEDTLS_CMAC_ALT */
169
170#ifdef __cplusplus
171extern "C" {
172#endif
173
Brian Murrayb439d452016-05-19 16:02:42 -0700174#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000175/**
176 * \brief Checkup routine
177 *
178 * \return 0 if successful, or 1 if the test failed
179 */
180int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700181#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000182
183#ifdef __cplusplus
184}
185#endif
186
187#endif /* MBEDTLS_CMAC_H */