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