forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add System.Security.Cryptography.Hashing.Algorithms Source and Test
- Loading branch information
1 parent
65f4d79
commit 6306469
Showing
43 changed files
with
2,858 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcrypto | ||
{ | ||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int HMAC_Init(out HMAC_CTX ctx, byte* key, int key_len, IntPtr md); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int HMAC_Update(ref HMAC_CTX ctx, byte* data, int len); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int HMAC_Final(ref HMAC_CTX ctx, byte* md, ref uint len); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe void HMAC_CTX_cleanup(ref HMAC_CTX ctx); | ||
|
||
[StructLayout(LayoutKind.Explicit, Size = 512)] | ||
internal struct HMAC_CTX { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcrypto | ||
{ | ||
private const int Success = 1; | ||
|
||
// On OSX, these functions are void returning (and the man pages give no indication of what would happen if they | ||
// fail), but upstack code is written against surface area where they return 1 on success and 0 on error. | ||
// These routines call the underlying APIs and then just return success | ||
|
||
internal static unsafe int HMAC_Init(out HMAC_CTX ctx, byte* key, int key_len, IntPtr md) | ||
{ | ||
HMAC_InitNative(out ctx, key, key_len, md); | ||
return Success; | ||
} | ||
|
||
internal static unsafe int HMAC_Update(ref HMAC_CTX ctx, byte* data, int len) | ||
{ | ||
HMAC_UpdateNative(ref ctx, data, len); | ||
return Success; | ||
} | ||
|
||
internal static unsafe int HMAC_Final(ref HMAC_CTX ctx, byte* md, ref uint len) | ||
{ | ||
HMAC_FinalNative(ref ctx, md, ref len); | ||
return Success; | ||
} | ||
|
||
[DllImport(Libraries.LibCrypto, EntryPoint = "HMAC_Init", ExactSpelling = true)] | ||
private extern static unsafe int HMAC_InitNative(out HMAC_CTX ctx, byte* key, int key_len, IntPtr md); | ||
|
||
[DllImport(Libraries.LibCrypto, EntryPoint = "HMAC_Update", ExactSpelling = true)] | ||
private extern static unsafe int HMAC_UpdateNative(ref HMAC_CTX ctx, byte* data, int len); | ||
|
||
[DllImport(Libraries.LibCrypto, EntryPoint = "HMAC_Final", ExactSpelling = true)] | ||
private extern static unsafe int HMAC_FinalNative(ref HMAC_CTX ctx, byte* md, ref uint len); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe void HMAC_CTX_cleanup(ref HMAC_CTX ctx); | ||
|
||
[StructLayout(LayoutKind.Explicit, Size = 512)] | ||
internal struct HMAC_CTX { } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Common/src/Interop/Unix/libcoreclr/Interop.EnsureOpenSslInitialized.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcoreclr | ||
{ | ||
[DllImport(Libraries.LibCoreClr)] | ||
internal static extern int EnsureOpenSslInitialized(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcrypto | ||
{ | ||
[DllImport(Libraries.LibCrypto)] | ||
private static extern void ERR_load_crypto_strings(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
private static extern uint ERR_get_error(); | ||
|
||
[DllImport(Libraries.LibCrypto, CharSet = CharSet.Ansi)] | ||
private static extern void ERR_error_string_n(uint e, StringBuilder buf, int len); | ||
|
||
internal static string GetOpenSslErrorString() | ||
{ | ||
uint error = ERR_get_error(); | ||
StringBuilder buf = new StringBuilder(1024); | ||
|
||
ERR_error_string_n(error, buf, buf.Capacity); | ||
return buf.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
using size_t = System.IntPtr; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcrypto | ||
{ | ||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static SafeEvpMdCtxHandle EVP_MD_CTX_create(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static int EVP_DigestInit_ex(SafeEvpMdCtxHandle ctx, IntPtr type, IntPtr impl); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int EVP_DigestUpdate(SafeEvpMdCtxHandle ctx, byte* d, size_t cnt); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int EVP_DigestFinal_ex(SafeEvpMdCtxHandle ctx, byte* md, ref uint s); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe void EVP_MD_CTX_destroy(IntPtr ctx); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static unsafe int EVP_MD_size(IntPtr md); | ||
|
||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static IntPtr EVP_md5(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static IntPtr EVP_sha1(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static IntPtr EVP_sha256(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static IntPtr EVP_sha384(); | ||
|
||
[DllImport(Libraries.LibCrypto)] | ||
internal extern static IntPtr EVP_sha512(); | ||
|
||
|
||
internal const int EVP_MAX_MD_SIZE = 64; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Common/src/Interop/Unix/libcrypto/Interop.Initialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Security.Cryptography; | ||
using System.Threading; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class libcrypto | ||
{ | ||
// Initialization of libcrypto threading support is done in a static constructor. | ||
// This enables a project simply to include this file, and any usage of any of | ||
// the libcrypto functions will trigger initialization of the threading support. | ||
static libcrypto() | ||
{ | ||
if (Interop.libcoreclr.EnsureOpenSslInitialized() != 0) | ||
{ | ||
throw new CryptographicException(); | ||
} | ||
|
||
// Ensure that the error message table is loaded. | ||
Interop.libcrypto.ERR_load_crypto_strings(); | ||
} | ||
} | ||
} |
Oops, something went wrong.