Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outbox PbeParameters and PbeEncryptionAlgorithm #113987

Merged
merged 7 commits into from
Mar 28, 2025
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
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Security.Cryptography
{
/// <summary>
/// Specifies encryption algorithms to be used with Password-Based Encryption (PBE).
/// </summary>
public enum PbeEncryptionAlgorithm
{
/// <summary>
/// Indicates that no encryption algorithm has been selected.
/// </summary>
Unknown = 0,

/// <summary>
/// Indicates the encryption should be performed with the AES-128 algorithm in CBC mode with PKCS#7 padding.
/// </summary>
Aes128Cbc = 1,

/// <summary>
/// Indicates the encryption should be performed with the AES-192 algorithm in CBC mode with PKCS#7 padding.
/// </summary>
Aes192Cbc = 2,

/// <summary>
/// Indicates the encryption should be performed with the AES-256 algorithm in CBC mode with PKCS#7 padding.
/// </summary>
Aes256Cbc = 3,

/// <summary>
/// Indicates the encryption should be performed with the TripleDES algorithm in CBC mode with a 192-bit key
/// derived using the Key Derivation Function (KDF) from PKCS#12.
/// </summary>
TripleDes3KeyPkcs12 = 4,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Security.Cryptography
{
/// <summary>
/// Represents parameters to be used for Password-Based Encryption (PBE).
/// </summary>
public sealed class PbeParameters
{
/// <summary>
/// Gets the algorithm to use when encrypting data.
/// </summary>
/// <value>
/// The algorithm to use when encrypting data.
/// </value>
public PbeEncryptionAlgorithm EncryptionAlgorithm { get; }

/// <summary>
/// Gets the name of the hash algorithm to use with the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </summary>
/// <value>
/// The name of the hash algorithm to use with the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </value>
public HashAlgorithmName HashAlgorithm { get; }

/// <summary>
/// Gets the iteration count to provide to the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </summary>
/// <value>
/// The iteration count to provide to the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </value>
public int IterationCount { get; }

/// <summary>
/// Initializes a new instance of the <see cref="PbeParameters" /> class.
/// </summary>
/// <param name="encryptionAlgorithm">The algorithm to use when encrypting data.</param>
/// <param name="hashAlgorithm">
/// The name of a hash algorithm to use with the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </param>
/// <param name="iterationCount">
/// The iteration count to provide to the Key Derivation Function (KDF) to turn a password
/// into an encryption key.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="iterationCount" /> is less than 1.
/// </exception>
public PbeParameters(
PbeEncryptionAlgorithm encryptionAlgorithm,
HashAlgorithmName hashAlgorithm,
int iterationCount)
{
#if NET
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(iterationCount);
#else
if (iterationCount <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(iterationCount),
iterationCount,
SR.Format(
SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero,
nameof(iterationCount),
iterationCount));
}
#endif

EncryptionAlgorithm = encryptionAlgorithm;
HashAlgorithm = hashAlgorithm;
IterationCount = iterationCount;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsa))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.SlhDsaAlgorithm))]
#endif
#if NET || NETSTANDARD2_1_OR_GREATER
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.PbeEncryptionAlgorithm))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Security.Cryptography.PbeParameters))]
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.0;netstandard2.1;$(NetFrameworkMinimum)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>true</IsPackable>
<PackageDescription>Provides support for some cryptographic primitives for .NET Framework and .NET Standard.</PackageDescription>
Expand All @@ -21,7 +21,7 @@
<Import Project="$(CommonPath)System\Security\Cryptography\Asn1\AsnXml.targets" Condition="'$(BuildX509Loader)' == 'true'" />
<Import Project="$(CommonPath)System\Security\Cryptography\Asn1Reader\System.Security.Cryptography.Asn1Reader.Shared.projitems" Condition="'$(BuildX509Loader)' == 'true' or '$(BuildPqc)' == 'true' " />

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<ItemGroup>
<Compile Include="Microsoft.Bcl.Cryptography.Forwards.cs" />
</ItemGroup>

Expand Down Expand Up @@ -74,6 +74,13 @@
<Compile Include="System\Security\Cryptography\SP800108HmacCounterKdfImplementationManaged.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1'))">
<Compile Include="$(CommonPath)System\Security\Cryptography\PbeEncryptionAlgorithm.cs"
Link="Common\System\Security\Cryptography\PbeEncryptionAlgorithm.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PbeParameters.cs"
Link="Common\System\Security\Cryptography\PbeParameters.cs" />
</ItemGroup>

