1/*
2 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16#include <aws/core/utils/Array.h>
17
18#include <aws/core/platform/Security.h>
19
20namespace Aws
21{
22 namespace Utils
23 {
24 Array<CryptoBuffer> CryptoBuffer::Slice(size_t sizeOfSlice) const
25 {
26 assert(sizeOfSlice <= GetLength());
27
28 size_t numberOfSlices = (GetLength() + sizeOfSlice - 1) / sizeOfSlice;
29 size_t currentSliceIndex = 0;
30 Array<CryptoBuffer> slices(numberOfSlices);
31
32 for (size_t i = 0; i < numberOfSlices - 1; ++i)
33 {
34 CryptoBuffer newArray(sizeOfSlice);
35 for (size_t cpyIdx = 0; cpyIdx < newArray.GetLength(); ++cpyIdx)
36 {
37 newArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
38 }
39 currentSliceIndex += sizeOfSlice;
40 slices[i] = std::move(newArray);
41 }
42
43 CryptoBuffer lastArray(GetLength() % sizeOfSlice == 0 ? sizeOfSlice : GetLength() % sizeOfSlice );
44 for (size_t cpyIdx = 0; cpyIdx < lastArray.GetLength(); ++cpyIdx)
45 {
46 lastArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
47 }
48 slices[slices.GetLength() - 1] = std::move(lastArray);
49
50 return slices;
51 }
52
53 CryptoBuffer& CryptoBuffer::operator^(const CryptoBuffer& operand)
54 {
55 size_t smallestSize = std::min<size_t>(GetLength(), operand.GetLength());
56 for (size_t i = 0; i < smallestSize; ++i)
57 {
58 (*this)[i] ^= operand[i];
59 }
60
61 return *this;
62 }
63
64 /**
65 * Zero out the array securely
66 */
67 void CryptoBuffer::Zero()
68 {
69 if (GetUnderlyingData())
70 {
71 Aws::Security::SecureMemClear(GetUnderlyingData(), GetLength());
72 }
73 }
74 }
75}