Skip to content

Commit 09c5809

Browse files
authored
Outbox AesGcm in to Microsoft.Bcl.Cryptography
1 parent 10532bf commit 09c5809

21 files changed

+550
-227
lines changed

src/libraries/Common/src/Interop/Windows/BCrypt/BCryptAeadHandleCache.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ namespace Internal.Cryptography
1010
{
1111
internal static class BCryptAeadHandleCache
1212
{
13-
private static SafeAlgorithmHandle? s_aesCcm;
1413
private static SafeAlgorithmHandle? s_aesGcm;
14+
#if NET
15+
private static SafeAlgorithmHandle? s_aesCcm;
1516
private static SafeAlgorithmHandle? s_chaCha20Poly1305;
1617

17-
internal static SafeAlgorithmHandle AesCcm => GetCachedAlgorithmHandle(ref s_aesCcm, Cng.BCRYPT_AES_ALGORITHM, Cng.BCRYPT_CHAIN_MODE_CCM);
18-
internal static SafeAlgorithmHandle AesGcm => GetCachedAlgorithmHandle(ref s_aesGcm, Cng.BCRYPT_AES_ALGORITHM, Cng.BCRYPT_CHAIN_MODE_GCM);
19-
2018
internal static bool IsChaCha20Poly1305Supported { get; } = OperatingSystem.IsWindowsVersionAtLeast(10, 0, 20142);
2119
internal static SafeAlgorithmHandle ChaCha20Poly1305 => GetCachedAlgorithmHandle(ref s_chaCha20Poly1305, Cng.BCRYPT_CHACHA20_POLY1305_ALGORITHM);
20+
internal static SafeAlgorithmHandle AesCcm => GetCachedAlgorithmHandle(ref s_aesCcm, Cng.BCRYPT_AES_ALGORITHM, Cng.BCRYPT_CHAIN_MODE_CCM);
21+
#endif
22+
23+
internal static SafeAlgorithmHandle AesGcm => GetCachedAlgorithmHandle(ref s_aesGcm, Cng.BCRYPT_AES_ALGORITHM, Cng.BCRYPT_CHAIN_MODE_GCM);
2224

2325
private static SafeAlgorithmHandle GetCachedAlgorithmHandle(ref SafeAlgorithmHandle? handle, string algId, string? chainingMode = null)
2426
{

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesAEAD.cs src/libraries/Common/src/System/Security/Cryptography/AesAEAD.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Internal.Cryptography;
5-
64
namespace System.Security.Cryptography
75
{
8-
internal static partial class AesAEAD
6+
internal static class AesAEAD
97
{
10-
public static void CheckKeySize(int keySizeInBytes)
8+
internal static void CheckKeySize(int keySizeInBytes)
119
{
12-
if (keySizeInBytes != (128 / 8) && keySizeInBytes != (192 / 8) && keySizeInBytes != (256 / 8))
10+
if (keySizeInBytes is not (128 / 8 or 192 / 8 or 256 / 8))
1311
{
1412
throw new CryptographicException(SR.Cryptography_InvalidKeySize);
1513
}

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesGcm.Windows.cs src/libraries/Common/src/System/Security/Cryptography/AesGcm.Windows.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
using System.Diagnostics.CodeAnalysis;
55
using Internal.Cryptography;
66
using Internal.NativeCrypto;
7+
using System.Runtime.InteropServices;
78

89
namespace System.Security.Cryptography
910
{
1011
public partial class AesGcm
1112
{
1213
private SafeKeyHandle _keyHandle;
14+
private static readonly KeySizes s_tagByteSizes = new KeySizes(12, 16, 1);
1315

14-
public static bool IsSupported => true;
15-
public static KeySizes TagByteSizes { get; } = new KeySizes(12, 16, 1);
16+
public static partial bool IsSupported => true;
17+
18+
public static partial KeySizes TagByteSizes => s_tagByteSizes;
1619

1720
[MemberNotNull(nameof(_keyHandle))]
18-
private void ImportKey(ReadOnlySpan<byte> key)
21+
private partial void ImportKey(ReadOnlySpan<byte> key)
1922
{
2023
_keyHandle = Interop.BCrypt.BCryptImportKey(BCryptAeadHandleCache.AesGcm, key);
2124
}
2225

23-
private void EncryptCore(
26+
private partial void EncryptCore(
2427
ReadOnlySpan<byte> nonce,
2528
ReadOnlySpan<byte> plaintext,
2629
Span<byte> ciphertext,
2730
Span<byte> tag,
28-
ReadOnlySpan<byte> associatedData = default)
31+
ReadOnlySpan<byte> associatedData)
2932
{
3033
AeadCommon.Encrypt(_keyHandle, nonce, associatedData, plaintext, ciphertext, tag);
3134
}
3235

33-
private void DecryptCore(
36+
private partial void DecryptCore(
3437
ReadOnlySpan<byte> nonce,
3538
ReadOnlySpan<byte> ciphertext,
3639
ReadOnlySpan<byte> tag,
3740
Span<byte> plaintext,
38-
ReadOnlySpan<byte> associatedData = default)
41+
ReadOnlySpan<byte> associatedData)
3942
{
4043
AeadCommon.Decrypt(_keyHandle, nonce, associatedData, ciphertext, tag, plaintext, clearPlaintextOnFailure: true);
4144
}
4245

43-
public void Dispose()
46+
public partial void Dispose()
4447
{
4548
_keyHandle.Dispose();
4649
}

0 commit comments

Comments
 (0)