2015-12-15 08:38:11 +01:00
|
|
|
/**
|
|
|
|
* \file cmac.h
|
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \brief Cipher-based Message Authentication Code (CMAC) Mode for
|
|
|
|
* Authentication
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* Copyright (C) 2015-2016, ARM Limited, All Rights Reserved
|
2015-12-15 08:38:11 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
|
|
*/
|
|
|
|
#ifndef MBEDTLS_CMAC_H
|
|
|
|
#define MBEDTLS_CMAC_H
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
#include "mbedtls/cipher.h"
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#define MBEDTLS_AES_BLOCK_SIZE 16
|
|
|
|
#define MBEDTLS_DES3_BLOCK_SIZE 8
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-10-06 13:49:58 +02:00
|
|
|
#define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
|
2016-10-05 15:09:11 +02:00
|
|
|
#else
|
2016-10-06 13:49:58 +02:00
|
|
|
#define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
|
2016-10-05 15:09:11 +02:00
|
|
|
#endif
|
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
/**
|
2016-10-05 16:12:59 +02:00
|
|
|
* CMAC context structure - Contains internal state information only
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 16:12:59 +02:00
|
|
|
struct mbedtls_cmac_context_t
|
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
/** Internal state of the CMAC algorithm */
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-10-06 16:23:39 +02:00
|
|
|
/** Unprocessed data - either data that was not block aligned and is still
|
2016-10-05 15:09:11 +02:00
|
|
|
* pending to be processed, or the final block */
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-10-05 15:09:11 +02:00
|
|
|
|
|
|
|
/** Length of data pending to be processed */
|
|
|
|
size_t unprocessed_len;
|
2016-10-05 16:12:59 +02:00
|
|
|
};
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/**
|
2016-10-05 15:09:11 +02:00
|
|
|
* \brief Set the CMAC key and prepare to authenticate the input
|
|
|
|
* data.
|
2016-12-14 16:27:22 +01:00
|
|
|
* Should be called with an initialized cipher context.
|
2016-10-05 15:09:11 +02:00
|
|
|
*
|
2016-12-14 16:27:22 +01:00
|
|
|
* \param ctx Cipher context. This should be a cipher context,
|
|
|
|
* initialized to be one of the following types:
|
|
|
|
* MBEDTLS_CIPHER_AES_128_ECB, MBEDTLS_CIPHER_AES_192_ECB,
|
|
|
|
* MBEDTLS_CIPHER_AES_256_ECB or
|
|
|
|
* MBEDTLS_CIPHER_DES_EDE3_ECB.
|
2016-10-05 15:09:11 +02:00
|
|
|
* \param key CMAC key
|
|
|
|
* \param keybits length of the CMAC key in bits
|
|
|
|
* (must be acceptable by the cipher)
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \return 0 if successful, or a cipher specific error code
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
|
2016-10-05 16:33:53 +02:00
|
|
|
const unsigned char *key, size_t keybits );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/**
|
2016-10-05 15:09:11 +02:00
|
|
|
* \brief Generic CMAC process buffer.
|
|
|
|
* Called between mbedtls_cipher_cmac_starts() or
|
|
|
|
* mbedtls_cipher_cmac_reset() and
|
|
|
|
* mbedtls_cipher_cmac_finish().
|
|
|
|
* May be called repeatedly.
|
|
|
|
*
|
|
|
|
* \param ctx CMAC context
|
|
|
|
* \param input buffer holding the data
|
|
|
|
* \param ilen length of the input data
|
|
|
|
*
|
|
|
|
* \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
|
|
|
|
const unsigned char *input, size_t ilen );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/**
|
2016-10-05 15:09:11 +02:00
|
|
|
* \brief Output CMAC.
|
|
|
|
* Called after mbedtls_cipher_cmac_update().
|
|
|
|
* Usually followed by mbedtls_cipher_cmac_reset(), then
|
|
|
|
* mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
|
|
|
|
*
|
|
|
|
* \param ctx CMAC context
|
|
|
|
* \param output Generic CMAC checksum result
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
|
|
|
|
unsigned char *output );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/**
|
2016-10-05 15:09:11 +02:00
|
|
|
* \brief Prepare to authenticate a new message with the same key.
|
|
|
|
* Called after mbedtls_cipher_cmac_finish() and before
|
|
|
|
* mbedtls_cipher_cmac_update().
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \param ctx CMAC context to be reset
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/**
|
2016-12-14 16:27:22 +01:00
|
|
|
* \brief Output = Generic_CMAC( cmac key, input buffer )
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* \param cipher_info message digest info
|
|
|
|
* \param key CMAC key
|
|
|
|
* \param keylen length of the CMAC key in bits
|
|
|
|
* \param input buffer holding the data
|
|
|
|
* \param ilen length of the input data
|
|
|
|
* \param output Generic CMAC-result
|
|
|
|
*
|
|
|
|
* \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
|
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2015-12-15 08:38:11 +01:00
|
|
|
/**
|
|
|
|
* \brief AES-CMAC-128-PRF
|
2016-10-05 15:09:11 +02:00
|
|
|
* Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
|
|
|
* \param key PRF key
|
2016-10-05 15:09:11 +02:00
|
|
|
* \param key_len PRF key length in bytes
|
2015-12-15 08:38:11 +01:00
|
|
|
* \param input buffer holding the input data
|
2016-01-13 11:48:02 +01:00
|
|
|
* \param in_len length of the input data in bytes
|
2016-10-05 15:09:11 +02:00
|
|
|
* \param output buffer holding the generated pseudorandom output (16 bytes)
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
|
|
|
* \return 0 if successful
|
|
|
|
*/
|
2016-05-18 23:29:51 +02:00
|
|
|
int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
|
2016-01-13 11:48:02 +01:00
|
|
|
const unsigned char *input, size_t in_len,
|
2016-10-05 15:09:11 +02:00
|
|
|
unsigned char output[16] );
|
2016-05-20 01:02:42 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-05-20 01:02:42 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
|
2015-12-15 08:38:11 +01:00
|
|
|
/**
|
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if the test failed
|
|
|
|
*/
|
|
|
|
int mbedtls_cmac_self_test( int verbose );
|
2016-05-20 01:02:42 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_CMAC_H */
|