arithmetic128.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <functional>
  6. #include "core/hw/aes/arithmetic128.h"
  7. namespace HW {
  8. namespace AES {
  9. AESKey Lrot128(const AESKey& in, u32 rot) {
  10. AESKey out;
  11. rot %= 128;
  12. const u32 byte_shift = rot / 8;
  13. const u32 bit_shift = rot % 8;
  14. for (u32 i = 0; i < 16; i++) {
  15. const u32 wrap_index_a = (i + byte_shift) % 16;
  16. const u32 wrap_index_b = (i + byte_shift + 1) % 16;
  17. out[i] = ((in[wrap_index_a] << bit_shift) | (in[wrap_index_b] >> (8 - bit_shift))) & 0xFF;
  18. }
  19. return out;
  20. }
  21. AESKey Add128(const AESKey& a, const AESKey& b) {
  22. AESKey out;
  23. u32 carry = 0;
  24. u32 sum = 0;
  25. for (int i = 15; i >= 0; i--) {
  26. sum = a[i] + b[i] + carry;
  27. carry = sum >> 8;
  28. out[i] = static_cast<u8>(sum & 0xff);
  29. }
  30. return out;
  31. }
  32. AESKey Xor128(const AESKey& a, const AESKey& b) {
  33. AESKey out;
  34. std::transform(a.cbegin(), a.cend(), b.cbegin(), out.begin(), std::bit_xor<>());
  35. return out;
  36. }
  37. } // namespace AES
  38. } // namespace HW