Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 58af9ad

Browse files
authored
Port ECDiffieHellman to .NET Core
This brings the ECDiffieHellman class as-is from .NET Framework (last edited for net47) to System.Security.Cryptography.Algorithms, and the ECDiffieHellmanCng class as-is to System.Security.Cryptography.Cng, and adds ECDiffieHellmanOpenSsl to System.Security.Cryptography.OpenSsl. A fair amount of this change is factoring out ECDSA code to general ECC code for each of the platforms. Since there isn't obvious value in having public ECDiffieHellmanPublicKey derived types, ECDiffieHellmanOpenSslPublicKey is not public. This PR also fixed some cases where a CryptographicException was thrown instead of a WindowsCryptographicException, because they got noticed during this PR.
1 parent 56ce6c1 commit 58af9ad

File tree

101 files changed

+7590
-1152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+7590
-1152
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Diagnostics;
7+
using System.Runtime.InteropServices;
8+
using System.Security.Cryptography;
9+
using System.Security.Cryptography.Apple;
10+
using Microsoft.Win32.SafeHandles;
11+
12+
internal static partial class Interop
13+
{
14+
internal static partial class AppleCrypto
15+
{
16+
[DllImport(Libraries.AppleCryptoNative)]
17+
private static extern int AppleCryptoNative_EcdhKeyAgree(
18+
SafeSecKeyRefHandle privateKey,
19+
SafeSecKeyRefHandle publicKey,
20+
out SafeCFDataHandle cfDataOut,
21+
out SafeCFErrorHandle cfErrorOut);
22+
23+
internal static byte[] EcdhKeyAgree(
24+
SafeSecKeyRefHandle privateKey,
25+
SafeSecKeyRefHandle publicKey,
26+
Span<byte> opportunisticDestination,
27+
out int bytesWritten)
28+
{
29+
const int Success = 1;
30+
const int kErrorSeeError = -2;
31+
32+
SafeCFDataHandle data;
33+
SafeCFErrorHandle error;
34+
35+
int status = AppleCryptoNative_EcdhKeyAgree(privateKey, publicKey, out data, out error);
36+
37+
using (data)
38+
using (error)
39+
{
40+
if (status == kErrorSeeError)
41+
{
42+
throw CreateExceptionForCFError(error);
43+
}
44+
45+
if (status == Success && !data.IsInvalid)
46+
{
47+
if (CoreFoundation.TryCFWriteData(data, opportunisticDestination, out bytesWritten))
48+
{
49+
return null;
50+
}
51+
52+
bytesWritten = 0;
53+
return CoreFoundation.CFGetData(data);
54+
}
55+
56+
Debug.Fail($"Unexpected status ({status})");
57+
throw new CryptographicException();
58+
}
59+
}
60+
}
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Diagnostics;
7+
using System.Runtime.InteropServices;
8+
using System.Security.Cryptography;
9+
using Microsoft.Win32.SafeHandles;
10+
11+
internal static partial class Interop
12+
{
13+
internal static partial class Crypto
14+
{
15+
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxCreate")]
16+
internal static extern SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerkey, out uint secretLength);
17+
18+
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyDeriveSecretAgreement")]
19+
private static extern int EvpPKeyDeriveSecretAgreement(
20+
ref byte secret,
21+
uint secretLength,
22+
SafeEvpPKeyCtxHandle ctx);
23+
24+
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxDestroy")]
25+
internal static extern void EvpPKeyCtxDestroy(IntPtr ctx);
26+
27+
internal static void EvpPKeyDeriveSecretAgreement(SafeEvpPKeyCtxHandle ctx, Span<byte> destination)
28+
{
29+
Debug.Assert(ctx != null);
30+
Debug.Assert(!ctx.IsInvalid);
31+
32+
int ret = EvpPKeyDeriveSecretAgreement(
33+
ref MemoryMarshal.GetReference(destination),
34+
(uint)destination.Length,
35+
ctx);
36+
37+
if (ret != 1)
38+
{
39+
throw CreateOpenSslCryptographicException();
40+
}
41+
}
42+
}
43+
}

src/Common/src/Interop/Windows/BCrypt/Interop.CreateCryptographicException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Security.Cryptography;
6+
using Internal.Cryptography;
77

88
internal partial class Interop
99
{
@@ -12,7 +12,7 @@ internal partial class BCrypt
1212
internal static Exception CreateCryptographicException(NTSTATUS ntStatus)
1313
{
1414
int hr = unchecked((int)ntStatus) | 0x01000000;
15-
return new CryptographicException(hr);
15+
return hr.ToCryptographicException();
1616
}
1717
}
1818
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal static partial class Interop
9+
{
10+
internal static partial class NCrypt
11+
{
12+
/// <summary>
13+
/// Types of NCryptBuffers
14+
/// </summary>
15+
internal enum BufferType
16+
{
17+
KdfHashAlgorithm = 0x00000000, // KDF_HASH_ALGORITHM
18+
KdfSecretPrepend = 0x00000001, // KDF_SECRET_PREPEND
19+
KdfSecretAppend = 0x00000002, // KDF_SECRET_APPEND
20+
KdfHmacKey = 0x00000003, // KDF_HMAC_KEY
21+
KdfTlsLabel = 0x00000004, // KDF_TLS_PRF_LABEL
22+
KdfTlsSeed = 0x00000005 // KDF_TLS_PRF_SEED
23+
}
24+
25+
[StructLayout(LayoutKind.Sequential)]
26+
internal struct NCryptBuffer
27+
{
28+
public int cbBuffer;
29+
public BufferType BufferType;
30+
public IntPtr pvBuffer;
31+
}
32+
33+
[StructLayout(LayoutKind.Sequential)]
34+
internal struct NCryptBufferDesc
35+
{
36+
public int ulVersion;
37+
public int cBuffers;
38+
public IntPtr pBuffers; // NCryptBuffer[cBuffers]
39+
}
40+
}
41+
}
42+

0 commit comments

Comments
 (0)