Skip to content

Commit 4da19d5

Browse files
committed
docs(keys): add consistent docs for instances and bases of IPrivateKey and IPublicKey
1 parent f95871a commit 4da19d5

File tree

12 files changed

+127
-12
lines changed

12 files changed

+127
-12
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/Curve25519PrivateKey.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@
1717

1818
namespace Yubico.YubiKey.Cryptography;
1919

20+
/// <summary>
21+
/// Represents a Curve25519 private key.
22+
/// </summary>
23+
/// <remarks>
24+
/// This sealed class encapsulates Curve25519 private key data and supports
25+
/// both Ed25519 and X25519 cryptographic operations.
26+
/// It also provides factory methods for creating instances from private key values or DER-encoded data.
27+
/// </remarks>
2028
public sealed class Curve25519PrivateKey : PrivateKey
2129
{
2230
private readonly Memory<byte> _privateKey;
2331

2432
/// <inheritdoc />
2533
public override KeyType KeyType => KeyDefinition.KeyType;
26-
34+
2735
/// <summary>
2836
/// Gets the key definition associated with this RSA private key.
2937
/// </summary>

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/Curve25519PublicKey.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
namespace Yubico.YubiKey.Cryptography;
2121

22+
/// <summary>
23+
/// Represents a Curve25519 public key.
24+
/// </summary>
25+
/// <remarks>
26+
/// This sealed class encapsulates Curve25519 public key data as a compressed point
27+
/// and supports both Ed25519 and X25519 key types.
28+
/// It also provides factory methods for creating instances from public point values or DER-encoded data.
29+
/// </remarks>
2230
public sealed class Curve25519PublicKey : PublicKey
2331
{
2432
private readonly Memory<byte> _publicPoint;

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPrivateKey.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ namespace Yubico.YubiKey.Cryptography
2121
/// Represents the parameters for an Elliptic Curve (EC) private key.
2222
/// </summary>
2323
/// <remarks>
24-
/// This class encapsulates the parameters specific to EC private keys and
25-
/// contains the necessary private key data.
24+
/// This class encapsulates the parameters specific to EC private keys
25+
/// and provides factory methods for creating instances from EC parameters
26+
/// or DER-encoded data.
2627
/// </remarks>
2728
public class ECPrivateKey : PrivateKey
2829
{
@@ -119,6 +120,12 @@ public static ECPrivateKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey)
119120
}
120121

121122
#pragma warning disable CS0618 // Type or member is obsolete.
123+
124+
/// <summary>
125+
/// Creates an instance of <see cref="ECPrivateKey"/> from the given <paramref name="parameters"/>.
126+
/// </summary>
127+
/// <param name="parameters">The parameters to create the key from.</param>
128+
/// <returns>An instance of <see cref="ECPrivateKey"/>.</returns>
122129
public static ECPrivateKey CreateFromParameters(ECParameters parameters) => new(parameters);
123130
#pragma warning restore CS0618 // Type or member is obsolete
124131

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPublicKey.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
namespace Yubico.YubiKey.Cryptography;
1919

2020
/// <summary>
21-
/// Represents the parameters for an Elliptic Curve (EC) public key.
21+
/// Represents an Elliptic Curve (EC) public key.
2222
/// </summary>
2323
/// <remarks>
24-
/// This class encapsulates the parameters specific to EC public keys,
25-
/// ensuring that the key only contains necessary public key components.
24+
/// This class encapsulates EC public key parameters and provides cryptographic operations
25+
/// for NIST elliptic curves and provides factory methods for creating instances from EC parameters or DER-encoded data.
2626
/// </remarks>
2727
public class ECPublicKey : PublicKey
2828
{

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IKeyBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
namespace Yubico.YubiKey.Cryptography;
1616

17+
/// <summary>
18+
/// Defines the base contract for all cryptographic keys, providing key type identification.
19+
/// </summary>
20+
/// <remarks>
21+
/// This interface serves as the foundation for both public and private key abstractions,
22+
/// enabling polymorphic key type handling across different cryptographic algorithms.
23+
/// </remarks>
1724
public interface IKeyBase
1825
{
1926
/// <summary>

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IPrivateKey.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
namespace Yubico.YubiKey.Cryptography;
1616

17+
/// <summary>
18+
/// Defines the contract for cryptographic private keys.
19+
/// </summary>
20+
/// <remarks>
21+
/// This interface extends <see cref="IKeyBase"/> to include private key-specific operations
22+
/// for PKCS#8 export and secure memory cleanup.
23+
/// Known implementations include <see cref="ECPrivateKey"/>, <see cref="RSAPrivateKey"/> and <see cref="Curve25519PrivateKey"/>,.
24+
/// </remarks>
1725
public interface IPrivateKey : IKeyBase
1826
{
1927
/// <summary>

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IPrivateKeyExtensions.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,33 @@
1616

1717
namespace Yubico.YubiKey.Cryptography;
1818

19+
20+
/// <summary>
21+
/// Extension methods for <see cref="IPrivateKey"/> to provide type-safe casting operations.
22+
/// </summary>
1923
public static class IPrivateKeyExtensions
2024
{
2125
/// <summary>
22-
/// Casts the key to the specified type.
26+
/// Safely casts an <see cref="IPrivateKey"/> instance to a specific derived type.
2327
/// </summary>
24-
/// <param name="key"></param>
25-
/// <typeparam name="T"></typeparam>
26-
/// <returns></returns>
27-
/// <exception cref="InvalidCastException"></exception>
28+
/// <typeparam name="T">The target type that implements <see cref="IPrivateKey"/>.</typeparam>
29+
/// <param name="key">The private key instance to cast.</param>
30+
/// <returns>The private key cast to the specified type <typeparamref name="T"/>.</returns>
31+
/// <exception cref="InvalidCastException">
32+
/// Thrown when the <paramref name="key"/> cannot be cast to type <typeparamref name="T"/>.
33+
/// The exception message includes both the source and target type names for debugging.
34+
/// </exception>
35+
/// <example>
36+
/// <code>
37+
/// IPrivateKey genericKey = GetPrivateKey();
38+
/// RsaPrivateKey rsaKey = genericKey.Cast&lt;RsaPrivateKey&gt;();
39+
/// // throws InvalidCastException if genericKey is not an RsaPrivateKey
40+
/// </code>
41+
/// </example>
42+
/// <remarks>
43+
/// This method provides a more explicit alternative to direct casting with clearer error messages.
44+
/// Prefer this over unsafe casting operations when type safety is critical for cryptographic operations.
45+
/// </remarks>
2846
public static T Cast<T>(this IPrivateKey key) where T : class, IPrivateKey =>
2947
key as T ?? throw new InvalidCastException($"Cannot cast {key.GetType()} to {typeof(T)}");
3048
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IPublicKey.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414

1515
namespace Yubico.YubiKey.Cryptography;
1616

17+
/// <summary>
18+
/// Defines the contract for cryptographic public keys.
19+
/// </summary>
20+
/// <remarks>
21+
/// This interface extends <see cref="IKeyBase"/> to include public key-specific operations
22+
/// for X.509 SubjectPublicKeyInfo export.
23+
/// <para>
24+
/// Concrete implementations include <see cref="ECPublicKey"/>, <see cref="RSAPublicKey"/> and <see cref="Curve25519PublicKey"/>, each providing
25+
/// algorithm-specific public key handling and export mechanisms.
26+
/// </para>
27+
/// </remarks>
1728
public interface IPublicKey : IKeyBase
1829
{
1930
/// <summary>

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/PrivateKey.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
namespace Yubico.YubiKey.Cryptography;
1818

19+
/// <summary>
20+
/// Abstract base class for private key implementations.
21+
/// </summary>
22+
/// <remarks>
23+
/// This class implements the standard .NET disposal pattern to ensure secure cleanup
24+
/// of sensitive cryptographic material and provides disposal state checking for derived classes.
25+
/// <para>
26+
/// Concrete implementations include <see cref="ECPrivateKey"/>, <see cref="RSAPrivateKey"/> and <see cref="Curve25519PrivateKey"/>,
27+
/// each providing algorithm-specific key handling and cryptographic operations.
28+
/// </para>
29+
/// </remarks>
1930
public abstract class PrivateKey : IPrivateKey, IDisposable
2031
{
2132
private bool _disposed;
@@ -43,6 +54,17 @@ public void Dispose()
4354
GC.SuppressFinalize(this);
4455
}
4556

57+
/// <summary>
58+
/// Throws an <see cref="ObjectDisposedException"/> if this instance has been disposed.
59+
/// </summary>
60+
/// <exception cref="ObjectDisposedException">
61+
/// Thrown when this method is called after the object has been disposed.
62+
/// </exception>
63+
/// <remarks>
64+
/// This method should be called at the beginning of all public methods and properties
65+
/// in derived classes to prevent operations on disposed cryptographic key material.
66+
/// The exception message includes the concrete type name for debugging purposes.
67+
/// </remarks>
4668
protected void ThrowIfDisposed()
4769
{
4870
if (_disposed)

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/PublicKey.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@
1414

1515
namespace Yubico.YubiKey.Cryptography;
1616

17+
/// <summary>
18+
/// Abstract base class for public key implementations.
19+
/// </summary>
20+
/// <remarks>
21+
/// This class provides common structure for public key types, requiring derived classes
22+
/// to implement key type identification and X.509 export operations.
23+
/// <para>
24+
/// Concrete implementations include <see cref="ECPublicKey"/>, <see cref="RSAPublicKey"/> and <see cref="Curve25519PublicKey"/>, each providing
25+
/// algorithm-specific public key handling and export mechanisms.
26+
/// </para>
27+
/// </remarks>
1728
public abstract class PublicKey : IPublicKey
1829
{
1930
/// <inheritdoc />
2031
public abstract KeyType KeyType { get; }
21-
32+
2233
/// <inheritdoc />
2334
public abstract byte[] ExportSubjectPublicKeyInfo();
2435
}

0 commit comments

Comments
 (0)