test_suite_poly1305.function 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. {
  38. TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key->x ) == 0 );
  39. TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x, 1 ) == 0 );
  40. TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 1, src_str->len - 1 ) == 0 );
  41. TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
  42. ASSERT_COMPARE( mac, expected_mac->len,
  43. expected_mac->x, expected_mac->len );
  44. }
  45. /*
  46. * Again with more pieces
  47. */
  48. if( src_str->len >= 2 )
  49. {
  50. TEST_ASSERT( mbedtls_poly1305_starts( &ctx, key->x ) == 0 );
  51. TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x, 1 ) == 0 );
  52. TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 1, 1 ) == 0 );
  53. TEST_ASSERT( mbedtls_poly1305_update( &ctx, src_str->x + 2, src_str->len - 2 ) == 0 );
  54. TEST_ASSERT( mbedtls_poly1305_finish( &ctx, mac ) == 0 );
  55. ASSERT_COMPARE( mac, expected_mac->len,
  56. expected_mac->x, expected_mac->len );
  57. }
  58. mbedtls_poly1305_free( &ctx );
  59. }
  60. /* END_CASE */
  61. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  62. void poly1305_bad_params()
  63. {
  64. unsigned char src[1];
  65. unsigned char key[32];
  66. unsigned char mac[16];
  67. size_t src_len = sizeof( src );
  68. mbedtls_poly1305_context ctx;
  69. TEST_INVALID_PARAM( mbedtls_poly1305_init( NULL ) );
  70. TEST_VALID_PARAM( mbedtls_poly1305_free( NULL ) );
  71. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  72. mbedtls_poly1305_starts( NULL, key ) );
  73. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  74. mbedtls_poly1305_starts( &ctx, NULL ) );
  75. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  76. mbedtls_poly1305_update( NULL, src, 0 ) );
  77. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  78. mbedtls_poly1305_update( &ctx, NULL, src_len ) );
  79. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  80. mbedtls_poly1305_finish( NULL, mac ) );
  81. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  82. mbedtls_poly1305_finish( &ctx, NULL ) );
  83. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  84. mbedtls_poly1305_mac( NULL, src, 0, mac ) );
  85. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  86. mbedtls_poly1305_mac( key, NULL, src_len, mac ) );
  87. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  88. mbedtls_poly1305_mac( key, src, 0, NULL ) );
  89. exit:
  90. return;
  91. }
  92. /* END_CASE */
  93. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  94. void poly1305_selftest()
  95. {
  96. TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 );
  97. }
  98. /* END_CASE */