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 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 29 | #include "mbedtls/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 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame^] | 64 | #else /* !MBEDTLS_CMAC_ALT */ |
| 65 | #include "cmac_alt.h" |
| 66 | #endif /* !MBEDTLS_CMAC_ALT */ |
| 67 | |
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 | * \brief This function sets the CMAC key, and prepares to authenticate |
| 70 | * the input data. |
| 71 | * Must be called with an initialized cipher context. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 72 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 73 | * \param ctx The cipher context used for the CMAC operation, initialized |
| 74 | * as one of the following types:<ul> |
| 75 | * <li>MBEDTLS_CIPHER_AES_128_ECB</li> |
| 76 | * <li>MBEDTLS_CIPHER_AES_192_ECB</li> |
| 77 | * <li>MBEDTLS_CIPHER_AES_256_ECB</li> |
| 78 | * <li>MBEDTLS_CIPHER_DES_EDE3_ECB</li></ul> |
| 79 | * \param key The CMAC key. |
| 80 | * \param keybits The length of the CMAC key in bits. |
| 81 | * Must be supported by the cipher. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 82 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 83 | * \return \c 0 on success, or a cipher-specific error code. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 84 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 85 | int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, |
Simon Butcher | 94ffde7 | 2016-10-05 15:33:53 +0100 | [diff] [blame] | 86 | const unsigned char *key, size_t keybits ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 89 | * \brief This function feeds an input buffer into an ongoing CMAC |
| 90 | * computation. |
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 | * It is called between mbedtls_cipher_cmac_starts() or |
| 93 | * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish(). |
| 94 | * Can be called repeatedly. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 95 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 96 | * \param ctx The cipher context used for the CMAC operation. |
| 97 | * \param input The buffer holding the input data. |
| 98 | * \param ilen The length of the input data. |
| 99 | * |
| 100 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 101 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 102 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 103 | int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, |
| 104 | const unsigned char *input, size_t ilen ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 107 | * \brief This function finishes the CMAC operation, and writes |
| 108 | * the result to the output buffer. |
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 | * It is called after mbedtls_cipher_cmac_update(). |
| 111 | * It can be followed by mbedtls_cipher_cmac_reset() and |
| 112 | * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free(). |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 113 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 114 | * \param ctx The cipher context used for the CMAC operation. |
| 115 | * \param output The output buffer for the CMAC checksum result. |
| 116 | * |
| 117 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 118 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 119 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 120 | int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, |
| 121 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 124 | * \brief This function prepares the authentication of another |
| 125 | * message with the same key as the previous CMAC |
| 126 | * operation. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 127 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 128 | * It is called after mbedtls_cipher_cmac_finish() |
| 129 | * and before mbedtls_cipher_cmac_update(). |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 130 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 131 | * \param ctx The cipher context used for the CMAC operation. |
| 132 | * |
| 133 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 134 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 135 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 136 | int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 139 | * \brief This function calculates the full generic CMAC |
| 140 | * on the input buffer with the provided key. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 141 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 142 | * The function allocates the context, performs the |
| 143 | * calculation, and frees the context. |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 144 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 145 | * The CMAC result is calculated as |
| 146 | * output = generic CMAC(cmac key, input buffer). |
| 147 | * |
| 148 | * |
| 149 | * \param cipher_info The cipher information. |
| 150 | * \param key The CMAC key. |
| 151 | * \param keylen The length of the CMAC key in bits. |
| 152 | * \param input The buffer holding the input data. |
| 153 | * \param ilen The length of the input data. |
| 154 | * \param output The buffer for the generic CMAC result. |
| 155 | * |
| 156 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA |
| 157 | * if parameter verification fails. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 158 | */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 159 | int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, |
| 160 | const unsigned char *key, size_t keylen, |
| 161 | const unsigned char *input, size_t ilen, |
| 162 | unsigned char *output ); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 163 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 164 | #if defined(MBEDTLS_AES_C) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 165 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 166 | * \brief This function implements the AES-CMAC-PRF-128 pseudorandom |
| 167 | * function, as defined in |
| 168 | * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based |
| 169 | * Message Authentication Code-Pseudo-Random Function-128 |
| 170 | * (AES-CMAC-PRF-128) Algorithm for the Internet Key |
| 171 | * Exchange Protocol (IKE).</em> |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 172 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 173 | * \param key The key to use. |
| 174 | * \param key_len The key length in Bytes. |
| 175 | * \param input The buffer holding the input data. |
| 176 | * \param in_len The length of the input data in Bytes. |
| 177 | * \param output The buffer holding the generated 16 Bytes of |
| 178 | * pseudorandom output. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 179 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 180 | * \return \c 0 on success. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 181 | */ |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 182 | 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] | 183 | const unsigned char *input, size_t in_len, |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 184 | unsigned char output[16] ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 185 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 186 | |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 187 | #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] | 188 | /** |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 189 | * \brief The CMAC checkup routine. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 190 | * |
Rose Zadik | 380d05d | 2018-01-25 21:52:41 +0000 | [diff] [blame] | 191 | * \return \c 0 on success, or \c 1 on failure. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 192 | */ |
| 193 | int mbedtls_cmac_self_test( int verbose ); |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 194 | #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef __cplusplus |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | #endif /* MBEDTLS_CMAC_H */ |