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 | /* |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
| 22 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 23 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 24 | */ |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 25 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 26 | #ifndef MBEDTLS_CMAC_H |
| 27 | #define MBEDTLS_CMAC_H |
| 28 | |
Ron Eldor | df9b93e | 2017-05-14 16:17:33 +0300 | [diff] [blame] | 29 | #include "cipher.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 35 | #define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 36 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 37 | #define MBEDTLS_AES_BLOCK_SIZE 16 |
| 38 | #define MBEDTLS_DES3_BLOCK_SIZE 8 |
| 39 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_AES_C) |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 41 | #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] | 42 | #else |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 43 | #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] | 44 | #endif |
| 45 | |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 46 | #if !defined(MBEDTLS_CMAC_ALT) |
| 47 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 48 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 49 | * The CMAC context structure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 50 | */ |
Simon Butcher | 8308a44 | 2016-10-05 15:12:59 +0100 | [diff] [blame] | 51 | struct mbedtls_cmac_context_t |
| 52 | { |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 53 | /** The internal state of the CMAC algorithm. */ |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 54 | unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 55 | |
Andres AG | a592dcc | 2016-10-06 15:23:39 +0100 | [diff] [blame] | 56 | /** Unprocessed data - either data that was not block aligned and is still |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 57 | * pending processing, or the final block. */ |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 58 | unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 59 | |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 60 | /** The length of data pending processing. */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 61 | size_t unprocessed_len; |
Simon Butcher | 8308a44 | 2016-10-05 15:12:59 +0100 | [diff] [blame] | 62 | }; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 65 | * \brief This function sets the CMAC key, and prepares to authenticate |
| 66 | * the input data. |
| 67 | * Must be called with an initialized cipher context. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 68 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 69 | * \param ctx The cipher context used for the CMAC operation, initialized |
| 70 | * as one of the following types:<ul> |
| 71 | * <li>MBEDTLS_CIPHER_AES_128_ECB</li> |
| 72 | * <li>MBEDTLS_CIPHER_AES_192_ECB</li> |
| 73 | * <li>MBEDTLS_CIPHER_AES_256_ECB</li> |
| 74 | * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul> |
| 75 | * \param key The CMAC key. |
| 76 | * \param keybits The length of the CMAC key in bits. |
| 77 | * Must be supported by the cipher. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 78 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 79 | * \return \c 0 on success, or a cipher-specific error code. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 80 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 81 | int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, |
Simon Butcher | 94ffde7 | 2016-10-05 15:33:53 +0100 | [diff] [blame] | 82 | const unsigned char *key, size_t keybits ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 85 | * \brief This function feeds an input buffer into an ongoing CMAC |
| 86 | * computation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 87 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 88 | * It is called between mbedtls_cipher_cmac_starts() or |
| 89 | * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish(). |
| 90 | * Can be called repeatedly. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 91 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 92 | * \param ctx The cipher context used for the CMAC operation. |
| 93 | * \param input The buffer holding the input data. |
| 94 | * \param ilen The length of the input data. |
| 95 | * |
| 96 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 97 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 98 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 99 | int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, |
| 100 | const unsigned char *input, size_t ilen ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 101 | |
| 102 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 103 | * \brief This function finishes the CMAC operation, and writes |
| 104 | * the result to the output buffer. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 105 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 106 | * It is called after mbedtls_cipher_cmac_update(). |
| 107 | * It can be followed by mbedtls_cipher_cmac_reset() and |
| 108 | * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free(). |
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 | * \param ctx The cipher context used for the CMAC operation. |
| 111 | * \param output The output buffer for the CMAC checksum result. |
| 112 | * |
| 113 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 114 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 115 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 116 | int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, |
| 117 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 120 | * \brief This function prepares the authentication of another |
| 121 | * message with the same key as the previous CMAC |
| 122 | * operation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 123 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 124 | * It is called after mbedtls_cipher_cmac_finish() |
| 125 | * and before mbedtls_cipher_cmac_update(). |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 126 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 127 | * \param ctx The cipher context used for the CMAC operation. |
| 128 | * |
| 129 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 130 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 131 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 132 | int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 135 | * \brief This function calculates the full generic CMAC |
| 136 | * on the input buffer with the provided key. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 137 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 138 | * The function allocates the context, performs the |
| 139 | * calculation, and frees the context. |
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 | * The CMAC result is calculated as |
| 142 | * output = generic CMAC(cmac key, input buffer). |
| 143 | * |
| 144 | * |
| 145 | * \param cipher_info The cipher information. |
| 146 | * \param key The CMAC key. |
| 147 | * \param keylen The length of the CMAC key in bits. |
| 148 | * \param input The buffer holding the input data. |
| 149 | * \param ilen The length of the input data. |
| 150 | * \param output The buffer for the generic CMAC result. |
| 151 | * |
| 152 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 153 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 154 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 155 | int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, |
| 156 | const unsigned char *key, size_t keylen, |
| 157 | const unsigned char *input, size_t ilen, |
| 158 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 159 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 160 | #if defined(MBEDTLS_AES_C) |
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 | * \brief This function implements the AES-CMAC-PRF-128 pseudorandom |
| 163 | * function, as defined in |
| 164 | * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based |
| 165 | * Message Authentication Code-Pseudo-Random Function-128 |
| 166 | * (AES-CMAC-PRF-128) Algorithm for the Internet Key |
| 167 | * Exchange Protocol (IKE).</em> |
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 | * \param key The key to use. |
| 170 | * \param key_len The key length in Bytes. |
| 171 | * \param input The buffer holding the input data. |
| 172 | * \param in_len The length of the input data in Bytes. |
| 173 | * \param output The buffer holding the generated 16 Bytes of |
| 174 | * pseudorandom output. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 175 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 176 | * \return \c 0 on success. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 177 | */ |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 178 | 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] | 179 | const unsigned char *input, size_t in_len, |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 180 | unsigned char output[16] ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 181 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 182 | |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 183 | #ifdef __cplusplus |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | #else /* !MBEDTLS_CMAC_ALT */ |
| 188 | #include "cmac_alt.h" |
| 189 | #endif /* !MBEDTLS_CMAC_ALT */ |
| 190 | |
| 191 | #ifdef __cplusplus |
| 192 | extern "C" { |
| 193 | #endif |
| 194 | |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 195 | #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] | 196 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 197 | * \brief The CMAC checkup routine. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 198 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 199 | * \return \c 0 on success, or \c 1 on failure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 200 | */ |
| 201 | int mbedtls_cmac_self_test( int verbose ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 202 | #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 203 | |
| 204 | #ifdef __cplusplus |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | #endif /* MBEDTLS_CMAC_H */ |