diff --git a/common/sha.c b/common/sha.c new file mode 100644 index 0000000000..1899aeac61 --- /dev/null +++ b/common/sha.c @@ -0,0 +1,17 @@ +// Copyright (c) Open Enclave SDK contributors. +// Licensed under the MIT License. + +#include +#include + +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; +} diff --git a/enclave/CMakeLists.txt b/enclave/CMakeLists.txt index 33dc961aac..dff9e6ef68 100644 --- a/enclave/CMakeLists.txt +++ b/enclave/CMakeLists.txt @@ -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 diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 8e87d8aade..f25dfd1414 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -337,6 +337,7 @@ list( ../common/attest_plugin.c ../common/datetime.c ../common/safecrt.c + ../common/sha.c hexdump.c dupenv.c fopen.c diff --git a/host/measure/CMakeLists.txt b/host/measure/CMakeLists.txt index e03fdad6d6..c902057dfc 100644 --- a/host/measure/CMakeLists.txt +++ b/host/measure/CMakeLists.txt @@ -74,6 +74,7 @@ list( APPEND PLATFORM_HOST_MR_SRC ../common/safecrt.c + ../common/sha.c hexdump.c dupenv.c fopen.c diff --git a/include/openenclave/internal/crypto/sha.h b/include/openenclave/internal/crypto/sha.h index 796b091c47..39aac0580f 100644 --- a/include/openenclave/internal/crypto/sha.h +++ b/include/openenclave/internal/crypto/sha.h @@ -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 */ diff --git a/tests/crypto/sha_tests.c b/tests/crypto/sha_tests.c index 5d62032cae..dbeee05a45 100644 --- a/tests/crypto/sha_tests.c +++ b/tests/crypto/sha_tests.c @@ -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__); }