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