Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cmac.h |
| 3 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 4 | * \brief The Cipher-based Message Authentication Code (CMAC) Mode for |
| 5 | * Authentication. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 6 | */ |
| 7 | /* |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 10 | * |
| 11 | * This file is provided under the Apache License 2.0, or the |
| 12 | * GNU General Public License v2.0 or later. |
| 13 | * |
| 14 | * ********** |
| 15 | * Apache License 2.0: |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 16 | * |
| 17 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | * not use this file except in compliance with the License. |
| 19 | * You may obtain a copy of the License at |
| 20 | * |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | * See the License for the specific language governing permissions and |
| 27 | * limitations under the License. |
| 28 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 29 | * ********** |
| 30 | * |
| 31 | * ********** |
| 32 | * GNU General Public License v2.0 or later: |
| 33 | * |
| 34 | * This program is free software; you can redistribute it and/or modify |
| 35 | * it under the terms of the GNU General Public License as published by |
| 36 | * the Free Software Foundation; either version 2 of the License, or |
| 37 | * (at your option) any later version. |
| 38 | * |
| 39 | * This program is distributed in the hope that it will be useful, |
| 40 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 41 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 42 | * GNU General Public License for more details. |
| 43 | * |
| 44 | * You should have received a copy of the GNU General Public License along |
| 45 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 46 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 47 | * |
| 48 | * ********** |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 49 | */ |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 50 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 51 | #ifndef MBEDTLS_CMAC_H |
| 52 | #define MBEDTLS_CMAC_H |
| 53 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 55 | #include "config.h" |
| 56 | #else |
| 57 | #include MBEDTLS_CONFIG_FILE |
| 58 | #endif |
| 59 | |
Ron Eldor | df9b93e | 2017-05-14 16:17:33 +0300 | [diff] [blame] | 60 | #include "cipher.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 61 | |
| 62 | #ifdef __cplusplus |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 66 | #define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 67 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 68 | #define MBEDTLS_AES_BLOCK_SIZE 16 |
| 69 | #define MBEDTLS_DES3_BLOCK_SIZE 8 |
| 70 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 71 | #if defined(MBEDTLS_AES_C) |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 72 | #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* The longest block used by CMAC is that of AES. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 73 | #else |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 74 | #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* The longest block used by CMAC is that of 3DES. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 75 | #endif |
| 76 | |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 77 | #if !defined(MBEDTLS_CMAC_ALT) |
| 78 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 79 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 80 | * The CMAC context structure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 81 | */ |
Simon Butcher | 8308a44 | 2016-10-05 15:12:59 +0100 | [diff] [blame] | 82 | struct mbedtls_cmac_context_t |
| 83 | { |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 84 | /** The internal state of the CMAC algorithm. */ |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 85 | unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 86 | |
Andres AG | a592dcc | 2016-10-06 15:23:39 +0100 | [diff] [blame] | 87 | /** Unprocessed data - either data that was not block aligned and is still |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 88 | * pending processing, or the final block. */ |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 89 | unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 90 | |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 91 | /** The length of data pending processing. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 92 | size_t unprocessed_len; |
Simon Butcher | 8308a44 | 2016-10-05 15:12:59 +0100 | [diff] [blame] | 93 | }; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 96 | * \brief This function sets the CMAC key, and prepares to authenticate |
| 97 | * the input data. |
| 98 | * Must be called with an initialized cipher context. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 99 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 100 | * \param ctx The cipher context used for the CMAC operation, initialized |
| 101 | * as one of the following types:<ul> |
| 102 | * <li>MBEDTLS_CIPHER_AES_128_ECB</li> |
| 103 | * <li>MBEDTLS_CIPHER_AES_192_ECB</li> |
| 104 | * <li>MBEDTLS_CIPHER_AES_256_ECB</li> |
| 105 | * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul> |
| 106 | * \param key The CMAC key. |
| 107 | * \param keybits The length of the CMAC key in bits. |
| 108 | * Must be supported by the cipher. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 109 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 110 | * \return \c 0 on success, or a cipher-specific error code. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 111 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 112 | int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, |
Simon Butcher | 94ffde7 | 2016-10-05 15:33:53 +0100 | [diff] [blame] | 113 | const unsigned char *key, size_t keybits ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 116 | * \brief This function feeds an input buffer into an ongoing CMAC |
| 117 | * computation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 118 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 119 | * It is called between mbedtls_cipher_cmac_starts() or |
| 120 | * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish(). |
| 121 | * Can be called repeatedly. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 122 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 123 | * \param ctx The cipher context used for the CMAC operation. |
| 124 | * \param input The buffer holding the input data. |
| 125 | * \param ilen The length of the input data. |
| 126 | * |
| 127 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 128 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 129 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 130 | int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, |
| 131 | const unsigned char *input, size_t ilen ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 134 | * \brief This function finishes the CMAC operation, and writes |
| 135 | * the result to the output buffer. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 136 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 137 | * It is called after mbedtls_cipher_cmac_update(). |
| 138 | * It can be followed by mbedtls_cipher_cmac_reset() and |
| 139 | * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free(). |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 140 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 141 | * \param ctx The cipher context used for the CMAC operation. |
| 142 | * \param output The output buffer for the CMAC checksum result. |
| 143 | * |
| 144 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 145 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 146 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 147 | int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, |
| 148 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 151 | * \brief This function prepares the authentication of another |
| 152 | * message with the same key as the previous CMAC |
| 153 | * operation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 154 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 155 | * It is called after mbedtls_cipher_cmac_finish() |
| 156 | * and before mbedtls_cipher_cmac_update(). |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 157 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 158 | * \param ctx The cipher context used for the CMAC operation. |
| 159 | * |
| 160 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 161 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 162 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 163 | int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 166 | * \brief This function calculates the full generic CMAC |
| 167 | * on the input buffer with the provided key. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 168 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 169 | * The function allocates the context, performs the |
| 170 | * calculation, and frees the context. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 171 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 172 | * The CMAC result is calculated as |
| 173 | * output = generic CMAC(cmac key, input buffer). |
| 174 | * |
| 175 | * |
| 176 | * \param cipher_info The cipher information. |
| 177 | * \param key The CMAC key. |
| 178 | * \param keylen The length of the CMAC key in bits. |
| 179 | * \param input The buffer holding the input data. |
| 180 | * \param ilen The length of the input data. |
| 181 | * \param output The buffer for the generic CMAC result. |
| 182 | * |
| 183 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 184 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 185 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 186 | int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, |
| 187 | const unsigned char *key, size_t keylen, |
| 188 | const unsigned char *input, size_t ilen, |
| 189 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 190 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 191 | #if defined(MBEDTLS_AES_C) |
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 | * \brief This function implements the AES-CMAC-PRF-128 pseudorandom |
| 194 | * function, as defined in |
| 195 | * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based |
| 196 | * Message Authentication Code-Pseudo-Random Function-128 |
| 197 | * (AES-CMAC-PRF-128) Algorithm for the Internet Key |
| 198 | * Exchange Protocol (IKE).</em> |
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 | * \param key The key to use. |
| 201 | * \param key_len The key length in Bytes. |
| 202 | * \param input The buffer holding the input data. |
| 203 | * \param in_len The length of the input data in Bytes. |
| 204 | * \param output The buffer holding the generated 16 Bytes of |
| 205 | * pseudorandom output. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 206 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 207 | * \return \c 0 on success. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 208 | */ |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 209 | int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len, |
Manuel Pégourié-Gonnard | 690083c | 2016-01-13 10:48:02 +0000 | [diff] [blame] | 210 | const unsigned char *input, size_t in_len, |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 211 | unsigned char output[16] ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 212 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 213 | |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 214 | #ifdef __cplusplus |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | #else /* !MBEDTLS_CMAC_ALT */ |
| 219 | #include "cmac_alt.h" |
| 220 | #endif /* !MBEDTLS_CMAC_ALT */ |
| 221 | |
| 222 | #ifdef __cplusplus |
| 223 | extern "C" { |
| 224 | #endif |
| 225 | |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 226 | #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] | 227 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 228 | * \brief The CMAC checkup routine. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 229 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 230 | * \return \c 0 on success, or \c 1 on failure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 231 | */ |
| 232 | int mbedtls_cmac_self_test( int verbose ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 233 | #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 234 | |
| 235 | #ifdef __cplusplus |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | #endif /* MBEDTLS_CMAC_H */ |