prg_aes_impl.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef DPFPP_PRG_AES_IMPL_H__
  2. #define DPFPP_PRG_AES_IMPL_H__
  3. #include "prg.h"
  4. #include "aes.h"
  5. namespace dpf
  6. {
  7. template<>
  8. inline void PRG(const AES_KEY & prgkey, const __m128i & seed, void * outbuf, const uint32_t len, const uint32_t from)
  9. {
  10. __m128i * outbuf128 = reinterpret_cast<__m128i *>(outbuf);
  11. for (size_t i = 0; i < len; ++i)
  12. {
  13. outbuf128[i] = _mm_xor_si128(seed, _mm_set_epi64x(0, from+i));
  14. }
  15. AES_ecb_encrypt_blks(outbuf128, static_cast<unsigned int>(len), &prgkey);
  16. for (size_t i = 0; i < len; ++i)
  17. {
  18. outbuf128[i] = _mm_xor_si128(outbuf128[i], _mm_set_epi64x(0, from+i));
  19. outbuf128[i] = _mm_xor_si128(outbuf128[i], seed);
  20. }
  21. } // PRG<AES_KEY>
  22. inline void PRG_aes(const AES_KEY & prgkey, const __m128i & seed, void * outbuf, const uint32_t len, const uint32_t from = 0)
  23. {
  24. __m128i * outbuf128 = reinterpret_cast<__m128i *>(outbuf);
  25. for (size_t i = 0; i < len; ++i)
  26. {
  27. outbuf128[i] = _mm_xor_si128(seed, _mm_set_epi64x(0, from+i));
  28. }
  29. AES_ecb_encrypt_blks(outbuf128, static_cast<unsigned int>(len), &prgkey);
  30. for (size_t i = 0; i < len; ++i)
  31. {
  32. outbuf128[i] = _mm_xor_si128(outbuf128[i], _mm_set_epi64x(0, from+i));
  33. outbuf128[i] = _mm_xor_si128(outbuf128[i], seed);
  34. }
  35. } // PRG<AES_KEY>
  36. inline void PRG_aes(const AES_KEY & prgkey, const __m256i & seed, void * outbuf, const uint32_t len, const uint32_t from = 0)
  37. {
  38. __m256i * outbuf256 = reinterpret_cast<__m256i *>(outbuf);
  39. for (size_t i = 0; i < len; ++i)
  40. {
  41. outbuf256[i] = _mm256_xor_si256(seed, _mm256_set_epi64x(0, 0, 0, from+i));
  42. }
  43. // AES_ecb_encrypt_blks(reinterpret_cast<__m128i *>(outbuf256), static_cast<unsigned int>(len), &prgkey);
  44. for (size_t i = 0; i < len; ++i)
  45. {
  46. outbuf256[i] = _mm256_xor_si256(outbuf256[i], _mm256_set_epi64x(0, 0, 0, from+i));
  47. outbuf256[i] = _mm256_xor_si256(outbuf256[i], seed);
  48. }
  49. } // PRG<AES_KEY>
  50. inline std::ostream & operator<<(std::ostream & os, const AES_KEY & prgkey)
  51. {
  52. return os.write(reinterpret_cast<const char *>(&prgkey.rd_key[0]), sizeof(__m128i));
  53. } // operator<<
  54. } // namespace dpf
  55. #endif // DPFPP_PRG_AES_IMPL_H