This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make derived types of SymmetricAlgorithm use field assignment in ctors.
Aes and TripleDES both did assignments via virtual property setters in their ctor, which were field assignments in .NET Framework. 3rd party implementations may have written properties which assumed that the ctor had run to completion, and they are broken by that behavioral change. This change makes the ctors look like they do in net462, aside from when net462 sets fields which don't exist. It also makes the tests fail if virtual setter dispatch was utilized (for any currently defined property, at least). cherry-pick of 5345525 and 207e52e to the release/1.1.0 branch, with master-only TODO comments removed.
- Loading branch information
Showing
5 changed files
with
281 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/System.Security.Cryptography.Algorithms/tests/AesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit; | ||
|
||
namespace System.Security.Cryptography.Algorithms.Tests | ||
{ | ||
public partial class AesTests | ||
{ | ||
[Fact] | ||
public static void AesDefaultCtor() | ||
{ | ||
using (Aes aes = new AesMinimal()) | ||
{ | ||
Assert.Equal(256, aes.KeySize); | ||
Assert.Equal(128, aes.BlockSize); | ||
Assert.Equal(CipherMode.CBC, aes.Mode); | ||
Assert.Equal(PaddingMode.PKCS7, aes.Padding); | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void EnsureLegalSizesValuesIsolated() | ||
{ | ||
new AesLegalSizesBreaker().Dispose(); | ||
|
||
using (Aes aes = Aes.Create()) | ||
{ | ||
Assert.Equal(128, aes.LegalKeySizes[0].MinSize); | ||
Assert.Equal(128, aes.LegalBlockSizes[0].MinSize); | ||
|
||
aes.Key = new byte[16]; | ||
} | ||
} | ||
|
||
private class AesLegalSizesBreaker : AesMinimal | ||
{ | ||
public AesLegalSizesBreaker() | ||
{ | ||
LegalKeySizesValue[0] = new KeySizes(1, 1, 0); | ||
LegalBlockSizesValue[0] = new KeySizes(1, 1, 0); | ||
} | ||
} | ||
|
||
private class AesMinimal : Aes | ||
{ | ||
// If the constructor uses a virtual call to any of the property setters | ||
// they will fail. | ||
private readonly bool _ready; | ||
|
||
public AesMinimal() | ||
{ | ||
// Don't set this as a field initializer, otherwise it runs before the base ctor. | ||
_ready = true; | ||
} | ||
|
||
public override int KeySize | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.KeySize = value; | ||
} | ||
} | ||
|
||
public override int BlockSize | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.BlockSize = value; | ||
} | ||
} | ||
|
||
public override byte[] IV | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.IV = value; | ||
} | ||
} | ||
|
||
public override byte[] Key | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.Key = value; | ||
} | ||
} | ||
|
||
public override CipherMode Mode | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.Mode = value; | ||
} | ||
} | ||
|
||
public override PaddingMode Padding | ||
{ | ||
set | ||
{ | ||
if (!_ready) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
base.Padding = value; | ||
} | ||
} | ||
|
||
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void GenerateIV() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void GenerateKey() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters