2 #include "mbedtls/poly1305.h"
7 * depends_on:MBEDTLS_POLY1305_C
12 void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string )
14 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 */
19 mbedtls_poly1305_context ctx;
21 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 ) );
26 src_len = unhexify( src_str, hex_src_string );
27 unhexify( key, hex_key_string );
30 * Test the integrated API
32 TEST_ASSERT( mbedtls_poly1305_mac( key, src_str, src_len, mac ) == 0 );
34 hexify( mac_str, mac, 16 );
35 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
38 * Test the streaming API
40 mbedtls_poly1305_init( &ctx );
42 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
44 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, src_len ) == 0 );
46 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
48 hexify( mac_str, mac, 16 );
49 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
52 * Test the streaming API again, piecewise
55 /* Don't free/init the context, in order to test that starts() does the
59 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
61 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, 1 ) == 0 );
62 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str + 1, src_len - 1 ) == 0 );
64 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
66 hexify( mac_str, mac, 16 );
67 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
71 * Again with more pieces
75 TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 );
77 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str, 1 ) == 0 );
78 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str + 1, 1 ) == 0 );
79 TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str + 2, src_len - 2 ) == 0 );
81 TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
83 hexify( mac_str, mac, 16 );
84 TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 );
87 mbedtls_poly1305_free( &ctx );
91 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
92 void poly1305_bad_params()
95 unsigned char key[32];
96 unsigned char mac[16];
97 size_t src_len = sizeof( src );
98 mbedtls_poly1305_context ctx;
100 TEST_INVALID_PARAM( mbedtls_poly1305_init( NULL ) );
101 TEST_VALID_PARAM( mbedtls_poly1305_free( NULL ) );
103 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
104 mbedtls_poly1305_starts( NULL, key ) );
105 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
106 mbedtls_poly1305_starts( &ctx, NULL ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
109 mbedtls_poly1305_update( NULL, src, 0 ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
111 mbedtls_poly1305_update( &ctx, NULL, src_len ) );
113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
114 mbedtls_poly1305_finish( NULL, mac ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
116 mbedtls_poly1305_finish( &ctx, NULL ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
119 mbedtls_poly1305_mac( NULL, src, 0, mac ) );
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
121 mbedtls_poly1305_mac( key, NULL, src_len, mac ) );
122 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
123 mbedtls_poly1305_mac( key, src, 0, NULL ) );
130 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
131 void poly1305_selftest()
133 TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 );