blob: c5e7989fe3c7e81f6445586d2557036752bd1dcb [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 */
Daniel Kingadc32c02016-05-16 18:25:45 -030012void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string )
13{
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020014 unsigned char src_str[375]; /* max size of binary input */
15 unsigned char key[32]; /* size set by the standard */
16 unsigned char mac[16]; /* size set by the standard */
17 unsigned char mac_str[33]; /* hex expansion of the above */
Daniel Kingadc32c02016-05-16 18:25:45 -030018 size_t src_len;
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020019 mbedtls_poly1305_context ctx;
Daniel Kingadc32c02016-05-16 18:25:45 -030020
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020021 memset( src_str, 0x00, sizeof( src_str ) );
22 memset( mac_str, 0x00, sizeof( mac_str ) );
23 memset( key, 0x00, sizeof( key ) );
24 memset( mac, 0x00, sizeof( mac ) );
Daniel Kingadc32c02016-05-16 18:25:45 -030025
26 src_len = unhexify( src_str, hex_src_string );
27 unhexify( key, hex_key_string );
28
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020029 /*
30 * Test the integrated API
31 */
Manuel Pégourié-Gonnarda8fa8b82018-05-10 10:12:36 +020032 TEST_ASSERT( mbedtls_poly1305_mac( key, src_str, src_len, mac ) == 0 );
Daniel Kingadc32c02016-05-16 18:25:45 -030033
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020034 hexify( mac_str, mac, 16 );
Daniel Kingadc32c02016-05-16 18:25:45 -030035 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
Manuel Pégourié-Gonnard14656022018-05-09 12:51:54 +020036
37 /*
38 * Test the streaming API
39 */
40 mbedtls_poly1305_init( &ctx );
41
42 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
43
44 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, src_len ) == 0 );
45
46 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
47
48 hexify( mac_str, mac, 16 );
49 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
50
51 /*
52 * Test the streaming API again, piecewise
53 */
54
55 /* Don't free/init the context, in order to test that starts() does the
56 * right thing. */
57 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
58
59 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, 1 ) == 0 );
60 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str + 1, src_len - 1) == 0 );
61
62 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
63
64 hexify( mac_str, mac, 16 );
65 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
66
67 mbedtls_poly1305_free( &ctx );
Daniel Kingadc32c02016-05-16 18:25:45 -030068}
69/* END_CASE */
70
Manuel Pégourié-Gonnarda8fa8b82018-05-10 10:12:36 +020071/* BEGIN_CASE */
72void poly1305_bad_params()
73{
74 unsigned char src[1];
75 unsigned char key[32];
76 unsigned char mac[16];
77 size_t src_len = sizeof( src );
78 mbedtls_poly1305_context ctx;
79
80 mbedtls_poly1305_init( NULL );
81 mbedtls_poly1305_free( NULL );
82
83 mbedtls_poly1305_init( &ctx );
84
85 TEST_ASSERT( mbedtls_poly1305_starts( NULL, key )
86 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
87 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, NULL )
88 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
89
90 TEST_ASSERT( mbedtls_poly1305_update( NULL, src, 0 )
91 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
92 TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, src_len )
93 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
94 TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, 0 )
95 == 0 );
96
97 TEST_ASSERT( mbedtls_poly1305_finish( NULL, mac )
98 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
99 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, NULL )
100 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
101
102 TEST_ASSERT( mbedtls_poly1305_mac( NULL, src, 0, mac )
103 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
104 TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, src_len, mac )
105 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
106 TEST_ASSERT( mbedtls_poly1305_mac( key, src, 0, NULL )
107 == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA );
108 TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, 0, mac )
109 == 0 );
110
111 mbedtls_poly1305_free( &ctx );
112}
113/* END_CASE */
114
115/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300116void poly1305_selftest()
117{
Manuel Pégourié-Gonnardfce88b22018-05-09 13:06:12 +0200118 TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 );
Daniel Kingadc32c02016-05-16 18:25:45 -0300119}
120/* END_CASE */