Skip to content

Commit 076e7fc

Browse files
author
Bret Ambrose
committed
Merge branch 'main' into MqttTestRefactor
2 parents ffe7718 + aef8478 commit 076e7fc

File tree

11 files changed

+440
-3
lines changed

11 files changed

+440
-3
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.37.1
1+
0.37.2

include/aws/crt/Api.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ namespace Aws
9393
*/
9494
void SetBYOCryptoNewSHA256Callback(Crypto::CreateHashCallback &&callback);
9595

96+
/**
97+
* BYO_CRYPTO: set callback for creating SHA256 hashes.
98+
* If using BYO_CRYPTO, you must call this.
99+
*/
100+
void SetBYOCryptoNewSHA512Callback(Crypto::CreateHashCallback &&callback);
101+
96102
/**
97103
* BYO_CRYPTO: set callback for creating SHA1 hashes.
98104
* If using BYO_CRYPTO, you must call this.
@@ -105,6 +111,12 @@ namespace Aws
105111
*/
106112
void SetBYOCryptoNewSHA256HMACCallback(Crypto::CreateHMACCallback &&callback);
107113

114+
/**
115+
* BYO_CRYPTO: set callback for creating Streaming SHA512 HMAC objects.
116+
* If using BYO_CRYPTO, you must call this.
117+
*/
118+
void SetBYOCryptoNewSHA512HMACCallback(Crypto::CreateHMACCallback &&callback);
119+
108120
/**
109121
* BYO_CRYPTO: set callback for creating a ClientTlsChannelHandler.
110122
* If using BYO_CRYPTO, you must call this prior to creating any client channels in the

include/aws/crt/checksum/XXHash.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
/**
3+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
* SPDX-License-Identifier: Apache-2.0.
5+
*/
6+
#include <aws/crt/Exports.h>
7+
#include <aws/crt/Types.h>
8+
9+
struct aws_xxhash;
10+
namespace Aws
11+
{
12+
namespace Crt
13+
{
14+
namespace Checksum
15+
{
16+
/**
17+
* Computes a XXHash64 Hash over input, and writes the digest to output.
18+
* Returns true on success. If this function fails,
19+
* Aws::Crt::LastError() will contain the error that occurred.
20+
*/
21+
bool AWS_CRT_CPP_API ComputeXXHash64(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept;
22+
23+
/**
24+
* Computes a XXHash3_64 Hash using the default allocator over input, and writes the digest to output.
25+
* Returns true on success. If this function fails,
26+
* Aws::Crt::LastError() will contain the error that occurred.
27+
*/
28+
bool AWS_CRT_CPP_API
29+
ComputeXXHash3_64(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept;
30+
31+
/**
32+
* Computes a XXHash3_128 Hash using the default allocator over input, and writes the digest to output.
33+
* Returns true on success. If this function fails,
34+
* Aws::Crt::LastError() will contain the error that occurred.
35+
*/
36+
bool AWS_CRT_CPP_API
37+
ComputeXXHash3_128(const ByteCursor &input, ByteBuf &output, uint64_t seed = 0) noexcept;
38+
39+
/**
40+
* Streaming Hash object. The typical use case is for computing the hash of an object that is too large to
41+
* load into memory. You can call Update() multiple times as you load chunks of data into memory. When
42+
* you're finished simply call Digest(). After Digest() is called, this object is no longer usable.
43+
*/
44+
class AWS_CRT_CPP_API XXHash final
45+
{
46+
public:
47+
XXHash(const XXHash &) = delete;
48+
XXHash &operator=(const XXHash &) = delete;
49+
XXHash(XXHash &&toMove) noexcept = default;
50+
XXHash &operator=(XXHash &&toMove) noexcept = default;
51+
52+
/**
53+
* Returns the value of the last aws error encountered by operations on this instance.
54+
*/
55+
inline int LastError() const noexcept { return m_lastError; }
56+
57+
/**
58+
* Creates an instance of a Streaming XXHash64 Hash.
59+
*/
60+
static XXHash CreateXXHash64(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept;
61+
62+
/**
63+
* Creates an instance of a Streaming XXHash3_64 Hash.
64+
*/
65+
static XXHash CreateXXHash3_64(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept;
66+
67+
/**
68+
* Creates an instance of a Streaming XXHash3_128 Hash.
69+
*/
70+
static XXHash CreateXXHash3_128(uint64_t seed = 0, Allocator *allocator = ApiAllocator()) noexcept;
71+
72+
/**
73+
* Updates the running hash object with data in toHash. Returns true on success. Call
74+
* LastError() for the reason this call failed.
75+
*/
76+
bool Update(const ByteCursor &toHash) noexcept;
77+
78+
/**
79+
* Finishes the running hash operation and writes the digest into output.
80+
* Returns true on success. Call LastError() for the reason this
81+
* call failed.
82+
*/
83+
bool Digest(ByteBuf &output) noexcept;
84+
85+
private:
86+
XXHash(aws_xxhash *hash) noexcept;
87+
XXHash() = delete;
88+
89+
ScopedResource<struct aws_xxhash> m_hash;
90+
int m_lastError;
91+
};
92+
} // namespace Checksum
93+
} // namespace Crt
94+
} // namespace Aws

include/aws/crt/crypto/Hash.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Aws
1717
{
1818
static const size_t SHA1_DIGEST_SIZE = AWS_SHA1_LEN;
1919
static const size_t SHA256_DIGEST_SIZE = AWS_SHA256_LEN;
20+
static const size_t SHA512_DIGEST_SIZE = AWS_SHA512_LEN;
2021
static const size_t MD5_DIGEST_SIZE = AWS_MD5_LEN;
2122

2223
/**
@@ -64,7 +65,7 @@ namespace Aws
6465
* Computes a SHA1 Hash over input, and writes the digest to output. If truncateTo is non-zero, the digest
6566
* will be truncated to the value of truncateTo. Returns true on success. If this function fails,
6667
* Aws::Crt::LastError() will contain the error that occurred. Unless you're using 'truncateTo',
67-
* output should have a minimum capacity of MD5_DIGEST_SIZE.
68+
* output should have a minimum capacity of SHA1_DIGEST_SIZE.
6869
*/
6970
bool AWS_CRT_CPP_API ComputeSHA1(
7071
Allocator *allocator,
@@ -80,6 +81,27 @@ namespace Aws
8081
*/
8182
bool AWS_CRT_CPP_API ComputeSHA1(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
8283

84+
/**
85+
* Computes a SHA512 Hash over input, and writes the digest to output. If truncateTo is non-zero, the digest
86+
* will be truncated to the value of truncateTo. Returns true on success. If this function fails,
87+
* Aws::Crt::LastError() will contain the error that occurred. Unless you're using 'truncateTo',
88+
* output should have a minimum capacity of SHA512_DIGEST_SIZE.
89+
*/
90+
bool AWS_CRT_CPP_API ComputeSHA512(
91+
Allocator *allocator,
92+
const ByteCursor &input,
93+
ByteBuf &output,
94+
size_t truncateTo = 0) noexcept;
95+
96+
/**
97+
* Computes a SHA512 Hash using the default allocator over input, and writes the digest to output. If
98+
* truncateTo is non-zero, the digest will be truncated to the value of truncateTo. Returns true on success.
99+
* If this function fails, Aws::Crt::LastError() will contain the error that occurred. Unless you're using
100+
* 'truncateTo', output should have a minimum capacity of SHA512_DIGEST_SIZE.
101+
*/
102+
bool AWS_CRT_CPP_API
103+
ComputeSHA512(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
104+
83105
/**
84106
* Streaming Hash object. The typical use case is for computing the hash of an object that is too large to
85107
* load into memory. You can call Update() multiple times as you load chunks of data into memory. When
@@ -114,6 +136,11 @@ namespace Aws
114136
*/
115137
static Hash CreateSHA1(Allocator *allocator = ApiAllocator()) noexcept;
116138

139+
/**
140+
* Creates an instance of a Stream SHA512 Hash.
141+
*/
142+
static Hash CreateSHA512(Allocator *allocator = ApiAllocator()) noexcept;
143+
117144
/**
118145
* Creates an instance of a Streaming MD5 Hash.
119146
*/

source/Api.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Aws
2424
{
2525
static Crypto::CreateHashCallback s_BYOCryptoNewMD5Callback;
2626
static Crypto::CreateHashCallback s_BYOCryptoNewSHA256Callback;
27+
static Crypto::CreateHashCallback s_BYOCryptoNewSHA512Callback;
2728
static Crypto::CreateHashCallback s_BYOCryptoNewSHA1Callback;
2829
static Crypto::CreateHMACCallback s_BYOCryptoNewSHA256HMACCallback;
2930
static Io::NewClientTlsHandlerCallback s_BYOCryptoNewClientTlsHandlerCallback;
@@ -84,6 +85,7 @@ namespace Aws
8485

8586
s_BYOCryptoNewMD5Callback = nullptr;
8687
s_BYOCryptoNewSHA256Callback = nullptr;
88+
s_BYOCryptoNewSHA512Callback = nullptr;
8789
s_BYOCryptoNewSHA256HMACCallback = nullptr;
8890
s_BYOCryptoNewClientTlsHandlerCallback = nullptr;
8991
s_BYOCryptoNewTlsContextImplCallback = nullptr;
@@ -189,6 +191,31 @@ namespace Aws
189191
aws_set_sha256_new_fn(s_Sha256New);
190192
}
191193

194+
static struct aws_hash *s_Sha512New(struct aws_allocator *allocator)
195+
{
196+
if (!s_BYOCryptoNewSHA512Callback)
197+
{
198+
AWS_LOGF_ERROR(
199+
AWS_LS_IO_TLS,
200+
"Must call ApiHandle::SetBYOCryptoNewSHA512Callback() before SHA256 hash can be created");
201+
aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
202+
return nullptr;
203+
}
204+
205+
auto hash = s_BYOCryptoNewSHA512Callback(AWS_SHA512_LEN, allocator);
206+
if (!hash)
207+
{
208+
return nullptr;
209+
}
210+
return hash->SeatForCInterop(hash);
211+
}
212+
213+
void ApiHandle::SetBYOCryptoNewSHA512Callback(Crypto::CreateHashCallback &&callback)
214+
{
215+
s_BYOCryptoNewSHA512Callback = std::move(callback);
216+
aws_set_sha512_new_fn(s_Sha512New);
217+
}
218+
192219
static struct aws_hash *s_Sha1New(struct aws_allocator *allocator)
193220
{
194221
if (!s_BYOCryptoNewSHA1Callback)
@@ -308,6 +335,12 @@ namespace Aws
308335
AWS_LS_IO_TLS, "SetBYOCryptoNewSHA256Callback() has no effect unless compiled with BYO_CRYPTO");
309336
}
310337

338+
void ApiHandle::SetBYOCryptoNewSHA512Callback(Crypto::CreateHashCallback &&)
339+
{
340+
AWS_LOGF_WARN(
341+
AWS_LS_IO_TLS, "SetBYOCryptoNewSHA512Callback() has no effect unless compiled with BYO_CRYPTO");
342+
}
343+
311344
void ApiHandle::SetBYOCryptoNewSHA1Callback(Crypto::CreateHashCallback &&)
312345
{
313346
AWS_LOGF_WARN(AWS_LS_IO_TLS, "SetBYOCryptoNewSHA1Callback() has no effect unless compiled with BYO_CRYPTO");
@@ -319,6 +352,12 @@ namespace Aws
319352
AWS_LS_IO_TLS, "SetBYOCryptoNewSHA256HMACCallback() has no effect unless compiled with BYO_CRYPTO");
320353
}
321354

355+
void ApiHandle::SetBYOCryptoNewSHA512HMACCallback(Crypto::CreateHMACCallback &&)
356+
{
357+
AWS_LOGF_WARN(
358+
AWS_LS_IO_TLS, "SetBYOCryptoNewSHA256HMACCallback() has no effect unless compiled with BYO_CRYPTO");
359+
}
360+
322361
void ApiHandle::SetBYOCryptoClientTlsCallback(Io::NewClientTlsHandlerCallback &&)
323362
{
324363
AWS_LOGF_WARN(

source/checksum/XXHash.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
#include <aws/checksums/xxhash.h>
6+
#include <aws/crt/Api.h>
7+
#include <aws/crt/checksum/XXHash.h>
8+
9+
namespace Aws
10+
{
11+
namespace Crt
12+
{
13+
namespace Checksum
14+
{
15+
bool ComputeXXHash64(const ByteCursor &input, ByteBuf &output, uint64_t seed) noexcept
16+
{
17+
return aws_xxhash64_compute(seed, input, &output) == AWS_OP_SUCCESS;
18+
}
19+
20+
bool ComputeXXHash3_64(const ByteCursor &input, ByteBuf &output, uint64_t seed) noexcept
21+
{
22+
return aws_xxhash3_64_compute(seed, input, &output) == AWS_OP_SUCCESS;
23+
}
24+
25+
bool ComputeXXHash3_128(const ByteCursor &input, ByteBuf &output, uint64_t seed) noexcept
26+
{
27+
return aws_xxhash3_128_compute(seed, input, &output) == AWS_OP_SUCCESS;
28+
}
29+
30+
XXHash::XXHash(aws_xxhash *hash) noexcept : m_hash(hash, aws_xxhash_destroy), m_lastError(0)
31+
{
32+
if (hash == nullptr)
33+
{
34+
m_lastError = Crt::LastError();
35+
}
36+
}
37+
38+
XXHash XXHash::CreateXXHash64(uint64_t seed, Allocator *allocator) noexcept
39+
{
40+
return XXHash(aws_xxhash64_new(allocator, seed));
41+
}
42+
43+
XXHash XXHash::CreateXXHash3_64(uint64_t seed, Allocator *allocator) noexcept
44+
{
45+
return XXHash(aws_xxhash3_64_new(allocator, seed));
46+
}
47+
48+
XXHash XXHash::CreateXXHash3_128(uint64_t seed, Allocator *allocator) noexcept
49+
{
50+
return XXHash(aws_xxhash3_128_new(allocator, seed));
51+
}
52+
53+
bool XXHash::Update(const ByteCursor &toHash) noexcept
54+
{
55+
if (AWS_OP_SUCCESS != aws_xxhash_update(m_hash.get(), toHash))
56+
{
57+
m_lastError = aws_last_error();
58+
return false;
59+
}
60+
return true;
61+
}
62+
63+
bool XXHash::Digest(ByteBuf &output) noexcept
64+
{
65+
if (aws_xxhash_finalize(m_hash.get(), &output) != AWS_OP_SUCCESS)
66+
{
67+
m_lastError = aws_last_error();
68+
return false;
69+
}
70+
return true;
71+
}
72+
} // namespace Checksum
73+
} // namespace Crt
74+
} // namespace Aws

source/crypto/Hash.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ namespace Aws
2727
return ComputeSHA256(ApiAllocator(), input, output, truncateTo);
2828
}
2929

30+
bool ComputeSHA512(
31+
Allocator *allocator,
32+
const ByteCursor &input,
33+
ByteBuf &output,
34+
size_t truncateTo) noexcept
35+
{
36+
auto hash = Hash::CreateSHA512(allocator);
37+
return hash.ComputeOneShot(input, output, truncateTo);
38+
}
39+
40+
bool ComputeSHA512(const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
41+
{
42+
return ComputeSHA512(ApiAllocator(), input, output, truncateTo);
43+
}
44+
3045
bool ComputeSHA1(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
3146
{
3247
auto hash = Hash::CreateSHA1(allocator);
@@ -102,6 +117,11 @@ namespace Aws
102117
return Hash(aws_sha1_new(allocator));
103118
}
104119

120+
Hash Hash::CreateSHA512(Allocator *allocator) noexcept
121+
{
122+
return Hash(aws_sha512_new(allocator));
123+
}
124+
105125
bool Hash::Update(const ByteCursor &toHash) noexcept
106126
{
107127
if (!*this)

tests/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ add_test_case(JsonExplicitNull)
6363
add_test_case(JsonBoolTest)
6464
add_test_case(JsonMoveTest)
6565
add_test_case(SHA256ResourceSafety)
66+
add_test_case(SHA512ResourceSafety)
6667
add_test_case(MD5ResourceSafety)
6768
add_test_case(SHA1ResourceSafety)
6869
add_test_case(SHA256HMACResourceSafety)
@@ -320,6 +321,14 @@ if (NOT BYO_CRYPTO)
320321
add_test_case(HKDFPiping)
321322
endif()
322323

324+
add_test_case(CRC32Piping)
325+
add_test_case(CRC32CPiping)
326+
add_test_case(CRC64NVMEPiping)
327+
328+
add_test_case(XXHash64Piping)
329+
add_test_case(XXHash3_64Piping)
330+
add_test_case(XXHash3_128Piping)
331+
323332
generate_cpp_test_driver(${TEST_BINARY_NAME})
324333

325334
aws_add_sanitizers(${TEST_BINARY_NAME})

0 commit comments

Comments
 (0)