• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/chacha20.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_CHACHA20_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void chacha20_crypt(data_t *key_str,
12                    data_t *nonce_str,
13                    int counter,
14                    data_t *src_str,
15                    data_t *expected_output_str)
16{
17    unsigned char output[375];
18    mbedtls_chacha20_context ctx;
19
20    memset(output, 0x00, sizeof(output));
21
22    TEST_ASSERT(src_str->len   == expected_output_str->len);
23    TEST_ASSERT(key_str->len   == 32U);
24    TEST_ASSERT(nonce_str->len == 12U);
25
26    /*
27     * Test the integrated API
28     */
29    TEST_ASSERT(mbedtls_chacha20_crypt(key_str->x, nonce_str->x, counter, src_str->len, src_str->x,
30                                       output) == 0);
31
32    TEST_MEMORY_COMPARE(output, expected_output_str->len,
33                        expected_output_str->x, expected_output_str->len);
34
35    /*
36     * Test the streaming API
37     */
38    mbedtls_chacha20_init(&ctx);
39
40    TEST_ASSERT(mbedtls_chacha20_setkey(&ctx, key_str->x) == 0);
41
42    TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0);
43
44    memset(output, 0x00, sizeof(output));
45    TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len, src_str->x, output) == 0);
46
47    TEST_MEMORY_COMPARE(output, expected_output_str->len,
48                        expected_output_str->x, expected_output_str->len);
49
50    /*
51     * Test the streaming API again, piecewise
52     */
53
54    /* Don't free/init the context nor set the key again,
55     * in order to test that starts() does the right thing. */
56    TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0);
57
58    memset(output, 0x00, sizeof(output));
59    TEST_ASSERT(mbedtls_chacha20_update(&ctx, 1, src_str->x, output) == 0);
60    TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len - 1,
61                                        src_str->x + 1, output + 1) == 0);
62
63    TEST_MEMORY_COMPARE(output, expected_output_str->len,
64                        expected_output_str->x, expected_output_str->len);
65
66    mbedtls_chacha20_free(&ctx);
67}
68/* END_CASE */
69
70/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
71void chacha20_bad_params()
72{
73    unsigned char key[32];
74    unsigned char nonce[12];
75    unsigned char src[1];
76    unsigned char dst[1];
77    uint32_t counter = 0;
78    size_t len = sizeof(src);
79    mbedtls_chacha20_context ctx;
80
81    TEST_INVALID_PARAM(mbedtls_chacha20_init(NULL));
82    TEST_VALID_PARAM(mbedtls_chacha20_free(NULL));
83
84    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
85                           mbedtls_chacha20_setkey(NULL, key));
86    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
87                           mbedtls_chacha20_setkey(&ctx, NULL));
88
89    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
90                           mbedtls_chacha20_starts(NULL, nonce, counter));
91    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
92                           mbedtls_chacha20_starts(&ctx, NULL, counter));
93
94    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
95                           mbedtls_chacha20_update(NULL, 0, src, dst));
96    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
97                           mbedtls_chacha20_update(&ctx, len, NULL, dst));
98    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
99                           mbedtls_chacha20_update(&ctx, len, src, NULL));
100
101    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
102                           mbedtls_chacha20_crypt(NULL, nonce, counter, 0, src, dst));
103    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
104                           mbedtls_chacha20_crypt(key, NULL, counter, 0, src, dst));
105    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
106                           mbedtls_chacha20_crypt(key, nonce, counter, len, NULL, dst));
107    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
108                           mbedtls_chacha20_crypt(key, nonce, counter, len, src, NULL));
109
110exit:
111    return;
112
113}
114/* END_CASE */
115
116/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
117void chacha20_self_test()
118{
119    TEST_ASSERT(mbedtls_chacha20_self_test(1) == 0);
120}
121/* END_CASE */
122