prg_aes_impl.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 2019 Anonymous
  2. *
  3. * This is a pre-release version of the DPF++ library distributed anonymously
  4. * for peer review. A public release of the software will be published under the
  5. * LPGL v2.1 license in the near future. Please do not redistribute this version
  6. * of the software.
  7. */
  8. #ifndef DPFPP_PRG_AES_IMPL_H__
  9. #define DPFPP_PRG_AES_IMPL_H__
  10. #include "prg.h"
  11. #include "aes.h"
  12. namespace dpf
  13. {
  14. template<>
  15. inline void PRG(const AES_KEY & prgkey, const __m128i & seed, void * outbuf, const uint32_t len, const uint32_t from)
  16. {
  17. __m128i * outbuf128 = reinterpret_cast<__m128i *>(outbuf);
  18. for (size_t i = 0; i < len; ++i)
  19. {
  20. outbuf128[i] = _mm_xor_si128(seed, _mm_set_epi64x(0, from+i));
  21. }
  22. AES_ecb_encrypt_blks(outbuf128, static_cast<unsigned int>(len), &prgkey);
  23. for (size_t i = 0; i < len; ++i)
  24. {
  25. outbuf128[i] = _mm_xor_si128(outbuf128[i], _mm_set_epi64x(0, from+i));
  26. outbuf128[i] = _mm_xor_si128(outbuf128[i], seed);
  27. }
  28. } // PRG<AES_KEY>
  29. inline void PRG_aes(const AES_KEY & prgkey, const __m128i & seed, void * outbuf, const uint32_t len, const uint32_t from = 0)
  30. {
  31. __m128i * outbuf128 = reinterpret_cast<__m128i *>(outbuf);
  32. for (size_t i = 0; i < len; ++i)
  33. {
  34. outbuf128[i] = _mm_xor_si128(seed, _mm_set_epi64x(0, from+i));
  35. }
  36. AES_ecb_encrypt_blks(outbuf128, static_cast<unsigned int>(len), &prgkey);
  37. for (size_t i = 0; i < len; ++i)
  38. {
  39. outbuf128[i] = _mm_xor_si128(outbuf128[i], _mm_set_epi64x(0, from+i));
  40. outbuf128[i] = _mm_xor_si128(outbuf128[i], seed);
  41. }
  42. } // PRG<AES_KEY>
  43. inline void PRG_aes(const AES_KEY & prgkey, const __m256i & seed, void * outbuf, const uint32_t len, const uint32_t from = 0)
  44. {
  45. __m256i * outbuf256 = reinterpret_cast<__m256i *>(outbuf);
  46. for (size_t i = 0; i < len; ++i)
  47. {
  48. outbuf256[i] = _mm256_xor_si256(seed, _mm256_set_epi64x(0, 0, 0, from+i));
  49. }
  50. // AES_ecb_encrypt_blks(reinterpret_cast<__m128i *>(outbuf256), static_cast<unsigned int>(len), &prgkey);
  51. for (size_t i = 0; i < len; ++i)
  52. {
  53. outbuf256[i] = _mm256_xor_si256(outbuf256[i], _mm256_set_epi64x(0, 0, 0, from+i));
  54. outbuf256[i] = _mm256_xor_si256(outbuf256[i], seed);
  55. }
  56. } // PRG<AES_KEY>
  57. inline std::ostream & operator<<(std::ostream & os, const AES_KEY & prgkey)
  58. {
  59. return os.write(reinterpret_cast<const char *>(&prgkey.rd_key[0]), sizeof(__m128i));
  60. } // operator<<
  61. } // namespace dpf
  62. #endif // DPFPP_PRG_AES_IMPL_H