Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file cmac.h |
| 3 | * |
| 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>. |
| 8 | */ |
| 9 | /* |
| 10 | * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 11 | * SPDX-License-Identifier: Apache-2.0 |
| 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
| 24 | * |
| 25 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 26 | */ |
| 27 | |
| 28 | #ifndef MBEDCRYPTO_CMAC_H |
| 29 | #define MBEDCRYPTO_CMAC_H |
| 30 | |
| 31 | #include "mbedcrypto/cipher.h" |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | #define MBEDCRYPTO_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */ |
| 38 | |
| 39 | #define MBEDCRYPTO_AES_BLOCK_SIZE 16 |
| 40 | #define MBEDCRYPTO_DES3_BLOCK_SIZE 8 |
| 41 | |
| 42 | #if defined(MBEDCRYPTO_AES_C) |
| 43 | #define MBEDCRYPTO_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */ |
| 44 | #else |
| 45 | #define MBEDCRYPTO_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */ |
| 46 | #endif |
| 47 | |
| 48 | #if !defined(MBEDCRYPTO_CMAC_ALT) |
| 49 | |
| 50 | /** |
| 51 | * The CMAC context structure. |
| 52 | */ |
| 53 | struct mbedcrypto_cmac_context_t |
| 54 | { |
| 55 | /** The internal state of the CMAC algorithm. */ |
| 56 | unsigned char state[MBEDCRYPTO_CIPHER_BLKSIZE_MAX]; |
| 57 | |
| 58 | /** Unprocessed data - either data that was not block aligned and is still |
| 59 | * pending processing, or the final block. */ |
| 60 | unsigned char unprocessed_block[MBEDCRYPTO_CIPHER_BLKSIZE_MAX]; |
| 61 | |
| 62 | /** The length of data pending processing. */ |
| 63 | size_t unprocessed_len; |
| 64 | }; |
| 65 | |
| 66 | #else /* !MBEDCRYPTO_CMAC_ALT */ |
| 67 | #include "cmac_alt.h" |
| 68 | #endif /* !MBEDCRYPTO_CMAC_ALT */ |
| 69 | |
| 70 | /** |
| 71 | * \brief This function sets the CMAC key, and prepares to authenticate |
| 72 | * the input data. |
| 73 | * Must be called with an initialized cipher context. |
| 74 | * |
| 75 | * \param ctx The cipher context used for the CMAC operation, initialized |
| 76 | * as one of the following types: MBEDCRYPTO_CIPHER_AES_128_ECB, |
| 77 | * MBEDCRYPTO_CIPHER_AES_192_ECB, MBEDCRYPTO_CIPHER_AES_256_ECB, |
| 78 | * or MBEDCRYPTO_CIPHER_DES_EDE3_ECB. |
| 79 | * \param key The CMAC key. |
| 80 | * \param keybits The length of the CMAC key in bits. |
| 81 | * Must be supported by the cipher. |
| 82 | * |
| 83 | * \return \c 0 on success. |
| 84 | * \return A cipher-specific error code on failure. |
| 85 | */ |
| 86 | int mbedcrypto_cipher_cmac_starts( mbedcrypto_cipher_context_t *ctx, |
| 87 | const unsigned char *key, size_t keybits ); |
| 88 | |
| 89 | /** |
| 90 | * \brief This function feeds an input buffer into an ongoing CMAC |
| 91 | * computation. |
| 92 | * |
| 93 | * It is called between mbedcrypto_cipher_cmac_starts() or |
| 94 | * mbedcrypto_cipher_cmac_reset(), and mbedcrypto_cipher_cmac_finish(). |
| 95 | * Can be called repeatedly. |
| 96 | * |
| 97 | * \param ctx The cipher context used for the CMAC operation. |
| 98 | * \param input The buffer holding the input data. |
| 99 | * \param ilen The length of the input data. |
| 100 | * |
| 101 | * \return \c 0 on success. |
| 102 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA |
| 103 | * if parameter verification fails. |
| 104 | */ |
| 105 | int mbedcrypto_cipher_cmac_update( mbedcrypto_cipher_context_t *ctx, |
| 106 | const unsigned char *input, size_t ilen ); |
| 107 | |
| 108 | /** |
| 109 | * \brief This function finishes the CMAC operation, and writes |
| 110 | * the result to the output buffer. |
| 111 | * |
| 112 | * It is called after mbedcrypto_cipher_cmac_update(). |
| 113 | * It can be followed by mbedcrypto_cipher_cmac_reset() and |
| 114 | * mbedcrypto_cipher_cmac_update(), or mbedcrypto_cipher_free(). |
| 115 | * |
| 116 | * \param ctx The cipher context used for the CMAC operation. |
| 117 | * \param output The output buffer for the CMAC checksum result. |
| 118 | * |
| 119 | * \return \c 0 on success. |
| 120 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA |
| 121 | * if parameter verification fails. |
| 122 | */ |
| 123 | int mbedcrypto_cipher_cmac_finish( mbedcrypto_cipher_context_t *ctx, |
| 124 | unsigned char *output ); |
| 125 | |
| 126 | /** |
| 127 | * \brief This function prepares the authentication of another |
| 128 | * message with the same key as the previous CMAC |
| 129 | * operation. |
| 130 | * |
| 131 | * It is called after mbedcrypto_cipher_cmac_finish() |
| 132 | * and before mbedcrypto_cipher_cmac_update(). |
| 133 | * |
| 134 | * \param ctx The cipher context used for the CMAC operation. |
| 135 | * |
| 136 | * \return \c 0 on success. |
| 137 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA |
| 138 | * if parameter verification fails. |
| 139 | */ |
| 140 | int mbedcrypto_cipher_cmac_reset( mbedcrypto_cipher_context_t *ctx ); |
| 141 | |
| 142 | /** |
| 143 | * \brief This function calculates the full generic CMAC |
| 144 | * on the input buffer with the provided key. |
| 145 | * |
| 146 | * The function allocates the context, performs the |
| 147 | * calculation, and frees the context. |
| 148 | * |
| 149 | * The CMAC result is calculated as |
| 150 | * output = generic CMAC(cmac key, input buffer). |
| 151 | * |
| 152 | * |
| 153 | * \param cipher_info The cipher information. |
| 154 | * \param key The CMAC key. |
| 155 | * \param keylen The length of the CMAC key in bits. |
| 156 | * \param input The buffer holding the input data. |
| 157 | * \param ilen The length of the input data. |
| 158 | * \param output The buffer for the generic CMAC result. |
| 159 | * |
| 160 | * \return \c 0 on success. |
| 161 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA |
| 162 | * if parameter verification fails. |
| 163 | */ |
| 164 | int mbedcrypto_cipher_cmac( const mbedcrypto_cipher_info_t *cipher_info, |
| 165 | const unsigned char *key, size_t keylen, |
| 166 | const unsigned char *input, size_t ilen, |
| 167 | unsigned char *output ); |
| 168 | |
| 169 | #if defined(MBEDCRYPTO_AES_C) |
| 170 | /** |
| 171 | * \brief This function implements the AES-CMAC-PRF-128 pseudorandom |
| 172 | * function, as defined in |
| 173 | * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based |
| 174 | * Message Authentication Code-Pseudo-Random Function-128 |
| 175 | * (AES-CMAC-PRF-128) Algorithm for the Internet Key |
| 176 | * Exchange Protocol (IKE).</em> |
| 177 | * |
| 178 | * \param key The key to use. |
| 179 | * \param key_len The key length in Bytes. |
| 180 | * \param input The buffer holding the input data. |
| 181 | * \param in_len The length of the input data in Bytes. |
| 182 | * \param output The buffer holding the generated 16 Bytes of |
| 183 | * pseudorandom output. |
| 184 | * |
| 185 | * \return \c 0 on success. |
| 186 | */ |
| 187 | int mbedcrypto_aes_cmac_prf_128( const unsigned char *key, size_t key_len, |
| 188 | const unsigned char *input, size_t in_len, |
| 189 | unsigned char output[16] ); |
| 190 | #endif /* MBEDCRYPTO_AES_C */ |
| 191 | |
| 192 | #if defined(MBEDCRYPTO_SELF_TEST) && ( defined(MBEDCRYPTO_AES_C) || defined(MBEDCRYPTO_DES_C) ) |
| 193 | /** |
| 194 | * \brief The CMAC checkup routine. |
| 195 | * |
| 196 | * \return \c 0 on success. |
| 197 | * \return \c 1 on failure. |
| 198 | */ |
| 199 | int mbedcrypto_cmac_self_test( int verbose ); |
| 200 | #endif /* MBEDCRYPTO_SELF_TEST && ( MBEDCRYPTO_AES_C || MBEDCRYPTO_DES_C ) */ |
| 201 | |
| 202 | #ifdef __cplusplus |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | #endif /* MBEDCRYPTO_CMAC_H */ |