Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cmac.h
00001 /** 00002 * \file cmac.h 00003 * 00004 * \brief This file contains CMAC definitions and functions. 00005 * 00006 * The Cipher-based Message Authentication Code (CMAC) Mode for 00007 * Authentication is defined in <em>RFC-4493: The AES-CMAC Algorithm</em>. 00008 */ 00009 /* 00010 * Copyright (C) 2015-2018, Arm Limited (or its affiliates), All Rights Reserved 00011 * SPDX-License-Identifier: Apache-2.0 00012 * 00013 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00014 * not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at 00016 * 00017 * http://www.apache.org/licenses/LICENSE-2.0 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00021 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 * This file is part of Mbed TLS (https://tls.mbed.org) 00026 */ 00027 00028 #ifndef MBEDTLS_CMAC_H 00029 #define MBEDTLS_CMAC_H 00030 00031 #include "cipher.h" 00032 00033 #ifdef __cplusplus 00034 extern "C" { 00035 #endif 00036 00037 #define MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED -0x007A /**< CMAC hardware accelerator failed. */ 00038 00039 #define MBEDTLS_AES_BLOCK_SIZE 16 00040 #define MBEDTLS_DES3_BLOCK_SIZE 8 00041 00042 #if defined(MBEDTLS_AES_C) 00043 #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /**< The longest block used by CMAC is that of AES. */ 00044 #else 00045 #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /**< The longest block used by CMAC is that of 3DES. */ 00046 #endif 00047 00048 #if !defined(MBEDTLS_CMAC_ALT) 00049 00050 /** 00051 * The CMAC context structure. 00052 */ 00053 struct mbedtls_cmac_context_t 00054 { 00055 /** The internal state of the CMAC algorithm. */ 00056 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX]; 00057 00058 /** Unprocessed data - either data that was not block aligned and is still 00059 * pending processing, or the final block. */ 00060 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX]; 00061 00062 /** The length of data pending processing. */ 00063 size_t unprocessed_len; 00064 }; 00065 00066 #else /* !MBEDTLS_CMAC_ALT */ 00067 #include "cmac_alt.h" 00068 #endif /* !MBEDTLS_CMAC_ALT */ 00069 00070 /** 00071 * \brief This function sets the CMAC key, and prepares to authenticate 00072 * the input data. 00073 * Must be called with an initialized cipher context. 00074 * 00075 * \param ctx The cipher context used for the CMAC operation, initialized 00076 * as one of the following types: MBEDTLS_CIPHER_AES_128_ECB, 00077 * MBEDTLS_CIPHER_AES_192_ECB, MBEDTLS_CIPHER_AES_256_ECB, 00078 * or MBEDTLS_CIPHER_DES_EDE3_ECB. 00079 * \param key The CMAC key. 00080 * \param keybits The length of the CMAC key in bits. 00081 * Must be supported by the cipher. 00082 * 00083 * \return \c 0 on success. 00084 * \return A cipher-specific error code on failure. 00085 */ 00086 int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, 00087 const unsigned char *key, size_t keybits ); 00088 00089 /** 00090 * \brief This function feeds an input buffer into an ongoing CMAC 00091 * computation. 00092 * 00093 * It is called between mbedtls_cipher_cmac_starts() or 00094 * mbedtls_cipher_cmac_reset(), and mbedtls_cipher_cmac_finish(). 00095 * Can be called repeatedly. 00096 * 00097 * \param ctx The cipher context used for the CMAC operation. 00098 * \param input The buffer holding the input data. 00099 * \param ilen The length of the input data. 00100 * 00101 * \return \c 0 on success. 00102 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA 00103 * if parameter verification fails. 00104 */ 00105 int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, 00106 const unsigned char *input, size_t ilen ); 00107 00108 /** 00109 * \brief This function finishes the CMAC operation, and writes 00110 * the result to the output buffer. 00111 * 00112 * It is called after mbedtls_cipher_cmac_update(). 00113 * It can be followed by mbedtls_cipher_cmac_reset() and 00114 * mbedtls_cipher_cmac_update(), or mbedtls_cipher_free(). 00115 * 00116 * \param ctx The cipher context used for the CMAC operation. 00117 * \param output The output buffer for the CMAC checksum result. 00118 * 00119 * \return \c 0 on success. 00120 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA 00121 * if parameter verification fails. 00122 */ 00123 int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, 00124 unsigned char *output ); 00125 00126 /** 00127 * \brief This function prepares the authentication of another 00128 * message with the same key as the previous CMAC 00129 * operation. 00130 * 00131 * It is called after mbedtls_cipher_cmac_finish() 00132 * and before mbedtls_cipher_cmac_update(). 00133 * 00134 * \param ctx The cipher context used for the CMAC operation. 00135 * 00136 * \return \c 0 on success. 00137 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA 00138 * if parameter verification fails. 00139 */ 00140 int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); 00141 00142 /** 00143 * \brief This function calculates the full generic CMAC 00144 * on the input buffer with the provided key. 00145 * 00146 * The function allocates the context, performs the 00147 * calculation, and frees the context. 00148 * 00149 * The CMAC result is calculated as 00150 * output = generic CMAC(cmac key, input buffer). 00151 * 00152 * 00153 * \param cipher_info The cipher information. 00154 * \param key The CMAC key. 00155 * \param keylen The length of the CMAC key in bits. 00156 * \param input The buffer holding the input data. 00157 * \param ilen The length of the input data. 00158 * \param output The buffer for the generic CMAC result. 00159 * 00160 * \return \c 0 on success. 00161 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA 00162 * if parameter verification fails. 00163 */ 00164 int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, 00165 const unsigned char *key, size_t keylen, 00166 const unsigned char *input, size_t ilen, 00167 unsigned char *output ); 00168 00169 #if defined(MBEDTLS_AES_C) 00170 /** 00171 * \brief This function implements the AES-CMAC-PRF-128 pseudorandom 00172 * function, as defined in 00173 * <em>RFC-4615: The Advanced Encryption Standard-Cipher-based 00174 * Message Authentication Code-Pseudo-Random Function-128 00175 * (AES-CMAC-PRF-128) Algorithm for the Internet Key 00176 * Exchange Protocol (IKE).</em> 00177 * 00178 * \param key The key to use. 00179 * \param key_len The key length in Bytes. 00180 * \param input The buffer holding the input data. 00181 * \param in_len The length of the input data in Bytes. 00182 * \param output The buffer holding the generated 16 Bytes of 00183 * pseudorandom output. 00184 * 00185 * \return \c 0 on success. 00186 */ 00187 int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len, 00188 const unsigned char *input, size_t in_len, 00189 unsigned char output[16] ); 00190 #endif /* MBEDTLS_AES_C */ 00191 00192 #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) ) 00193 /** 00194 * \brief The CMAC checkup routine. 00195 * 00196 * \return \c 0 on success. 00197 * \return \c 1 on failure. 00198 */ 00199 int mbedtls_cmac_self_test( int verbose ); 00200 #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 00201 00202 #ifdef __cplusplus 00203 } 00204 #endif 00205 00206 #endif /* MBEDTLS_CMAC_H */
Generated on Tue Jul 12 2022 13:53:04 by
