blob: 964d1297a9562f64967983d82abb3318cef2567e [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
6/* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C */
7void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string )
8{
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +02009 unsigned char src_str[375]; /* max size of binary input */
10 unsigned char key[32]; /* size set by the standard */
11 unsigned char mac[16]; /* size set by the standard */
12 unsigned char mac_str[33]; /* hex expansion of the above */
Daniel Kingadc32c02016-05-16 18:25:45 -030013 size_t src_len;
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020014 mbedtls_poly1305_context ctx;
Daniel Kingadc32c02016-05-16 18:25:45 -030015
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020016 memset( src_str, 0x00, sizeof( src_str ) );
17 memset( mac_str, 0x00, sizeof( mac_str ) );
18 memset( key, 0x00, sizeof( key ) );
19 memset( mac, 0x00, sizeof( mac ) );
Daniel Kingadc32c02016-05-16 18:25:45 -030020
21 src_len = unhexify( src_str, hex_src_string );
22 unhexify( key, hex_key_string );
23
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020024 /*
25 * Test the integrated API
26 */
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +020027 mbedtls_poly1305_mac( key, src_str, src_len, mac );
Daniel Kingadc32c02016-05-16 18:25:45 -030028
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020029 hexify( mac_str, mac, 16 );
Daniel Kingadc32c02016-05-16 18:25:45 -030030 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020031
32 /*
33 * Test the streaming API
34 */
35 mbedtls_poly1305_init( &ctx );
36
37 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
38
39 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, src_len ) == 0 );
40
41 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
42
43 hexify( mac_str, mac, 16 );
44 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
45
46 /*
47 * Test the streaming API again, piecewise
48 */
49
50 /* Don't free/init the context, in order to test that starts() does the
51 * right thing. */
52 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
53
54 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, 1 ) == 0 );
55 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str + 1, src_len - 1) == 0 );
56
57 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
58
59 hexify( mac_str, mac, 16 );
60 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
61
62 mbedtls_poly1305_free( &ctx );
Daniel Kingadc32c02016-05-16 18:25:45 -030063}
64/* END_CASE */
65
66/* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C:MBEDTLS_SELF_TEST */
67void poly1305_selftest()
68{
69 TEST_ASSERT( mbedtls_poly1305_self_test( 0 ) == 0 );
70}
71/* END_CASE */