Skip to content

Commit 899bf97

Browse files
authored
Merge System.Security.Cryptography.Algorithms to System.Security.Cryptography
1 parent 13024af commit 899bf97

File tree

245 files changed

+3269
-3258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+3269
-3258
lines changed

src/libraries/Common/src/Internal/Cryptography/HashProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void AppendHashData(byte[] data, int offset, int count)
2020
// an invalid number of bytes read. Since our implementations of AppendHashDataCore
2121
// end up using unsafe code, we want to be sure the arguments are valid.
2222
if (data == null)
23-
throw new ArgumentNullException(nameof(data), SR.ArgumentNull_Buffer);
23+
throw new ArgumentNullException(nameof(data));
2424
if (offset < 0)
2525
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
2626
if (count < 0)

src/libraries/Common/src/Internal/Cryptography/Helpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ namespace Internal.Cryptography
1010
{
1111
internal static partial class Helpers
1212
{
13+
[UnsupportedOSPlatformGuard("browser")]
14+
internal static bool HasSymmetricEncryption { get; } =
15+
#if NET5_0_OR_GREATER
16+
!OperatingSystem.IsBrowser();
17+
#else
18+
true;
19+
#endif
20+
21+
[UnsupportedOSPlatformGuard("browser")]
22+
internal static bool HasHMAC { get; } =
23+
#if NET5_0_OR_GREATER
24+
!OperatingSystem.IsBrowser();
25+
#else
26+
true;
27+
#endif
28+
1329
#if NET5_0_OR_GREATER
1430
[UnsupportedOSPlatformGuard("ios")]
1531
[UnsupportedOSPlatformGuard("tvos")]
@@ -20,6 +36,7 @@ internal static partial class Helpers
2036

2137
#if NET5_0_OR_GREATER
2238
[UnsupportedOSPlatformGuard("android")]
39+
[UnsupportedOSPlatformGuard("browser")]
2340
public static bool IsRC2Supported => !OperatingSystem.IsAndroid();
2441
#else
2542
public static bool IsRC2Supported => true;

src/libraries/Common/src/Internal/Cryptography/UniversalCryptoTransform.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
7878
if (inputCount % InputBlockSize != 0)
7979
throw new ArgumentOutOfRangeException(nameof(inputCount), SR.Cryptography_MustTransformWholeBlock);
8080
if (inputCount > inputBuffer.Length - inputOffset)
81-
throw new ArgumentOutOfRangeException(nameof(inputCount), SR.Cryptography_TransformBeyondEndOfBuffer);
81+
throw new ArgumentOutOfRangeException(nameof(inputCount), SR.Argument_InvalidOffLen);
8282
if (outputBuffer == null)
8383
throw new ArgumentNullException(nameof(outputBuffer));
8484
if (outputOffset > outputBuffer.Length)
8585
throw new ArgumentOutOfRangeException(nameof(outputOffset));
8686
if (inputCount > outputBuffer.Length - outputOffset)
87-
throw new ArgumentOutOfRangeException(nameof(outputOffset), SR.Cryptography_TransformBeyondEndOfBuffer);
87+
throw new ArgumentOutOfRangeException(nameof(outputOffset), SR.Argument_InvalidOffLen);
8888

8989
int numBytesWritten = UncheckedTransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
9090
Debug.Assert(numBytesWritten >= 0 && numBytesWritten <= inputCount);
@@ -102,7 +102,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
102102
if (inputOffset > inputBuffer.Length)
103103
throw new ArgumentOutOfRangeException(nameof(inputOffset));
104104
if (inputCount > inputBuffer.Length - inputOffset)
105-
throw new ArgumentOutOfRangeException(nameof(inputCount), SR.Cryptography_TransformBeyondEndOfBuffer);
105+
throw new ArgumentOutOfRangeException(nameof(inputCount), SR.Argument_InvalidOffLen);
106106

107107
byte[] output = UncheckedTransformFinalBlock(inputBuffer, inputOffset, inputCount);
108108
return output;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class ECDsa : AsymmetricAlgorithm
1313
/// <summary>
1414
/// Creates an instance of the platform specific implementation of the cref="ECDsa" algorithm.
1515
/// </summary>
16-
public static new ECDsa Create()
16+
public static new partial ECDsa Create()
1717
{
1818
return new ECDsaImplementation.ECDsaSecurityTransforms();
1919
}
@@ -24,7 +24,7 @@ public partial class ECDsa : AsymmetricAlgorithm
2424
/// <param name="curve">
2525
/// The <see cref="ECCurve"/> representing the elliptic curve.
2626
/// </param>
27-
public static ECDsa Create(ECCurve curve)
27+
public static partial ECDsa Create(ECCurve curve)
2828
{
2929
ECDsa ecdsa = Create();
3030
ecdsa.GenerateKey(curve);
@@ -37,7 +37,7 @@ public static ECDsa Create(ECCurve curve)
3737
/// <param name="parameters">
3838
/// The <see cref="ECParameters"/> representing the elliptic curve parameters.
3939
/// </param>
40-
public static ECDsa Create(ECParameters parameters)
40+
public static partial ECDsa Create(ECParameters parameters)
4141
{
4242
ECDsa ecdsa = Create();
4343
ecdsa.ImportParameters(parameters);

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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 System.Diagnostics;
4+
using Internal.Cryptography;
55

66
namespace System.Security.Cryptography
77
{
@@ -15,25 +15,28 @@ public static int MacData(
1515
ReadOnlySpan<byte> source,
1616
Span<byte> destination)
1717
{
18-
if (hashAlgorithm == HashAlgorithmName.SHA256)
18+
if (Helpers.HasHMAC)
1919
{
20-
return HMACSHA256.HashData(key, source, destination);
21-
}
22-
else if (hashAlgorithm == HashAlgorithmName.SHA1)
23-
{
24-
return HMACSHA1.HashData(key, source, destination);
25-
}
26-
else if (hashAlgorithm == HashAlgorithmName.SHA512)
27-
{
28-
return HMACSHA512.HashData(key, source, destination);
29-
}
30-
else if (hashAlgorithm == HashAlgorithmName.SHA384)
31-
{
32-
return HMACSHA384.HashData(key, source, destination);
33-
}
34-
else if (hashAlgorithm == HashAlgorithmName.MD5)
35-
{
36-
return HMACMD5.HashData(key, source, destination);
20+
if (hashAlgorithm == HashAlgorithmName.SHA256)
21+
{
22+
return HMACSHA256.HashData(key, source, destination);
23+
}
24+
else if (hashAlgorithm == HashAlgorithmName.SHA1)
25+
{
26+
return HMACSHA1.HashData(key, source, destination);
27+
}
28+
else if (hashAlgorithm == HashAlgorithmName.SHA512)
29+
{
30+
return HMACSHA512.HashData(key, source, destination);
31+
}
32+
else if (hashAlgorithm == HashAlgorithmName.SHA384)
33+
{
34+
return HMACSHA384.HashData(key, source, destination);
35+
}
36+
else if (hashAlgorithm == HashAlgorithmName.MD5)
37+
{
38+
return HMACMD5.HashData(key, source, destination);
39+
}
3740
}
3841

3942
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name));

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ internal static unsafe int Decrypt(
7474
{
7575
Debug.Assert(destination.Length >= encryptedData.Length);
7676

77+
if (!Helpers.HasSymmetricEncryption)
78+
{
79+
throw new CryptographicException(
80+
SR.Format(
81+
SR.Cryptography_UnknownAlgorithmIdentifier,
82+
algorithmIdentifier.Algorithm));
83+
}
84+
7785
// Don't check that algorithmIdentifier.Parameters is set here.
7886
// Maybe some future PBES3 will have one with a default.
7987

@@ -229,6 +237,14 @@ internal static void InitiateEncryption(
229237
{
230238
Debug.Assert(pbeParameters != null);
231239

240+
if (!Helpers.HasSymmetricEncryption)
241+
{
242+
throw new CryptographicException(
243+
SR.Format(
244+
SR.Cryptography_UnknownAlgorithmIdentifier,
245+
pbeParameters.EncryptionAlgorithm));
246+
}
247+
232248
isPkcs12 = false;
233249

234250
switch (pbeParameters.EncryptionAlgorithm)
@@ -258,7 +274,7 @@ internal static void InitiateEncryption(
258274
throw new CryptographicException(
259275
SR.Format(
260276
SR.Cryptography_UnknownAlgorithmIdentifier,
261-
pbeParameters.HashAlgorithm.Name));
277+
pbeParameters.EncryptionAlgorithm));
262278
}
263279

264280
HashAlgorithmName prf = pbeParameters.HashAlgorithm;
@@ -377,6 +393,12 @@ internal static unsafe int Encrypt(
377393
Debug.Assert(pwdTmpBytes!.Length == 0);
378394
}
379395

396+
if (!Helpers.HasHMAC)
397+
{
398+
throw new CryptographicException(
399+
SR.Format(SR.Cryptography_AlgorithmNotSupported, "HMAC" + prf.Name));
400+
}
401+
380402
using (var pbkdf2 = new Rfc2898DeriveBytes(pwdTmpBytes, salt.ToArray(), iterationCount, prf))
381403
{
382404
derivedKey = pbkdf2.GetBytes(keySizeBytes);
@@ -518,6 +540,8 @@ private static unsafe int Pbes2Decrypt(
518540
Rfc2898DeriveBytes pbkdf2 =
519541
OpenPbkdf2(password, pbes2Params.KeyDerivationFunc.Parameters, out int? requestedKeyLength);
520542

543+
Debug.Assert(Helpers.HasHMAC);
544+
521545
using (pbkdf2)
522546
{
523547
// The biggest block size (for IV) we support is AES (128-bit / 16 byte)
@@ -556,6 +580,12 @@ private static SymmetricAlgorithm OpenCipher(
556580
{
557581
string? algId = encryptionScheme.Algorithm;
558582

583+
if (!Helpers.HasSymmetricEncryption)
584+
{
585+
throw new CryptographicException(
586+
SR.Format(SR.Cryptography_AlgorithmNotSupported, algId));
587+
}
588+
559589
if (algId == Oids.Aes128Cbc ||
560590
algId == Oids.Aes192Cbc ||
561591
algId == Oids.Aes256Cbc)
@@ -747,6 +777,12 @@ private static unsafe Rfc2898DeriveBytes OpenPbkdf2(
747777
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
748778
}
749779

780+
if (!Helpers.HasHMAC)
781+
{
782+
throw new CryptographicException(
783+
SR.Format(SR.Cryptography_AlgorithmNotSupported, "HMAC" + prf.Name));
784+
}
785+
750786
int iterationCount = NormalizeIterationCount(pbkdf2Params.IterationCount);
751787
ReadOnlyMemory<byte> saltMemory = pbkdf2Params.Salt.Specified.Value;
752788

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace System.Security.Cryptography
1414
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
1515
public partial class RSA : AsymmetricAlgorithm
1616
{
17-
public static new RSA Create() => new RSAImplementation.RSAOpenSsl();
17+
public static new partial RSA Create() => new RSAImplementation.RSAOpenSsl();
1818
}
1919

2020
internal static partial class RSAImplementation

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace System.Security.Cryptography
1616
#if INTERNAL_ASYMMETRIC_IMPLEMENTATIONS
1717
public partial class RSA : AsymmetricAlgorithm
1818
{
19-
public static new RSA Create()
19+
public static new partial RSA Create()
2020
{
2121
return new RSAImplementation.RSASecurityTransforms();
2222
}

src/libraries/System.DirectoryServices/src/System.DirectoryServices.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ System.DirectoryServices.ActiveDirectory.DomainController</PackageDescription>
182182
<Reference Include="System.Threading" />
183183
<Reference Include="System.Threading.Thread" />
184184
</ItemGroup>
185+
<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
186+
<Reference Include="System.Security.Cryptography" />
187+
</ItemGroup>
185188
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
186189
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="$(SystemIOFileSystemAccessControlVersion)" />
187190
<PackageReference Include="System.Security.AccessControl" Version="$(SystemSecurityAccessControlVersion)" />

src/libraries/System.Net.WebSockets/src/System.Net.WebSockets.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
5252
<Reference Include="System.Runtime.Extensions" />
5353
<Reference Include="System.Runtime.InteropServices" />
54-
<Reference Include="System.Security.Cryptography.Algorithms" />
54+
<Reference Include="System.Security.Cryptography" />
5555
<Reference Include="System.Security.Principal" />
5656
<Reference Include="System.Text.Encoding.Extensions" />
5757
<Reference Include="System.Threading" />

src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.Forwards.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,63 @@
44
// Changes to this file must follow the https://aka.ms/api-review process.
55
// ------------------------------------------------------------------------------
66

7+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.Aes))]
8+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AesCcm))]
9+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AesGcm))]
10+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AesManaged))]
11+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AsymmetricKeyExchangeDeformatter))]
712
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AsymmetricKeyExchangeFormatter))]
13+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AsymmetricSignatureDeformatter))]
14+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.AsymmetricSignatureFormatter))]
15+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ChaCha20Poly1305))]
16+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.CryptoConfig))]
17+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DES))]
18+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DSA))]
19+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DSAParameters))]
20+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DSASignatureDeformatter))]
21+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DSASignatureFormat))]
22+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DSASignatureFormatter))]
23+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.DeriveBytes))]
24+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECCurve))]
25+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECDiffieHellman))]
26+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECDiffieHellmanPublicKey))]
27+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECDsa))]
28+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECParameters))]
29+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.ECPoint))]
30+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HKDF))]
31+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HMACMD5))]
32+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HMACSHA1))]
33+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HMACSHA256))]
34+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HMACSHA384))]
35+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.HMACSHA512))]
36+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.IncrementalHash))]
37+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MD5))]
38+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.MaskGenerationMethod))]
39+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.PKCS1MaskGenerationMethod))]
40+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RC2))]
41+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSA))]
42+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAEncryptionPadding))]
43+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAEncryptionPaddingMode))]
44+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter))]
45+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAOAEPKeyExchangeFormatter))]
46+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter))]
47+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter))]
48+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAPKCS1SignatureDeformatter))]
49+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAPKCS1SignatureFormatter))]
50+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSAParameters))]
51+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSASignaturePadding))]
52+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RSASignaturePaddingMode))]
53+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RandomNumberGenerator))]
54+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.Rfc2898DeriveBytes))]
55+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.Rijndael))]
56+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.RijndaelManaged))]
57+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA1))]
58+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA1Managed))]
59+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA256))]
60+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA256Managed))]
61+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA384))]
62+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA384Managed))]
63+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA512))]
64+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SHA512Managed))]
65+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SignatureDescription))]
66+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.TripleDES))]

0 commit comments

Comments
 (0)