Skip to content

Commit a92d069

Browse files
committed
more PR revisions
1 parent 243722c commit a92d069

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

src/aws-cpp-sdk-core/include/aws/core/utils/crypto/crt/CRTHash.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Aws
2323
CRTHash &operator=(const CRTHash &) = delete;
2424
CRTHash(CRTHash &&) = default;
2525
CRTHash &operator=(CRTHash &&) = default;
26+
explicit CRTHash(Crt::Crypto::Hash &&);
2627

2728
HashResult Calculate(const String &str) override;
2829

@@ -31,12 +32,8 @@ namespace Aws
3132
void Update(unsigned char *string, size_t bufferSize) override;
3233

3334
HashResult GetHash() override;
34-
35-
// @private but made public for the sake of make_shared.
36-
CRTHash(Crt::Crypto::Hash &&);
3735
private:
3836
Crt::Crypto::Hash m_hash;
39-
4037
};
4138
}
4239
}

src/aws-cpp-sdk-core/source/utils/HashingUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ using namespace Aws::Utils;
2222
using namespace Aws::Utils::Base64;
2323
using namespace Aws::Utils::Crypto;
2424

25+
// internal buffers are fixed-size arrays, so this is harmless memory-management wise
26+
static Aws::Utils::Base64::Base64 s_base64;
27+
2528
// Aws Glacier Tree Hash calculates hash value for each 1MB data
2629
const static size_t TREE_HASH_ONE_MB = 1024 * 1024;
2730

2831
Aws::String HashingUtils::Base64Encode(const ByteBuffer& message)
2932
{
30-
Base64::Base64 base64Encoder;
31-
return base64Encoder.Encode(message);
33+
return s_base64.Encode(message);
3234
}
3335

3436
ByteBuffer HashingUtils::Base64Decode(const Aws::String& encodedMessage)
3537
{
36-
Base64::Base64 base64Decoder;
37-
return base64Decoder.Decode(encodedMessage);
38+
return s_base64.Decode(encodedMessage);
3839
}
3940

4041
ByteBuffer HashingUtils::CalculateSHA256HMAC(const ByteBuffer& toSign, const ByteBuffer& secret)

src/aws-cpp-sdk-core/source/utils/crypto/CryptoBuf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ namespace Aws
295295
//the assumption here is that tellp() will always be 0 or >= 16 bytes. The block offset should only
296296
//be the offset of the first block read.
297297
size_t len = cryptoBuf.GetLength();
298-
size_t blockOffset = m_stream.tellp() > m_blockOffset ? 0 : m_blockOffset;
298+
size_t blockOffset = m_stream.good() && m_stream.tellp() > m_blockOffset ? 0 : m_blockOffset;
299299
if (len > blockOffset)
300300
{
301301
m_stream.write(reinterpret_cast<char*>(cryptoBuf.GetUnderlyingData() + blockOffset), len - blockOffset);

src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTHash.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Aws
1010
{
1111
namespace Crypto
1212
{
13+
static const char CRT_HASH_LOG_TAG[] = "CRTHash";
14+
1315
CRTHash::CRTHash(Crt::Crypto::Hash &&toSeat) : m_hash(std::move(toSeat)) {}
1416

1517
HashResult Crypto::CRTHash::Calculate(const String &str)
@@ -28,7 +30,17 @@ namespace Aws
2830
}
2931

3032
Crypto::HashResult Crypto::CRTHash::Calculate(IStream &stream) {
33+
if (stream.bad())
34+
{
35+
AWS_LOGSTREAM_ERROR(CRT_HASH_LOG_TAG, "CRT Hash Update Failed stream in valid state")
36+
return false;
37+
}
3138
stream.seekg(0, stream.beg);
39+
if (stream.bad())
40+
{
41+
AWS_LOGSTREAM_ERROR(CRT_HASH_LOG_TAG, "CRT Hash Update Failed stream in valid state")
42+
return false;
43+
}
3244
const auto result = [this, &stream]() -> HashResult {
3345
uint8_t streamBuffer[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE];
3446
while (stream.good())
@@ -40,7 +52,7 @@ namespace Aws
4052
auto curToHash = Crt::ByteCursorFromArray(streamBuffer, static_cast<size_t>(bytesRead));
4153
if (!m_hash.Update(curToHash))
4254
{
43-
// log here.
55+
AWS_LOGSTREAM_ERROR(CRT_HASH_LOG_TAG, "CRT Hash Update Failed with error code: " << m_hash.LastError())
4456
return false;
4557
}
4658
}
@@ -55,7 +67,7 @@ namespace Aws
5567
auto outBuffer = Crt::ByteBufFromEmptyArray(resultBuffer.GetUnderlyingData(), resultBuffer.GetSize());
5668
if (!m_hash.Digest(outBuffer))
5769
{
58-
//log
70+
AWS_LOGSTREAM_ERROR(CRT_HASH_LOG_TAG, "CRT Hash Digest Failed with error code: " << m_hash.LastError())
5971
return false;
6072
}
6173
resultBuffer.SetLength(m_hash.DigestSize());
@@ -77,7 +89,7 @@ namespace Aws
7789
auto outBuffer = Crt::ByteBufFromEmptyArray(resultBuffer.GetUnderlyingData(), resultBuffer.GetSize());
7890
if (!m_hash.Digest(outBuffer))
7991
{
80-
//log
92+
AWS_LOGSTREAM_ERROR(CRT_HASH_LOG_TAG, "CRT Hash Digest Failed with error code: " << m_hash.LastError())
8193
return false;
8294
}
8395

src/aws-cpp-sdk-core/source/utils/crypto/crt/CRTSecureRandomBytes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ namespace Aws
1111
{
1212
namespace Crypto
1313
{
14+
static const char SECURE_RANDOM_BYTES_LOG_TAG[] = "CRTSecureRandomBytes";
15+
1416
void CRTSecureRandomBytes::GetBytes(unsigned char *buffer, std::size_t bufferSize)
1517
{
1618
auto outputBuf = Crt::ByteBufFromEmptyArray(buffer, bufferSize);
1719
if (!Crt::Crypto::GenerateRandomBytes(outputBuf, bufferSize))
1820
{
21+
AWS_LOGSTREAM_ERROR(SECURE_RANDOM_BYTES_LOG_TAG, "CRT Generate Randomg Bytes Failed")
1922
AWS_UNREACHABLE();
2023
}
2124
}

0 commit comments

Comments
 (0)