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 | |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 6 | /* BEGIN_DEPENDENCIES |
| 7 | * depends_on:MBEDTLS_POLY1305_C |
| 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
| 11 | /* BEGIN_CASE */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 12 | void mbedtls_poly1305( data_t *key, char *hex_mac_string, data_t *src_str ) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 13 | { |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 14 | unsigned char mac[16]; /* size set by the standard */ |
| 15 | unsigned char mac_str[33]; /* hex expansion of the above */ |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 16 | mbedtls_poly1305_context ctx; |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 18 | memset( mac_str, 0x00, sizeof( mac_str ) ); |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 19 | memset( mac, 0x00, sizeof( mac ) ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 20 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 21 | /* |
| 22 | * Test the integrated API |
| 23 | */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 24 | TEST_ASSERT( mbedtls_poly1305_mac( key->x, src_str->x, |
| 25 | src_str->len, mac ) == 0 ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 26 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 27 | mbedtls_test_hexify( mac_str, mac, 16 ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 28 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Test the streaming API |
| 32 | */ |
| 33 | mbedtls_poly1305_init( &ctx ); |
| 34 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 35 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key->x ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 36 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 37 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x, src_str->len ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 38 | |
| 39 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); |
| 40 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 41 | mbedtls_test_hexify( mac_str, mac, 16 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 42 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); |
| 43 | |
| 44 | /* |
| 45 | * Test the streaming API again, piecewise |
| 46 | */ |
| 47 | |
| 48 | /* Don't free/init the context, in order to test that starts() does the |
| 49 | * right thing. */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 50 | if( src_str->len >= 1 ) |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 51 | { |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 52 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key->x ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 53 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 54 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x, 1 ) == 0 ); |
| 55 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 1, src_str->len - 1 ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 57 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 58 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 59 | mbedtls_test_hexify( mac_str, mac, 16 ); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 60 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Again with more pieces |
| 65 | */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 66 | if( src_str->len >= 2 ) |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 67 | { |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 68 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key->x ) == 0 ); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 69 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 70 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x, 1 ) == 0 ); |
| 71 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 1, 1 ) == 0 ); |
| 72 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 2, src_str->len - 2 ) == 0 ); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 73 | |
| 74 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); |
| 75 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 76 | mbedtls_test_hexify( mac_str, mac, 16 ); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 77 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); |
| 78 | } |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 79 | |
| 80 | mbedtls_poly1305_free( &ctx ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 81 | } |
| 82 | /* END_CASE */ |
| 83 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 84 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 85 | void poly1305_bad_params() |
| 86 | { |
| 87 | unsigned char src[1]; |
| 88 | unsigned char key[32]; |
| 89 | unsigned char mac[16]; |
| 90 | size_t src_len = sizeof( src ); |
| 91 | mbedtls_poly1305_context ctx; |
| 92 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 93 | TEST_INVALID_PARAM( mbedtls_poly1305_init( NULL ) ); |
| 94 | TEST_VALID_PARAM( mbedtls_poly1305_free( NULL ) ); |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 95 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 96 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 97 | mbedtls_poly1305_starts( NULL, key ) ); |
| 98 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 99 | mbedtls_poly1305_starts( &ctx, NULL ) ); |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 100 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 101 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 102 | mbedtls_poly1305_update( NULL, src, 0 ) ); |
| 103 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 104 | mbedtls_poly1305_update( &ctx, NULL, src_len ) ); |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 105 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 106 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 107 | mbedtls_poly1305_finish( NULL, mac ) ); |
| 108 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 109 | mbedtls_poly1305_finish( &ctx, NULL ) ); |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 110 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 111 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 112 | mbedtls_poly1305_mac( NULL, src, 0, mac ) ); |
| 113 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 114 | mbedtls_poly1305_mac( key, NULL, src_len, mac ) ); |
| 115 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA, |
| 116 | mbedtls_poly1305_mac( key, src, 0, NULL ) ); |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 117 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 118 | exit: |
| 119 | return; |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 120 | } |
| 121 | /* END_CASE */ |
| 122 | |
| 123 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 124 | void poly1305_selftest() |
| 125 | { |
Manuel Pégourié-Gonnard | fce88b2 | 2018-05-09 13:06:12 +0200 | [diff] [blame] | 126 | TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 127 | } |
| 128 | /* END_CASE */ |