diff --git a/src/coreclr/ilasm/CMakeLists.txt b/src/coreclr/ilasm/CMakeLists.txt index 818171c25c749..d7f5ce9a15aa8 100644 --- a/src/coreclr/ilasm/CMakeLists.txt +++ b/src/coreclr/ilasm/CMakeLists.txt @@ -34,8 +34,18 @@ set(ILASM_HEADERS nvpair.h typar.hpp portable_pdb.h + sha256.h ) +if(CLR_CMAKE_TARGET_UNIX AND NOT CLR_CMAKE_TARGET_APPLE) + configure_file( + ../../native/libs/System.Security.Cryptography.Native/pal_crypto_config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/pal_crypto_config.h) + include_directories(../../native/libs/Common) + include_directories(../../native/libs/System.Security.Cryptography.Native) + include_directories(${CMAKE_CURRENT_BINARY_DIR}) +endif(CLR_CMAKE_TARGET_UNIX AND NOT CLR_CMAKE_TARGET_APPLE) + if(CLR_CMAKE_TARGET_WIN32) list(APPEND ILASM_SOURCES ${ILASM_HEADERS}) @@ -107,6 +117,16 @@ else() ) endif(CLR_CMAKE_TARGET_WIN32) +if(CLR_CMAKE_TARGET_UNIX AND NOT CLR_CMAKE_TARGET_APPLE) + list(APPEND ILASM_LINK_LIBRARIES System.Security.Cryptography.Native.OpenSsl-Static) + if(NOT FEATURE_DISTRO_AGNOSTIC_SSL) + find_package(OpenSSL QUIET) + if(OPENSSL_FOUND) + list(APPEND ILASM_LINK_LIBRARIES OpenSSL::SSL OpenSSL::Crypto) + endif(OPENSSL_FOUND) + endif(NOT FEATURE_DISTRO_AGNOSTIC_SSL) +endif(CLR_CMAKE_TARGET_UNIX AND NOT CLR_CMAKE_TARGET_APPLE) + if(CLR_CMAKE_HOST_UNIX) target_link_libraries(ilasm diff --git a/src/coreclr/ilasm/sha256.h b/src/coreclr/ilasm/sha256.h new file mode 100644 index 0000000000000..b0189cfb34880 --- /dev/null +++ b/src/coreclr/ilasm/sha256.h @@ -0,0 +1,104 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +//***************************************************************************** +// sha256.h +// + +// +// contains implementation of sha256 hash algorithm +// +//***************************************************************************** +#ifndef HAVE_SHA256_H +#define HAVE_SHA256_H + +#ifdef _WIN32 +inline HRESULT Sha256Hash(BYTE* pSrc, DWORD srcSize, BYTE* pDst, DWORD dstSize) +{ + if (dstSize != 32) + { + return E_FAIL; + } + + BCRYPT_ALG_HANDLE algHandle = NULL; + BCRYPT_HASH_HANDLE hashHandle = NULL; + + NTSTATUS status = BCryptOpenAlgorithmProvider(&algHandle, BCRYPT_SHA256_ALGORITHM, NULL, 0); + + if (!NT_SUCCESS(status)) + { + goto cleanup; + } + + status = BCryptCreateHash(algHandle, &hashHandle, NULL, 0, NULL, 0, 0); + + if (!NT_SUCCESS(status)) + { + goto cleanup; + } + + status = BCryptHashData(hashHandle, pSrc, srcSize, 0); + + if (!NT_SUCCESS(status)) + { + goto cleanup; + } + + status = BCryptFinishHash(hashHandle, pDst, dstSize, 0); + +cleanup: + if (hashHandle != NULL) + { + BCryptDestroyHash(hashHandle); + } + + if (algHandle != NULL) + { + BCryptCloseAlgorithmProvider(algHandle, 0); + } + + return status; +} +#elif defined(__APPLE__) +#include +#include + +inline HRESULT Sha256Hash(BYTE* pSrc, DWORD srcSize, BYTE* pDst, DWORD dstSize) +{ + if (dstSize != CC_SHA256_DIGEST_LENGTH) + { + return E_FAIL; + } + + CC_SHA256(pSrc, (CC_LONG)srcSize, pDst); + return S_OK; +} +#else +extern "C" { + #include "openssl.h" + #include "pal_evp.h" +} + +inline bool IsOpenSslAvailable() +{ + return CryptoNative_OpenSslAvailable() != 0; +} + +inline HRESULT Sha256Hash(BYTE* pSrc, DWORD srcSize, BYTE* pDst, DWORD dstSize) +{ + if (CryptoNative_EnsureOpenSslInitialized() || (dstSize != 32)) + { + return E_FAIL; + } + + uint32_t hashLength = 0; + + if (!CryptoNative_EvpDigestOneShot(CryptoNative_EvpSha256(), pSrc, srcSize, pDst, &hashLength)) + { + return E_FAIL; + } + + return S_OK; +} +#endif + +#endif // HAVE_SHA256_H