Skip to content

Commit 2611ca5

Browse files
authored
Rename SecretKey to PrivateKey for SLH-DSA
1 parent 51a4123 commit 2611ca5

File tree

24 files changed

+247
-247
lines changed

24 files changed

+247
-247
lines changed

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ public bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritte
616616
3 + // Version Integer
617617
2 + // AlgorithmIdentifier Sequence
618618
3 + // AlgorithmIdentifier OID value, undervalued to be safe
619-
2 + // Secret key Octet String prefix, undervalued to be safe
620-
Algorithm.SecretKeySizeInBytes;
619+
2 + // Private key Octet String prefix, undervalued to be safe
620+
Algorithm.PrivateKeySizeInBytes;
621621

622622
if (destination.Length < MinimumPossiblePkcs8SlhDsaKey)
623623
{
@@ -650,19 +650,19 @@ public bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritte
650650
/// </exception>
651651
protected virtual bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten)
652652
{
653-
// Secret key size for SLH-DSA is at most 128 bytes so we can stack allocate it.
654-
int secretKeySizeInBytes = Algorithm.SecretKeySizeInBytes;
655-
Debug.Assert(secretKeySizeInBytes is <= 128);
656-
Span<byte> secretKey = (stackalloc byte[128])[..secretKeySizeInBytes];
653+
// Private key size for SLH-DSA is at most 128 bytes so we can stack allocate it.
654+
int privateKeySizeInBytes = Algorithm.PrivateKeySizeInBytes;
655+
Debug.Assert(privateKeySizeInBytes is <= 128);
656+
Span<byte> privateKey = (stackalloc byte[128])[..privateKeySizeInBytes];
657657

658658
try
659659
{
660-
ExportSlhDsaSecretKey(secretKey);
660+
ExportSlhDsaPrivateKey(privateKey);
661661

662662
// The ASN.1 overhead of a PrivateKeyInfo encoding a private key is 22 bytes.
663663
// Round it off to 32. This checked operation should never throw because the inputs are not
664664
// user provided.
665-
int capacity = checked(32 + secretKeySizeInBytes);
665+
int capacity = checked(32 + privateKeySizeInBytes);
666666
AsnWriter writer = new AsnWriter(AsnEncodingRules.DER, capacity);
667667

668668
using (writer.PushSequence())
@@ -674,15 +674,15 @@ protected virtual bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out
674674
writer.WriteObjectIdentifier(Algorithm.Oid);
675675
}
676676

677-
writer.WriteOctetString(secretKey);
677+
writer.WriteOctetString(privateKey);
678678
}
679679

680680
Debug.Assert(writer.GetEncodedLength() <= capacity);
681681
return writer.TryEncode(destination, out bytesWritten);
682682
}
683683
finally
684684
{
685-
CryptographicOperations.ZeroMemory(secretKey);
685+
CryptographicOperations.ZeroMemory(privateKey);
686686
}
687687
}
688688

@@ -1108,55 +1108,55 @@ public byte[] ExportSlhDsaPublicKey()
11081108
}
11091109

11101110
/// <summary>
1111-
/// Exports the current key in the FIPS 205 secret key format.
1111+
/// Exports the current key in the FIPS 205 private key format.
11121112
/// </summary>
11131113
/// <param name="destination">
1114-
/// The buffer to receive the secret key. Its length must be exactly
1115-
/// <see cref="SlhDsaAlgorithm.SecretKeySizeInBytes"/>.
1114+
/// The buffer to receive the private key. Its length must be exactly
1115+
/// <see cref="SlhDsaAlgorithm.PrivateKeySizeInBytes"/>.
11161116
/// </param>
11171117
/// <exception cref="ArgumentException">
1118-
/// <paramref name="destination"/> is the incorrect length to receive the secret key.
1118+
/// <paramref name="destination"/> is the incorrect length to receive the private key.
11191119
/// </exception>
11201120
/// <exception cref="CryptographicException">
1121-
/// <para>The current instance cannot export a secret key.</para>
1121+
/// <para>The current instance cannot export a private key.</para>
11221122
/// <para>-or-</para>
11231123
/// <para>An error occurred while exporting the key.</para>
11241124
/// </exception>
11251125
/// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
1126-
public void ExportSlhDsaSecretKey(Span<byte> destination)
1126+
public void ExportSlhDsaPrivateKey(Span<byte> destination)
11271127
{
1128-
int secretKeySizeInBytes = Algorithm.SecretKeySizeInBytes;
1128+
int privateKeySizeInBytes = Algorithm.PrivateKeySizeInBytes;
11291129

1130-
if (destination.Length != secretKeySizeInBytes)
1130+
if (destination.Length != privateKeySizeInBytes)
11311131
{
11321132
throw new ArgumentException(
1133-
SR.Format(SR.Argument_DestinationImprecise, secretKeySizeInBytes),
1133+
SR.Format(SR.Argument_DestinationImprecise, privateKeySizeInBytes),
11341134
nameof(destination));
11351135
}
11361136

11371137
ThrowIfDisposed();
11381138

1139-
ExportSlhDsaSecretKeyCore(destination);
1139+
ExportSlhDsaPrivateKeyCore(destination);
11401140
}
11411141

