Skip to content

Commit

Permalink
Use new language features
Browse files Browse the repository at this point in the history
  • Loading branch information
ektrah committed Apr 29, 2024
1 parent befe9e0 commit f79e377
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 94 deletions.
20 changes: 9 additions & 11 deletions src/Cryptography/Ed25519.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ namespace NSec.Cryptography
//
public sealed class Ed25519 : SignatureAlgorithm
{
private static readonly PrivateKeyFormatter s_nsecPrivateKeyFormatter = new Ed25519PrivateKeyFormatter(new byte[] { 0xDE, 0x64, 0x42, 0xDE, crypto_sign_ed25519_SEEDBYTES, 0, crypto_sign_ed25519_BYTES, 0 });
private static readonly PrivateKeyFormatter s_nsecPrivateKeyFormatter = new Ed25519PrivateKeyFormatter([0xDE, 0x64, 0x42, 0xDE, crypto_sign_ed25519_SEEDBYTES, 0, crypto_sign_ed25519_BYTES, 0]);

private static readonly PublicKeyFormatter s_nsecPublicKeyFormatter = new Ed25519PublicKeyFormatter(new byte[] { 0xDE, 0x65, 0x42, 0xDE, crypto_sign_ed25519_PUBLICKEYBYTES, 0, crypto_sign_ed25519_BYTES, 0 });
private static readonly PublicKeyFormatter s_nsecPublicKeyFormatter = new Ed25519PublicKeyFormatter([0xDE, 0x65, 0x42, 0xDE, crypto_sign_ed25519_PUBLICKEYBYTES, 0, crypto_sign_ed25519_BYTES, 0]);

private static readonly PrivateKeyFormatter s_pkixPrivateKeyFormatter = new Ed25519PrivateKeyFormatter(new byte[]
{
private static readonly PrivateKeyFormatter s_pkixPrivateKeyFormatter = new Ed25519PrivateKeyFormatter([
// +-- SEQUENCE (3 elements)
// +-- INTEGER 0
// +-- SEQUENCE (1 element)
Expand All @@ -49,21 +48,20 @@ public sealed class Ed25519 : SignatureAlgorithm
// +-- OCTET STRING (32 bytes)
0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
0x03, 0x2B, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
});
]);

private static readonly PublicKeyFormatter s_pkixPublicKeyFormatter = new Ed25519PublicKeyFormatter(new byte[]
{
private static readonly PublicKeyFormatter s_pkixPublicKeyFormatter = new Ed25519PublicKeyFormatter([
// +-- SEQUENCE (2 elements)
// +-- SEQUENCE (1 element)
// | +-- OBJECT IDENTIFIER 1.3.101.112
// +-- BIT STRING (256 bits)
0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65,
0x70, 0x03, 0x21, 0x00,
});
]);

private static readonly PrivateKeyFormatter s_rawPrivateKeyFormatter = new Ed25519PrivateKeyFormatter(Array.Empty<byte>());
private static readonly PrivateKeyFormatter s_rawPrivateKeyFormatter = new Ed25519PrivateKeyFormatter([]);

private static readonly PublicKeyFormatter s_rawPublicKeyFormatter = new Ed25519PublicKeyFormatter(Array.Empty<byte>());
private static readonly PublicKeyFormatter s_rawPublicKeyFormatter = new Ed25519PublicKeyFormatter([]);

private static int s_selfTest;

Expand Down Expand Up @@ -199,7 +197,7 @@ internal override bool TryImportPublicKey(
}

private protected unsafe override bool VerifyCore(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
ReadOnlySpan<byte> data,
ReadOnlySpan<byte> signature)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Cryptography/Ed25519ph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ namespace NSec.Cryptography
//
public sealed class Ed25519ph : SignatureAlgorithm2
{
private static readonly PrivateKeyFormatter s_nsecPrivateKeyFormatter = new Ed25519PrivateKeyFormatter(new byte[] { 0xDE, 0x64, 0x48, 0xDE, crypto_sign_ed25519_SEEDBYTES, 0, crypto_sign_ed25519_BYTES, 0 });
private static readonly PrivateKeyFormatter s_nsecPrivateKeyFormatter = new Ed25519PrivateKeyFormatter([0xDE, 0x64, 0x48, 0xDE, crypto_sign_ed25519_SEEDBYTES, 0, crypto_sign_ed25519_BYTES, 0]);

private static readonly PublicKeyFormatter s_nsecPublicKeyFormatter = new Ed25519PublicKeyFormatter(new byte[] { 0xDE, 0x65, 0x48, 0xDE, crypto_sign_ed25519_PUBLICKEYBYTES, 0, crypto_sign_ed25519_BYTES, 0 });
private static readonly PublicKeyFormatter s_nsecPublicKeyFormatter = new Ed25519PublicKeyFormatter([0xDE, 0x65, 0x48, 0xDE, crypto_sign_ed25519_PUBLICKEYBYTES, 0, crypto_sign_ed25519_BYTES, 0]);

private static readonly PrivateKeyFormatter s_rawPrivateKeyFormatter = new Ed25519PrivateKeyFormatter(Array.Empty<byte>());
private static readonly PrivateKeyFormatter s_rawPrivateKeyFormatter = new Ed25519PrivateKeyFormatter([]);

private static readonly PublicKeyFormatter s_rawPublicKeyFormatter = new Ed25519PublicKeyFormatter(Array.Empty<byte>());
private static readonly PublicKeyFormatter s_rawPublicKeyFormatter = new Ed25519PublicKeyFormatter([]);

private static int s_selfTest;

Expand Down Expand Up @@ -114,7 +114,7 @@ private protected unsafe override void SignCore(
}

private protected unsafe override bool VerifyCore(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
ReadOnlySpan<byte> data,
ReadOnlySpan<byte> signature)
{
Expand Down Expand Up @@ -199,7 +199,7 @@ internal unsafe override void FinalSignCore(

internal unsafe override bool FinalVerifyCore(
ref IncrementalSignatureState state,
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
ReadOnlySpan<byte> signature)
{
if (Unsafe.SizeOf<PublicKeyBytes>() != crypto_sign_ed25519_PUBLICKEYBYTES)
Expand Down
12 changes: 4 additions & 8 deletions src/Cryptography/Formatting/Ed25519PublicKeyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

namespace NSec.Cryptography.Formatting
{
internal sealed class Ed25519PublicKeyFormatter : PublicKeyFormatter
internal sealed class Ed25519PublicKeyFormatter(byte[] blobHeader) : PublicKeyFormatter(
crypto_sign_ed25519_PUBLICKEYBYTES,
blobHeader)
{
public Ed25519PublicKeyFormatter(byte[] blobHeader) : base(
crypto_sign_ed25519_PUBLICKEYBYTES,
blobHeader)
{
}

protected override void Deserialize(
ReadOnlySpan<byte> span,
out PublicKeyBytes publicKeyBytes)
Expand All @@ -28,7 +24,7 @@ protected override void Deserialize(
}

protected override void Serialize(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
Span<byte> span)
{
if (Unsafe.SizeOf<PublicKeyBytes>() != crypto_sign_ed25519_PUBLICKEYBYTES)
Expand Down
20 changes: 10 additions & 10 deletions src/Cryptography/Formatting/PublicKeyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ namespace NSec.Cryptography.Formatting
internal abstract class PublicKeyFormatter
{
private static readonly byte[] s_beginLabel =
{
[
// "-----BEGIN PUBLIC KEY-----"
0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x42, 0x45, 0x47,
0x49, 0x4E, 0x20, 0x50, 0x55, 0x42, 0x4C, 0x49,
0x43, 0x20, 0x4B, 0x45, 0x59, 0x2D, 0x2D, 0x2D,
0x2D, 0x2D,
};
];

private static readonly byte[] s_endLabel =
{
[
// "-----END PUBLIC KEY-----"
0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x45, 0x4E, 0x44,
0x20, 0x50, 0x55, 0x42, 0x4C, 0x49, 0x43, 0x20,
0x4B, 0x45, 0x59, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D,
};
];

private readonly byte[] _blobHeader;
private readonly int _blobSize;
Expand All @@ -41,7 +41,7 @@ public PublicKeyFormatter(
}

public bool TryExport(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
Span<byte> blob,
out int blobSize)
{
Expand All @@ -58,7 +58,7 @@ public bool TryExport(
}

public bool TryExportText(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
Span<byte> blob,
out int blobSize)
{
Expand All @@ -72,9 +72,9 @@ public bool TryExportText(
Span<byte> temp = stackalloc byte[_blobSize];

_blobHeader.CopyTo(temp);
Serialize(in publicKeyBytes, temp.Slice(_blobHeader.Length));
Serialize(in publicKeyBytes, temp[_blobHeader.Length..]);

Armor.EncodeToUtf8(temp, s_beginLabel, s_endLabel, blob.Slice(0, _blobTextSize));
Armor.EncodeToUtf8(temp, s_beginLabel, s_endLabel, blob[.._blobTextSize]);
return true;
}

Expand All @@ -88,7 +88,7 @@ public bool TryImport(
return false;
}

Deserialize(blob.Slice(_blobHeader.Length), out result);
Deserialize(blob[_blobHeader.Length..], out result);
return true;
}

Expand All @@ -112,7 +112,7 @@ protected abstract void Deserialize(
out PublicKeyBytes publicKeyBytes);

protected abstract void Serialize(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
Span<byte> span);
}
}
12 changes: 4 additions & 8 deletions src/Cryptography/Formatting/X25519PublicKeyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

namespace NSec.Cryptography.Formatting
{
internal sealed class X25519PublicKeyFormatter : PublicKeyFormatter
internal sealed class X25519PublicKeyFormatter(byte[] blobHeader) : PublicKeyFormatter(
crypto_scalarmult_curve25519_SCALARBYTES,
blobHeader)
{
public X25519PublicKeyFormatter(byte[] blobHeader) : base(
crypto_scalarmult_curve25519_SCALARBYTES,
blobHeader)
{
}

protected override void Deserialize(
ReadOnlySpan<byte> span,
out PublicKeyBytes publicKeyBytes)
Expand All @@ -29,7 +25,7 @@ protected override void Deserialize(
}

protected override void Serialize(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
Span<byte> span)
{
if (Unsafe.SizeOf<PublicKeyBytes>() != crypto_scalarmult_curve25519_SCALARBYTES)
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography/IncrementalSignatureVerification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static bool FinalizeAndVerify(
try
{
return signature.Length == state._algorithm.SignatureSize &&
state._algorithm.FinalVerifyCore(ref Unsafe.AsRef(in state._state), state._publicKey.GetPinnableReference(), signature);
state._algorithm.FinalVerifyCore(ref Unsafe.AsRef(in state._state), in state._publicKey.GetPinnableReference(), signature);
}
finally
{
Expand Down
25 changes: 8 additions & 17 deletions src/Cryptography/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Key(

internal Key(
Algorithm algorithm,
in KeyCreationParameters creationParameters,
ref readonly KeyCreationParameters creationParameters,
SecureMemoryHandle keyHandle,
PublicKey? publicKey)
{
Expand Down Expand Up @@ -170,10 +170,7 @@ public byte[] Export(

if (format < 0)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Key).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

if ((_exportPolicy & KeyExportPolicies.AllowPlaintextExport) == 0)
{
Expand All @@ -187,7 +184,7 @@ public byte[] Export(
}
}

_algorithm.TryExportKey(_handle, format, Span<byte>.Empty, out blobSize);
_algorithm.TryExportKey(_handle, format, [], out blobSize);
blob = new byte[blobSize];

if (!_algorithm.TryExportKey(_handle, format, blob, out blobSize))
Expand All @@ -206,7 +203,7 @@ public byte[] Export(
throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
}

_algorithm.TryExportPublicKey(_publicKey, format, Span<byte>.Empty, out blobSize);
_algorithm.TryExportPublicKey(_publicKey, format, [], out blobSize);
blob = new byte[blobSize];

if (!_algorithm.TryExportPublicKey(_publicKey, format, blob, out blobSize))
Expand All @@ -226,12 +223,9 @@ public int GetExportBlobSize(

if (format < 0)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Key).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

_algorithm.TryExportKey(_handle, format, Span<byte>.Empty, out blobSize);
_algorithm.TryExportKey(_handle, format, [], out blobSize);
}
else
{
Expand All @@ -240,7 +234,7 @@ public int GetExportBlobSize(
throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
}

_algorithm.TryExportPublicKey(_publicKey, format, Span<byte>.Empty, out blobSize);
_algorithm.TryExportPublicKey(_publicKey, format, [], out blobSize);
}

return blobSize;
Expand All @@ -259,10 +253,7 @@ public bool TryExport(
{
if (format < 0)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Key).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

if ((_exportPolicy & KeyExportPolicies.AllowPlaintextExport) == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography/KeyAgreementAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal sealed override int GetPublicKeySize()

private protected abstract bool AgreeCore(
SecureMemoryHandle keyHandle,
in PublicKeyBytes otherPartyPublicKey,
ref readonly PublicKeyBytes otherPartyPublicKey,
out SecureMemoryHandle? sharedSecretHandle);
}
}
2 changes: 1 addition & 1 deletion src/Cryptography/KeyDerivationAlgorithm2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public byte[] Expand(
}
if (count == 0)
{
return Array.Empty<byte>();
return [];
}

byte[] bytes = new byte[count];
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography/PasswordBasedKeyDerivationAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public byte[] DeriveBytes(
}
if (count == 0)
{
return Array.Empty<byte>();
return [];
}

byte[] bytes = new byte[count];
Expand Down
19 changes: 5 additions & 14 deletions src/Cryptography/SharedSecret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public void Dispose()
public byte[] Export(
SharedSecretBlobFormat format)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(SharedSecret).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

if ((_exportPolicy & KeyExportPolicies.AllowPlaintextExport) == 0)
{
Expand All @@ -113,7 +110,7 @@ public byte[] Export(
}
}

TryExportCore(_handle, format, Span<byte>.Empty, out int blobSize);
TryExportCore(_handle, format, [], out int blobSize);
byte[] blob = new byte[blobSize];

if (!TryExportCore(_handle, format, blob, out blobSize))
Expand All @@ -129,12 +126,9 @@ public byte[] Export(
public int GetExportBlobSize(
SharedSecretBlobFormat format)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(SharedSecret).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

TryExportCore(_handle, format, Span<byte>.Empty, out int blobSize);
TryExportCore(_handle, format, [], out int blobSize);
return blobSize;
}

Expand All @@ -149,10 +143,7 @@ public bool TryExport(
Span<byte> blob,
out int blobSize)
{
if (_handle.IsClosed)
{
throw new ObjectDisposedException(typeof(SharedSecret).FullName);
}
ObjectDisposedException.ThrowIf(_handle.IsClosed, this);

if ((_exportPolicy & KeyExportPolicies.AllowPlaintextExport) == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography/SignatureAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private protected abstract void SignCore(
Span<byte> signature);

private protected abstract bool VerifyCore(
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
ReadOnlySpan<byte> data,
ReadOnlySpan<byte> signature);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cryptography/SignatureAlgorithm2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal abstract void FinalSignCore(

internal abstract bool FinalVerifyCore(
ref IncrementalSignatureState state,
in PublicKeyBytes publicKeyBytes,
ref readonly PublicKeyBytes publicKeyBytes,
ReadOnlySpan<byte> signature);
}
}
Loading

0 comments on commit f79e377

Please sign in to comment.