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