11421142
/// <summary>
1143-
/// Exports the current key in the FIPS 205 secret key format.
1143+
/// Exports the current key in the FIPS 205 private key format.
11441144
/// </summary>
11451145
/// <returns>
1146-
/// The FIPS 205 secret key.
1146+
/// The FIPS 205 private key.
11471147
/// </returns>
11481148
/// <exception cref="CryptographicException">
1149-
/// <para>The current instance cannot export a secret key.</para>
1149+
/// <para>The current instance cannot export a private key.</para>
11501150
/// <para>-or-</para>
11511151
/// <para>An error occurred while exporting the key.</para>
11521152
/// </exception>
11531153
/// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
1154-
public byte[] ExportSlhDsaSecretKey()
1154+
public byte[] ExportSlhDsaPrivateKey()
11551155
{
11561156
ThrowIfDisposed();
11571157

1158-
byte[] destination = new byte[Algorithm.SecretKeySizeInBytes];
1159-
ExportSlhDsaSecretKeyCore(destination);
1158+
byte[] destination = new byte[Algorithm.PrivateKeySizeInBytes];
1159+
ExportSlhDsaPrivateKeyCore(destination);
11601160
return destination;
11611161
}
11621162

@@ -1293,12 +1293,12 @@ public static SlhDsa ImportPkcs8PrivateKey(ReadOnlySpan<byte> source)
12931293
SlhDsaAlgorithm info = GetAlgorithmIdentifier(in algId);
12941294
ReadOnlySpan<byte> privateKey = key.Span;
12951295

1296-
if (privateKey.Length != info.SecretKeySizeInBytes)
1296+
if (privateKey.Length != info.PrivateKeySizeInBytes)
12971297
{
12981298
throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
12991299
}
13001300

1301-
ret = ImportSlhDsaSecretKey(info, key.Span);
1301+
ret = ImportSlhDsaPrivateKey(info, key.Span);
13021302
},
13031303
out int read,
13041304
out SlhDsa slhDsa);
@@ -1704,13 +1704,13 @@ public static SlhDsa ImportSlhDsaPublicKey(SlhDsaAlgorithm algorithm, byte[] sou
17041704
}
17051705

17061706
/// <summary>
1707-
/// Imports an SLH-DSA private key in the FIPS 205 secret key format.
1707+
/// Imports an SLH-DSA private key in the FIPS 205 private key format.
17081708
/// </summary>
17091709
/// <param name="algorithm">
17101710
/// The specific SLH-DSA algorithm for this key.
17111711
/// </param>
17121712
/// <param name="source">
1713-
/// The bytes of a FIPS 205 secret key.
1713+
/// The bytes of a FIPS 205 private key.
17141714
/// </param>
17151715
/// <returns>
17161716
/// The imported key.
@@ -1728,29 +1728,29 @@ public static SlhDsa ImportSlhDsaPublicKey(SlhDsaAlgorithm algorithm, byte[] sou
17281728
/// The platform does not support SLH-DSA. Callers can use the <see cref="IsSupported" /> property
17291729
/// to determine if the platform supports SLH-DSA.
17301730
/// </exception>
1731-
public static SlhDsa ImportSlhDsaSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
1731+
public static SlhDsa ImportSlhDsaPrivateKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source)
17321732
{
17331733
ArgumentNullException.ThrowIfNull(algorithm);
17341734

1735-
if (source.Length != algorithm.SecretKeySizeInBytes)
1735+
if (source.Length != algorithm.PrivateKeySizeInBytes)
17361736
{
1737-
throw new ArgumentException(SR.Argument_SecretKeyWrongSizeForAlgorithm, nameof(source));
1737+
throw new ArgumentException(SR.Argument_PrivateKeyWrongSizeForAlgorithm, nameof(source));
17381738
}
17391739

17401740
ThrowIfNotSupported();
17411741

1742-
return SlhDsaImplementation.ImportSecretKey(algorithm, source);
1742+
return SlhDsaImplementation.ImportPrivateKey(algorithm, source);
17431743
}
17441744

