blob: fd3d8a2bc74c010b3588ae183dea35abecf9495b [file] [log] [blame]
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001/**
2 * \file cmac.h
3 *
Rose Zadik8c154932018-03-27 10:45:16 +01004 * \brief This file contains CMAC definitions and functions.
5 *
6 * The Cipher-based Message Authentication Code (CMAC) Mode for
7 * Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>.
Gilles Peskine16bb83c2023-06-14 17:42:26 +02008 * It is supported with AES and DES.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020011 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Robert Cragie3d23b1d2015-12-15 07:38:11 +000013 */
Rose Zadik380d05d2018-01-25 21:52:41 +000014
Robert Cragie3d23b1d2015-12-15 07:38:11 +000015#ifndef MBEDTLS_CMAC_H
16#define MBEDTLS_CMAC_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020017#include "mbedtls/private_access.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000018
Bence Szépkútic662b362021-05-27 11:25:03 +020019#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010021#include "mbedtls/cipher.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000022
23#ifdef __cplusplus
24extern "C" {
25#endif
26
Simon Butcher69283e52016-10-06 12:49:58 +010027#define MBEDTLS_AES_BLOCK_SIZE 16
28#define MBEDTLS_DES3_BLOCK_SIZE 8
29
Gilles Peskine16bb83c2023-06-14 17:42:26 +020030/* We don't support Camellia or ARIA in this module */
Simon Butcher327398a2016-10-05 14:09:11 +010031#if defined(MBEDTLS_AES_C)
Gilles Peskine7282a9e2023-06-14 17:49:02 +020032#define MBEDTLS_CMAC_MAX_BLOCK_SIZE 16 /**< The longest block used by CMAC is that of AES. */
Simon Butcher327398a2016-10-05 14:09:11 +010033#else
Gilles Peskine7282a9e2023-06-14 17:49:02 +020034#define MBEDTLS_CMAC_MAX_BLOCK_SIZE 8 /**< The longest block used by CMAC is that of 3DES. */
Simon Butcher327398a2016-10-05 14:09:11 +010035#endif
36
Gilles Peskinec453e2e2023-06-14 17:54:38 +020037#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine7282a9e2023-06-14 17:49:02 +020038/** The longest block supported by the cipher module.
39 *
40 * \deprecated
41 * For the maximum block size of a cipher supported by the CMAC module,
42 * use #MBEDTLS_CMAC_MAX_BLOCK_SIZE.
43 * For the maximum block size of a cipher supported by the cipher module,
44 * use #MBEDTLS_MAX_BLOCK_LENGTH.
45 */
46/* Before Mbed TLS 3.5, this was the maximum block size supported by the CMAC
47 * module, so it didn't take Camellia or ARIA into account. Since the name
48 * of the macro doesn't even convey "CMAC", this was misleading. Now the size
49 * is sufficient for any cipher, but the name is defined in cmac.h for
50 * backward compatibility. */
51#define MBEDTLS_CIPHER_BLKSIZE_MAX MBEDTLS_MAX_BLOCK_LENGTH
Gilles Peskinec453e2e2023-06-14 17:54:38 +020052#endif /* MBEDTLS_DEPRECATED_REMOVED */
Gilles Peskine7282a9e2023-06-14 17:49:02 +020053
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010057struct mbedtls_cmac_context_t {
Rose Zadik380d05d2018-01-25 21:52:41 +000058 /** The internal state of the CMAC algorithm. */
Gilles Peskine9e930e22023-06-14 17:52:54 +020059 unsigned char MBEDTLS_PRIVATE(state)[MBEDTLS_CMAC_MAX_BLOCK_SIZE];
Simon Butcher327398a2016-10-05 14:09:11 +010060
Andres AGa592dcc2016-10-06 15:23:39 +010061 /** Unprocessed data - either data that was not block aligned and is still
Rose Zadik380d05d2018-01-25 21:52:41 +000062 * pending processing, or the final block. */
Gilles Peskine9e930e22023-06-14 17:52:54 +020063 unsigned char MBEDTLS_PRIVATE(unprocessed_block)[MBEDTLS_CMAC_MAX_BLOCK_SIZE];
Simon Butcher327398a2016-10-05 14:09:11 +010064
Rose Zadik380d05d2018-01-25 21:52:41 +000065 /** The length of data pending processing. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020066 size_t MBEDTLS_PRIVATE(unprocessed_len);
Simon Butcher8308a442016-10-05 15:12:59 +010067};
Robert Cragie3d23b1d2015-12-15 07:38:11 +000068
69/**
David Horstmann3d5dfa52021-12-06 18:58:02 +000070 * \brief This function starts a new CMAC computation
71 * by setting the CMAC key, and preparing to authenticate
Rose Zadik380d05d2018-01-25 21:52:41 +000072 * the input data.
David Horstmann3d5dfa52021-12-06 18:58:02 +000073 * It must be called with an initialized cipher context.
74 *
75 * Once this function has completed, data can be supplied
76 * to the CMAC computation by calling
77 * mbedtls_cipher_cmac_update().
78 *
79 * To start a CMAC computation using the same key as a previous
80 * CMAC computation, use mbedtls_cipher_cmac_finish().
Robert Cragie3d23b1d2015-12-15 07:38:11 +000081 *
Rose Zadik380d05d2018-01-25 21:52:41 +000082 * \param ctx The cipher context used for the CMAC operation, initialized
Rose Zadik8c154932018-03-27 10:45:16 +010083 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB,
84 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB,
85 * or MBEDTLS_CIPHER_DES_EDE3_ECB.
Rose Zadik380d05d2018-01-25 21:52:41 +000086 * \param key The CMAC key.
87 * \param keybits The length of the CMAC key in bits.
88 * Must be supported by the cipher.
Simon Butcher327398a2016-10-05 14:09:11 +010089 *
Rose Zadikc138bb72018-04-16 11:11:25 +010090 * \return \c 0 on success.
91 * \return A cipher-specific error code on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000092 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
94 const unsigned char *key, size_t keybits);
Robert Cragie3d23b1d2015-12-15 07:38:11 +000095
96/**
Rose Zadik380d05d2018-01-25 21:52:41 +000097 * \brief This function feeds an input buffer into an ongoing CMAC
98 * computation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000099 *
David Horstmann3d5dfa52021-12-06 18:58:02 +0000100 * The CMAC computation must have previously been started
101 * by calling mbedtls_cipher_cmac_starts() or
102 * mbedtls_cipher_cmac_reset().
103 *
104 * Call this function as many times as needed to input the
105 * data to be authenticated.
106 * Once all of the required data has been input,
107 * call mbedtls_cipher_cmac_finish() to obtain the result
108 * of the CMAC operation.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000109 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000110 * \param ctx The cipher context used for the CMAC operation.
111 * \param input The buffer holding the input data.
112 * \param ilen The length of the input data.
113 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100114 * \return \c 0 on success.
115 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik8c154932018-03-27 10:45:16 +0100116 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
119 const unsigned char *input, size_t ilen);
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000120
121/**
David Horstmann3d5dfa52021-12-06 18:58:02 +0000122 * \brief This function finishes an ongoing CMAC operation, and
123 * writes the result to the output buffer.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000124 *
David Horstmann3d5dfa52021-12-06 18:58:02 +0000125 * It should be followed either by
126 * mbedtls_cipher_cmac_reset(), which starts another CMAC
127 * operation with the same key, or mbedtls_cipher_free(),
128 * which clears the cipher context.
Simon Butcher327398a2016-10-05 14:09:11 +0100129 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000130 * \param ctx The cipher context used for the CMAC operation.
131 * \param output The output buffer for the CMAC checksum result.
132 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100133 * \return \c 0 on success.
134 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000135 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
138 unsigned char *output);
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000139
140/**
David Horstmann3d5dfa52021-12-06 18:58:02 +0000141 * \brief This function starts a new CMAC operation with the same
142 * key as the previous one.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000143 *
David Horstmann3d5dfa52021-12-06 18:58:02 +0000144 * It should be called after finishing the previous CMAC
145 * operation with mbedtls_cipher_cmac_finish().
146 * After calling this function,
147 * call mbedtls_cipher_cmac_update() to supply the new
148 * CMAC operation with data.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000149 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000150 * \param ctx The cipher context used for the CMAC operation.
151 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100152 * \return \c 0 on success.
153 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000154 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx);
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000157
158/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000159 * \brief This function calculates the full generic CMAC
160 * on the input buffer with the provided key.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000161 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000162 * The function allocates the context, performs the
163 * calculation, and frees the context.
Simon Butcher327398a2016-10-05 14:09:11 +0100164 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000165 * The CMAC result is calculated as
166 * output = generic CMAC(cmac key, input buffer).
167 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000168 * \param cipher_info The cipher information.
169 * \param key The CMAC key.
170 * \param keylen The length of the CMAC key in bits.
171 * \param input The buffer holding the input data.
172 * \param ilen The length of the input data.
173 * \param output The buffer for the generic CMAC result.
174 *
Rose Zadikc138bb72018-04-16 11:11:25 +0100175 * \return \c 0 on success.
176 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA
Rose Zadik380d05d2018-01-25 21:52:41 +0000177 * if parameter verification fails.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000178 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info,
180 const unsigned char *key, size_t keylen,
181 const unsigned char *input, size_t ilen,
182 unsigned char *output);
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000183
Simon Butcher69283e52016-10-06 12:49:58 +0100184#if defined(MBEDTLS_AES_C)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000185/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000186 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom
187 * function, as defined in
188 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based
189 * Message Authentication Code-Pseudo-Random Function-128
190 * (AES-CMAC-PRF-128) Algorithm for the Internet Key
191 * Exchange Protocol (IKE).</em>
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000192 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000193 * \param key The key to use.
194 * \param key_len The key length in Bytes.
195 * \param input The buffer holding the input data.
196 * \param in_len The length of the input data in Bytes.
197 * \param output The buffer holding the generated 16 Bytes of
198 * pseudorandom output.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000199 *
Rose Zadik380d05d2018-01-25 21:52:41 +0000200 * \return \c 0 on success.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_len,
203 const unsigned char *input, size_t in_len,
204 unsigned char output[16]);
Brian Murrayb439d452016-05-19 16:02:42 -0700205#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207#if defined(MBEDTLS_SELF_TEST) && (defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C))
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000208/**
Rose Zadik380d05d2018-01-25 21:52:41 +0000209 * \brief The CMAC checkup routine.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000210 *
Steven Cooreman894b9c42021-04-23 08:19:43 +0200211 *
Rose Zadik8c154932018-03-27 10:45:16 +0100212 * \return \c 0 on success.
213 * \return \c 1 on failure.
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215int mbedtls_cmac_self_test(int verbose);
Brian Murrayb439d452016-05-19 16:02:42 -0700216#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000217
218#ifdef __cplusplus
219}
220#endif
221
222#endif /* MBEDTLS_CMAC_H */