chacha20test.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <gmssl/chacha20.h>
  13. int main(void)
  14. {
  15. int err = 0;
  16. const unsigned char key[] = {
  17. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  19. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  20. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  21. };
  22. const unsigned char nonce[] = {
  23. 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a,
  24. 0x00, 0x00, 0x00, 0x00,
  25. };
  26. uint32_t counter = 1;
  27. const unsigned char testdata[] = {
  28. 0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15,
  29. 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
  30. 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03,
  31. 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
  32. 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09,
  33. 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
  34. 0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
  35. 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
  36. };
  37. unsigned char buf[64];
  38. CHACHA20_STATE state;
  39. chacha20_init(&state, key, nonce, counter);
  40. chacha20_generate_keystream(&state, 1, buf);
  41. printf("chacha20 test ");
  42. if (memcmp(buf, testdata, sizeof(testdata)) != 0) {
  43. printf("failed\n");
  44. err++;
  45. } else {
  46. printf("ok\n");
  47. }
  48. return err;
  49. }