test_suite_poly1305.function 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/poly1305.h"
  3. #include <stddef.h>
  4. /* END_HEADER */
  5. /* BEGIN_DEPENDENCIES
  6. * depends_on:MBEDTLS_POLY1305_C
  7. * END_DEPENDENCIES
  8. */
  9. /* BEGIN_CASE */
  10. void mbedtls_poly1305(data_t *key, data_t *expected_mac, data_t *src_str)
  11. {
  12. unsigned char mac[16]; /* size set by the standard */
  13. mbedtls_poly1305_context ctx;
  14. memset(mac, 0x00, sizeof(mac));
  15. /*
  16. * Test the integrated API
  17. */
  18. TEST_ASSERT(mbedtls_poly1305_mac(key->x, src_str->x,
  19. src_str->len, mac) == 0);
  20. ASSERT_COMPARE(mac, expected_mac->len,
  21. expected_mac->x, expected_mac->len);
  22. /*
  23. * Test the streaming API
  24. */
  25. mbedtls_poly1305_init(&ctx);
  26. TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
  27. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, src_str->len) == 0);
  28. TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
  29. ASSERT_COMPARE(mac, expected_mac->len,
  30. expected_mac->x, expected_mac->len);
  31. /*
  32. * Test the streaming API again, piecewise
  33. */
  34. /* Don't free/init the context, in order to test that starts() does the
  35. * right thing. */
  36. if (src_str->len >= 1) {
  37. TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
  38. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0);
  39. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 1, src_str->len - 1) == 0);
  40. TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
  41. ASSERT_COMPARE(mac, expected_mac->len,
  42. expected_mac->x, expected_mac->len);
  43. }
  44. /*
  45. * Again with more pieces
  46. */
  47. if (src_str->len >= 2) {
  48. TEST_ASSERT(mbedtls_poly1305_starts(&ctx, key->x) == 0);
  49. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x, 1) == 0);
  50. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 1, 1) == 0);
  51. TEST_ASSERT(mbedtls_poly1305_update(&ctx, src_str->x + 2, src_str->len - 2) == 0);
  52. TEST_ASSERT(mbedtls_poly1305_finish(&ctx, mac) == 0);
  53. ASSERT_COMPARE(mac, expected_mac->len,
  54. expected_mac->x, expected_mac->len);
  55. }
  56. mbedtls_poly1305_free(&ctx);
  57. }
  58. /* END_CASE */
  59. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  60. void poly1305_selftest()
  61. {
  62. TEST_ASSERT(mbedtls_poly1305_self_test(1) == 0);
  63. }
  64. /* END_CASE */