Skip to content

Commit

Permalink
[ILASM] Add wrappers for platform-specific SHA-256 implementations (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
amanasifkhalid authored Nov 7, 2024
1 parent 8975db4 commit 36bcc2c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/coreclr/ilasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions src/coreclr/ilasm/sha256.h
Original file line number Diff line number Diff line change
@@ -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 <CommonCrypto/CommonCrypto.h>
#include <CommonCrypto/CommonDigest.h>

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

0 comments on commit 36bcc2c

Please sign in to comment.