1745-
/// <inheritdoc cref="ImportSlhDsaSecretKey(SlhDsaAlgorithm, ReadOnlySpan{byte})" />
1745+
/// <inheritdoc cref="ImportSlhDsaPrivateKey(SlhDsaAlgorithm, ReadOnlySpan{byte})" />
17461746
/// <exception cref="ArgumentNullException">
17471747
/// <paramref name="algorithm"/> or <paramref name="source" /> is <see langword="null" />.
17481748
/// </exception>
1749-
public static SlhDsa ImportSlhDsaSecretKey(SlhDsaAlgorithm algorithm, byte[] source)
1749+
public static SlhDsa ImportSlhDsaPrivateKey(SlhDsaAlgorithm algorithm, byte[] source)
17501750
{
17511751
ArgumentNullException.ThrowIfNull(source);
17521752

1753-
return ImportSlhDsaSecretKey(algorithm, new ReadOnlySpan<byte>(source));
1753+
return ImportSlhDsaPrivateKey(algorithm, new ReadOnlySpan<byte>(source));
17541754
}
17551755

17561756
/// <summary>
@@ -1856,12 +1856,12 @@ protected virtual void Dispose(bool disposing)
18561856
protected abstract void ExportSlhDsaPublicKeyCore(Span<byte> destination);
18571857

18581858
/// <summary>
1859-
/// When overridden in a derived class, exports the FIPS 205 secret key to the specified buffer.
1859+
/// When overridden in a derived class, exports the FIPS 205 private key to the specified buffer.
18601860
/// </summary>
18611861
/// <param name="destination">
1862-
/// The buffer to receive the secret key.
1862+
/// The buffer to receive the private key.
18631863
/// </param>
1864-
protected abstract void ExportSlhDsaSecretKeyCore(Span<byte> destination);
1864+
protected abstract void ExportSlhDsaPrivateKeyCore(Span<byte> destination);
18651865

