1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci#include "mbedtls/chacha20.h"
3a8e1175bSopenharmony_ci/* END_HEADER */
4a8e1175bSopenharmony_ci
5a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES
6a8e1175bSopenharmony_ci * depends_on:MBEDTLS_CHACHA20_C
7a8e1175bSopenharmony_ci * END_DEPENDENCIES
8a8e1175bSopenharmony_ci */
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci/* BEGIN_CASE */
11a8e1175bSopenharmony_civoid chacha20_crypt(data_t *key_str,
12a8e1175bSopenharmony_ci                    data_t *nonce_str,
13a8e1175bSopenharmony_ci                    int counter,
14a8e1175bSopenharmony_ci                    data_t *src_str,
15a8e1175bSopenharmony_ci                    data_t *expected_output_str)
16a8e1175bSopenharmony_ci{
17a8e1175bSopenharmony_ci    unsigned char output[375];
18a8e1175bSopenharmony_ci    mbedtls_chacha20_context ctx;
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci    memset(output, 0x00, sizeof(output));
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci    TEST_ASSERT(src_str->len   == expected_output_str->len);
23a8e1175bSopenharmony_ci    TEST_ASSERT(key_str->len   == 32U);
24a8e1175bSopenharmony_ci    TEST_ASSERT(nonce_str->len == 12U);
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_ci    /*
27a8e1175bSopenharmony_ci     * Test the integrated API
28a8e1175bSopenharmony_ci     */
29a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_crypt(key_str->x, nonce_str->x, counter, src_str->len, src_str->x,
30a8e1175bSopenharmony_ci                                       output) == 0);
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(output, expected_output_str->len,
33a8e1175bSopenharmony_ci                        expected_output_str->x, expected_output_str->len);
34a8e1175bSopenharmony_ci
35a8e1175bSopenharmony_ci    /*
36a8e1175bSopenharmony_ci     * Test the streaming API
37a8e1175bSopenharmony_ci     */
38a8e1175bSopenharmony_ci    mbedtls_chacha20_init(&ctx);
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_setkey(&ctx, key_str->x) == 0);
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0);
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci    memset(output, 0x00, sizeof(output));
45a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len, src_str->x, output) == 0);
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(output, expected_output_str->len,
48a8e1175bSopenharmony_ci                        expected_output_str->x, expected_output_str->len);
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci    /*
51a8e1175bSopenharmony_ci     * Test the streaming API again, piecewise
52a8e1175bSopenharmony_ci     */
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_ci    /* Don't free/init the context nor set the key again,
55a8e1175bSopenharmony_ci     * in order to test that starts() does the right thing. */
56a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0);
57a8e1175bSopenharmony_ci
58a8e1175bSopenharmony_ci    memset(output, 0x00, sizeof(output));
59a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_update(&ctx, 1, src_str->x, output) == 0);
60a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len - 1,
61a8e1175bSopenharmony_ci                                        src_str->x + 1, output + 1) == 0);
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci    TEST_MEMORY_COMPARE(output, expected_output_str->len,
64a8e1175bSopenharmony_ci                        expected_output_str->x, expected_output_str->len);
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ci    mbedtls_chacha20_free(&ctx);
67a8e1175bSopenharmony_ci}
68a8e1175bSopenharmony_ci/* END_CASE */
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
71a8e1175bSopenharmony_civoid chacha20_self_test()
72a8e1175bSopenharmony_ci{
73a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_chacha20_self_test(1) == 0);
74a8e1175bSopenharmony_ci}
75a8e1175bSopenharmony_ci/* END_CASE */
76