fastrandombytes.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * File: lattisigns512-20130329/fastrandombytes.c
  3. * Author: Gim Güneysu, Tobias Oder, Thomas Pöppelmann, Peter Schwabe
  4. * Public Domain
  5. */
  6. #include "crypto_stream_salsa20.h"
  7. #include "randombytes.h"
  8. #include <omp.h>
  9. #include <inttypes.h>
  10. #include <iostream>
  11. static int init = 0;
  12. static unsigned char key[crypto_stream_salsa20_KEYBYTES];
  13. static unsigned char nonce[crypto_stream_salsa20_NONCEBYTES] = {0};
  14. void fastrandombytes(unsigned char *r, unsigned long long rlen)
  15. {
  16. unsigned long long n=0;
  17. int i;
  18. if(!init)
  19. {
  20. randombytes(key, crypto_stream_salsa20_KEYBYTES);
  21. init = 1;
  22. }
  23. //crypto_stream(r,rlen,nonce,key);
  24. crypto_stream_salsa20(r,rlen,nonce,key);
  25. // Increase 64-bit counter (nonce)
  26. for(i=0;i<8;i++)
  27. n ^= ((unsigned long long)nonce[i]) << 8*i;
  28. n++;
  29. for(i=0;i<8;i++)
  30. nonce[i] = (n >> 8*i) & 0xff;
  31. }
  32. /*int main(int argc, char**argv) {
  33. int randlen = 1024;
  34. unsigned char t[randlen*sizeof(unsigned long long)];
  35. double start = omp_get_wtime();
  36. fastrandombytes((unsigned char *)t, randlen*sizeof(unsigned long long));
  37. double end= omp_get_wtime();
  38. std::cout << (end-start) << std::endl;
  39. for(int i=0;i<1024*sizeof(unsigned long long);i++) std::cout<<std::hex<<(int)t[i]<<" ";
  40. std::cout<<std::endl;
  41. }*/