Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cmac.h |
| 3 | * |
Rose Zadik | 8c15493 | 2018-03-27 10:45:16 +0100 | [diff] [blame] | 4 | * \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 Peskine | 16bb83c | 2023-06-14 17:42:26 +0200 | [diff] [blame] | 8 | * It is supported with AES and DES. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 9 | */ |
| 10 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 11 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 12 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 13 | */ |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 14 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 15 | #ifndef MBEDTLS_CMAC_H |
| 16 | #define MBEDTLS_CMAC_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 17 | #include "mbedtls/private_access.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 18 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 19 | #include "mbedtls/build_info.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 20 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 21 | #include "mbedtls/cipher.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 27 | #define MBEDTLS_AES_BLOCK_SIZE 16 |
| 28 | #define MBEDTLS_DES3_BLOCK_SIZE 8 |
| 29 | |
Gilles Peskine | 16bb83c | 2023-06-14 17:42:26 +0200 | [diff] [blame] | 30 | /* We don't support Camellia or ARIA in this module */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 31 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 7282a9e | 2023-06-14 17:49:02 +0200 | [diff] [blame] | 32 | #define MBEDTLS_CMAC_MAX_BLOCK_SIZE 16 /**< The longest block used by CMAC is that of AES. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 33 | #else |
Gilles Peskine | 7282a9e | 2023-06-14 17:49:02 +0200 | [diff] [blame] | 34 | #define MBEDTLS_CMAC_MAX_BLOCK_SIZE 8 /**< The longest block used by CMAC is that of 3DES. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 35 | #endif |
| 36 | |
Gilles Peskine | c453e2e | 2023-06-14 17:54:38 +0200 | [diff] [blame] | 37 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 7282a9e | 2023-06-14 17:49:02 +0200 | [diff] [blame] | 38 | /** 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 Peskine | c453e2e | 2023-06-14 17:54:38 +0200 | [diff] [blame] | 52 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Gilles Peskine | 7282a9e | 2023-06-14 17:49:02 +0200 | [diff] [blame] | 53 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 54 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 55 | * The CMAC context structure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 56 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | struct mbedtls_cmac_context_t { |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 58 | /** The internal state of the CMAC algorithm. */ |
Gilles Peskine | 9e930e2 | 2023-06-14 17:52:54 +0200 | [diff] [blame] | 59 | unsigned char MBEDTLS_PRIVATE(state)[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 60 | |
Andres AG | a592dcc | 2016-10-06 15:23:39 +0100 | [diff] [blame] | 61 | /** Unprocessed data - either data that was not block aligned and is still |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 62 | * pending processing, or the final block. */ |
Gilles Peskine | 9e930e2 | 2023-06-14 17:52:54 +0200 | [diff] [blame] | 63 | unsigned char MBEDTLS_PRIVATE(unprocessed_block)[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 64 | |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 65 | /** The length of data pending processing. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 66 | size_t MBEDTLS_PRIVATE(unprocessed_len); |
Simon Butcher | 8308a44 | 2016-10-05 15:12:59 +0100 | [diff] [blame] | 67 | }; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 70 | * \brief This function starts a new CMAC computation |
| 71 | * by setting the CMAC key, and preparing to authenticate |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 72 | * the input data. |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 73 | * 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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 81 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 82 | * \param ctx The cipher context used for the CMAC operation, initialized |
Rose Zadik | 8c15493 | 2018-03-27 10:45:16 +0100 | [diff] [blame] | 83 | * 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 Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 86 | * \param key The CMAC key. |
| 87 | * \param keybits The length of the CMAC key in bits. |
| 88 | * Must be supported by the cipher. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 89 | * |
Rose Zadik | c138bb7 | 2018-04-16 11:11:25 +0100 | [diff] [blame] | 90 | * \return \c 0 on success. |
| 91 | * \return A cipher-specific error code on failure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 92 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx, |
| 94 | const unsigned char *key, size_t keybits); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 97 | * \brief This function feeds an input buffer into an ongoing CMAC |
| 98 | * computation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 99 | * |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 100 | * 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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 109 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 110 | * \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 Zadik | c138bb7 | 2018-04-16 11:11:25 +0100 | [diff] [blame] | 114 | * \return \c 0 on success. |
| 115 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
Rose Zadik | 8c15493 | 2018-03-27 10:45:16 +0100 | [diff] [blame] | 116 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 117 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx, |
| 119 | const unsigned char *input, size_t ilen); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 122 | * \brief This function finishes an ongoing CMAC operation, and |
| 123 | * writes the result to the output buffer. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 124 | * |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 125 | * 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 Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 129 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 130 | * \param ctx The cipher context used for the CMAC operation. |
| 131 | * \param output The output buffer for the CMAC checksum result. |
| 132 | * |
Rose Zadik | c138bb7 | 2018-04-16 11:11:25 +0100 | [diff] [blame] | 133 | * \return \c 0 on success. |
| 134 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 135 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 136 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx, |
| 138 | unsigned char *output); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 141 | * \brief This function starts a new CMAC operation with the same |
| 142 | * key as the previous one. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 143 | * |
David Horstmann | 3d5dfa5 | 2021-12-06 18:58:02 +0000 | [diff] [blame] | 144 | * 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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 149 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 150 | * \param ctx The cipher context used for the CMAC operation. |
| 151 | * |
Rose Zadik | c138bb7 | 2018-04-16 11:11:25 +0100 | [diff] [blame] | 152 | * \return \c 0 on success. |
| 153 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 154 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 155 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 157 | |
| 158 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 159 | * \brief This function calculates the full generic CMAC |
| 160 | * on the input buffer with the provided key. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 161 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 162 | * The function allocates the context, performs the |
| 163 | * calculation, and frees the context. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 164 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 165 | * The CMAC result is calculated as |
| 166 | * output = generic CMAC(cmac key, input buffer). |
| 167 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 168 | * \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 Zadik | c138bb7 | 2018-04-16 11:11:25 +0100 | [diff] [blame] | 175 | * \return \c 0 on success. |
| 176 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 177 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | int 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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 183 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 184 | #if defined(MBEDTLS_AES_C) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 185 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 186 | * \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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 192 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 193 | * \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 Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 199 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 200 | * \return \c 0 on success. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 201 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | int 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 Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 205 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | #if defined(MBEDTLS_SELF_TEST) && (defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 208 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 209 | * \brief The CMAC checkup routine. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 210 | * |
Steven Cooreman | 894b9c4 | 2021-04-23 08:19:43 +0200 | [diff] [blame] | 211 | * |
Rose Zadik | 8c15493 | 2018-03-27 10:45:16 +0100 | [diff] [blame] | 212 | * \return \c 0 on success. |
| 213 | * \return \c 1 on failure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 214 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | int mbedtls_cmac_self_test(int verbose); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 216 | #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 217 | |
| 218 | #ifdef __cplusplus |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | #endif /* MBEDTLS_CMAC_H */ |