18661866
private AsnWriter ExportSubjectPublicKeyInfoCore()
18671867
{
@@ -1952,7 +1952,7 @@ private TResult ExportPkcs8PrivateKeyCallback<TResult>(ExportPkcs8PrivateKeyFunc
19521952
{
19531953
// A PKCS#8 SLH-DSA-SHA2-256s private key has an ASN.1 overhead of 22 bytes, assuming no attributes.
19541954
// Make it an even 32 and that should give a good starting point for a buffer size.
1955-
int size = Algorithm.SecretKeySizeInBytes + 32;
1955+
int size = Algorithm.PrivateKeySizeInBytes + 32;
19561956
// The buffer is only being passed out as a span, so the derived type can't meaningfully
19571957
// hold on to it without being malicious.
19581958
byte[] buffer = CryptoPool.Rent(size);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public sealed class SlhDsaAlgorithm : IEquatable<SlhDsaAlgorithm>
2222
public string Name { get; }
2323

2424
/// <summary>
25-
/// Gets the size of the secret key in bytes for this algorithm.
25+
/// Gets the size of the private key in bytes for this algorithm.
2626
/// </summary>
2727
/// <value>
28-
/// The size of the secret key in bytes for this algorithm.
28+
/// The size of the private key in bytes for this algorithm.
2929
/// </value>
30-
public int SecretKeySizeInBytes { get; }
30+
public int PrivateKeySizeInBytes { get; }
3131

3232
/// <summary>
3333
/// Gets the size of the public key in bytes for this algorithm.
@@ -72,9 +72,9 @@ private SlhDsaAlgorithm(string name, int n, int signatureSizeInBytes, string oid
7272
{
7373
Name = name;
7474

75-
// The secret key and public key sizes are shown to be 4n and 2n respectively in
75+
// The private key and public key sizes are shown to be 4n and 2n respectively in
7676
// section 9.1 "Key Generation", particularly figure 15 and 16.
77-
SecretKeySizeInBytes = 4 * n;
77+
PrivateKeySizeInBytes = 4 * n;
7878
PublicKeySizeInBytes = 2 * n;
7979
SignatureSizeInBytes = signatureSizeInBytes;
8080
Oid = oid;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected override void ExportSlhDsaPublicKeyCore(Span<byte> destination) =>
8282
throw new PlatformNotSupportedException();
8383

8484
/// <inheritdoc />
85-
protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
85+
protected override void ExportSlhDsaPrivateKeyCore(Span<byte> destination) =>
8686
throw new PlatformNotSupportedException();
8787

8888
/// <inheritdoc />

src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.NotSupported.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override bool VerifyPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<
3232
protected override void ExportSlhDsaPublicKeyCore(Span<byte> destination) =>
3333
throw new PlatformNotSupportedException();
3434

35-
protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
35+
protected override void ExportSlhDsaPrivateKeyCore(Span<byte> destination) =>
3636
throw new PlatformNotSupportedException();
3737

3838
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
@@ -44,7 +44,7 @@ internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm alg
4444
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4545
throw new PlatformNotSupportedException();
4646

47-
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
47+
internal static partial SlhDsaImplementation ImportPrivateKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4848
throw new PlatformNotSupportedException();
4949
}
5050
}

src/libraries/Common/src/System/Security/Cryptography/SlhDsaImplementation.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override bool VerifyPreHashCore(ReadOnlySpan<byte> hash, ReadOnlySpan<
3232
protected override void ExportSlhDsaPublicKeyCore(Span<byte> destination) =>
3333
throw new PlatformNotSupportedException();
3434

35-
protected override void ExportSlhDsaSecretKeyCore(Span<byte> destination) =>
35+
protected override void ExportSlhDsaPrivateKeyCore(Span<byte> destination) =>
3636
throw new PlatformNotSupportedException();
3737

3838
protected override bool TryExportPkcs8PrivateKeyCore(Span<byte> destination, out int bytesWritten) =>
@@ -44,7 +44,7 @@ internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm alg
4444
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4545
throw new PlatformNotSupportedException();
4646

47-
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
47+
internal static partial SlhDsaImplementation ImportPrivateKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source) =>
4848
throw new PlatformNotSupportedException();
4949
}
5050
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal sealed partial class SlhDsaImplementation : SlhDsa
1414
internal static partial SlhDsaImplementation GenerateKeyCore(SlhDsaAlgorithm algorithm);
1515
internal static partial SlhDsaImplementation ImportPublicKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
1616
internal static partial SlhDsaImplementation ImportPkcs8PrivateKeyValue(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
17-
internal static partial SlhDsaImplementation ImportSecretKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
17+
internal static partial SlhDsaImplementation ImportPrivateKey(SlhDsaAlgorithm algorithm, ReadOnlySpan<byte> source);
1818

1919
/// <summary>
2020
/// Duplicates an SLH-DSA private key by export/import.
@@ -23,14 +23,14 @@ internal sealed partial class SlhDsaImplementation : SlhDsa
2323
internal static SlhDsaImplementation DuplicatePrivateKey(SlhDsa key)
2424
{
2525
Debug.Assert(key is not SlhDsaImplementation);
26-
Debug.Assert(key.Algorithm.SecretKeySizeInBytes <= 128);
26+
Debug.Assert(key.Algorithm.PrivateKeySizeInBytes <= 128);
2727

28-
Span<byte> secretKey = (stackalloc byte[128])[..key.Algorithm.SecretKeySizeInBytes];
29-
key.ExportSlhDsaSecretKey(secretKey);
28+
Span<byte> secretKey = (stackalloc byte[128])[..key.Algorithm.PrivateKeySizeInBytes];
29+
key.ExportSlhDsaPrivateKey(secretKey);
3030

3131
try
3232
{
33-
return ImportSecretKey(key.Algorithm, secretKey);
33+
return ImportPrivateKey(key.Algorithm, secretKey);
3434
}
3535
finally
3636
{

0 commit comments

Comments
 (0)