blob: 4d3f2d2f4fe7a6e8baf54e8ce87a64256d7af73c [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 Butcher69283e52016-10-06 12:49:58 +010033#define MBEDTLS_AES_BLOCK_SIZE 16
34#define MBEDTLS_DES3_BLOCK_SIZE 8
35
Simon Butcher327398a2016-10-05 14:09:11 +010036#if defined(MBEDTLS_AES_C)
Simon Butcher69283e52016-10-06 12:49:58 +010037#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
Simon Butcher327398a2016-10-05 14:09:11 +010038#else
Simon Butcher69283e52016-10-06 12:49:58 +010039#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
Simon Butcher327398a2016-10-05 14:09:11 +010040#endif
41
Steven Cooreman63342772017-04-04 11:47:16 +020042#if !defined(MBEDTLS_CMAC_ALT)
43
Robert Cragie3d23b1d2015-12-15 07:38:11 +000044/**
Simon Butcher8308a442016-10-05 15:12:59 +010045 * CMAC context structure - Contains internal state information only
Robert Cragie3d23b1d2015-12-15 07:38:11 +000046 */
Simon Butcher8308a442016-10-05 15:12:59 +010047struct mbedtls_cmac_context_t
48{
Simon Butcher327398a2016-10-05 14:09:11 +010049 /** Internal state of the CMAC algorithm */
Simon Butcher69283e52016-10-06 12:49:58 +010050 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010051
Andres AGa592dcc2016-10-06 15:23:39 +010052 /** Unprocessed data - either data that was not block aligned and is still
Simon Butcher327398a2016-10-05 14:09:11 +010053 * pending to be processed, or the final block */
Simon Butcher69283e52016-10-06 12:49:58 +010054 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +010055
56 /** Length of data pending to be processed */
57 size_t unprocessed_len;
Simon Butcher8308a442016-10-05 15:12:59 +010058};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000059
60/**
Simon Butcher327398a2016-10-05 14:09:11 +010061 * \brief Set the CMAC key and prepare to authenticate the input
62 * data.
Simon Butcherf4957a82016-12-14 15:27:22 +000063 * Should be called with an initialized cipher context.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000064 *
Simon Butcherf4957a82016-12-14 15:27:22 +000065 * \param ctx Cipher context. This should be a cipher context,
66 * initialized to be one of the following types:
67 * MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB,
68 * MBEDTLS_CIPHER_AES_256_ECB or
69 * MBEDTLS_CIPHER_DES_EDE3_ECB.
Simon Butcher327398a2016-10-05 14:09:11 +010070 * \param key CMAC key
71 * \param keybits length of the CMAC key in bits
72 * (must be acceptable by the cipher)
73 *
74 * \return 0 if successful, or a cipher specific error code
Robert Cragie3d23b1d2015-12-15 07:38:11 +000075 */
Simon Butcher327398a2016-10-05 14:09:11 +010076int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +010077 const unsigned char *key, size_t keybits );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000078
79/**
Simon Butcher327398a2016-10-05 14:09:11 +010080 * \brief Generic CMAC process buffer.
81 * Called between mbedtls_cipher_cmac_starts() or
82 * mbedtls_cipher_cmac_reset() and
83 * mbedtls_cipher_cmac_finish().
84 * May be called repeatedly.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000085 *
Simon Butcher327398a2016-10-05 14:09:11 +010086 * \param ctx CMAC context
87 * \param input buffer holding the data
88 * \param ilen length of the input data
Robert Cragie3d23b1d2015-12-15 07:38:11 +000089 *
Simon Butcher327398a2016-10-05 14:09:11 +010090 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
91 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000092 */
Simon Butcher327398a2016-10-05 14:09:11 +010093int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
94 const unsigned char *input, size_t ilen );
Robert Cragie3d23b1d2015-12-15 07:38:11 +000095
96/**
Simon Butcher327398a2016-10-05 14:09:11 +010097 * \brief Output CMAC.
98 * Called after mbedtls_cipher_cmac_update().
99 * Usually followed by mbedtls_cipher_cmac_reset(), then
100 * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000101 *
Simon Butcher327398a2016-10-05 14:09:11 +0100102 * \param ctx CMAC context
103 * \param output Generic CMAC checksum result
104 *
105 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
106 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000107 */
Simon Butcher327398a2016-10-05 14:09:11 +0100108int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
109 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000110
111/**
Simon Butcher327398a2016-10-05 14:09:11 +0100112 * \brief Prepare to authenticate a new message with the same key.
113 * Called after mbedtls_cipher_cmac_finish() and before
114 * mbedtls_cipher_cmac_update().
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000115 *
Simon Butcher327398a2016-10-05 14:09:11 +0100116 * \param ctx CMAC context to be reset
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000117 *
Simon Butcher327398a2016-10-05 14:09:11 +0100118 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
119 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120 */
Simon Butcher327398a2016-10-05 14:09:11 +0100121int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000122
123/**
Simon Butcherf4957a82016-12-14 15:27:22 +0000124 * \brief Output = Generic_CMAC( cmac key, input buffer )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000125 *
Simon Butcher327398a2016-10-05 14:09:11 +0100126 * \param cipher_info message digest info
127 * \param key CMAC key
128 * \param keylen length of the CMAC key in bits
129 * \param input buffer holding the data
130 * \param ilen length of the input data
131 * \param output Generic CMAC-result
132 *
133 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
134 * verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000135 */
Simon Butcher327398a2016-10-05 14:09:11 +0100136int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
137 const unsigned char *key, size_t keylen,
138 const unsigned char *input, size_t ilen,
139 unsigned char *output );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000140
Simon Butcher69283e52016-10-06 12:49:58 +0100141#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000142/**
143 * \brief AES-CMAC-128-PRF
Simon Butcher327398a2016-10-05 14:09:11 +0100144 * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000145 *
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000146 * \param key PRF key
Simon Butcher327398a2016-10-05 14:09:11 +0100147 * \param key_len PRF key length in bytes
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000148 * \param input buffer holding the input data
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000149 * \param in_len length of the input data in bytes
Simon Butcher327398a2016-10-05 14:09:11 +0100150 * \param output buffer holding the generated pseudorandom output (16 bytes)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000151 *
152 * \return 0 if successful
153 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700154int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000155 const unsigned char *input, size_t in_len,
Simon Butcher327398a2016-10-05 14:09:11 +0100156 unsigned char output[16] );
Brian Murrayb439d452016-05-19 16:02:42 -0700157#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000158
Steven Cooreman63342772017-04-04 11:47:16 +0200159#ifdef __cplusplus
160}
161#endif
162
163#else /* !MBEDTLS_CMAC_ALT */
164#include "cmac_alt.h"
165#endif /* !MBEDTLS_CMAC_ALT */
166
167#ifdef __cplusplus
168extern "C" {
169#endif
170
Brian Murrayb439d452016-05-19 16:02:42 -0700171#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000172/**
173 * \brief Checkup routine
174 *
175 * \return 0 if successful, or 1 if the test failed
176 */
177int mbedtls_cmac_self_test( int verbose );
Brian Murrayb439d452016-05-19 16:02:42 -0700178#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000179
180#ifdef __cplusplus
181}
182#endif
183
184#endif /* MBEDTLS_CMAC_H */