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.

prg.c 1.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. prg.c version 20150811
  3. Andreas Hülsing
  4. Public domain.
  5. */
  6. #include "chacha.h"
  7. #include "prg.h"
  8. #include <stdio.h>
  9. const unsigned char zero_nonce[12] = {0};
  10. /**
  11. * Generates rlen output bytes using ChaCha20 with a zero nonce and counter = 0
  12. */
  13. void prg(unsigned char *r, unsigned long long rlen, const unsigned char *key, unsigned int key_len)
  14. {
  15. CRYPTO_chacha_20_keystream(r, rlen, key, zero_nonce, 0);
  16. }
  17. /**
  18. * Generates rlen output bytes using ChaCha20.
  19. * Nonce and counter are set depending on the address addr.
  20. */
  21. void prg_with_counter(unsigned char *r, unsigned long long rlen, const unsigned char *key, unsigned int key_len, const unsigned char addr[16])
  22. {
  23. int i;
  24. unsigned char nonce[12];
  25. if(key_len == 32){
  26. for(i = 0; i < 12; i++)
  27. {
  28. nonce[i] = addr[i];
  29. }
  30. uint32_t counter;
  31. counter = (((uint32_t)addr[12]) << 24)|(((uint32_t)addr[13]) << 16)|(((uint32_t)addr[14]) << 8)|addr[15];
  32. // TODO: Check address handling. Endianess?
  33. CRYPTO_chacha_20_keystream(r, rlen, key, nonce, counter);
  34. }
  35. else
  36. {
  37. if(key_len == 64)
  38. {
  39. for(i = 0; i < 12; i++)
  40. {
  41. nonce[i] = addr[i];
  42. }
  43. uint32_t counter;
  44. counter = (((uint32_t)addr[12]) << 24)|(((uint32_t)addr[13]) << 16)|(((uint32_t)addr[14]) << 8)|addr[15];
  45. // TODO: WRONG! Uses only 32 byte of key. However, does not compile with HMAC-SHA512
  46. CRYPTO_chacha_20_keystream(r, rlen, key, nonce, counter);
  47. } else {
  48. fprintf(stderr,"prg.c:: Code only supports 32 byte and 64 byte seeds");
  49. }
  50. }
  51. }