| 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 */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 12 | void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string  ) | 
|  | 13 | { | 
| Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 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 */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 18 | size_t src_len; | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 19 | mbedtls_poly1305_context ctx; | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 20 |  | 
| Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 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 ) ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 25 |  | 
|  | 26 | src_len = unhexify( src_str, hex_src_string ); | 
|  | 27 | unhexify( key, hex_key_string ); | 
|  | 28 |  | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 29 | /* | 
|  | 30 | * Test the integrated API | 
|  | 31 | */ | 
| Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 32 | TEST_ASSERT( mbedtls_poly1305_mac( key, src_str, src_len, mac ) == 0 ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 33 |  | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 34 | hexify( mac_str, mac, 16 ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 35 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 36 |  | 
|  | 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. */ | 
| Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 57 | if( src_len >= 1 ) | 
|  | 58 | { | 
|  | 59 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 ); | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 60 |  | 
| Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 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 ); | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 63 |  | 
| Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 64 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 65 |  | 
| Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 66 | hexify( mac_str, mac, 16 ); | 
|  | 67 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | /* | 
|  | 71 | * Again with more pieces | 
|  | 72 | */ | 
|  | 73 | if( src_len >= 2 ) | 
|  | 74 | { | 
|  | 75 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key ) == 0 ); | 
|  | 76 |  | 
|  | 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 ); | 
|  | 80 |  | 
|  | 81 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 ); | 
|  | 82 |  | 
|  | 83 | hexify( mac_str, mac, 16 ); | 
|  | 84 | TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); | 
|  | 85 | } | 
| Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 86 |  | 
|  | 87 | mbedtls_poly1305_free( &ctx ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 88 | } | 
|  | 89 | /* END_CASE */ | 
|  | 90 |  | 
| Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 91 | /* BEGIN_CASE */ | 
|  | 92 | void poly1305_bad_params() | 
|  | 93 | { | 
|  | 94 | unsigned char src[1]; | 
|  | 95 | unsigned char key[32]; | 
|  | 96 | unsigned char mac[16]; | 
|  | 97 | size_t src_len = sizeof( src ); | 
|  | 98 | mbedtls_poly1305_context ctx; | 
|  | 99 |  | 
|  | 100 | mbedtls_poly1305_init( NULL ); | 
|  | 101 | mbedtls_poly1305_free( NULL ); | 
|  | 102 |  | 
|  | 103 | mbedtls_poly1305_init( &ctx ); | 
|  | 104 |  | 
|  | 105 | TEST_ASSERT( mbedtls_poly1305_starts( NULL, key ) | 
|  | 106 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 107 | TEST_ASSERT( mbedtls_poly1305_starts( &ctx, NULL ) | 
|  | 108 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 109 |  | 
|  | 110 | TEST_ASSERT( mbedtls_poly1305_update( NULL, src, 0 ) | 
|  | 111 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 112 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, src_len ) | 
|  | 113 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 114 | TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, 0 ) | 
|  | 115 | == 0 ); | 
|  | 116 |  | 
|  | 117 | TEST_ASSERT( mbedtls_poly1305_finish( NULL, mac ) | 
|  | 118 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 119 | TEST_ASSERT( mbedtls_poly1305_finish( &ctx, NULL ) | 
|  | 120 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 121 |  | 
|  | 122 | TEST_ASSERT( mbedtls_poly1305_mac( NULL, src, 0, mac ) | 
|  | 123 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 124 | TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, src_len, mac ) | 
|  | 125 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 126 | TEST_ASSERT( mbedtls_poly1305_mac( key, src, 0, NULL ) | 
|  | 127 | == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | 
|  | 128 | TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, 0, mac ) | 
|  | 129 | == 0 ); | 
|  | 130 |  | 
|  | 131 | mbedtls_poly1305_free( &ctx ); | 
|  | 132 | } | 
|  | 133 | /* END_CASE */ | 
|  | 134 |  | 
|  | 135 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 136 | void poly1305_selftest() | 
|  | 137 | { | 
| Manuel Pégourié-Gonnard | fce88b2 | 2018-05-09 13:06:12 +0200 | [diff] [blame] | 138 | TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 139 | } | 
|  | 140 | /* END_CASE */ |