Skip to content

Commit

Permalink
Add System.Security.Cryptography.Hashing.Algorithms Source and Test
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnet-bot authored and bartonjs committed May 14, 2015
1 parent 65f4d79 commit 6306469
Show file tree
Hide file tree
Showing 43 changed files with 2,858 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Common/src/Interop/Linux/libcrypto/Interop.HMAC.cs
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 { }
}
}
50 changes: 50 additions & 0 deletions src/Common/src/Interop/OSX/libcrypto/Interop.HMAC.cs
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 { }
}
}
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();
}
}
29 changes: 29 additions & 0 deletions src/Common/src/Interop/Unix/libcrypto/Interop.ERR.cs
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();
}
}
}
51 changes: 51 additions & 0 deletions src/Common/src/Interop/Unix/libcrypto/Interop.EVP.cs
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 src/Common/src/Interop/Unix/libcrypto/Interop.Initialization.cs
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();
}
}
}
Loading

0 comments on commit 6306469

Please sign in to comment.