Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public sealed partial class AesGcm : IDisposable
/// </value>
public int? TagSizeInBytes { get; }

/// <summary>
/// Gets the size of the key, in bytes.
/// </summary>
/// <value>
/// The size of the key, in bytes.
/// </value>
public int KeySizeInBytes { get; }

/// <summary>
/// Gets the tag sizes, in bytes, supported by this instance.
/// </summary>
Expand Down Expand Up @@ -78,6 +86,7 @@ public AesGcm(ReadOnlySpan<byte> key, int tagSizeInBytes)
throw new ArgumentException(SR.Cryptography_InvalidTagLength, nameof(tagSizeInBytes));
}

KeySizeInBytes = key.Length;
TagSizeInBytes = tagSizeInBytes;
ImportKey(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ public static void InvalidKeyLength(int keyLength)
Assert.Throws<CryptographicException>(() => 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public sealed partial class AesCcm : System.IDisposable
public AesCcm(byte[] key) { }
public AesCcm(System.ReadOnlySpan<byte> 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) { }
Expand Down Expand Up @@ -144,6 +145,7 @@ public AesGcm(byte[] key, int tagSizeInBytes) { }
public AesGcm(System.ReadOnlySpan<byte> key) { }
public AesGcm(System.ReadOnlySpan<byte> 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; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ 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);

/// <summary>
/// Gets the size of the key, in bytes.
/// </summary>
/// <value>
/// The size of the key, in bytes.
/// </value>
public int KeySizeInBytes { get; }

public AesCcm(ReadOnlySpan<byte> key)
{
ThrowIfNotSupported();

AesAEAD.CheckKeySize(key.Length);
KeySizeInBytes = key.Length;
ImportKey(key);
}

Expand All @@ -29,6 +38,7 @@ public AesCcm(byte[] key)
ArgumentNullException.ThrowIfNull(key);

AesAEAD.CheckKeySize(key.Length);
KeySizeInBytes = key.Length;
ImportKey(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public AesGcm(ReadOnlySpan<byte> key)
ThrowIfNotSupported();

AesAEAD.CheckKeySize(key.Length);
KeySizeInBytes = key.Length;
ImportKey(key);
}

Expand Down
19 changes: 19 additions & 0 deletions src/libraries/System.Security.Cryptography/tests/AesCcmTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ public static void InvalidKeyLength(int keyLength)
Assert.Throws<CryptographicException>(() => 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)
Expand Down
Loading