You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB

  1. #define TEST_NAME "chacha20"
  2. #include "cmptest.h"
  3. static void tv(void)
  4. {
  5. static struct {
  6. const char *key_hex;
  7. const char *nonce_hex;
  8. } tests[]
  9. = { { "0000000000000000000000000000000000000000000000000000000000000000",
  10. "0000000000000000" },
  11. { "0000000000000000000000000000000000000000000000000000000000000001",
  12. "0000000000000000" },
  13. { "0000000000000000000000000000000000000000000000000000000000000000",
  14. "0000000000000001" },
  15. { "0000000000000000000000000000000000000000000000000000000000000000",
  16. "0100000000000000" },
  17. { "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
  18. "0001020304050607" } };
  19. unsigned char key[crypto_stream_chacha20_KEYBYTES];
  20. unsigned char nonce[crypto_stream_chacha20_NONCEBYTES];
  21. unsigned char out[160];
  22. char out_hex[160 * 2 + 1];
  23. size_t i = 0U;
  24. do {
  25. sodium_hex2bin((unsigned char *)key, sizeof key, tests[i].key_hex,
  26. strlen(tests[i].key_hex), NULL, NULL, NULL);
  27. sodium_hex2bin(nonce, sizeof nonce, tests[i].nonce_hex,
  28. strlen(tests[i].nonce_hex), NULL, NULL, NULL);
  29. crypto_stream_chacha20(out, sizeof out, nonce, key);
  30. sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
  31. printf("[%s]\n", out_hex);
  32. } while (++i < (sizeof tests) / (sizeof tests[0]));
  33. memset(out, 0x42, sizeof out);
  34. assert(crypto_stream_chacha20(out, 0U, nonce, key) == 0);
  35. assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
  36. assert(crypto_stream_chacha20_xor(out, out, 0U, nonce, key) == 0);
  37. assert(crypto_stream_chacha20_xor_ic(out, out, 0U, nonce, 1U, key) == 0);
  38. crypto_stream_chacha20_xor(out, out, sizeof out, nonce, key);
  39. sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
  40. printf("[%s]\n", out_hex);
  41. crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 0U, key);
  42. sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
  43. printf("[%s]\n", out_hex);
  44. crypto_stream_chacha20_xor_ic(out, out, sizeof out, nonce, 1U, key);
  45. sodium_bin2hex(out_hex, sizeof out_hex, out, sizeof out);
  46. printf("[%s]\n", out_hex);
  47. };
  48. int main(void)
  49. {
  50. tv();
  51. assert(crypto_stream_chacha20_keybytes() > 0U);
  52. assert(crypto_stream_chacha20_noncebytes() > 0U);
  53. return 0;
  54. }