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 */
David Horstmann71159f42023-01-03 12:51:59 +000012void 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
David Horstmann71159f42023-01-03 12:51:59 +000017 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 */
David Horstmann71159f42023-01-03 12:51:59 +000022 TEST_ASSERT(mbedtls_poly1305_mac(key->x, src_str->x,
23 src_str->len, mac) == 0);
Daniel Kingadc32c02016-05-16 18:25:45 -030024
David Horstmann71159f42023-01-03 12:51:59 +000025 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 */
David Horstmann71159f42023-01-03 12:51:59 +000031 mbedtls_poly1305_init(&ctx);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020032
David Horstmann71159f42023-01-03 12:51:59 +000033 TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020034
David Horstmann71159f42023-01-03 12:51:59 +000035 TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, src_str->len) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020036
David Horstmann71159f42023-01-03 12:51:59 +000037 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020038
David Horstmann71159f42023-01-03 12:51:59 +000039 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. */
David Horstmann71159f42023-01-03 12:51:59 +000048 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
David Horstmann71159f42023-01-03 12:51:59 +000051 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
David Horstmann71159f42023-01-03 12:51:59 +000054 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020055
David Horstmann71159f42023-01-03 12:51:59 +000056 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 */
David Horstmann71159f42023-01-03 12:51:59 +000063 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
David Horstmann71159f42023-01-03 12:51:59 +000066 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
David Horstmann71159f42023-01-03 12:51:59 +000070 TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
Manuel Pégourié-Gonnard444f7112018-05-10 11:06:46 +020071
David Horstmann71159f42023-01-03 12:51:59 +000072 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
David Horstmann71159f42023-01-03 12:51:59 +000076 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{
David Horstmann71159f42023-01-03 12:51:59 +000083 TEST_ASSERT(mbedtls_poly1305_self_test(1) == 0);
Daniel Kingadc32c02016-05-16 18:25:45 -030084}
85/* END_CASE */