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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 25 | TEST_MEMORY_COMPARE(mac, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 26 | expected_mac->x, expected_mac->len); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Test the streaming API |
| 30 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | mbedtls_poly1305_init(&ctx); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 32 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 34 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | 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] | 36 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 38 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 39 | TEST_MEMORY_COMPARE(mac, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 40 | expected_mac->x, expected_mac->len); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Test the streaming API again, piecewise |
| 44 | */ |
| 45 | |
| 46 | /* Don't free/init the context, in order to test that starts() does the |
| 47 | * right thing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | if (src_str->len >= 1) { |
| 49 | TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0); |
| 52 | 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] | 53 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0); |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 55 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 56 | TEST_MEMORY_COMPARE(mac, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 57 | expected_mac->x, expected_mac->len); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Again with more pieces |
| 62 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | if (src_str->len >= 2) { |
| 64 | TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0); |
| 67 | TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 1, 1) == 0); |
| 68 | 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] | 69 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 71 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 72 | TEST_MEMORY_COMPARE(mac, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 73 | expected_mac->x, expected_mac->len); |
Manuel Pégourié-Gonnard | 444f711 | 2018-05-10 11:06:46 +0200 | [diff] [blame] | 74 | } |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | mbedtls_poly1305_free(&ctx); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 77 | } |
| 78 | /* END_CASE */ |
| 79 | |
Manuel Pégourié-Gonnard | a8fa8b8 | 2018-05-10 10:12:36 +0200 | [diff] [blame] | 80 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 81 | void poly1305_selftest() |
| 82 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | TEST_ASSERT(mbedtls_poly1305_self_test(1) == 0); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 84 | } |
| 85 | /* END_CASE */ |