Skip to content

Commit

Permalink
Add oe_sha256() internal convenience function
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Tendyck <[email protected]>
  • Loading branch information
thomasten committed May 7, 2020
1 parent c6aaf8f commit dffbd47
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
17 changes: 17 additions & 0 deletions common/sha.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.

#include <openenclave/internal/crypto/sha.h>
#include <openenclave/internal/raise.h>

oe_result_t oe_sha256(const void* data, size_t size, OE_SHA256* sha256)
{
oe_result_t result = OE_FAILURE;
oe_sha256_context_t ctx = {0};
OE_CHECK(oe_sha256_init(&ctx));
OE_CHECK(oe_sha256_update(&ctx, data, size));
OE_CHECK(oe_sha256_final(&ctx, sha256));
result = OE_OK;
done:
return result;
}
1 change: 1 addition & 0 deletions enclave/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_enclave_library(
STATIC
../common/attest_plugin.c
../common/datetime.c
../common/sha.c
asym_keys.c
link.c
tls_cert.c
Expand Down
1 change: 1 addition & 0 deletions host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ list(
../common/attest_plugin.c
../common/datetime.c
../common/safecrt.c
../common/sha.c
hexdump.c
dupenv.c
fopen.c
Expand Down
1 change: 1 addition & 0 deletions host/measure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ list(
APPEND
PLATFORM_HOST_MR_SRC
../common/safecrt.c
../common/sha.c
hexdump.c
dupenv.c
fopen.c
Expand Down
14 changes: 14 additions & 0 deletions include/openenclave/internal/crypto/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ oe_result_t oe_sha256_update(
*/
oe_result_t oe_sha256_final(oe_sha256_context_t* context, OE_SHA256* sha256);

/**
* Computes the SHA-256 hash of the input data
*
* This is a convenience function that can be used if the full data is
* available at once.
*
* @param[in] data buffer of data to be hashed
* @param[in] size size of the buffer
* @param[out] sha256 buffer where hash is written
*
* @return OE_OK upon success
*/
oe_result_t oe_sha256(const void* data, size_t size, OE_SHA256* sha256);

OE_EXTERNC_END

#endif /* _OE_SHA_H */
20 changes: 14 additions & 6 deletions tests/crypto/sha_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ void TestSHA(void)
{
printf("=== begin %s()\n", __FUNCTION__);

OE_SHA256 hash = {0};
oe_sha256_context_t ctx = {0};
oe_sha256_init(&ctx);
oe_sha256_update(&ctx, ALPHABET, strlen(ALPHABET));
oe_sha256_final(&ctx, &hash);
OE_TEST(memcmp(&hash, &ALPHABET_HASH, sizeof(OE_SHA256)) == 0);
{
OE_SHA256 hash = {0};
oe_sha256_context_t ctx = {0};
oe_sha256_init(&ctx);
oe_sha256_update(&ctx, ALPHABET, strlen(ALPHABET));
oe_sha256_final(&ctx, &hash);
OE_TEST(memcmp(&hash, &ALPHABET_HASH, sizeof(OE_SHA256)) == 0);
}

{
OE_SHA256 hash = {0};
OE_TEST(oe_sha256(ALPHABET, strlen(ALPHABET), &hash) == OE_OK);
OE_TEST(memcmp(&hash, &ALPHABET_HASH, sizeof(OE_SHA256)) == 0);
}

printf("=== passed %s()\n", __FUNCTION__);
}

0 comments on commit dffbd47

Please sign in to comment.