-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathHashTest.cpp
265 lines (206 loc) · 9.09 KB
/
HashTest.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* 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/Hash.h>
#include <aws/testing/aws_test_harness.h>
#include <utility>
#if !BYO_CRYPTO
static int s_TestSHA256ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Crypto::Hash sha256 = Aws::Crt::Crypto::Hash::CreateSHA256(allocator);
ASSERT_TRUE(sha256);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("abc");
uint8_t expected[] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
};
Aws::Crt::ByteBuf expectedBuf = Aws::Crt::ByteBufFromArray(expected, sizeof(expected));
uint8_t output[Aws::Crt::Crypto::SHA256_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(sha256.Update(input));
ASSERT_TRUE(sha256.Digest(outputBuf));
ASSERT_UINT_EQUALS(Aws::Crt::Crypto::SHA256_DIGEST_SIZE, sha256.DigestSize());
ASSERT_FALSE(sha256);
ASSERT_BIN_ARRAYS_EQUALS(expectedBuf.buffer, expectedBuf.len, outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(SHA256ResourceSafety, s_TestSHA256ResourceSafety)
static int s_TestMD5ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Crypto::Hash md5 = Aws::Crt::Crypto::Hash::CreateMD5(allocator);
ASSERT_TRUE(md5);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("abc");
uint8_t expected[] = {
0x90,
0x01,
0x50,
0x98,
0x3c,
0xd2,
0x4f,
0xb0,
0xd6,
0x96,
0x3f,
0x7d,
0x28,
0xe1,
0x7f,
0x72,
};
Aws::Crt::ByteBuf expectedBuf = Aws::Crt::ByteBufFromArray(expected, sizeof(expected));
uint8_t output[Aws::Crt::Crypto::MD5_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(md5.Update(input));
ASSERT_TRUE(md5.Digest(outputBuf));
ASSERT_UINT_EQUALS(Aws::Crt::Crypto::MD5_DIGEST_SIZE, md5.DigestSize());
ASSERT_FALSE(md5);
ASSERT_BIN_ARRAYS_EQUALS(expectedBuf.buffer, expectedBuf.len, outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(MD5ResourceSafety, s_TestMD5ResourceSafety)
static int s_TestSHA1ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Crypto::Hash sha1 = Aws::Crt::Crypto::Hash::CreateSHA1(allocator);
ASSERT_TRUE(sha1);
Aws::Crt::ByteCursor input =
aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnop"
"jklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
uint8_t expected[] = {0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19,
0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59};
Aws::Crt::ByteBuf expectedBuf = Aws::Crt::ByteBufFromArray(expected, sizeof(expected));
uint8_t output[Aws::Crt::Crypto::SHA1_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(sha1.Update(input));
ASSERT_TRUE(sha1.Digest(outputBuf));
ASSERT_UINT_EQUALS(Aws::Crt::Crypto::SHA1_DIGEST_SIZE, sha1.DigestSize());
ASSERT_FALSE(sha1);
ASSERT_BIN_ARRAYS_EQUALS(expectedBuf.buffer, expectedBuf.len, outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(SHA1ResourceSafety, s_TestSHA1ResourceSafety)
#else
class ByoCryptoHashInterceptor : public Aws::Crt::Crypto::ByoHash
{
public:
ByoCryptoHashInterceptor(size_t digestSize, Aws::Crt::Allocator *allocator, Aws::Crt::String output)
: ByoHash(digestSize, allocator), m_output(std::move(output))
{
}
~ByoCryptoHashInterceptor() {}
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; }
private:
Aws::Crt::String m_receivedInput;
Aws::Crt::String m_output;
};
static int s_TestSHA256ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
uint8_t expected[] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
};
Aws::Crt::String expectedStr = Aws::Crt::String(reinterpret_cast<const char *>(expected), sizeof(expected));
apiHandle.SetBYOCryptoNewSHA256Callback(
[&](size_t digestSize, Aws::Crt::Allocator *allocator)
{ return Aws::Crt::MakeShared<ByoCryptoHashInterceptor>(allocator, digestSize, allocator, expectedStr); });
Aws::Crt::Crypto::Hash sha256 = Aws::Crt::Crypto::Hash::CreateSHA256(allocator);
ASSERT_TRUE(sha256);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("abc");
uint8_t output[Aws::Crt::Crypto::SHA256_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(sha256.Update(input));
ASSERT_TRUE(sha256.Digest(outputBuf));
ASSERT_FALSE(sha256);
ASSERT_BIN_ARRAYS_EQUALS(expectedStr.c_str(), expectedStr.length(), outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(SHA256ResourceSafety, s_TestSHA256ResourceSafety)
static int s_TestSHA1ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
uint8_t expected[] = {
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41,
0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3,
};
Aws::Crt::String expectedStr = Aws::Crt::String(reinterpret_cast<const char *>(expected), sizeof(expected));
apiHandle.SetBYOCryptoNewSHA1Callback(
[&](size_t digestSize, Aws::Crt::Allocator *allocator)
{ return Aws::Crt::MakeShared<ByoCryptoHashInterceptor>(allocator, digestSize, allocator, expectedStr); });
Aws::Crt::Crypto::Hash sha1 = Aws::Crt::Crypto::Hash::CreateSHA1(allocator);
ASSERT_TRUE(sha1);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("abc");
uint8_t output[Aws::Crt::Crypto::SHA1_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(sha1.Update(input));
ASSERT_TRUE(sha1.Digest(outputBuf));
ASSERT_FALSE(sha1);
ASSERT_BIN_ARRAYS_EQUALS(expectedStr.c_str(), expectedStr.length(), outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(SHA1ResourceSafety, s_TestSHA1ResourceSafety)
static int s_TestMD5ResourceSafety(struct aws_allocator *allocator, void *)
{
{
Aws::Crt::ApiHandle apiHandle(allocator);
uint8_t expected[] = {
0x90,
0x01,
0x50,
0x98,
0x3c,
0xd2,
0x4f,
0xb0,
0xd6,
0x96,
0x3f,
0x7d,
0x28,
0xe1,
0x7f,
0x72,
};
Aws::Crt::String expectedStr = Aws::Crt::String(reinterpret_cast<const char *>(expected), sizeof(expected));
apiHandle.SetBYOCryptoNewMD5Callback(
[&](size_t digestSize, struct aws_allocator *allocator)
{ return Aws::Crt::MakeShared<ByoCryptoHashInterceptor>(allocator, digestSize, allocator, expectedStr); });
Aws::Crt::Crypto::Hash md5 = Aws::Crt::Crypto::Hash::CreateMD5(allocator);
ASSERT_TRUE(md5);
Aws::Crt::ByteCursor input = aws_byte_cursor_from_c_str("abc");
uint8_t output[Aws::Crt::Crypto::MD5_DIGEST_SIZE] = {0};
Aws::Crt::ByteBuf outputBuf = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
ASSERT_TRUE(md5.Update(input));
ASSERT_TRUE(md5.Digest(outputBuf));
ASSERT_FALSE(md5);
ASSERT_BIN_ARRAYS_EQUALS(expectedStr.c_str(), expectedStr.length(), outputBuf.buffer, outputBuf.len);
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(MD5ResourceSafety, s_TestMD5ResourceSafety)
#endif /* BYO_CRYPTO */