Andres Amaya Garcia | af610a0 | 2016-12-14 10:13:43 +0000 | [diff] [blame] | 1 | /** |
Brian Murray | 53e23b6 | 2016-09-13 14:00:15 -0700 | [diff] [blame] | 2 | * \file cmac.c |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 3 | * |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 4 | * \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 5 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 6 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
Brian Murray | 53e23b6 | 2016-09-13 14:00:15 -0700 | [diff] [blame] | 11 | * References: |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 12 | * |
| 13 | * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The |
| 14 | * CMAC Mode for Authentication |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 15 | * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 16 | * |
| 17 | * - RFC 4493 - The AES-CMAC Algorithm |
| 18 | * https://tools.ietf.org/html/rfc4493 |
| 19 | * |
| 20 | * - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message |
| 21 | * Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128) |
| 22 | * Algorithm for the Internet Key Exchange Protocol (IKE) |
| 23 | * https://tools.ietf.org/html/rfc4615 |
| 24 | * |
| 25 | * Additional test vectors: ISO/IEC 9797-1 |
| 26 | * |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 29 | #include "common.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 30 | |
| 31 | #if defined(MBEDTLS_CMAC_C) |
| 32 | |
| 33 | #include "mbedtls/cmac.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 34 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 35 | #include "mbedtls/error.h" |
Steven Cooreman | 655b012 | 2021-01-11 14:34:51 +0100 | [diff] [blame] | 36 | #include "mbedtls/platform.h" |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 37 | |
| 38 | #include <string.h> |
| 39 | |
Ron Eldor | 621080d | 2017-12-21 10:57:43 +0200 | [diff] [blame] | 40 | #if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 41 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 42 | /* |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 43 | * Multiplication by u in the Galois field of GF(2^n) |
Manuel Pégourié-Gonnard | a610b4c | 2016-01-13 11:28:16 +0000 | [diff] [blame] | 44 | * |
Brian Murray | 72b69e3 | 2016-09-13 14:21:01 -0700 | [diff] [blame] | 45 | * As explained in NIST SP 800-38B, this can be computed: |
Manuel Pégourié-Gonnard | a610b4c | 2016-01-13 11:28:16 +0000 | [diff] [blame] | 46 | * |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 47 | * If MSB(p) = 0, then p = (p << 1) |
| 48 | * If MSB(p) = 1, then p = (p << 1) ^ R_n |
| 49 | * with R_64 = 0x1B and R_128 = 0x87 |
| 50 | * |
| 51 | * Input and output MUST NOT point to the same buffer |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 52 | * Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES. |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 53 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | static int cmac_multiply_by_u(unsigned char *output, |
| 55 | const unsigned char *input, |
| 56 | size_t blocksize) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 57 | { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 58 | const unsigned char R_128 = 0x87; |
| 59 | const unsigned char R_64 = 0x1B; |
| 60 | unsigned char R_n, mask; |
| 61 | unsigned char overflow = 0x00; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 62 | int i; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 63 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | if (blocksize == MBEDTLS_AES_BLOCK_SIZE) { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 65 | R_n = R_128; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 66 | } else if (blocksize == MBEDTLS_DES3_BLOCK_SIZE) { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 67 | R_n = R_64; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | } else { |
| 69 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | for (i = (int) blocksize - 1; i >= 0; i--) { |
Manuel Pégourié-Gonnard | a610b4c | 2016-01-13 11:28:16 +0000 | [diff] [blame] | 73 | output[i] = input[i] << 1 | overflow; |
| 74 | overflow = input[i] >> 7; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 75 | } |
Manuel Pégourié-Gonnard | 3da5402 | 2016-01-13 11:00:47 +0000 | [diff] [blame] | 76 | |
Manuel Pégourié-Gonnard | 475f06f | 2016-01-13 13:05:03 +0000 | [diff] [blame] | 77 | /* mask = ( input[0] >> 7 ) ? 0xff : 0x00 |
| 78 | * using bit operations to avoid branches */ |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 475f06f | 2016-01-13 13:05:03 +0000 | [diff] [blame] | 80 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 81 | * well-defined and precisely what we want to do here */ |
| 82 | #if defined(_MSC_VER) |
| 83 | #pragma warning( push ) |
| 84 | #pragma warning( disable : 4146 ) |
| 85 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | mask = -(input[0] >> 7); |
Manuel Pégourié-Gonnard | 475f06f | 2016-01-13 13:05:03 +0000 | [diff] [blame] | 87 | #if defined(_MSC_VER) |
| 88 | #pragma warning( pop ) |
| 89 | #endif |
| 90 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | output[blocksize - 1] ^= R_n & mask; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 92 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | return 0; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Generate subkeys |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 98 | * |
| 99 | * - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 100 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | static int cmac_generate_subkeys(mbedtls_cipher_context_t *ctx, |
| 102 | unsigned char *K1, unsigned char *K2) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 103 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 105 | unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 106 | size_t olen, block_size; |
| 107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | mbedtls_platform_zeroize(L, sizeof(L)); |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 109 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 110 | block_size = ctx->cipher_info->block_size; |
| 111 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 112 | /* Calculate Ek(0) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | if ((ret = mbedtls_cipher_update(ctx, L, block_size, L, &olen)) != 0) { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 114 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 3da5402 | 2016-01-13 11:00:47 +0000 | [diff] [blame] | 117 | /* |
Manuel Pégourié-Gonnard | a610b4c | 2016-01-13 11:28:16 +0000 | [diff] [blame] | 118 | * Generate K1 and K2 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 119 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | if ((ret = cmac_multiply_by_u(K1, L, block_size)) != 0) { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 121 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | } |
Manuel Pégourié-Gonnard | 3da5402 | 2016-01-13 11:00:47 +0000 | [diff] [blame] | 123 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | if ((ret = cmac_multiply_by_u(K2, K1, block_size)) != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 125 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 127 | |
| 128 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | mbedtls_platform_zeroize(L, sizeof(L)); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | return ret; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 132 | } |
Ron Eldor | 621080d | 2017-12-21 10:57:43 +0200 | [diff] [blame] | 133 | #endif /* !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 134 | |
Ron Eldor | 621080d | 2017-12-21 10:57:43 +0200 | [diff] [blame] | 135 | #if !defined(MBEDTLS_CMAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | static void cmac_xor_block(unsigned char *output, const unsigned char *input1, |
| 137 | const unsigned char *input2, |
| 138 | const size_t block_size) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 139 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 140 | size_t idx; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | for (idx = 0; idx < block_size; idx++) { |
| 143 | output[idx] = input1[idx] ^ input2[idx]; |
| 144 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Manuel Pégourié-Gonnard | d2c3d3e | 2016-01-13 13:14:04 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Create padded last block from (partial) last block. |
| 149 | * |
| 150 | * We can't use the padding option from the cipher layer, as it only works for |
| 151 | * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition. |
| 152 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX], |
| 154 | size_t padded_block_len, |
| 155 | const unsigned char *last_block, |
| 156 | size_t last_block_len) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 157 | { |
| 158 | size_t j; |
| 159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | for (j = 0; j < padded_block_len; j++) { |
| 161 | if (j < last_block_len) { |
Manuel Pégourié-Gonnard | d2c3d3e | 2016-01-13 13:14:04 +0000 | [diff] [blame] | 162 | padded_block[j] = last_block[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | } else if (j == last_block_len) { |
Manuel Pégourié-Gonnard | d2c3d3e | 2016-01-13 13:14:04 +0000 | [diff] [blame] | 164 | padded_block[j] = 0x80; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 165 | } else { |
Manuel Pégourié-Gonnard | d2c3d3e | 2016-01-13 13:14:04 +0000 | [diff] [blame] | 166 | padded_block[j] = 0x00; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 171 | int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx, |
| 172 | const unsigned char *key, size_t keybits) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 173 | { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 174 | mbedtls_cipher_type_t type; |
| 175 | mbedtls_cmac_context_t *cmac_ctx; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 176 | int retval; |
| 177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | if (ctx == NULL || ctx->cipher_info == NULL || key == NULL) { |
| 179 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 180 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | if ((retval = mbedtls_cipher_setkey(ctx, key, (int) keybits, |
| 183 | MBEDTLS_ENCRYPT)) != 0) { |
| 184 | return retval; |
| 185 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 186 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 187 | type = ctx->cipher_info->type; |
| 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | switch (type) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 190 | case MBEDTLS_CIPHER_AES_128_ECB: |
| 191 | case MBEDTLS_CIPHER_AES_192_ECB: |
| 192 | case MBEDTLS_CIPHER_AES_256_ECB: |
| 193 | case MBEDTLS_CIPHER_DES_EDE3_ECB: |
| 194 | break; |
| 195 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* Allocated and initialise in the cipher context memory for the CMAC |
| 200 | * context */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | cmac_ctx = mbedtls_calloc(1, sizeof(mbedtls_cmac_context_t)); |
| 202 | if (cmac_ctx == NULL) { |
| 203 | return MBEDTLS_ERR_CIPHER_ALLOC_FAILED; |
| 204 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 205 | |
| 206 | ctx->cmac_ctx = cmac_ctx; |
| 207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 208 | mbedtls_platform_zeroize(cmac_ctx->state, sizeof(cmac_ctx->state)); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx, |
| 214 | const unsigned char *input, size_t ilen) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 215 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | mbedtls_cmac_context_t *cmac_ctx; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 217 | unsigned char *state; |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 218 | int ret = 0; |
| 219 | size_t n, j, olen, block_size; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | if (ctx == NULL || ctx->cipher_info == NULL || input == NULL || |
| 222 | ctx->cmac_ctx == NULL) { |
| 223 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 224 | } |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 225 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 226 | cmac_ctx = ctx->cmac_ctx; |
| 227 | block_size = ctx->cipher_info->block_size; |
| 228 | state = ctx->cmac_ctx->state; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 229 | |
Simon Butcher | 6b0774a | 2016-10-10 21:37:42 +0100 | [diff] [blame] | 230 | /* Is there data still to process from the last call, that's greater in |
| 231 | * size than a block? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | if (cmac_ctx->unprocessed_len > 0 && |
| 233 | ilen > block_size - cmac_ctx->unprocessed_len) { |
| 234 | memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len], |
| 235 | input, |
| 236 | block_size - cmac_ctx->unprocessed_len); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | cmac_xor_block(state, cmac_ctx->unprocessed_block, state, block_size); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | if ((ret = mbedtls_cipher_update(ctx, state, block_size, state, |
| 241 | &olen)) != 0) { |
| 242 | goto exit; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Simon Butcher | 6b0774a | 2016-10-10 21:37:42 +0100 | [diff] [blame] | 245 | input += block_size - cmac_ctx->unprocessed_len; |
| 246 | ilen -= block_size - cmac_ctx->unprocessed_len; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 247 | cmac_ctx->unprocessed_len = 0; |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 250 | /* n is the number of blocks including any final partial block */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | n = (ilen + block_size - 1) / block_size; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 252 | |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 253 | /* Iterate across the input data in block sized chunks, excluding any |
| 254 | * final partial or complete block */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | for (j = 1; j < n; j++) { |
| 256 | cmac_xor_block(state, input, state, block_size); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | if ((ret = mbedtls_cipher_update(ctx, state, block_size, state, |
| 259 | &olen)) != 0) { |
| 260 | goto exit; |
| 261 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 262 | |
| 263 | ilen -= block_size; |
| 264 | input += block_size; |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 265 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 266 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 267 | /* If there is data left over that wasn't aligned to a block */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | if (ilen > 0) { |
| 269 | memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len], |
| 270 | input, |
| 271 | ilen); |
Simon Butcher | 6b0774a | 2016-10-10 21:37:42 +0100 | [diff] [blame] | 272 | cmac_ctx->unprocessed_len += ilen; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx, |
| 280 | unsigned char *output) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 281 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | mbedtls_cmac_context_t *cmac_ctx; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 283 | unsigned char *state, *last_block; |
| 284 | unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 285 | unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 286 | unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 287 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 288 | size_t olen, block_size; |
| 289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL || |
| 291 | output == NULL) { |
| 292 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 293 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 294 | |
| 295 | cmac_ctx = ctx->cmac_ctx; |
| 296 | block_size = ctx->cipher_info->block_size; |
| 297 | state = cmac_ctx->state; |
| 298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 299 | mbedtls_platform_zeroize(K1, sizeof(K1)); |
| 300 | mbedtls_platform_zeroize(K2, sizeof(K2)); |
| 301 | cmac_generate_subkeys(ctx, K1, K2); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 302 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 303 | last_block = cmac_ctx->unprocessed_block; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 304 | |
| 305 | /* Calculate last block */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | if (cmac_ctx->unprocessed_len < block_size) { |
| 307 | cmac_pad(M_last, block_size, last_block, cmac_ctx->unprocessed_len); |
| 308 | cmac_xor_block(M_last, M_last, K2, block_size); |
| 309 | } else { |
Manuel Pégourié-Gonnard | 2c06306 | 2016-01-13 14:27:55 +0000 | [diff] [blame] | 310 | /* Last block is complete block */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | cmac_xor_block(M_last, last_block, K1, block_size); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 314 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 315 | cmac_xor_block(state, M_last, state, block_size); |
| 316 | if ((ret = mbedtls_cipher_update(ctx, state, block_size, state, |
| 317 | &olen)) != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 318 | goto exit; |
| 319 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | memcpy(output, state, block_size); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 322 | |
| 323 | exit: |
| 324 | /* Wipe the generated keys on the stack, and any other transients to avoid |
| 325 | * side channel leakage */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 326 | mbedtls_platform_zeroize(K1, sizeof(K1)); |
| 327 | mbedtls_platform_zeroize(K2, sizeof(K2)); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 328 | |
| 329 | cmac_ctx->unprocessed_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | mbedtls_platform_zeroize(cmac_ctx->unprocessed_block, |
| 331 | sizeof(cmac_ctx->unprocessed_block)); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 332 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | mbedtls_platform_zeroize(state, MBEDTLS_CIPHER_BLKSIZE_MAX); |
| 334 | return ret; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 338 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | mbedtls_cmac_context_t *cmac_ctx; |
Manuel Pégourié-Gonnard | 3da5402 | 2016-01-13 11:00:47 +0000 | [diff] [blame] | 340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL) { |
| 342 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 343 | } |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 344 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 345 | cmac_ctx = ctx->cmac_ctx; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 346 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 347 | /* Reset the internal state */ |
| 348 | cmac_ctx->unprocessed_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | mbedtls_platform_zeroize(cmac_ctx->unprocessed_block, |
| 350 | sizeof(cmac_ctx->unprocessed_block)); |
| 351 | mbedtls_platform_zeroize(cmac_ctx->state, |
| 352 | sizeof(cmac_ctx->state)); |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | return 0; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info, |
| 358 | const unsigned char *key, size_t keylen, |
| 359 | const unsigned char *input, size_t ilen, |
| 360 | unsigned char *output) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 361 | { |
| 362 | mbedtls_cipher_context_t ctx; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 363 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | if (cipher_info == NULL || key == NULL || input == NULL || output == NULL) { |
| 366 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 367 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 368 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | mbedtls_cipher_init(&ctx); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 371 | if ((ret = mbedtls_cipher_setup(&ctx, cipher_info)) != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 372 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | ret = mbedtls_cipher_cmac_starts(&ctx, key, keylen); |
| 376 | if (ret != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 377 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 379 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | ret = mbedtls_cipher_cmac_update(&ctx, input, ilen); |
| 381 | if (ret != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 382 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | ret = mbedtls_cipher_cmac_finish(&ctx, output); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 386 | |
| 387 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | mbedtls_cipher_free(&ctx); |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 391 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 392 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 393 | #if defined(MBEDTLS_AES_C) |
Manuel Pégourié-Gonnard | 7b555f2 | 2016-01-13 15:09:09 +0000 | [diff] [blame] | 394 | /* |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 395 | * Implementation of AES-CMAC-PRF-128 defined in RFC 4615 |
Manuel Pégourié-Gonnard | 7b555f2 | 2016-01-13 15:09:09 +0000 | [diff] [blame] | 396 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length, |
| 398 | const unsigned char *input, size_t in_len, |
| 399 | unsigned char output[16]) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 400 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 401 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 402 | const mbedtls_cipher_info_t *cipher_info; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 403 | unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE]; |
| 404 | unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE]; |
| 405 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | if (key == NULL || input == NULL || output == NULL) { |
| 407 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 408 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB); |
| 411 | if (cipher_info == NULL) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 412 | /* Failing at this point must be due to a build issue */ |
| 413 | ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 414 | goto exit; |
| 415 | } |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | if (key_length == MBEDTLS_AES_BLOCK_SIZE) { |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 418 | /* Use key as is */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | memcpy(int_key, key, MBEDTLS_AES_BLOCK_SIZE); |
| 420 | } else { |
| 421 | memset(zero_key, 0, MBEDTLS_AES_BLOCK_SIZE); |
Manuel Pégourié-Gonnard | 7b555f2 | 2016-01-13 15:09:09 +0000 | [diff] [blame] | 422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | ret = mbedtls_cipher_cmac(cipher_info, zero_key, 128, key, |
| 424 | key_length, int_key); |
| 425 | if (ret != 0) { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 426 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 427 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | ret = mbedtls_cipher_cmac(cipher_info, int_key, 128, input, in_len, |
| 431 | output); |
Manuel Pégourié-Gonnard | d6cf754 | 2016-01-13 11:30:00 +0000 | [diff] [blame] | 432 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 433 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | mbedtls_platform_zeroize(int_key, sizeof(int_key)); |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | return ret; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 437 | } |
Brian Murray | b439d45 | 2016-05-19 16:02:42 -0700 | [diff] [blame] | 438 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 439 | |
Steven Cooreman | 6334277 | 2017-04-04 11:47:16 +0200 | [diff] [blame] | 440 | #endif /* !MBEDTLS_CMAC_ALT */ |
| 441 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 442 | #if defined(MBEDTLS_SELF_TEST) |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 443 | /* |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 444 | * CMAC test data for SP800-38B |
| 445 | * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf |
| 446 | * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 447 | * |
| 448 | * AES-CMAC-PRF-128 test data from RFC 4615 |
| 449 | * https://tools.ietf.org/html/rfc4615#page-4 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 450 | */ |
| 451 | |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 452 | #define NB_CMAC_TESTS_PER_KEY 4 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 453 | #define NB_PRF_TESTS 3 |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 454 | |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 455 | #if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) |
| 456 | /* All CMAC test inputs are truncated from the same 64 byte buffer. */ |
| 457 | static const unsigned char test_message[] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 458 | /* PT */ |
| 459 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 460 | 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 461 | 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
| 462 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
| 463 | 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
| 464 | 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 465 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
| 466 | 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 467 | }; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 468 | #endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */ |
Brian Murray | 0cf14c1 | 2016-05-23 12:49:50 -0700 | [diff] [blame] | 469 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 470 | #if defined(MBEDTLS_AES_C) |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 471 | /* Truncation point of message for AES CMAC tests */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 472 | static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 473 | /* Mlen */ |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 474 | 0, |
| 475 | 16, |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 476 | 20, |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 477 | 64 |
| 478 | }; |
| 479 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 480 | /* CMAC-AES128 Test Data */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 481 | static const unsigned char aes_128_key[16] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 482 | 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 483 | 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 484 | }; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 485 | static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 486 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 487 | /* K1 */ |
| 488 | 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66, |
| 489 | 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 490 | }, |
| 491 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 492 | /* K2 */ |
| 493 | 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc, |
| 494 | 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 495 | } |
| 496 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 497 | static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = |
| 498 | { |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 499 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 500 | /* Example #1 */ |
| 501 | 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, |
| 502 | 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 503 | }, |
| 504 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 505 | /* Example #2 */ |
| 506 | 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, |
| 507 | 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 508 | }, |
| 509 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 510 | /* Example #3 */ |
| 511 | 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8, |
| 512 | 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 513 | }, |
| 514 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 515 | /* Example #4 */ |
| 516 | 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, |
| 517 | 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 518 | } |
| 519 | }; |
| 520 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 521 | /* CMAC-AES192 Test Data */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 522 | static const unsigned char aes_192_key[24] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 523 | 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, |
| 524 | 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, |
| 525 | 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 526 | }; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 527 | static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 528 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 529 | /* K1 */ |
| 530 | 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27, |
| 531 | 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 532 | }, |
| 533 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 534 | /* K2 */ |
| 535 | 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e, |
| 536 | 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 537 | } |
| 538 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 539 | static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = |
| 540 | { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 541 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 542 | /* Example #1 */ |
| 543 | 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5, |
| 544 | 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 545 | }, |
| 546 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 547 | /* Example #2 */ |
| 548 | 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90, |
| 549 | 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 550 | }, |
| 551 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 552 | /* Example #3 */ |
| 553 | 0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04, |
| 554 | 0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 555 | }, |
| 556 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 557 | /* Example #4 */ |
| 558 | 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79, |
| 559 | 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 560 | } |
| 561 | }; |
| 562 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 563 | /* CMAC-AES256 Test Data */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 564 | static const unsigned char aes_256_key[32] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 565 | 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, |
| 566 | 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, |
| 567 | 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, |
| 568 | 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 569 | }; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 570 | static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = { |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 571 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 572 | /* K1 */ |
| 573 | 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac, |
| 574 | 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 575 | }, |
| 576 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 577 | /* K2 */ |
| 578 | 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58, |
| 579 | 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 580 | } |
| 581 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = |
| 583 | { |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 584 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 585 | /* Example #1 */ |
| 586 | 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e, |
| 587 | 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 588 | }, |
| 589 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 590 | /* Example #2 */ |
| 591 | 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82, |
| 592 | 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 593 | }, |
| 594 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 595 | /* Example #3 */ |
| 596 | 0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a, |
| 597 | 0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 598 | }, |
| 599 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 600 | /* Example #4 */ |
| 601 | 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5, |
| 602 | 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 603 | } |
| 604 | }; |
| 605 | #endif /* MBEDTLS_AES_C */ |
| 606 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 607 | #if defined(MBEDTLS_DES_C) |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 608 | /* Truncation point of message for 3DES CMAC tests */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 609 | static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = { |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 610 | 0, |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 611 | 16, |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 612 | 20, |
| 613 | 32 |
| 614 | }; |
| 615 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 616 | /* CMAC-TDES (Generation) - 2 Key Test Data */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 617 | static const unsigned char des3_2key_key[24] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 618 | /* Key1 */ |
| 619 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
| 620 | /* Key2 */ |
| 621 | 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01, |
| 622 | /* Key3 */ |
| 623 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 624 | }; |
| 625 | static const unsigned char des3_2key_subkeys[2][8] = { |
| 626 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 627 | /* K1 */ |
| 628 | 0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 629 | }, |
| 630 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 631 | /* K2 */ |
| 632 | 0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 633 | } |
| 634 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 635 | static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] |
| 636 | = { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 637 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 638 | /* Sample #1 */ |
| 639 | 0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 640 | }, |
| 641 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 642 | /* Sample #2 */ |
| 643 | 0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 644 | }, |
| 645 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 646 | /* Sample #3 */ |
| 647 | 0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 648 | }, |
| 649 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 650 | /* Sample #4 */ |
| 651 | 0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 652 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 653 | }; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 654 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 655 | /* CMAC-TDES (Generation) - 3 Key Test Data */ |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 656 | static const unsigned char des3_3key_key[24] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 657 | /* Key1 */ |
| 658 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef, |
| 659 | /* Key2 */ |
| 660 | 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, |
| 661 | /* Key3 */ |
| 662 | 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 663 | }; |
| 664 | static const unsigned char des3_3key_subkeys[2][8] = { |
| 665 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 666 | /* K1 */ |
| 667 | 0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0 |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 668 | }, |
| 669 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 670 | /* K2 */ |
| 671 | 0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 672 | } |
| 673 | }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 674 | static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] |
| 675 | = { |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 676 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 677 | /* Sample #1 */ |
| 678 | 0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 679 | }, |
| 680 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 681 | /* Sample #2 */ |
| 682 | 0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 683 | }, |
| 684 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 685 | /* Sample #3 */ |
| 686 | 0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 687 | }, |
| 688 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 689 | /* Sample #4 */ |
| 690 | 0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5 |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 691 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 692 | }; |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 693 | |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 694 | #endif /* MBEDTLS_DES_C */ |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 695 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 696 | #if defined(MBEDTLS_AES_C) |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 697 | /* AES AES-CMAC-PRF-128 Test Data */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 698 | static const unsigned char PRFK[] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 699 | /* Key */ |
| 700 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 701 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 702 | 0xed, 0xcb |
| 703 | }; |
| 704 | |
| 705 | /* Sizes in bytes */ |
| 706 | static const size_t PRFKlen[NB_PRF_TESTS] = { |
| 707 | 18, |
| 708 | 16, |
| 709 | 10 |
| 710 | }; |
| 711 | |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 712 | /* Message */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 713 | static const unsigned char PRFM[] = { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 714 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 715 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
Manuel Pégourié-Gonnard | 3da5402 | 2016-01-13 11:00:47 +0000 | [diff] [blame] | 716 | 0x10, 0x11, 0x12, 0x13 |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | static const unsigned char PRFT[NB_PRF_TESTS][16] = { |
| 720 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 721 | 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b, |
| 722 | 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 723 | }, |
| 724 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 725 | 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52, |
| 726 | 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 727 | }, |
| 728 | { |
Janos Follath | cd13bd2 | 2016-12-13 11:51:04 +0000 | [diff] [blame] | 729 | 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee, |
| 730 | 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 731 | } |
| 732 | }; |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 733 | #endif /* MBEDTLS_AES_C */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | static int cmac_test_subkeys(int verbose, |
| 736 | const char *testname, |
| 737 | const unsigned char *key, |
| 738 | int keybits, |
| 739 | const unsigned char *subkeys, |
| 740 | mbedtls_cipher_type_t cipher_type, |
| 741 | int block_size, |
| 742 | int num_tests) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 743 | { |
Brian Murray | 2fab5c9 | 2016-12-15 18:51:13 -0800 | [diff] [blame] | 744 | int i, ret = 0; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 745 | mbedtls_cipher_context_t ctx; |
| 746 | const mbedtls_cipher_info_t *cipher_info; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 747 | unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
| 748 | unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 749 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 750 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 751 | if (cipher_info == NULL) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 752 | /* Failing at this point must be due to a build issue */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 753 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | for (i = 0; i < num_tests; i++) { |
| 757 | if (verbose != 0) { |
| 758 | mbedtls_printf(" %s CMAC subkey #%d: ", testname, i + 1); |
| 759 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 760 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 761 | mbedtls_cipher_init(&ctx); |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 762 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 763 | if ((ret = mbedtls_cipher_setup(&ctx, cipher_info)) != 0) { |
| 764 | if (verbose != 0) { |
| 765 | mbedtls_printf("test execution failed\n"); |
| 766 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 767 | |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 768 | goto cleanup; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 771 | if ((ret = mbedtls_cipher_setkey(&ctx, key, keybits, |
| 772 | MBEDTLS_ENCRYPT)) != 0) { |
Steven Cooreman | 830d5af | 2021-01-08 18:01:46 +0100 | [diff] [blame] | 773 | /* When CMAC is implemented by an alternative implementation, or |
| 774 | * the underlying primitive itself is implemented alternatively, |
Steven Cooreman | c7da6a4 | 2021-01-29 11:09:50 +0100 | [diff] [blame] | 775 | * AES-192 may be unavailable. This should not cause the selftest |
| 776 | * function to fail. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 777 | if ((ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED || |
| 778 | ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) && |
| 779 | cipher_type == MBEDTLS_CIPHER_AES_192_ECB) { |
| 780 | if (verbose != 0) { |
| 781 | mbedtls_printf("skipped\n"); |
| 782 | } |
Steven Cooreman | 830d5af | 2021-01-08 18:01:46 +0100 | [diff] [blame] | 783 | goto next_test; |
| 784 | } |
| 785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 786 | if (verbose != 0) { |
| 787 | mbedtls_printf("test execution failed\n"); |
| 788 | } |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 789 | |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 790 | goto cleanup; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 793 | ret = cmac_generate_subkeys(&ctx, K1, K2); |
| 794 | if (ret != 0) { |
| 795 | if (verbose != 0) { |
| 796 | mbedtls_printf("failed\n"); |
| 797 | } |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 798 | |
| 799 | goto cleanup; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 800 | } |
| 801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 802 | if ((ret = memcmp(K1, subkeys, block_size)) != 0 || |
| 803 | (ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) { |
| 804 | if (verbose != 0) { |
| 805 | mbedtls_printf("failed\n"); |
| 806 | } |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 807 | |
| 808 | goto cleanup; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 811 | if (verbose != 0) { |
| 812 | mbedtls_printf("passed\n"); |
| 813 | } |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 814 | |
Steven Cooreman | 830d5af | 2021-01-08 18:01:46 +0100 | [diff] [blame] | 815 | next_test: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 816 | mbedtls_cipher_free(&ctx); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Gilles Peskine | df761d5 | 2018-03-01 22:18:14 +0100 | [diff] [blame] | 819 | ret = 0; |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 820 | goto exit; |
| 821 | |
| 822 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 823 | mbedtls_cipher_free(&ctx); |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 824 | |
Janos Follath | d444358 | 2016-10-12 10:00:42 +0100 | [diff] [blame] | 825 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 826 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 829 | static int cmac_test_wth_cipher(int verbose, |
| 830 | const char *testname, |
| 831 | const unsigned char *key, |
| 832 | int keybits, |
| 833 | const unsigned char *messages, |
| 834 | const unsigned int message_lengths[4], |
| 835 | const unsigned char *expected_result, |
| 836 | mbedtls_cipher_type_t cipher_type, |
| 837 | int block_size, |
| 838 | int num_tests) |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 839 | { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 840 | const mbedtls_cipher_info_t *cipher_info; |
Brian Murray | 2fab5c9 | 2016-12-15 18:51:13 -0800 | [diff] [blame] | 841 | int i, ret = 0; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 842 | unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX]; |
Brian Murray | 57863ad | 2016-05-19 16:38:36 -0700 | [diff] [blame] | 843 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 844 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 845 | if (cipher_info == NULL) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 846 | /* Failing at this point must be due to a build issue */ |
| 847 | ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 848 | goto exit; |
| 849 | } |
| 850 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 851 | for (i = 0; i < num_tests; i++) { |
| 852 | if (verbose != 0) { |
| 853 | mbedtls_printf(" %s CMAC #%d: ", testname, i + 1); |
| 854 | } |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 856 | if ((ret = mbedtls_cipher_cmac(cipher_info, key, keybits, messages, |
| 857 | message_lengths[i], output)) != 0) { |
Steven Cooreman | 830d5af | 2021-01-08 18:01:46 +0100 | [diff] [blame] | 858 | /* When CMAC is implemented by an alternative implementation, or |
| 859 | * the underlying primitive itself is implemented alternatively, |
Steven Cooreman | 146e7fc | 2021-02-15 13:42:35 +0100 | [diff] [blame] | 860 | * AES-192 and/or 3DES may be unavailable. This should not cause |
| 861 | * the selftest function to fail. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 862 | if ((ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED || |
| 863 | ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) && |
| 864 | (cipher_type == MBEDTLS_CIPHER_AES_192_ECB || |
| 865 | cipher_type == MBEDTLS_CIPHER_DES_EDE3_ECB)) { |
| 866 | if (verbose != 0) { |
| 867 | mbedtls_printf("skipped\n"); |
| 868 | } |
Steven Cooreman | 830d5af | 2021-01-08 18:01:46 +0100 | [diff] [blame] | 869 | continue; |
| 870 | } |
| 871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 872 | if (verbose != 0) { |
| 873 | mbedtls_printf("failed\n"); |
| 874 | } |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 875 | goto exit; |
| 876 | } |
Brian Murray | 9ce2e09 | 2016-05-24 22:46:43 -0700 | [diff] [blame] | 877 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 878 | if ((ret = memcmp(output, &expected_result[i * block_size], block_size)) != 0) { |
| 879 | if (verbose != 0) { |
| 880 | mbedtls_printf("failed\n"); |
| 881 | } |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 882 | goto exit; |
| 883 | } |
| 884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 885 | if (verbose != 0) { |
| 886 | mbedtls_printf("passed\n"); |
| 887 | } |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 888 | } |
Gilles Peskine | df761d5 | 2018-03-01 22:18:14 +0100 | [diff] [blame] | 889 | ret = 0; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 890 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 891 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 892 | return ret; |
Brian Murray | 00dc5f0 | 2016-05-19 14:23:50 -0700 | [diff] [blame] | 893 | } |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 894 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 895 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 896 | static int test_aes128_cmac_prf(int verbose) |
Brian Murray | 6a3c0d2 | 2016-05-20 18:25:43 -0700 | [diff] [blame] | 897 | { |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 898 | int i; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 899 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 900 | unsigned char output[MBEDTLS_AES_BLOCK_SIZE]; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 901 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 902 | for (i = 0; i < NB_PRF_TESTS; i++) { |
| 903 | mbedtls_printf(" AES CMAC 128 PRF #%d: ", i); |
| 904 | ret = mbedtls_aes_cmac_prf_128(PRFK, PRFKlen[i], PRFM, 20, output); |
| 905 | if (ret != 0 || |
| 906 | memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) { |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 908 | if (verbose != 0) { |
| 909 | mbedtls_printf("failed\n"); |
| 910 | } |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 911 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 912 | return ret; |
| 913 | } else if (verbose != 0) { |
| 914 | mbedtls_printf("passed\n"); |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 915 | } |
| 916 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 917 | return ret; |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 918 | } |
| 919 | #endif /* MBEDTLS_AES_C */ |
| 920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 921 | int mbedtls_cmac_self_test(int verbose) |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 922 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 923 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 924 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 925 | #if defined(MBEDTLS_AES_C) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 926 | /* AES-128 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 927 | if ((ret = cmac_test_subkeys(verbose, |
| 928 | "AES 128", |
| 929 | aes_128_key, |
| 930 | 128, |
| 931 | (const unsigned char *) aes_128_subkeys, |
| 932 | MBEDTLS_CIPHER_AES_128_ECB, |
| 933 | MBEDTLS_AES_BLOCK_SIZE, |
| 934 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 935 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 938 | if ((ret = cmac_test_wth_cipher(verbose, |
| 939 | "AES 128", |
| 940 | aes_128_key, |
| 941 | 128, |
| 942 | test_message, |
| 943 | aes_message_lengths, |
| 944 | (const unsigned char *) aes_128_expected_result, |
| 945 | MBEDTLS_CIPHER_AES_128_ECB, |
| 946 | MBEDTLS_AES_BLOCK_SIZE, |
| 947 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 948 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /* AES-192 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 952 | if ((ret = cmac_test_subkeys(verbose, |
| 953 | "AES 192", |
| 954 | aes_192_key, |
| 955 | 192, |
| 956 | (const unsigned char *) aes_192_subkeys, |
| 957 | MBEDTLS_CIPHER_AES_192_ECB, |
| 958 | MBEDTLS_AES_BLOCK_SIZE, |
| 959 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 960 | return ret; |
Brian Murray | 9044b02 | 2016-05-19 16:36:56 -0700 | [diff] [blame] | 961 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 963 | if ((ret = cmac_test_wth_cipher(verbose, |
| 964 | "AES 192", |
| 965 | aes_192_key, |
| 966 | 192, |
| 967 | test_message, |
| 968 | aes_message_lengths, |
| 969 | (const unsigned char *) aes_192_expected_result, |
| 970 | MBEDTLS_CIPHER_AES_192_ECB, |
| 971 | MBEDTLS_AES_BLOCK_SIZE, |
| 972 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 973 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* AES-256 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 977 | if ((ret = cmac_test_subkeys(verbose, |
| 978 | "AES 256", |
| 979 | aes_256_key, |
| 980 | 256, |
| 981 | (const unsigned char *) aes_256_subkeys, |
| 982 | MBEDTLS_CIPHER_AES_256_ECB, |
| 983 | MBEDTLS_AES_BLOCK_SIZE, |
| 984 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 985 | return ret; |
Brian Murray | 9044b02 | 2016-05-19 16:36:56 -0700 | [diff] [blame] | 986 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 988 | if ((ret = cmac_test_wth_cipher(verbose, |
| 989 | "AES 256", |
| 990 | aes_256_key, |
| 991 | 256, |
| 992 | test_message, |
| 993 | aes_message_lengths, |
| 994 | (const unsigned char *) aes_256_expected_result, |
| 995 | MBEDTLS_CIPHER_AES_256_ECB, |
| 996 | MBEDTLS_AES_BLOCK_SIZE, |
| 997 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 998 | return ret; |
Brian Murray | 9044b02 | 2016-05-19 16:36:56 -0700 | [diff] [blame] | 999 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1000 | #endif /* MBEDTLS_AES_C */ |
| 1001 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 1002 | #if defined(MBEDTLS_DES_C) |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 1003 | /* 3DES 2 key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1004 | if ((ret = cmac_test_subkeys(verbose, |
| 1005 | "3DES 2 key", |
| 1006 | des3_2key_key, |
| 1007 | 192, |
| 1008 | (const unsigned char *) des3_2key_subkeys, |
| 1009 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
| 1010 | MBEDTLS_DES3_BLOCK_SIZE, |
| 1011 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 1012 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1015 | if ((ret = cmac_test_wth_cipher(verbose, |
| 1016 | "3DES 2 key", |
| 1017 | des3_2key_key, |
| 1018 | 192, |
| 1019 | test_message, |
| 1020 | des3_message_lengths, |
| 1021 | (const unsigned char *) des3_2key_expected_result, |
| 1022 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
| 1023 | MBEDTLS_DES3_BLOCK_SIZE, |
| 1024 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 1025 | return ret; |
Brian Murray | 9044b02 | 2016-05-19 16:36:56 -0700 | [diff] [blame] | 1026 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1027 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 1028 | /* 3DES 3 key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1029 | if ((ret = cmac_test_subkeys(verbose, |
| 1030 | "3DES 3 key", |
| 1031 | des3_3key_key, |
| 1032 | 192, |
| 1033 | (const unsigned char *) des3_3key_subkeys, |
| 1034 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
| 1035 | MBEDTLS_DES3_BLOCK_SIZE, |
| 1036 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 1037 | return ret; |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1040 | if ((ret = cmac_test_wth_cipher(verbose, |
| 1041 | "3DES 3 key", |
| 1042 | des3_3key_key, |
| 1043 | 192, |
| 1044 | test_message, |
| 1045 | des3_message_lengths, |
| 1046 | (const unsigned char *) des3_3key_expected_result, |
| 1047 | MBEDTLS_CIPHER_DES_EDE3_ECB, |
| 1048 | MBEDTLS_DES3_BLOCK_SIZE, |
| 1049 | NB_CMAC_TESTS_PER_KEY)) != 0) { |
| 1050 | return ret; |
Brian Murray | 9044b02 | 2016-05-19 16:36:56 -0700 | [diff] [blame] | 1051 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1052 | #endif /* MBEDTLS_DES_C */ |
| 1053 | |
Simon Butcher | 69283e5 | 2016-10-06 12:49:58 +0100 | [diff] [blame] | 1054 | #if defined(MBEDTLS_AES_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1055 | if ((ret = test_aes128_cmac_prf(verbose)) != 0) { |
| 1056 | return ret; |
| 1057 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1058 | #endif /* MBEDTLS_AES_C */ |
Brian Murray | b0c3c43 | 2016-05-18 14:29:51 -0700 | [diff] [blame] | 1059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1060 | if (verbose != 0) { |
| 1061 | mbedtls_printf("\n"); |
| 1062 | } |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1063 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1064 | return 0; |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Brian Murray | 0f6af73 | 2016-05-19 15:59:23 -0700 | [diff] [blame] | 1067 | #endif /* MBEDTLS_SELF_TEST */ |
Robert Cragie | 3d23b1d | 2015-12-15 07:38:11 +0000 | [diff] [blame] | 1068 | |
| 1069 | #endif /* MBEDTLS_CMAC_C */ |