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.

36 lines
950 B

  1. /*
  2. prg.c version 20150811
  3. Andreas Hülsing
  4. Public domain.
  5. */
  6. #include "chacha.h"
  7. #include "prg.h"
  8. const unsigned char zero_nonce[12] = {0};
  9. /**
  10. * Generates rlen output bytes using ChaCha20 with a zero nonce and counter = 0
  11. */
  12. void prg(unsigned char *r, unsigned long long rlen, const unsigned char *key, unsigned int key_len)
  13. {
  14. CRYPTO_chacha_20_keystream(r, rlen, key, zero_nonce, 0);
  15. }
  16. /**
  17. * Generates rlen output bytes using ChaCha20.
  18. * Nonce and counter are set depending on the address addr.
  19. */
  20. void prg_with_counter(unsigned char *r, unsigned long long rlen, const unsigned char *key, unsigned int key_len, const unsigned char addr[16])
  21. {
  22. int i;
  23. unsigned char nonce[12];
  24. for(i = 0; i < 12; i++)
  25. {
  26. nonce[i] = addr[i];
  27. }
  28. uint32_t counter;
  29. counter = (addr[12] << 24)|(addr[13] << 16)|(addr[14] << 8)|addr[15];
  30. // TODO: Check address handling. Endianess?
  31. CRYPTO_chacha_20_keystream(r, rlen, key, nonce, counter);
  32. }