1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci#include "block_cipher_internal.h"
3a8e1175bSopenharmony_ci
4a8e1175bSopenharmony_ci#define BLOCK_SIZE 16
5a8e1175bSopenharmony_ci
6a8e1175bSopenharmony_ci#if defined(MBEDTLS_AES_C)
7a8e1175bSopenharmony_ci#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_AES
8a8e1175bSopenharmony_ci#define BADKEY_ERROR MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
9a8e1175bSopenharmony_ci#elif defined(MBEDTLS_ARIA_C)
10a8e1175bSopenharmony_ci#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_ARIA
11a8e1175bSopenharmony_ci#define BADKEY_ERROR MBEDTLS_ERR_ARIA_BAD_INPUT_DATA
12a8e1175bSopenharmony_ci#elif defined(MBEDTLS_CAMELLIA_C)
13a8e1175bSopenharmony_ci#define VALID_CIPHER_ID MBEDTLS_CIPHER_ID_CAMELLIA
14a8e1175bSopenharmony_ci#define BADKEY_ERROR MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA
15a8e1175bSopenharmony_ci#else
16a8e1175bSopenharmony_ci#undef VALID_CIPHER_ID
17a8e1175bSopenharmony_ci#endif
18a8e1175bSopenharmony_ci/* END_HEADER */
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES
21a8e1175bSopenharmony_ci * depends_on:MBEDTLS_BLOCK_CIPHER_C
22a8e1175bSopenharmony_ci * END_DEPENDENCIES
23a8e1175bSopenharmony_ci */
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:VALID_CIPHER_ID */
26a8e1175bSopenharmony_civoid invalid()
27a8e1175bSopenharmony_ci{
28a8e1175bSopenharmony_ci    /* That size is valid for a key or an input/output block. */
29a8e1175bSopenharmony_ci    unsigned char buf[16] = { 0 };
30a8e1175bSopenharmony_ci
31a8e1175bSopenharmony_ci    mbedtls_block_cipher_context_t ctx;
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci    mbedtls_block_cipher_init(&ctx);
34a8e1175bSopenharmony_ci
35a8e1175bSopenharmony_ci    /* Bad parameters to setup */
36a8e1175bSopenharmony_ci    TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
37a8e1175bSopenharmony_ci               mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_NONE));
38a8e1175bSopenharmony_ci    TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
39a8e1175bSopenharmony_ci               mbedtls_block_cipher_setup(&ctx, MBEDTLS_CIPHER_ID_DES));
40a8e1175bSopenharmony_ci
41a8e1175bSopenharmony_ci    /* setkey() before successful setup() */
42a8e1175bSopenharmony_ci    TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT,
43a8e1175bSopenharmony_ci               mbedtls_block_cipher_setkey(&ctx, buf, 128));
44a8e1175bSopenharmony_ci
45a8e1175bSopenharmony_ci    /* encrypt() before successful setup() */
46a8e1175bSopenharmony_ci    TEST_EQUAL(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT,
47a8e1175bSopenharmony_ci               mbedtls_block_cipher_encrypt(&ctx, buf, buf));
48a8e1175bSopenharmony_ci
49a8e1175bSopenharmony_ci    /* free() before successful setup()
50a8e1175bSopenharmony_ci     * No return value to check, but shouldn't cause memory errors. */
51a8e1175bSopenharmony_ci    mbedtls_block_cipher_free(&ctx);
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci    /* Now properly setup the context */
54a8e1175bSopenharmony_ci    mbedtls_block_cipher_init(&ctx);
55a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, VALID_CIPHER_ID));
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci    /* Bad parameters to setkey() */
58a8e1175bSopenharmony_ci    TEST_EQUAL(BADKEY_ERROR,
59a8e1175bSopenharmony_ci               mbedtls_block_cipher_setkey(&ctx, buf, 42));
60a8e1175bSopenharmony_ci
61a8e1175bSopenharmony_ciexit:
62a8e1175bSopenharmony_ci    mbedtls_block_cipher_free(&ctx);
63a8e1175bSopenharmony_ci}
64a8e1175bSopenharmony_ci/* END_CASE */
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ci/* BEGIN_CASE */
67a8e1175bSopenharmony_civoid test_vec(int cipher_id_arg, data_t *key, data_t *input, data_t *outref)
68a8e1175bSopenharmony_ci{
69a8e1175bSopenharmony_ci    mbedtls_block_cipher_context_t ctx;
70a8e1175bSopenharmony_ci    mbedtls_cipher_id_t cipher_id = cipher_id_arg;
71a8e1175bSopenharmony_ci    unsigned char output[BLOCK_SIZE];
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci    mbedtls_block_cipher_init(&ctx);
74a8e1175bSopenharmony_ci
75a8e1175bSopenharmony_ci    memset(output, 0x00, sizeof(output));
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, cipher_id));
78a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_setkey(&ctx, key->x, 8 * key->len));
79a8e1175bSopenharmony_ci
80a8e1175bSopenharmony_ci    /* Encrypt with input != output */
81a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, input->x, output));
82a8e1175bSopenharmony_ci    ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len);
83a8e1175bSopenharmony_ci
84a8e1175bSopenharmony_ci    /* Encrypt with input == output.
85a8e1175bSopenharmony_ci     * (Also, encrypting again ensures the previous call to encrypt()
86a8e1175bSopenharmony_ci     * did not change the state of the context.) */
87a8e1175bSopenharmony_ci    memcpy(output, input->x, BLOCK_SIZE);
88a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_encrypt(&ctx, output, output));
89a8e1175bSopenharmony_ci    ASSERT_COMPARE(output, BLOCK_SIZE, outref->x, outref->len);
90a8e1175bSopenharmony_ci
91a8e1175bSopenharmony_ciexit:
92a8e1175bSopenharmony_ci    mbedtls_block_cipher_free(&ctx);
93a8e1175bSopenharmony_ci}
94a8e1175bSopenharmony_ci/* END_CASE */
95a8e1175bSopenharmony_ci
96a8e1175bSopenharmony_ci/* BEGIN_CASE */
97a8e1175bSopenharmony_civoid block_cipher_psa_dynamic_dispatch(int cipher_type, int pre_psa_ret, int post_psa_engine)
98a8e1175bSopenharmony_ci{
99a8e1175bSopenharmony_ci    mbedtls_block_cipher_context_t ctx;
100a8e1175bSopenharmony_ci    (void) post_psa_engine;
101a8e1175bSopenharmony_ci
102a8e1175bSopenharmony_ci    /* Intentionally no PSA init here! (Will be done later.) */
103a8e1175bSopenharmony_ci
104a8e1175bSopenharmony_ci    mbedtls_block_cipher_init(&ctx);
105a8e1175bSopenharmony_ci
106a8e1175bSopenharmony_ci    /* Before PSA crypto init */
107a8e1175bSopenharmony_ci    TEST_EQUAL(pre_psa_ret, mbedtls_block_cipher_setup(&ctx, cipher_type));
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
110a8e1175bSopenharmony_ci    TEST_EQUAL(ctx.engine, MBEDTLS_BLOCK_CIPHER_ENGINE_LEGACY);
111a8e1175bSopenharmony_ci#endif
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci    mbedtls_block_cipher_free(&ctx);
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci    /* Now initilize PSA Crypto */
116a8e1175bSopenharmony_ci    BLOCK_CIPHER_PSA_INIT();
117a8e1175bSopenharmony_ci
118a8e1175bSopenharmony_ci    mbedtls_block_cipher_init(&ctx);
119a8e1175bSopenharmony_ci    /* After PSA Crypto init */
120a8e1175bSopenharmony_ci    TEST_EQUAL(0, mbedtls_block_cipher_setup(&ctx, cipher_type));
121a8e1175bSopenharmony_ci#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
122a8e1175bSopenharmony_ci    TEST_EQUAL(ctx.engine, post_psa_engine);
123a8e1175bSopenharmony_ci#endif
124a8e1175bSopenharmony_ci
125a8e1175bSopenharmony_ciexit:
126a8e1175bSopenharmony_ci    mbedtls_block_cipher_free(&ctx);
127a8e1175bSopenharmony_ci    BLOCK_CIPHER_PSA_DONE();
128a8e1175bSopenharmony_ci}
129a8e1175bSopenharmony_ci/* END_CASE */
130