Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /**
switches 0:5c4d7b2438d3 2 * \file cmac.h
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * \brief Cipher-based Message Authentication Code (CMAC) Mode for
switches 0:5c4d7b2438d3 5 * Authentication
switches 0:5c4d7b2438d3 6 *
switches 0:5c4d7b2438d3 7 * Copyright (C) 2015-2016, ARM Limited, All Rights Reserved
switches 0:5c4d7b2438d3 8 * SPDX-License-Identifier: Apache-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
switches 0:5c4d7b2438d3 11 * not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 12 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 13 *
switches 0:5c4d7b2438d3 14 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 15 *
switches 0:5c4d7b2438d3 16 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
switches 0:5c4d7b2438d3 18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 19 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 20 * limitations under the License.
switches 0:5c4d7b2438d3 21 *
switches 0:5c4d7b2438d3 22 * This file is part of mbed TLS (https://tls.mbed.org)
switches 0:5c4d7b2438d3 23 */
switches 0:5c4d7b2438d3 24 #ifndef MBEDTLS_CMAC_H
switches 0:5c4d7b2438d3 25 #define MBEDTLS_CMAC_H
switches 0:5c4d7b2438d3 26
switches 0:5c4d7b2438d3 27 #include "mbedtls/cipher.h"
switches 0:5c4d7b2438d3 28
switches 0:5c4d7b2438d3 29 #ifdef __cplusplus
switches 0:5c4d7b2438d3 30 extern "C" {
switches 0:5c4d7b2438d3 31 #endif
switches 0:5c4d7b2438d3 32
switches 0:5c4d7b2438d3 33 #define MBEDTLS_AES_BLOCK_SIZE 16
switches 0:5c4d7b2438d3 34 #define MBEDTLS_DES3_BLOCK_SIZE 8
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 #if defined(MBEDTLS_AES_C)
switches 0:5c4d7b2438d3 37 #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */
switches 0:5c4d7b2438d3 38 #else
switches 0:5c4d7b2438d3 39 #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */
switches 0:5c4d7b2438d3 40 #endif
switches 0:5c4d7b2438d3 41
switches 0:5c4d7b2438d3 42 /**
switches 0:5c4d7b2438d3 43 * CMAC context structure - Contains internal state information only
switches 0:5c4d7b2438d3 44 */
switches 0:5c4d7b2438d3 45 struct mbedtls_cmac_context_t
switches 0:5c4d7b2438d3 46 {
switches 0:5c4d7b2438d3 47 /** Internal state of the CMAC algorithm */
switches 0:5c4d7b2438d3 48 unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX];
switches 0:5c4d7b2438d3 49
switches 0:5c4d7b2438d3 50 /** Unprocessed data - either data that was not block aligned and is still
switches 0:5c4d7b2438d3 51 * pending to be processed, or the final block */
switches 0:5c4d7b2438d3 52 unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX];
switches 0:5c4d7b2438d3 53
switches 0:5c4d7b2438d3 54 /** Length of data pending to be processed */
switches 0:5c4d7b2438d3 55 size_t unprocessed_len;
switches 0:5c4d7b2438d3 56 };
switches 0:5c4d7b2438d3 57
switches 0:5c4d7b2438d3 58 /**
switches 0:5c4d7b2438d3 59 * \brief Set the CMAC key and prepare to authenticate the input
switches 0:5c4d7b2438d3 60 * data.
switches 0:5c4d7b2438d3 61 * Should be called with an initialised cipher context.
switches 0:5c4d7b2438d3 62 *
switches 0:5c4d7b2438d3 63 * \param ctx Cipher context
switches 0:5c4d7b2438d3 64 * \param key CMAC key
switches 0:5c4d7b2438d3 65 * \param keybits length of the CMAC key in bits
switches 0:5c4d7b2438d3 66 * (must be acceptable by the cipher)
switches 0:5c4d7b2438d3 67 *
switches 0:5c4d7b2438d3 68 * \return 0 if successful, or a cipher specific error code
switches 0:5c4d7b2438d3 69 */
switches 0:5c4d7b2438d3 70 int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
switches 0:5c4d7b2438d3 71 const unsigned char *key, size_t keybits );
switches 0:5c4d7b2438d3 72
switches 0:5c4d7b2438d3 73 /**
switches 0:5c4d7b2438d3 74 * \brief Generic CMAC process buffer.
switches 0:5c4d7b2438d3 75 * Called between mbedtls_cipher_cmac_starts() or
switches 0:5c4d7b2438d3 76 * mbedtls_cipher_cmac_reset() and
switches 0:5c4d7b2438d3 77 * mbedtls_cipher_cmac_finish().
switches 0:5c4d7b2438d3 78 * May be called repeatedly.
switches 0:5c4d7b2438d3 79 *
switches 0:5c4d7b2438d3 80 * \param ctx CMAC context
switches 0:5c4d7b2438d3 81 * \param input buffer holding the data
switches 0:5c4d7b2438d3 82 * \param ilen length of the input data
switches 0:5c4d7b2438d3 83 *
switches 0:5c4d7b2438d3 84 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
switches 0:5c4d7b2438d3 85 * verification fails.
switches 0:5c4d7b2438d3 86 */
switches 0:5c4d7b2438d3 87 int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
switches 0:5c4d7b2438d3 88 const unsigned char *input, size_t ilen );
switches 0:5c4d7b2438d3 89
switches 0:5c4d7b2438d3 90 /**
switches 0:5c4d7b2438d3 91 * \brief Output CMAC.
switches 0:5c4d7b2438d3 92 * Called after mbedtls_cipher_cmac_update().
switches 0:5c4d7b2438d3 93 * Usually followed by mbedtls_cipher_cmac_reset(), then
switches 0:5c4d7b2438d3 94 * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free().
switches 0:5c4d7b2438d3 95 *
switches 0:5c4d7b2438d3 96 * \param ctx CMAC context
switches 0:5c4d7b2438d3 97 * \param output Generic CMAC checksum result
switches 0:5c4d7b2438d3 98 *
switches 0:5c4d7b2438d3 99 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
switches 0:5c4d7b2438d3 100 * verification fails.
switches 0:5c4d7b2438d3 101 */
switches 0:5c4d7b2438d3 102 int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
switches 0:5c4d7b2438d3 103 unsigned char *output );
switches 0:5c4d7b2438d3 104
switches 0:5c4d7b2438d3 105 /**
switches 0:5c4d7b2438d3 106 * \brief Prepare to authenticate a new message with the same key.
switches 0:5c4d7b2438d3 107 * Called after mbedtls_cipher_cmac_finish() and before
switches 0:5c4d7b2438d3 108 * mbedtls_cipher_cmac_update().
switches 0:5c4d7b2438d3 109 *
switches 0:5c4d7b2438d3 110 * \param ctx CMAC context to be reset
switches 0:5c4d7b2438d3 111 *
switches 0:5c4d7b2438d3 112 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
switches 0:5c4d7b2438d3 113 * verification fails.
switches 0:5c4d7b2438d3 114 */
switches 0:5c4d7b2438d3 115 int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx );
switches 0:5c4d7b2438d3 116
switches 0:5c4d7b2438d3 117 /**
switches 0:5c4d7b2438d3 118 * \brief Output = Generic_CMAC( hmac key, input buffer )
switches 0:5c4d7b2438d3 119 *
switches 0:5c4d7b2438d3 120 * \param cipher_info message digest info
switches 0:5c4d7b2438d3 121 * \param key CMAC key
switches 0:5c4d7b2438d3 122 * \param keylen length of the CMAC key in bits
switches 0:5c4d7b2438d3 123 * \param input buffer holding the data
switches 0:5c4d7b2438d3 124 * \param ilen length of the input data
switches 0:5c4d7b2438d3 125 * \param output Generic CMAC-result
switches 0:5c4d7b2438d3 126 *
switches 0:5c4d7b2438d3 127 * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
switches 0:5c4d7b2438d3 128 * verification fails.
switches 0:5c4d7b2438d3 129 */
switches 0:5c4d7b2438d3 130 int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
switches 0:5c4d7b2438d3 131 const unsigned char *key, size_t keylen,
switches 0:5c4d7b2438d3 132 const unsigned char *input, size_t ilen,
switches 0:5c4d7b2438d3 133 unsigned char *output );
switches 0:5c4d7b2438d3 134
switches 0:5c4d7b2438d3 135 #if defined(MBEDTLS_AES_C)
switches 0:5c4d7b2438d3 136 /**
switches 0:5c4d7b2438d3 137 * \brief AES-CMAC-128-PRF
switches 0:5c4d7b2438d3 138 * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615
switches 0:5c4d7b2438d3 139 *
switches 0:5c4d7b2438d3 140 * \param key PRF key
switches 0:5c4d7b2438d3 141 * \param key_len PRF key length in bytes
switches 0:5c4d7b2438d3 142 * \param input buffer holding the input data
switches 0:5c4d7b2438d3 143 * \param in_len length of the input data in bytes
switches 0:5c4d7b2438d3 144 * \param output buffer holding the generated pseudorandom output (16 bytes)
switches 0:5c4d7b2438d3 145 *
switches 0:5c4d7b2438d3 146 * \return 0 if successful
switches 0:5c4d7b2438d3 147 */
switches 0:5c4d7b2438d3 148 int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len,
switches 0:5c4d7b2438d3 149 const unsigned char *input, size_t in_len,
switches 0:5c4d7b2438d3 150 unsigned char output[16] );
switches 0:5c4d7b2438d3 151 #endif /* MBEDTLS_AES_C */
switches 0:5c4d7b2438d3 152
switches 0:5c4d7b2438d3 153 #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) )
switches 0:5c4d7b2438d3 154 /**
switches 0:5c4d7b2438d3 155 * \brief Checkup routine
switches 0:5c4d7b2438d3 156 *
switches 0:5c4d7b2438d3 157 * \return 0 if successful, or 1 if the test failed
switches 0:5c4d7b2438d3 158 */
switches 0:5c4d7b2438d3 159 int mbedtls_cmac_self_test( int verbose );
switches 0:5c4d7b2438d3 160 #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
switches 0:5c4d7b2438d3 161
switches 0:5c4d7b2438d3 162 #ifdef __cplusplus
switches 0:5c4d7b2438d3 163 }
switches 0:5c4d7b2438d3 164 #endif
switches 0:5c4d7b2438d3 165
switches 0:5c4d7b2438d3 166 #endif /* MBEDTLS_CMAC_H */