Array.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/core/utils/Array.h>
  6. #include <aws/core/platform/Security.h>
  7. namespace Aws
  8. {
  9. namespace Utils
  10. {
  11. Array<CryptoBuffer> CryptoBuffer::Slice(size_t sizeOfSlice) const
  12. {
  13. assert(sizeOfSlice <= GetLength());
  14. size_t numberOfSlices = (GetLength() + sizeOfSlice - 1) / sizeOfSlice;
  15. size_t currentSliceIndex = 0;
  16. Array<CryptoBuffer> slices(numberOfSlices);
  17. for (size_t i = 0; i < numberOfSlices - 1; ++i)
  18. {
  19. CryptoBuffer newArray(sizeOfSlice);
  20. for (size_t cpyIdx = 0; cpyIdx < newArray.GetLength(); ++cpyIdx)
  21. {
  22. newArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
  23. }
  24. currentSliceIndex += sizeOfSlice;
  25. slices[i] = std::move(newArray);
  26. }
  27. CryptoBuffer lastArray(GetLength() % sizeOfSlice == 0 ? sizeOfSlice : GetLength() % sizeOfSlice );
  28. for (size_t cpyIdx = 0; cpyIdx < lastArray.GetLength(); ++cpyIdx)
  29. {
  30. lastArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
  31. }
  32. slices[slices.GetLength() - 1] = std::move(lastArray);
  33. return slices;
  34. }
  35. CryptoBuffer& CryptoBuffer::operator^(const CryptoBuffer& operand)
  36. {
  37. size_t smallestSize = std::min<size_t>(GetLength(), operand.GetLength());
  38. for (size_t i = 0; i < smallestSize; ++i)
  39. {
  40. (*this)[i] ^= operand[i];
  41. }
  42. return *this;
  43. }
  44. /**
  45. * Zero out the array securely
  46. */
  47. void CryptoBuffer::Zero()
  48. {
  49. if (GetUnderlyingData())
  50. {
  51. Aws::Security::SecureMemClear(GetUnderlyingData(), GetLength());
  52. }
  53. }
  54. }
  55. }