Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/poly1305.h" |
| 3 | #include <stddef.h> |
| 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C */ |
| 7 | void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string ) |
| 8 | { |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 9 | 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 King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 13 | size_t src_len; |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 14 | mbedtls_poly1305_context ctx; |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 16 | 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 King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 20 | |
| 21 | src_len = unhexify( src_str, hex_src_string ); |
| 22 | unhexify( key, hex_key_string ); |
| 23 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 24 | /* |
| 25 | * Test the integrated API |
| 26 | */ |
Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 27 | mbedtls_poly1305_mac( key, src_str, src_len, mac ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 29 | hexify( mac_str, mac, 16 ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 30 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 31 | |
| 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 King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 63 | } |
| 64 | /* END_CASE */ |
| 65 | |
| 66 | /* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C:MBEDTLS_SELF_TEST */ |
| 67 | void poly1305_selftest() |
| 68 | { |
Manuel Pégourié-Gonnard | fce88b2 | 2018-05-09 13:06:12 +0200 | [diff] [blame] | 69 | TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 70 | } |
| 71 | /* END_CASE */ |