-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathHMACTest.cpp
121 lines (97 loc) · 4.6 KB
/
HMACTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Api.h>
#include <aws/crt/crypto/HMAC.h>
#include <aws/testing/aws_test_harness.h>
#if !BYO_CRYPTO
static int s_TestSHA256HMACResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
uint8_t secret[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
};
Aws::Crt::ByteCursor secretCur = aws_byte_cursor_from_array(secret, sizeof(secret));
Aws::Crt::Crypto::HMAC sha256Hmac = Aws::Crt::Crypto::HMAC::CreateSHA256HMAC(allocator, secretCur);
ASSERT_TRUE(sha256Hmac);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("Hi There");
uint8_t expected[] = {
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
};
Aws::Crt::ByteBuf expectedBuf = Aws::Crt::ByteBufFromArray(expected, sizeof(expected));
uint8_t output[Aws::Crt::Crypto::SHA256_HMAC_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_UINT_EQUALS(Aws::Crt::Crypto::SHA256_HMAC_DIGEST_SIZE, sha256Hmac.DigestSize());
ASSERT_TRUE(sha256Hmac.Update(input));
ASSERT_TRUE(sha256Hmac.Digest(outputBuf));
ASSERT_FALSE(sha256Hmac);
ASSERT_BIN_ARRAYS_EQUALS(expectedBuf.buffer, expectedBuf.len, outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
#else
class ByoCryptoHMACInterceptor : public Aws::Crt::Crypto::ByoHMAC
{
public:
ByoCryptoHMACInterceptor(
size_t digestSize,
Aws::Crt::Allocator *allocator,
const Aws::Crt::ByteCursor &secret,
Aws::Crt::String output)
: ByoHMAC(digestSize, secret, allocator), m_output(std::move(output))
{
m_secret = Aws::Crt::String(reinterpret_cast<const char *>(secret.ptr), secret.len);
}
bool UpdateInternal(const Aws::Crt::ByteCursor &toHash) noexcept
{
m_receivedInput = Aws::Crt::String(reinterpret_cast<const char *>(toHash.ptr), toHash.len);
return true;
}
bool DigestInternal(Aws::Crt::ByteBuf &output, size_t) noexcept
{
aws_byte_buf_write(&output, reinterpret_cast<const uint8_t *>(m_output.data()), m_output.length());
return true;
}
const Aws::Crt::String &GetReceivedInput() const { return m_receivedInput; }
const Aws::Crt::String &GetSecret() const { return m_secret; }
private:
Aws::Crt::String m_secret;
Aws::Crt::String m_receivedInput;
Aws::Crt::String m_output;
};
static int s_TestSHA256HMACResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
uint8_t secret[] = {
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
};
Aws::Crt::ByteCursor secretCur = aws_byte_cursor_from_array(secret, sizeof(secret));
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("Hi There");
uint8_t expected[] = {
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
};
Aws::Crt::String expectedStr(reinterpret_cast<const char *>(expected), sizeof(expected));
apiHandle.SetBYOCryptoNewSHA256HMACCallback(
[&](size_t digest_size, const Aws::Crt::ByteCursor &secretCur, Aws::Crt::Allocator *allocator) {
return Aws::Crt::MakeShared<ByoCryptoHMACInterceptor>(
allocator, digest_size, allocator, secretCur, expectedStr);
});
uint8_t output[Aws::Crt::Crypto::SHA256_HMAC_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
Aws::Crt::Crypto::HMAC sha256Hmac = Aws::Crt::Crypto::HMAC::CreateSHA256HMAC(allocator, secretCur);
ASSERT_TRUE(sha256Hmac.Update(input));
ASSERT_TRUE(sha256Hmac.Digest(outputBuf));
ASSERT_FALSE(sha256Hmac);
ASSERT_BIN_ARRAYS_EQUALS(expectedStr.c_str(), expectedStr.length(), outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
#endif
AWS_TEST_CASE(SHA256HMACResourceSafety, s_TestSHA256HMACResourceSafety)