Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "block_cipher_internal.h" |
| 3 | |
| 4 | #define BLOCK_SIZE 16 |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 5 | |
| 6 | #if defined(MBEDTLS_AES_C) |
| 7 | #define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_AES |
| 8 | #define BADKEY_ERROR MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 9 | #elif defined(MBEDTLS_ARIA_C) |
| 10 | #define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_ARIA |
| 11 | #define BADKEY_ERROR MBEDTLS_ERR_ARIA_BAD_INPUT_DATA |
| 12 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 13 | #define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_CAMELLIA |
| 14 | #define BADKEY_ERROR MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA |
| 15 | #else |
| 16 | #undef VALID_CIPHER_ID |
| 17 | #endif |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 18 | /* END_HEADER */ |
| 19 | |
| 20 | /* BEGIN_DEPENDENCIES |
| 21 | * depends_on:MBEDTLS_BLOCK_CIPHER_C |
| 22 | * END_DEPENDENCIES |
| 23 | */ |
| 24 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 25 | /* BEGIN_CASE depends_on:VALID_CIPHER_ID */ |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 26 | void invalid() |
| 27 | { |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 28 | /* That size is valid for a key or an input/output block. */ |
| 29 | unsigned char buf[16] = { 0 }; |
| 30 | |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 31 | mbedtls_block_cipher_context_t ctx; |
| 32 | |
| 33 | mbedtls_block_cipher_init(&ctx); |
| 34 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 35 | /* Bad parameters to setup */ |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 36 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 37 | mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_NONE)); |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 38 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 39 | mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_DES)); |
| 40 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 41 | /* setkey() before successful setup() */ |
| 42 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT, |
| 43 | mbedtls_block_cipher_setkey(&ctx, buf, 128)); |
| 44 | |
Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 45 | /* encrypt() before successful setup() */ |
| 46 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT, |
| 47 | mbedtls_block_cipher_encrypt(&ctx, buf, buf)); |
| 48 | |
| 49 | /* free() before successful setup() |
| 50 | * No return value to check, but shouldn't cause memory errors. */ |
| 51 | mbedtls_block_cipher_free(&ctx); |
| 52 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 53 | /* Now properly setup the context */ |
Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 54 | mbedtls_block_cipher_init(&ctx); |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 55 | TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, VALID_CIPHER_ID)); |
| 56 | |
| 57 | /* Bad parameters to setkey() */ |
| 58 | TEST_EQUAL(BADKEY_ERROR, |
| 59 | mbedtls_block_cipher_setkey(&ctx, buf, 42)); |
| 60 | |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 61 | exit: |
| 62 | mbedtls_block_cipher_free(&ctx); |
| 63 | } |
| 64 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 65 | |
| 66 | /* BEGIN_CASE */ |
| 67 | void test_vec(int cipher_id_arg, data_t *key, data_t *input, data_t *outref) |
| 68 | { |
| 69 | mbedtls_block_cipher_context_t ctx; |
| 70 | mbedtls_cipher_id_t cipher_id = cipher_id_arg; |
| 71 | unsigned char output[BLOCK_SIZE]; |
| 72 | |
| 73 | mbedtls_block_cipher_init(&ctx); |
| 74 | |
| 75 | memset(output, 0x00, sizeof(output)); |
| 76 | |
| 77 | TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, cipher_id)); |
| 78 | TEST_EQUAL(0, mbedtls_block_cipher_setkey(&ctx, key->x, 8 * key->len)); |
| 79 | |
| 80 | /* Encrypt with input != output */ |
| 81 | TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, input->x, output)); |
| 82 | ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len); |
| 83 | |
| 84 | /* Encrypt with input == output. |
| 85 | * (Also, encrypting again ensures the previous call to encrypt() |
| 86 | * did not change the state of the context.) */ |
| 87 | memcpy(output, input->x, BLOCK_SIZE); |
| 88 | TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, output, output)); |
| 89 | ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len); |
| 90 | |
| 91 | exit: |
| 92 | mbedtls_block_cipher_free(&ctx); |
| 93 | } |
| 94 | /* END_CASE */ |