Tom Rini | 0344c60 | 2024-10-08 13:56:50 -0600 | [diff] [blame^] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/cipher.h" |
| 3 | #include "mbedtls/cmac.h" |
| 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_DEPENDENCIES |
| 7 | * depends_on:MBEDTLS_CMAC_C |
| 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
| 11 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 12 | void mbedtls_cmac_self_test() |
| 13 | { |
| 14 | TEST_ASSERT(mbedtls_cmac_self_test(1) == 0); |
| 15 | } |
| 16 | /* END_CASE */ |
| 17 | |
| 18 | /* BEGIN_CASE */ |
| 19 | void mbedtls_cmac_null_args() |
| 20 | { |
| 21 | mbedtls_cipher_context_t ctx; |
| 22 | const mbedtls_cipher_info_t *cipher_info; |
| 23 | unsigned char test_key[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
| 24 | unsigned char test_data[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
| 25 | unsigned char test_output[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
| 26 | |
| 27 | mbedtls_cipher_init(&ctx); |
| 28 | |
| 29 | /* Test NULL cipher info */ |
| 30 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, test_data, 16) == |
| 31 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 32 | |
| 33 | cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB); |
| 34 | TEST_ASSERT(mbedtls_cipher_setup(&ctx, cipher_info) == 0); |
| 35 | |
| 36 | TEST_ASSERT(mbedtls_cipher_cmac_starts(NULL, test_key, 128) == |
| 37 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 38 | |
| 39 | TEST_ASSERT(mbedtls_cipher_cmac_starts(&ctx, NULL, 128) == |
| 40 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 41 | |
| 42 | TEST_ASSERT(mbedtls_cipher_cmac_update(NULL, test_data, 16) == |
| 43 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 44 | |
| 45 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, NULL, 16) == |
| 46 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 47 | |
| 48 | TEST_ASSERT(mbedtls_cipher_cmac_finish(NULL, test_output) == |
| 49 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 50 | |
| 51 | TEST_ASSERT(mbedtls_cipher_cmac_finish(&ctx, NULL) == |
| 52 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 53 | |
| 54 | TEST_ASSERT(mbedtls_cipher_cmac_reset(NULL) == |
| 55 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 56 | |
| 57 | TEST_ASSERT(mbedtls_cipher_cmac(NULL, |
| 58 | test_key, 128, |
| 59 | test_data, 16, |
| 60 | test_output) == |
| 61 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 62 | |
| 63 | TEST_ASSERT(mbedtls_cipher_cmac(cipher_info, |
| 64 | NULL, 128, |
| 65 | test_data, 16, |
| 66 | test_output) == |
| 67 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 68 | |
| 69 | TEST_ASSERT(mbedtls_cipher_cmac(cipher_info, |
| 70 | test_key, 128, |
| 71 | NULL, 16, |
| 72 | test_output) == |
| 73 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 74 | |
| 75 | TEST_ASSERT(mbedtls_cipher_cmac(cipher_info, |
| 76 | test_key, 128, |
| 77 | test_data, 16, |
| 78 | NULL) == |
| 79 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 80 | #if defined(MBEDTLS_AES_C) |
| 81 | TEST_ASSERT(mbedtls_aes_cmac_prf_128(NULL, 16, |
| 82 | test_data, 16, |
| 83 | test_output) == |
| 84 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 85 | |
| 86 | TEST_ASSERT(mbedtls_aes_cmac_prf_128(test_key, 16, |
| 87 | NULL, 16, |
| 88 | test_output) == |
| 89 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 90 | |
| 91 | TEST_ASSERT(mbedtls_aes_cmac_prf_128(test_key, 16, |
| 92 | test_data, 16, |
| 93 | NULL) == |
| 94 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| 95 | #endif |
| 96 | exit: |
| 97 | mbedtls_cipher_free(&ctx); |
| 98 | } |
| 99 | /* END_CASE */ |
| 100 | |
| 101 | /* BEGIN_CASE */ |
| 102 | void mbedtls_cmac_setkey(int cipher_type, int key_size, int result) |
| 103 | { |
| 104 | const mbedtls_cipher_info_t *cipher_info; |
| 105 | unsigned char key[32]; |
| 106 | unsigned char buf[16]; |
| 107 | unsigned char tmp[16]; |
| 108 | |
| 109 | memset(key, 0x2A, sizeof(key)); |
| 110 | TEST_ASSERT((unsigned) key_size <= 8 * sizeof(key)); |
| 111 | |
| 112 | TEST_ASSERT((cipher_info = mbedtls_cipher_info_from_type(cipher_type)) |
| 113 | != NULL); |
| 114 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 115 | TEST_LE_U(mbedtls_cipher_info_get_block_size(cipher_info), |
| 116 | MBEDTLS_CIPHER_BLKSIZE_MAX); |
| 117 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 118 | TEST_LE_U(mbedtls_cipher_info_get_block_size(cipher_info), |
| 119 | MBEDTLS_CMAC_MAX_BLOCK_SIZE); |
| 120 | |
| 121 | memset(buf, 0x2A, sizeof(buf)); |
| 122 | TEST_ASSERT((result == mbedtls_cipher_cmac(cipher_info, key, key_size, |
| 123 | buf, 16, tmp)) != 0); |
| 124 | } |
| 125 | /* END_CASE */ |
| 126 | |
| 127 | /* BEGIN_CASE */ |
| 128 | void mbedtls_cmac_multiple_blocks(int cipher_type, data_t *key, |
| 129 | int keybits, int block_size, |
| 130 | data_t *block1, int block1_len, |
| 131 | data_t *block2, int block2_len, |
| 132 | data_t *block3, int block3_len, |
| 133 | data_t *block4, int block4_len, |
| 134 | data_t *expected_result) |
| 135 | { |
| 136 | const mbedtls_cipher_info_t *cipher_info; |
| 137 | mbedtls_cipher_context_t ctx; |
| 138 | unsigned char output[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
| 139 | |
| 140 | /* Convert the test parameters to binary data */ |
| 141 | |
| 142 | mbedtls_cipher_init(&ctx); |
| 143 | |
| 144 | /* Validate the test inputs */ |
| 145 | TEST_ASSERT(block1_len <= 100); |
| 146 | TEST_ASSERT(block2_len <= 100); |
| 147 | TEST_ASSERT(block3_len <= 100); |
| 148 | TEST_ASSERT(block4_len <= 100); |
| 149 | |
| 150 | /* Set up */ |
| 151 | TEST_ASSERT((cipher_info = mbedtls_cipher_info_from_type(cipher_type)) |
| 152 | != NULL); |
| 153 | |
| 154 | TEST_ASSERT(mbedtls_cipher_setup(&ctx, cipher_info) == 0); |
| 155 | |
| 156 | TEST_ASSERT(mbedtls_cipher_cmac_starts(&ctx, |
| 157 | (const unsigned char *) key->x, |
| 158 | keybits) == 0); |
| 159 | |
| 160 | /* Multiple partial and complete blocks. A negative length means skip the |
| 161 | * update operation */ |
| 162 | if (block1_len >= 0) { |
| 163 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 164 | (unsigned char *) block1->x, |
| 165 | block1_len) == 0); |
| 166 | } |
| 167 | |
| 168 | if (block2_len >= 0) { |
| 169 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 170 | (unsigned char *) block2->x, |
| 171 | block2_len) == 0); |
| 172 | } |
| 173 | |
| 174 | if (block3_len >= 0) { |
| 175 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 176 | (unsigned char *) block3->x, |
| 177 | block3_len) == 0); |
| 178 | } |
| 179 | |
| 180 | if (block4_len >= 0) { |
| 181 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 182 | (unsigned char *) block4->x, |
| 183 | block4_len) == 0); |
| 184 | } |
| 185 | |
| 186 | TEST_ASSERT(mbedtls_cipher_cmac_finish(&ctx, output) == 0); |
| 187 | |
| 188 | TEST_ASSERT(memcmp(output, expected_result->x, block_size) == 0); |
| 189 | |
| 190 | exit: |
| 191 | mbedtls_cipher_free(&ctx); |
| 192 | } |
| 193 | /* END_CASE */ |
| 194 | |
| 195 | /* BEGIN_CASE */ |
| 196 | void mbedtls_cmac_multiple_operations_same_key(int cipher_type, |
| 197 | data_t *key, int keybits, |
| 198 | int block_size, |
| 199 | data_t *block_a1, |
| 200 | int block_a1_len, |
| 201 | data_t *block_a2, |
| 202 | int block_a2_len, |
| 203 | data_t *block_a3, |
| 204 | int block_a3_len, |
| 205 | data_t *expected_result_a, |
| 206 | data_t *block_b1, |
| 207 | int block_b1_len, |
| 208 | data_t *block_b2, |
| 209 | int block_b2_len, |
| 210 | data_t *block_b3, |
| 211 | int block_b3_len, |
| 212 | data_t *expected_result_b |
| 213 | ) |
| 214 | { |
| 215 | const mbedtls_cipher_info_t *cipher_info; |
| 216 | mbedtls_cipher_context_t ctx; |
| 217 | unsigned char output[MBEDTLS_CMAC_MAX_BLOCK_SIZE]; |
| 218 | |
| 219 | /* Convert the test parameters to binary data */ |
| 220 | |
| 221 | |
| 222 | |
| 223 | mbedtls_cipher_init(&ctx); |
| 224 | |
| 225 | /* Validate the test inputs */ |
| 226 | TEST_ASSERT(block_a1_len <= 100); |
| 227 | TEST_ASSERT(block_a2_len <= 100); |
| 228 | TEST_ASSERT(block_a3_len <= 100); |
| 229 | |
| 230 | TEST_ASSERT(block_b1_len <= 100); |
| 231 | TEST_ASSERT(block_b2_len <= 100); |
| 232 | TEST_ASSERT(block_b3_len <= 100); |
| 233 | |
| 234 | /* Set up */ |
| 235 | TEST_ASSERT((cipher_info = mbedtls_cipher_info_from_type(cipher_type)) |
| 236 | != NULL); |
| 237 | |
| 238 | TEST_ASSERT(mbedtls_cipher_setup(&ctx, cipher_info) == 0); |
| 239 | |
| 240 | TEST_ASSERT(mbedtls_cipher_cmac_starts(&ctx, |
| 241 | (const unsigned char *) key->x, |
| 242 | keybits) == 0); |
| 243 | |
| 244 | /* Sequence A */ |
| 245 | |
| 246 | /* Multiple partial and complete blocks. A negative length means skip the |
| 247 | * update operation */ |
| 248 | if (block_a1_len >= 0) { |
| 249 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 250 | (unsigned char *) block_a1->x, |
| 251 | block_a1_len) == 0); |
| 252 | } |
| 253 | |
| 254 | if (block_a2_len >= 0) { |
| 255 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 256 | (unsigned char *) block_a2->x, |
| 257 | block_a2_len) == 0); |
| 258 | } |
| 259 | |
| 260 | if (block_a3_len >= 0) { |
| 261 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 262 | (unsigned char *) block_a3->x, |
| 263 | block_a3_len) == 0); |
| 264 | } |
| 265 | |
| 266 | TEST_ASSERT(mbedtls_cipher_cmac_finish(&ctx, output) == 0); |
| 267 | |
| 268 | TEST_ASSERT(memcmp(output, expected_result_a->x, block_size) == 0); |
| 269 | |
| 270 | TEST_ASSERT(mbedtls_cipher_cmac_reset(&ctx) == 0); |
| 271 | |
| 272 | /* Sequence B */ |
| 273 | |
| 274 | /* Multiple partial and complete blocks. A negative length means skip the |
| 275 | * update operation */ |
| 276 | if (block_b1_len >= 0) { |
| 277 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 278 | (unsigned char *) block_b1->x, |
| 279 | block_b1_len) == 0); |
| 280 | } |
| 281 | |
| 282 | if (block_b2_len >= 0) { |
| 283 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 284 | (unsigned char *) block_b2->x, |
| 285 | block_b2_len) == 0); |
| 286 | } |
| 287 | |
| 288 | if (block_b3_len >= 0) { |
| 289 | TEST_ASSERT(mbedtls_cipher_cmac_update(&ctx, |
| 290 | (unsigned char *) block_b3->x, |
| 291 | block_b3_len) == 0); |
| 292 | } |
| 293 | |
| 294 | TEST_ASSERT(mbedtls_cipher_cmac_finish(&ctx, output) == 0); |
| 295 | |
| 296 | TEST_ASSERT(memcmp(output, expected_result_b->x, block_size) == 0); |
| 297 | |
| 298 | exit: |
| 299 | mbedtls_cipher_free(&ctx); |
| 300 | } |
| 301 | /* END_CASE */ |