blob: fffa89f6fd7e377310e33356cdd1d6f3361fd37f [file] [log] [blame]
Daniel Kingadc32c02016-05-16 18:25:45 -03001/* BEGIN_HEADER */
2#include "mbedtls/poly1305.h"
3#include <stddef.h>
4/* END_HEADER */
5
Manuel Pégourié-Gonnarda8fa8b82018-05-10 10:12:36 +02006/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_POLY1305_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010012void mbedtls_poly1305(data_t *key, data_t *expected_mac, data_t *src_str)
Daniel Kingadc32c02016-05-16 18:25:45 -030013{
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020014 unsigned char mac[16]; /* size set by the standard */
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020015 mbedtls_poly1305_context ctx;
Daniel Kingadc32c02016-05-16 18:25:45 -030016
Gilles Peskine449bd832023-01-11 14:50:10 +010017 memset(mac, 0x00, sizeof(mac));
Daniel Kingadc32c02016-05-16 18:25:45 -030018
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020019 /*
20 * Test the integrated API
21 */
Gilles Peskine449bd832023-01-11 14:50:10 +010022 TEST_ASSERT(mbedtls_poly1305_mac(key->x, src_str->x,
23 src_str->len, mac) == 0);
Daniel Kingadc32c02016-05-16 18:25:45 -030024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 ASSERT_COMPARE(mac, expected_mac->len,
26 expected_mac->x, expected_mac->len);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020027
28 /*
29 * Test the streaming API
30 */
Gilles Peskine449bd832023-01-11 14:50:10 +010031 mbedtls_poly1305_init(&ctx);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020034
Gilles Peskine449bd832023-01-11 14:50:10 +010035 TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, src_str->len) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020038
Gilles Peskine449bd832023-01-11 14:50:10 +010039 ASSERT_COMPARE(mac, expected_mac->len,
40 expected_mac->x, expected_mac->len);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020041
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. */
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (src_str->len >= 1) {
49 TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 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);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 ASSERT_COMPARE(mac, expected_mac->len,
57 expected_mac->x, expected_mac->len);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020058 }
59
60 /*
61 * Again with more pieces
62 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (src_str->len >= 2) {
64 TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 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);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 ASSERT_COMPARE(mac, expected_mac->len,
73 expected_mac->x, expected_mac->len);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020074 }
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_poly1305_free(&ctx);
Daniel Kingadc32c02016-05-16 18:25:45 -030077}
78/* END_CASE */
79
Manuel Pégourié-Gonnarda8fa8b82018-05-10 10:12:36 +020080/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -030081void poly1305_selftest()
82{
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(mbedtls_poly1305_self_test(1) == 0);
Daniel Kingadc32c02016-05-16 18:25:45 -030084}
85/* END_CASE */