blob: 6d531dddb56d56efad8d448fbb0eab6c00060520 [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
Robert Cragie3d23b1d2015-12-15 07:38:11 +00006 *
Simon Butcher327398a2016-10-05 14:09:11 +01007 * Copyright (C) 2015-2016, ARM Limited, All Rights Reserved
Robert Cragie3d23b1d2015-12-15 07:38:11 +00008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24#ifndef MBEDTLS_CMAC_H
25#define MBEDTLS_CMAC_H
26
Simon Butcher327398a2016-10-05 14:09:11 +010027#include "mbedtls/cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000028
29#ifdef __cplusplus
30extern "C" {
31#endif
32
Simon Butcher327398a2016-10-05 14:09:11 +010033#if defined(MBEDTLS_AES_C)
34#define MBEDTLS_CIPHER_BLKSIZE_MAX_SIZE 16 /* longest known is AES */
35#else
36#define MBEDTLS_CIPHER_BLKSIZE_MAX_SIZE 8 /* longest known is 3DES */
37#endif
38
Robert Cragie3d23b1d2015-12-15 07:38:11 +000039/**
Simon Butcher8308a442016-10-05 15:12:59 +010040 * CMAC context structure - Contains internal state information only
Robert Cragie3d23b1d2015-12-15 07:38:11 +000041 */
Simon Butcher8308a442016-10-05 15:12:59 +010042struct mbedtls_cmac_context_t
43{
Simon Butcher327398a2016-10-05 14:09:11 +010044
45 /** Internal state of the CMAC algorithm */
46 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX_SIZE];
47
48 /** Unprocessed data - either data that was not block aligned and is still
49 * pending to be processed, or the final block */
50 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX_SIZE];
51
52 /** Length of data pending to be processed */
53 size_t unprocessed_len;
54
55 /** Flag to indicate if the last block needs padding */
56 int padding_flag;
Simon Butcher8308a442016-10-05 15:12:59 +010057};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000058
59/**
Simon Butcher327398a2016-10-05 14:09:11 +010060 * \brief Set the CMAC key and prepare to authenticate the input
61 * data.
62 * Should be called with an initialised cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000063 *
Simon Butcher327398a2016-10-05 14:09:11 +010064 * \param ctx Cipher context
65 * \param key CMAC key
66 * \param keybits length of the CMAC key in bits
67 * (must be acceptable by the cipher)
68 *
69 * \return 0 if successful, or a cipher specific error code
Robert Cragie3d23b1d2015-12-15 07:38:11 +000070 */
Simon Butcher327398a2016-10-05 14:09:11 +010071int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
72 const unsigned char *key, size_t keylen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000073
74/**
Simon Butcher327398a2016-10-05 14:09:11 +010075 * \brief Generic CMAC process buffer.
76 * Called between mbedtls_cipher_cmac_starts() or
77 * mbedtls_cipher_cmac_reset() and
78 * mbedtls_cipher_cmac_finish().
79 * May be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000080 *
Simon Butcher327398a2016-10-05 14:09:11 +010081 * \param ctx CMAC context
82 * \param input buffer holding the data
83 * \param ilen length of the input data
Robert Cragie3d23b1d2015-12-15 07:38:11 +000084 *
Simon Butcher327398a2016-10-05 14:09:11 +010085 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
86 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000087 */
Simon Butcher327398a2016-10-05 14:09:11 +010088int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
89 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000090
91/**
Simon Butcher327398a2016-10-05 14:09:11 +010092 * \brief Output CMAC.
93 * Called after mbedtls_cipher_cmac_update().
94 * Usually followed by mbedtls_cipher_cmac_reset(), then
95 * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
Robert Cragie3d23b1d2015-12-15 07:38:11 +000096 *
Simon Butcher327398a2016-10-05 14:09:11 +010097 * \param ctx CMAC context
98 * \param output Generic CMAC checksum result
99 *
100 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
101 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000102 */
Simon Butcher327398a2016-10-05 14:09:11 +0100103int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
104 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000105
106/**
Simon Butcher327398a2016-10-05 14:09:11 +0100107 * \brief Prepare to authenticate a new message with the same key.
108 * Called after mbedtls_cipher_cmac_finish() and before
109 * mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000110 *
Simon Butcher327398a2016-10-05 14:09:11 +0100111 * \param ctx CMAC context to be reset
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000112 *
Simon Butcher327398a2016-10-05 14:09:11 +0100113 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
114 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000115 */
Simon Butcher327398a2016-10-05 14:09:11 +0100116int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000117
118/**
Simon Butcher327398a2016-10-05 14:09:11 +0100119 * \brief Output = Generic_CMAC( hmac key, input buffer )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120 *
Simon Butcher327398a2016-10-05 14:09:11 +0100121 * \param cipher_info message digest info
122 * \param key CMAC key
123 * \param keylen length of the CMAC key in bits
124 * \param input buffer holding the data
125 * \param ilen length of the input data
126 * \param output Generic CMAC-result
127 *
128 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
129 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000130 */
Simon Butcher327398a2016-10-05 14:09:11 +0100131int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
132 const unsigned char *key, size_t keylen,
133 const unsigned char *input, size_t ilen,
134 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000135
Brian Murrayb439d452016-05-19 16:02:42 -0700136#ifdef MBEDTLS_AES_C
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000137/**
138 * \brief AES-CMAC-128-PRF
Simon Butcher327398a2016-10-05 14:09:11 +0100139 * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000140 *
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000141 * \param key PRF key
Simon Butcher327398a2016-10-05 14:09:11 +0100142 * \param key_len PRF key length in bytes
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143 * \param input buffer holding the input data
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000144 * \param in_len length of the input data in bytes
Simon Butcher327398a2016-10-05 14:09:11 +0100145 * \param output buffer holding the generated pseudorandom output (16 bytes)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000146 *
147 * \return 0 if successful
148 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700149int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000150 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100151 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700152#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000153
Brian Murrayb439d452016-05-19 16:02:42 -0700154#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000155/**
156 * \brief Checkup routine
157 *
158 * \return 0 if successful, or 1 if the test failed
159 */
160int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700161#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* MBEDTLS_CMAC_H */