Rizky Ardi Maulana / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cmac.h Source File

cmac.h

Go to the documentation of this file.
00001 /**
00002  * \file cmac.h
00003  *
00004  * \brief Cipher-based Message Authentication Code (CMAC) Mode for
00005  *        Authentication
00006  *
00007  *  Copyright (C) 2015-2016, ARM Limited, All Rights Reserved
00008  *  SPDX-License-Identifier: Apache-2.0
00009  *
00010  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  *  not use this file except in compliance with the License.
00012  *  You may obtain a copy of the License at
00013  *
00014  *  http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  *  Unless required by applicable law or agreed to in writing, software
00017  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  *  See the License for the specific language governing permissions and
00020  *  limitations under the License.
00021  *
00022  *  This file is part of mbed TLS (https://tls.mbed.org)
00023  */
00024 #ifndef MBEDTLS_CMAC_H
00025 #define MBEDTLS_CMAC_H
00026 
00027 #include "mbedtls/cipher.h"
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00033 #define MBEDTLS_AES_BLOCK_SIZE          16
00034 #define MBEDTLS_DES3_BLOCK_SIZE         8
00035 
00036 #if defined(MBEDTLS_AES_C)
00037 #define MBEDTLS_CIPHER_BLKSIZE_MAX      16  /* longest used by CMAC is AES */
00038 #else
00039 #define MBEDTLS_CIPHER_BLKSIZE_MAX      8   /* longest used by CMAC is 3DES */
00040 #endif
00041 
00042 /**
00043  * CMAC context structure - Contains internal state information only
00044  */
00045 struct mbedtls_cmac_context_t
00046 {
00047     /** Internal state of the CMAC algorithm  */
00048     unsigned char       state[MBEDTLS_CIPHER_BLKSIZE_MAX];
00049 
00050     /** Unprocessed data - either data that was not block aligned and is still
00051      *  pending to be processed, or the final block */
00052     unsigned char       unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
00053 
00054     /** Length of data pending to be processed */
00055     size_t              unprocessed_len;
00056 };
00057 
00058 /**
00059  * \brief               Set the CMAC key and prepare to authenticate the input
00060  *                      data.
00061  *                      Should be called with an initialised cipher context.
00062  *
00063  * \param ctx           Cipher context
00064  * \param key           CMAC key
00065  * \param keybits       length of the CMAC key in bits
00066  *                      (must be acceptable by the cipher)
00067  *
00068  * \return              0 if successful, or a cipher specific error code
00069  */
00070 int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
00071                                 const unsigned char *key, size_t keybits );
00072 
00073 /**
00074  * \brief               Generic CMAC process buffer.
00075  *                      Called between mbedtls_cipher_cmac_starts() or
00076  *                      mbedtls_cipher_cmac_reset() and
00077  *                      mbedtls_cipher_cmac_finish().
00078  *                      May be called repeatedly.
00079  *
00080  * \param ctx           CMAC context
00081  * \param input         buffer holding the  data
00082  * \param ilen          length of the input data
00083  *
00084  * \returns             0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
00085  *                      verification fails.
00086  */
00087 int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
00088                                 const unsigned char *input, size_t ilen );
00089 
00090 /**
00091  * \brief               Output CMAC.
00092  *                      Called after mbedtls_cipher_cmac_update().
00093  *                      Usually followed by mbedtls_cipher_cmac_reset(), then
00094  *                      mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
00095  *
00096  * \param ctx           CMAC context
00097  * \param output        Generic CMAC checksum result
00098  *
00099  * \returns             0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
00100  *                      verification fails.
00101  */
00102 int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
00103                                 unsigned char *output );
00104 
00105 /**
00106  * \brief               Prepare to authenticate a new message with the same key.
00107  *                      Called after mbedtls_cipher_cmac_finish() and before
00108  *                      mbedtls_cipher_cmac_update().
00109  *
00110  * \param ctx           CMAC context to be reset
00111  *
00112  * \returns             0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
00113  *                      verification fails.
00114  */
00115 int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
00116 
00117 /**
00118  * \brief               Output = Generic_CMAC( hmac key, input buffer )
00119  *
00120  * \param cipher_info   message digest info
00121  * \param key           CMAC key
00122  * \param keylen        length of the CMAC key in bits
00123  * \param input         buffer holding the  data
00124  * \param ilen          length of the input data
00125  * \param output        Generic CMAC-result
00126  *
00127  * \returns             0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
00128  *                      verification fails.
00129  */
00130 int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
00131                          const unsigned char *key, size_t keylen,
00132                          const unsigned char *input, size_t ilen,
00133                          unsigned char *output );
00134 
00135 #if defined(MBEDTLS_AES_C)
00136 /**
00137  * \brief           AES-CMAC-128-PRF
00138  *                  Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
00139  *
00140  * \param key       PRF key
00141  * \param key_len   PRF key length in bytes
00142  * \param input     buffer holding the input data
00143  * \param in_len    length of the input data in bytes
00144  * \param output    buffer holding the generated pseudorandom output (16 bytes)
00145  *
00146  * \return          0 if successful
00147  */
00148 int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
00149                               const unsigned char *input, size_t in_len,
00150                               unsigned char output[16] );
00151 #endif /* MBEDTLS_AES_C */
00152 
00153 #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
00154 /**
00155  * \brief          Checkup routine
00156  *
00157  * \return         0 if successful, or 1 if the test failed
00158  */
00159 int mbedtls_cmac_self_test( int verbose );
00160 #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
00161 
00162 #ifdef __cplusplus
00163 }
00164 #endif
00165 
00166 #endif /* MBEDTLS_CMAC_H */