From a6934ddf7acbb5d12362a06bf05de0f445f83ca4 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 23 Jun 2026 20:20:16 +0000
Subject: [PATCH 1/3] Add AES AEAD key size API
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
---
.../System/Security/Cryptography/AesGcm.cs | 9 +++++
.../Security/Cryptography/AesGcmTests.cs | 33 +++++++++++++++++++
.../ref/System.Security.Cryptography.cs | 2 ++
.../System/Security/Cryptography/AesCcm.cs | 4 +++
.../System/Security/Cryptography/AesGcm.cs | 1 +
.../tests/AesCcmTests.cs | 19 +++++++++++
6 files changed, 68 insertions(+)
diff --git a/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs b/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
index 6728fb1fa088ea..2c9b305ada3c9c 100644
--- a/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
+++ b/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
@@ -31,6 +31,14 @@ public sealed partial class AesGcm : IDisposable
///
public int? TagSizeInBytes { get; }
+ ///
+ /// Gets the size, in bytes, of the key used by this instance.
+ ///
+ ///
+ /// The size, in bytes, of the key used by this instance.
+ ///
+ public int KeySizeInBytes { get; }
+
///
/// Gets the tag sizes, in bytes, supported by this instance.
///
@@ -78,6 +86,7 @@ public AesGcm(ReadOnlySpan key, int tagSizeInBytes)
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tagSizeInBytes));
}
+ KeySizeInBytes = key.Length;
TagSizeInBytes = tagSizeInBytes;
ImportKey(key);
}
diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AesGcmTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/AesGcmTests.cs
index 8cf9eb6f078a7b..99f5a8fc486938 100644
--- a/src/libraries/Common/tests/System/Security/Cryptography/AesGcmTests.cs
+++ b/src/libraries/Common/tests/System/Security/Cryptography/AesGcmTests.cs
@@ -60,6 +60,39 @@ public static void InvalidKeyLength(int keyLength)
Assert.Throws(() => new AesGcm(key.AsSpan(), AesGcm.TagByteSizes.MinSize));
}
+ [Theory]
+ [InlineData(16)]
+ [InlineData(24)]
+ [InlineData(32)]
+ public static void KeySizeInBytes(int keyLength)
+ {
+ byte[] key = new byte[keyLength];
+
+#if NET
+#pragma warning disable SYSLIB0053
+ using (var aesGcm = new AesGcm(key))
+ {
+ Assert.Equal(keyLength, aesGcm.KeySizeInBytes);
+ }
+
+ using (var aesGcm = new AesGcm(key.AsSpan()))
+ {
+ Assert.Equal(keyLength, aesGcm.KeySizeInBytes);
+ }
+#pragma warning restore SYSLIB0053
+#endif
+
+ using (var aesGcm = new AesGcm(key, AesGcm.TagByteSizes.MinSize))
+ {
+ Assert.Equal(keyLength, aesGcm.KeySizeInBytes);
+ }
+
+ using (var aesGcm = new AesGcm(key.AsSpan(), AesGcm.TagByteSizes.MinSize))
+ {
+ Assert.Equal(keyLength, aesGcm.KeySizeInBytes);
+ }
+ }
+
[Theory]
[MemberData(nameof(GetInvalidNonceSizes))]
public static void InvalidNonceSize(int nonceSize)
diff --git a/src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs b/src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs
index a47171f62274c4..66ea6f7b80e2c1 100644
--- a/src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs
+++ b/src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs
@@ -71,6 +71,7 @@ public sealed partial class AesCcm : System.IDisposable
public AesCcm(byte[] key) { }
public AesCcm(System.ReadOnlySpan key) { }
public static bool IsSupported { get { throw null; } }
+ public int KeySizeInBytes { get { throw null; } }
public static System.Security.Cryptography.KeySizes NonceByteSizes { get { throw null; } }
public static System.Security.Cryptography.KeySizes TagByteSizes { get { throw null; } }
public void Decrypt(byte[] nonce, byte[] ciphertext, byte[] tag, byte[] plaintext, byte[]? associatedData = null) { }
@@ -144,6 +145,7 @@ public AesGcm(byte[] key, int tagSizeInBytes) { }
public AesGcm(System.ReadOnlySpan key) { }
public AesGcm(System.ReadOnlySpan key, int tagSizeInBytes) { }
public static bool IsSupported { get { throw null; } }
+ public int KeySizeInBytes { get { throw null; } }
public static System.Security.Cryptography.KeySizes NonceByteSizes { get { throw null; } }
public static System.Security.Cryptography.KeySizes TagByteSizes { get { throw null; } }
public int? TagSizeInBytes { get { throw null; } }
diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
index 74d974f7cefe72..b3a2dd330c76ff 100644
--- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
+++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
@@ -14,11 +14,14 @@ public sealed partial class AesCcm : IDisposable
public static KeySizes NonceByteSizes { get; } = new KeySizes(7, 13, 1);
public static KeySizes TagByteSizes { get; } = new KeySizes(4, 16, 2);
+ public int KeySizeInBytes { get; }
+
public AesCcm(ReadOnlySpan key)
{
ThrowIfNotSupported();
AesAEAD.CheckKeySize(key.Length);
+ KeySizeInBytes = key.Length;
ImportKey(key);
}
@@ -29,6 +32,7 @@ public AesCcm(byte[] key)
ArgumentNullException.ThrowIfNull(key);
AesAEAD.CheckKeySize(key.Length);
+ KeySizeInBytes = key.Length;
ImportKey(key);
}
diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesGcm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesGcm.cs
index b076a145fc99af..39507a910cd187 100644
--- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesGcm.cs
+++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesGcm.cs
@@ -18,6 +18,7 @@ public AesGcm(ReadOnlySpan key)
ThrowIfNotSupported();
AesAEAD.CheckKeySize(key.Length);
+ KeySizeInBytes = key.Length;
ImportKey(key);
}
diff --git a/src/libraries/System.Security.Cryptography/tests/AesCcmTests.cs b/src/libraries/System.Security.Cryptography/tests/AesCcmTests.cs
index b91efe38baeaa9..5720a4a139f329 100644
--- a/src/libraries/System.Security.Cryptography/tests/AesCcmTests.cs
+++ b/src/libraries/System.Security.Cryptography/tests/AesCcmTests.cs
@@ -51,6 +51,25 @@ public static void InvalidKeyLength(int keyLength)
Assert.Throws(() => new AesCcm(key));
}
+ [Theory]
+ [InlineData(16)]
+ [InlineData(24)]
+ [InlineData(32)]
+ public static void KeySizeInBytes(int keyLength)
+ {
+ byte[] key = new byte[keyLength];
+
+ using (var aesCcm = new AesCcm(key))
+ {
+ Assert.Equal(keyLength, aesCcm.KeySizeInBytes);
+ }
+
+ using (var aesCcm = new AesCcm(key.AsSpan()))
+ {
+ Assert.Equal(keyLength, aesCcm.KeySizeInBytes);
+ }
+ }
+
[Theory]
[MemberData(nameof(GetInvalidNonceSizes))]
public static void InvalidNonceSize(int nonceSize)
From d34ab294c990126dabbd466cebf62f40eaa4e63e Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 23 Jun 2026 20:35:48 +0000
Subject: [PATCH 2/3] Document AesCcm key size API
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
---
.../src/System/Security/Cryptography/AesCcm.cs | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
index b3a2dd330c76ff..9441c5b87adb26 100644
--- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
+++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
@@ -14,6 +14,12 @@ public sealed partial class AesCcm : IDisposable
public static KeySizes NonceByteSizes { get; } = new KeySizes(7, 13, 1);
public static KeySizes TagByteSizes { get; } = new KeySizes(4, 16, 2);
+ ///
+ /// Gets the size, in bytes, of the key used by this instance.
+ ///
+ ///
+ /// The size, in bytes, of the key used by this instance.
+ ///
public int KeySizeInBytes { get; }
public AesCcm(ReadOnlySpan key)
From 629214d146999009c15c2005439017c6d8d27d87 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Tue, 23 Jun 2026 21:14:07 +0000
Subject: [PATCH 3/3] Update key size XML docs
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
---
.../Common/src/System/Security/Cryptography/AesGcm.cs | 4 ++--
.../src/System/Security/Cryptography/AesCcm.cs | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs b/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
index 2c9b305ada3c9c..2d1b7e60b135cc 100644
--- a/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
+++ b/src/libraries/Common/src/System/Security/Cryptography/AesGcm.cs
@@ -32,10 +32,10 @@ public sealed partial class AesGcm : IDisposable
public int? TagSizeInBytes { get; }
///
- /// Gets the size, in bytes, of the key used by this instance.
+ /// Gets the size of the key, in bytes.
///
///
- /// The size, in bytes, of the key used by this instance.
+ /// The size of the key, in bytes.
///
public int KeySizeInBytes { get; }
diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
index 9441c5b87adb26..5da39bd6b5cdc9 100644
--- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
+++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesCcm.cs
@@ -15,10 +15,10 @@ public sealed partial class AesCcm : IDisposable
public static KeySizes TagByteSizes { get; } = new KeySizes(4, 16, 2);
///
- /// Gets the size, in bytes, of the key used by this instance.
+ /// Gets the size of the key, in bytes.
///
///
- /// The size, in bytes, of the key used by this instance.
+ /// The size of the key, in bytes.
///
public int KeySizeInBytes { get; }