Skip to content

Commit bd9d368

Browse files
Bind out hkdf (#791)
1 parent 1b54c87 commit bd9d368

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

include/aws/crt/crypto/HKDF.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
namespace Aws
10+
{
11+
namespace Crt
12+
{
13+
namespace Crypto
14+
{
15+
/**
16+
* Derives an SHA256 HMAC HKDF using the default allocator and writes it to out.
17+
* If this function fails, Aws::Crt::LastError() will contain the error that occurred.
18+
*/
19+
bool AWS_CRT_CPP_API DeriveSHA512HMACHKDF(
20+
Allocator *allocator,
21+
ByteCursor ikm,
22+
ByteCursor salt,
23+
ByteCursor info,
24+
ByteBuf &out,
25+
size_t length) noexcept;
26+
} // namespace Crypto
27+
} // namespace Crt
28+
} // namespace Aws

include/aws/crt/crypto/HMAC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ namespace Aws
144144

145145
/**
146146
* Complete the HMAC computation and write the final digest to output.
147-
* This cannote be called more than once.
147+
* This cannot be called more than once.
148148
* If truncate_to is something other than 0, the output must be truncated to that number of bytes.
149149
* Raise an AWS error and return false to indicate failure.
150150
*/

source/crypto/HKDF.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
#include <aws/crt/crypto/HKDF.h>
6+
7+
#include <aws/cal/hkdf.h>
8+
9+
namespace Aws
10+
{
11+
namespace Crt
12+
{
13+
namespace Crypto
14+
{
15+
bool DeriveSHA512HMACHKDF(
16+
Allocator *allocator,
17+
ByteCursor ikm,
18+
ByteCursor salt,
19+
ByteCursor info,
20+
ByteBuf &out,
21+
size_t length) noexcept
22+
{
23+
return aws_hkdf_derive(allocator, HKDF_HMAC_SHA512, ikm, salt, info, &out, length) == AWS_OP_SUCCESS;
24+
}
25+
} // namespace Crypto
26+
} // namespace Crt
27+
} // namespace Aws

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ add_test_case(CborSanityTest)
318318
add_test_case(CborTimeStampTest)
319319
add_test_case(CborLastErrorTest)
320320

321+
if (NOT BYO_CRYPTO)
322+
add_test_case(HKDFPiping)
323+
endif()
324+
321325
generate_cpp_test_driver(${TEST_BINARY_NAME})
322326

323327
aws_add_sanitizers(${TEST_BINARY_NAME})

tests/HKDFTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
#include <aws/crt/Api.h>
6+
#include <aws/crt/crypto/HKDF.h>
7+
#include <aws/testing/aws_test_harness.h>
8+
9+
static int s_TestHKDFPiping(struct aws_allocator *allocator, void *)
10+
{
11+
Aws::Crt::ApiHandle apiHandle(allocator);
12+
13+
uint8_t ikm[] = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
14+
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
15+
Aws::Crt::ByteCursor ikm_cur = aws_byte_cursor_from_array(ikm, sizeof(ikm));
16+
17+
uint8_t salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c};
18+
Aws::Crt::ByteCursor salt_cur = aws_byte_cursor_from_array(salt, sizeof(salt));
19+
20+
uint8_t info[] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9};
21+
Aws::Crt::ByteCursor info_cur = aws_byte_cursor_from_array(info, sizeof(info));
22+
23+
uint8_t output[64] = {0};
24+
Aws::Crt::ByteBuf ret = Aws::Crt::ByteBufFromEmptyArray(output, sizeof(output));
25+
26+
ASSERT_TRUE(Aws::Crt::Crypto::DeriveSHA512HMACHKDF(allocator, ikm_cur, salt_cur, info_cur, ret, 42));
27+
28+
uint8_t expected[] = {0x83, 0x23, 0x90, 0x08, 0x6c, 0xda, 0x71, 0xfb, 0x47, 0x62, 0x5b, 0xb5, 0xce, 0xb1,
29+
0x68, 0xe4, 0xc8, 0xe2, 0x6a, 0x1a, 0x16, 0xed, 0x34, 0xd9, 0xfc, 0x7f, 0xe9, 0x2c,
30+
0x14, 0x81, 0x57, 0x93, 0x38, 0xda, 0x36, 0x2c, 0xb8, 0xd9, 0xf9, 0x25, 0xd7, 0xcb};
31+
32+
ASSERT_BIN_ARRAYS_EQUALS(ret.buffer, ret.len, expected, sizeof(expected));
33+
34+
return AWS_OP_SUCCESS;
35+
}
36+
AWS_TEST_CASE(HKDFPiping, s_TestHKDFPiping)

0 commit comments

Comments
 (0)