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