• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/poly1305.h"
3#include <stddef.h>
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_POLY1305_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE */
12void mbedtls_poly1305(data_t *key, data_t *expected_mac, data_t *src_str)
13{
14    unsigned char mac[16]; /* size set by the standard */
15    mbedtls_poly1305_context ctx;
16
17    memset(mac, 0x00, sizeof(mac));
18
19    /*
20     * Test the integrated API
21     */
22    TEST_ASSERT(mbedtls_poly1305_mac(key->x, src_str->x,
23                                     src_str->len, mac) == 0);
24
25    TEST_MEMORY_COMPARE(mac, expected_mac->len,
26                        expected_mac->x, expected_mac->len);
27
28    /*
29     * Test the streaming API
30     */
31    mbedtls_poly1305_init(&ctx);
32
33    TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
34
35    TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, src_str->len) == 0);
36
37    TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
38
39    TEST_MEMORY_COMPARE(mac, expected_mac->len,
40                        expected_mac->x, expected_mac->len);
41
42    /*
43     * Test the streaming API again, piecewise
44     */
45
46    /* Don't free/init the context, in order to test that starts() does the
47     * right thing. */
48    if (src_str->len >= 1) {
49        TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
50
51        TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0);
52        TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 1, src_str->len - 1) == 0);
53
54        TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
55
56        TEST_MEMORY_COMPARE(mac, expected_mac->len,
57                            expected_mac->x, expected_mac->len);
58    }
59
60    /*
61     * Again with more pieces
62     */
63    if (src_str->len >= 2) {
64        TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
65
66        TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0);
67        TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 1, 1) == 0);
68        TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 2, src_str->len - 2) == 0);
69
70        TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
71
72        TEST_MEMORY_COMPARE(mac, expected_mac->len,
73                            expected_mac->x, expected_mac->len);
74    }
75
76    mbedtls_poly1305_free(&ctx);
77}
78/* END_CASE */
79
80/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
81void poly1305_bad_params()
82{
83    unsigned char src[1];
84    unsigned char key[32];
85    unsigned char mac[16];
86    size_t src_len = sizeof(src);
87    mbedtls_poly1305_context ctx;
88
89    TEST_INVALID_PARAM(mbedtls_poly1305_init(NULL));
90    TEST_VALID_PARAM(mbedtls_poly1305_free(NULL));
91
92    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
93                           mbedtls_poly1305_starts(NULL, key));
94    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
95                           mbedtls_poly1305_starts(&ctx, NULL));
96
97    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
98                           mbedtls_poly1305_update(NULL, src, 0));
99    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
100                           mbedtls_poly1305_update(&ctx, NULL, src_len));
101
102    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
103                           mbedtls_poly1305_finish(NULL, mac));
104    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
105                           mbedtls_poly1305_finish(&ctx, NULL));
106
107    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
108                           mbedtls_poly1305_mac(NULL, src, 0, mac));
109    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
110                           mbedtls_poly1305_mac(key, NULL, src_len, mac));
111    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
112                           mbedtls_poly1305_mac(key, src, 0, NULL));
113
114exit:
115    return;
116}
117/* END_CASE */
118
119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
120void poly1305_selftest()
121{
122    TEST_ASSERT(mbedtls_poly1305_self_test(1) == 0);
123}
124/* END_CASE */
125