blob: 239568c342b1040cd7ec788200956fbdb8e6ec64 [file] [log] [blame]
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +01001/* BEGIN_HEADER */
2#include "block_cipher_internal.h"
3
4#define BLOCK_SIZE 16
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +01005
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é-Gonnard21718762023-11-10 11:21:17 +010018/* END_HEADER */
19
20/* BEGIN_DEPENDENCIES
21 * depends_on:MBEDTLS_BLOCK_CIPHER_C
22 * END_DEPENDENCIES
23 */
24
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010025/* BEGIN_CASE depends_on:VALID_CIPHER_ID */
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +010026void invalid()
27{
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010028 /* That size is valid for a key or an input/output block. */
29 unsigned char buf[16] = { 0 };
30
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +010031 mbedtls_block_cipher_context_t ctx;
32
33 mbedtls_block_cipher_init(&ctx);
34
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010035 /* Bad parameters to setup */
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +010036 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
37 mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_NONE));
Manuel Pégourié-Gonnard21718762023-11-10 11:21:17 +010038 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
39 mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_DES));
40
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010041 /* setkey() before successful setup() */
42 TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT,
43 mbedtls_block_cipher_setkey(&ctx, buf, 128));
44
Manuel Pégourié-Gonnard76fa16c2023-11-10 12:02:53 +010045 /* 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é-Gonnard3e0884f2023-11-10 11:52:10 +010053 /* Now properly setup the context */
Manuel Pégourié-Gonnard76fa16c2023-11-10 12:02:53 +010054 mbedtls_block_cipher_init(&ctx);
Manuel Pégourié-Gonnard3e0884f2023-11-10 11:52:10 +010055 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é-Gonnard21718762023-11-10 11:21:17 +010061exit:
62 mbedtls_block_cipher_free(&ctx);
63}
64/* END_CASE */
Manuel Pégourié-Gonnard76fa16c2023-11-10 12:02:53 +010065
66/* BEGIN_CASE */
67void 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
91exit:
92 mbedtls_block_cipher_free(&ctx);
93}
94/* END_CASE */