test_suite_chacha20.function 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/chacha20.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_CHACHA20_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void chacha20_crypt( data_t *key_str,
  10. data_t *nonce_str,
  11. int counter,
  12. data_t *src_str,
  13. data_t *expected_output_str )
  14. {
  15. unsigned char output[375];
  16. mbedtls_chacha20_context ctx;
  17. memset( output, 0x00, sizeof( output ) );
  18. TEST_ASSERT( src_str->len == expected_output_str->len );
  19. TEST_ASSERT( key_str->len == 32U );
  20. TEST_ASSERT( nonce_str->len == 12U );
  21. /*
  22. * Test the integrated API
  23. */
  24. TEST_ASSERT( mbedtls_chacha20_crypt( key_str->x, nonce_str->x, counter, src_str->len, src_str->x, output ) == 0 );
  25. ASSERT_COMPARE( output, expected_output_str->len,
  26. expected_output_str->x, expected_output_str->len );
  27. /*
  28. * Test the streaming API
  29. */
  30. mbedtls_chacha20_init( &ctx );
  31. TEST_ASSERT( mbedtls_chacha20_setkey( &ctx, key_str->x ) == 0 );
  32. TEST_ASSERT( mbedtls_chacha20_starts( &ctx, nonce_str->x, counter ) == 0 );
  33. memset( output, 0x00, sizeof( output ) );
  34. TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len, src_str->x, output ) == 0 );
  35. ASSERT_COMPARE( output, expected_output_str->len,
  36. expected_output_str->x, expected_output_str->len );
  37. /*
  38. * Test the streaming API again, piecewise
  39. */
  40. /* Don't free/init the context nor set the key again,
  41. * in order to test that starts() does the right thing. */
  42. TEST_ASSERT( mbedtls_chacha20_starts( &ctx, nonce_str->x, counter ) == 0 );
  43. memset( output, 0x00, sizeof( output ) );
  44. TEST_ASSERT( mbedtls_chacha20_update( &ctx, 1, src_str->x, output ) == 0 );
  45. TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len - 1,
  46. src_str->x + 1, output + 1 ) == 0 );
  47. ASSERT_COMPARE( output, expected_output_str->len,
  48. expected_output_str->x, expected_output_str->len );
  49. mbedtls_chacha20_free( &ctx );
  50. }
  51. /* END_CASE */
  52. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  53. void chacha20_bad_params()
  54. {
  55. unsigned char key[32];
  56. unsigned char nonce[12];
  57. unsigned char src[1];
  58. unsigned char dst[1];
  59. uint32_t counter = 0;
  60. size_t len = sizeof( src );
  61. mbedtls_chacha20_context ctx;
  62. TEST_INVALID_PARAM( mbedtls_chacha20_init( NULL ) );
  63. TEST_VALID_PARAM( mbedtls_chacha20_free( NULL ) );
  64. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  65. mbedtls_chacha20_setkey( NULL, key ) );
  66. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  67. mbedtls_chacha20_setkey( &ctx, NULL ) );
  68. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  69. mbedtls_chacha20_starts( NULL, nonce, counter ) );
  70. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  71. mbedtls_chacha20_starts( &ctx, NULL, counter ) );
  72. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  73. mbedtls_chacha20_update( NULL, 0, src, dst ) );
  74. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  75. mbedtls_chacha20_update( &ctx, len, NULL, dst ) );
  76. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  77. mbedtls_chacha20_update( &ctx, len, src, NULL ) );
  78. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  79. mbedtls_chacha20_crypt( NULL, nonce, counter, 0, src, dst ) );
  80. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  81. mbedtls_chacha20_crypt( key, NULL, counter, 0, src, dst ) );
  82. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  83. mbedtls_chacha20_crypt( key, nonce, counter, len, NULL, dst ) );
  84. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA,
  85. mbedtls_chacha20_crypt( key, nonce, counter, len, src, NULL ) );
  86. exit:
  87. return;
  88. }
  89. /* END_CASE */
  90. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  91. void chacha20_self_test()
  92. {
  93. TEST_ASSERT( mbedtls_chacha20_self_test( 1 ) == 0 );
  94. }
  95. /* END_CASE */