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

Commit 6af4fb2

Browse files
committed
Minor cleanup for AES from review feedback.
* Added BitsPerByte, and replaced all of the magic 8s with it * Removed some "this."s which were not necessary for compilation * Switched to Array.Empty<byte>() vs new byte[0] * Tweaked the wording of a comment to hopefully be better understood on the first pass.
1 parent 40bd910 commit 6af4fb2

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/System.Security.Cryptography.Encryption.Aes/src/Internal/Cryptography/AesCngCryptoTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected override void Reset()
7777
protected abstract byte[] UncheckedTransformFinalBlock(SafeKeyHandle hKey, byte[] currentIv, byte[] inputBuffer, int inputOffset, int inputCount);
7878

7979
private SafeKeyHandle _hKey;
80-
private byte[] _iv; // This IV never changes.
80+
private byte[] _iv; // _iv holds a copy of the original IV for Reset(), until it is cleared by Dispose().
8181
private byte[] _currentIv; // CNG mutates this with the updated IV for the next stage on each Encrypt/Decrypt call.
8282

8383
private static readonly SafeAlgorithmHandle s_hAlg = Cng.BCryptOpenAlgorithmProvider(Cng.BCRYPT_AES_ALGORITHM, null, Cng.OpenAlgorithmProviderFlags.NONE);

src/System.Security.Cryptography.Encryption.Aes/src/Internal/Cryptography/AesImplementation.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using System.Diagnostics;
65
using System.Security.Cryptography;
76

87
namespace Internal.Cryptography
@@ -21,16 +20,16 @@ public sealed override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rg
2120

2221
public sealed override void GenerateIV()
2322
{
24-
byte[] iv = new byte[this.BlockSize / 8];
23+
byte[] iv = new byte[BlockSize / BitsPerByte];
2524
s_rng.GetBytes(iv);
26-
this.IV = iv;
25+
IV = iv;
2726
}
2827

2928
public sealed override void GenerateKey()
3029
{
31-
byte[] key = new byte[this.KeySize / 8];
30+
byte[] key = new byte[KeySize / BitsPerByte];
3231
s_rng.GetBytes(key);
33-
this.Key = key;
32+
Key = key;
3433
}
3534

3635
protected sealed override void Dispose(bool disposing)
@@ -42,18 +41,19 @@ private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encry
4241
{
4342
if (rgbKey == null)
4443
throw new ArgumentNullException("key");
45-
int keySize = rgbKey.Length * 8;
44+
int keySize = rgbKey.Length * BitsPerByte;
4645
if (!keySize.IsLegalSize(this.LegalKeySizes))
4746
throw new ArgumentException(SR.Format(SR.Cryptography_InvalidKeySize, "key"));
48-
if (rgbIV != null && rgbIV.Length * 8 != this.BlockSize)
47+
if (rgbIV != null && rgbIV.Length * BitsPerByte != BlockSize)
4948
throw new ArgumentException(SR.Format(SR.Cryptography_InvalidIVSize, "iv"));
5049

5150
if (encrypting)
52-
return CreateEncryptor(Mode, Padding, rgbKey, rgbIV, BlockSize / 8);
51+
return CreateEncryptor(Mode, Padding, rgbKey, rgbIV, BlockSize / BitsPerByte);
5352
else
54-
return CreateDecryptor(Mode, Padding, rgbKey, rgbIV, BlockSize / 8);
53+
return CreateDecryptor(Mode, Padding, rgbKey, rgbIV, BlockSize / BitsPerByte);
5554
}
5655

56+
private const int BitsPerByte = 8;
5757
private static readonly RandomNumberGenerator s_rng = RandomNumberGenerator.Create();
5858
}
5959
}

src/System.Security.Cryptography.Encryption.Aes/src/Internal/Cryptography/AesOpenSslCryptoTransform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private byte[] DecryptFinalBlock(byte[] inputBuffer, int inputOffset, int inputC
231231

232232
if (rawDecrypt.Length == 0)
233233
{
234-
return new byte[0];
234+
return Array.Empty<byte>();
235235
}
236236

237237
return DepadBlock(rawDecrypt, 0, rawDecrypt.Length);
@@ -262,7 +262,7 @@ private unsafe byte[] ProcessFinalBlock(byte[] paddedBlock, int offset, int leng
262262

263263
if (outputBytes == 0)
264264
{
265-
return new byte[0];
265+
return Array.Empty<byte>();
266266
}
267267

268268
byte[] userData = new byte[outputBytes];

0 commit comments

Comments
 (0)