<ItemGroup Condition="'$(BuildX509Loader)' == 'true' or '$(BuildPqc)' == 'true' ">
<AsnXml Include="$(CommonPath)System\Security\Cryptography\Asn1\AlgorithmIdentifierAsn.xml">
<Link>Common\System\Security\Cryptography\Asn1\AlgorithmIdentifierAsn.xml</Link>
Expand Down Expand Up @@ -284,15 +291,11 @@
Link="Common\System\Security\Cryptography\IncrementalHash.netfx.cs" />

<Compile Include="Microsoft\Win32\SafeHandles\SafePasswordHandle.cs" />
<Compile Include="System\Security\Cryptography\PbeEncryptionAlgorithm.netstandard.cs" />
<Compile Include="System\Security\Cryptography\PbeParameters.netstandard.cs" />
<Compile Include="System\Security\Cryptography\X509Certificates\X509CertificateLoader.netfx.cs" />
<Compile Include="System\Security\Cryptography\X509Certificates\X509CertificateLoader.ProcessedPkcs12.cs" />
</ItemGroup>

<ItemGroup Condition="'$(BuildX509Loader)' == 'true' and '$(TargetFrameworkIdentifier)' != '.NETFramework'">
<Compile Include="System\Security\Cryptography\PbeEncryptionAlgorithm.netstandard.cs" />
<Compile Include="System\Security\Cryptography\PbeParameters.netstandard.cs" />
<Compile Include="System\Security\Cryptography\X509Certificates\X509CertificateLoader.netstandard.cs" />
<Compile Include="System\Security\Cryptography\X509Certificates\X509CertificateLoader.ProcessedPkcs12.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ internal static bool TryGetHashAndReset(
}
}

#if !NETSTANDARD2_1_OR_GREATER
internal static class CryptographicOperations
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
Expand Down Expand Up @@ -146,6 +147,7 @@ internal static bool FixedTimeEquals(ReadOnlySpan<byte> left, ReadOnlySpan<byte>
return accum == 0;
}
}
#endif
}

namespace System.Runtime.CompilerServices
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
Link="CommonTest\System\Security\Cryptography\MLKemKeyTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemNotSupportedTests.cs"
Link="CommonTest\System\Security\Cryptography\MLKemNotSupportedTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PbeParametersTests.cs"
Link="CommonTest\System\Security\Cryptography\PbeParametersTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\SP800108HmacCounterKdfTests.ArgValidation.cs"
Link="CommonTest\System\Security\Cryptography\SP800108HmacCounterKdfTests.ArgValidation.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\SP800108HmacCounterKdfTests.Functional.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@
Link="Common\System\Security\Cryptography\Oids.Shared.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PasswordBasedEncryption.cs"
Link="Common\System\Security\Cryptography\PasswordBasedEncryption.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PbeEncryptionAlgorithm.cs"
Link="Common\System\Security\Cryptography\PbeEncryptionAlgorithm.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PbeParameters.cs"
Link="Common\System\Security\Cryptography\PbeParameters.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\PemLabels.cs"
Link="Common\System\Security\Cryptography\PemLabels.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\Pkcs12Kdf.cs"
Expand Down Expand Up @@ -572,8 +576,6 @@
<Compile Include="System\Security\Cryptography\OidLookup.cs" />
<Compile Include="System\Security\Cryptography\PaddingMode.cs" />
<Compile Include="System\Security\Cryptography\PasswordDeriveBytes.cs" />
<Compile Include="System\Security\Cryptography\PbeEncryptionAlgorithm.cs" />
<Compile Include="System\Security\Cryptography\PbeParameters.cs" />
<Compile Include="System\Security\Cryptography\PemEncoding.cs" />
<Compile Include="System\Security\Cryptography\PemEnumerator.cs" />
<Compile Include="System\Security\Cryptography\PemFields.cs" />
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
Link="CommonTest\System\Security\Cryptography\MLKemTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\MLKemKeyTests.cs"
Link="CommonTest\System\Security\Cryptography\MLKemKeyTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\PbeParametersTests.cs"
Link="CommonTest\System\Security\Cryptography\PbeParametersTests.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\SP800108HmacCounterKdfTests.ArgValidation.cs"
Link="CommonTest\System\Security\Cryptography\SP800108HmacCounterKdfTests.ArgValidation.cs" />
<Compile Include="$(CommonTestPath)System\Security\Cryptography\SP800108HmacCounterKdfTests.Functional.cs"
Expand Down Expand Up @@ -434,7 +436,6 @@
<Compile Include="OidCollectionTests.cs" />
<Compile Include="OpenSslNamedKeysTests.manual.cs" />
<Compile Include="PaddingModeTests.cs" />
<Compile Include="PbeParametersTests.cs" />
<Compile Include="PemEncodingTests.cs" />
<Compile Include="PemEncodingFindTests.cs" />
<Compile Include="PKCS1MaskGenerationMethodTest.cs" />
Expand Down
Loading