Skip to content

Commit

Permalink
Switch to LibraryImport
Browse files Browse the repository at this point in the history
  • Loading branch information
ektrah committed May 4, 2024
1 parent 0bf1ab2 commit bb6cc6c
Show file tree
Hide file tree
Showing 52 changed files with 1,278 additions and 1,391 deletions.
72 changes: 30 additions & 42 deletions src/Cryptography/Aegis128L.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal override void CreateKey(
keyHandle = SecureMemoryHandle.CreateFrom(seed);
}

private protected unsafe override void EncryptCore(
private protected override void EncryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -75,33 +75,27 @@ private protected unsafe override void EncryptCore(
Debug.Assert(nonce.Length == crypto_aead_aegis128l_NPUBBYTES);
Debug.Assert(ciphertext.Length == plaintext.Length + crypto_aead_aegis128l_ABYTES);

fixed (byte* c = ciphertext)
fixed (byte* m = plaintext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aegis128l_encrypt(
c,
out ulong clen_p,
m,
(ulong)plaintext.Length,
ad,
(ulong)associatedData.Length,
null,
n,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen_p);
}
int error = crypto_aead_aegis128l_encrypt(
ciphertext,
out ulong clen,
plaintext,
(ulong)plaintext.Length,
associatedData,
(ulong)associatedData.Length,
IntPtr.Zero,
nonce,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen);
}

internal override int GetSeedSize()
{
return crypto_aead_aegis128l_KEYBYTES;
}

private protected unsafe override bool DecryptCore(
private protected override bool DecryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -112,27 +106,21 @@ private protected unsafe override bool DecryptCore(
Debug.Assert(nonce.Length == crypto_aead_aegis128l_NPUBBYTES);
Debug.Assert(plaintext.Length == ciphertext.Length - crypto_aead_aegis128l_ABYTES);

fixed (byte* m = plaintext)
fixed (byte* c = ciphertext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aegis128l_decrypt(
m,
out ulong mlen_p,
null,
c,
(ulong)ciphertext.Length,
ad,
(ulong)associatedData.Length,
n,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen_p);
return error == 0;
}
int error = crypto_aead_aegis128l_decrypt(
plaintext,
out ulong mlen,
IntPtr.Zero,
ciphertext,
(ulong)ciphertext.Length,
associatedData,
(ulong)associatedData.Length,
nonce,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen);
return error == 0;
}

internal override bool TryExportKey(
Expand Down
72 changes: 30 additions & 42 deletions src/Cryptography/Aegis256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal override void CreateKey(
keyHandle = SecureMemoryHandle.CreateFrom(seed);
}

private protected unsafe override void EncryptCore(
private protected override void EncryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -75,33 +75,27 @@ private protected unsafe override void EncryptCore(
Debug.Assert(nonce.Length == crypto_aead_aegis256_NPUBBYTES);
Debug.Assert(ciphertext.Length == plaintext.Length + crypto_aead_aegis256_ABYTES);

fixed (byte* c = ciphertext)
fixed (byte* m = plaintext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aegis256_encrypt(
c,
out ulong clen_p,
m,
(ulong)plaintext.Length,
ad,
(ulong)associatedData.Length,
null,
n,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen_p);
}
int error = crypto_aead_aegis256_encrypt(
ciphertext,
out ulong clen,
plaintext,
(ulong)plaintext.Length,
associatedData,
(ulong)associatedData.Length,
IntPtr.Zero,
nonce,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen);
}

internal override int GetSeedSize()
{
return crypto_aead_aegis256_KEYBYTES;
}

private protected unsafe override bool DecryptCore(
private protected override bool DecryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -112,27 +106,21 @@ private protected unsafe override bool DecryptCore(
Debug.Assert(nonce.Length == crypto_aead_aegis256_NPUBBYTES);
Debug.Assert(plaintext.Length == ciphertext.Length - crypto_aead_aegis256_ABYTES);

fixed (byte* m = plaintext)
fixed (byte* c = ciphertext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aegis256_decrypt(
m,
out ulong mlen_p,
null,
c,
(ulong)ciphertext.Length,
ad,
(ulong)associatedData.Length,
n,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen_p);
return error == 0;
}
int error = crypto_aead_aegis256_decrypt(
plaintext,
out ulong mlen,
IntPtr.Zero,
ciphertext,
(ulong)ciphertext.Length,
associatedData,
(ulong)associatedData.Length,
nonce,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen);
return error == 0;
}

internal override bool TryExportKey(
Expand Down
72 changes: 30 additions & 42 deletions src/Cryptography/Aes256Gcm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal override void CreateKey(
keyHandle = SecureMemoryHandle.CreateFrom(seed);
}

private protected unsafe override void EncryptCore(
private protected override void EncryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -100,33 +100,27 @@ private protected unsafe override void EncryptCore(
Debug.Assert(nonce.Length == crypto_aead_aes256gcm_NPUBBYTES);
Debug.Assert(ciphertext.Length == plaintext.Length + crypto_aead_aes256gcm_ABYTES);

fixed (byte* c = ciphertext)
fixed (byte* m = plaintext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aes256gcm_encrypt(
c,
out ulong clen_p,
m,
(ulong)plaintext.Length,
ad,
(ulong)associatedData.Length,
null,
n,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen_p);
}
int error = crypto_aead_aes256gcm_encrypt(
ciphertext,
out ulong clen,
plaintext,
(ulong)plaintext.Length,
associatedData,
(ulong)associatedData.Length,
IntPtr.Zero,
nonce,
keyHandle);

Debug.Assert(error == 0);
Debug.Assert((ulong)ciphertext.Length == clen);
}

internal override int GetSeedSize()
{
return crypto_aead_aes256gcm_KEYBYTES;
}

private protected unsafe override bool DecryptCore(
private protected override bool DecryptCore(
SecureMemoryHandle keyHandle,
ReadOnlySpan<byte> nonce,
ReadOnlySpan<byte> associatedData,
Expand All @@ -137,27 +131,21 @@ private protected unsafe override bool DecryptCore(
Debug.Assert(nonce.Length == crypto_aead_aes256gcm_NPUBBYTES);
Debug.Assert(plaintext.Length == ciphertext.Length - crypto_aead_aes256gcm_ABYTES);

fixed (byte* m = plaintext)
fixed (byte* c = ciphertext)
fixed (byte* ad = associatedData)
fixed (byte* n = nonce)
{
int error = crypto_aead_aes256gcm_decrypt(
m,
out ulong mlen_p,
null,
c,
(ulong)ciphertext.Length,
ad,
(ulong)associatedData.Length,
n,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen_p);
return error == 0;
}
int error = crypto_aead_aes256gcm_decrypt(
plaintext,
out ulong mlen,
IntPtr.Zero,
ciphertext,
(ulong)ciphertext.Length,
associatedData,
(ulong)associatedData.Length,
nonce,
keyHandle);

// libsodium clears plaintext if decryption fails

Debug.Assert(error != 0 || (ulong)plaintext.Length == mlen);
return error == 0;
}

internal override bool TryExportKey(
Expand Down
41 changes: 18 additions & 23 deletions src/Cryptography/Argon2id.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using static Interop.Libsodium;

Expand Down Expand Up @@ -117,7 +117,7 @@ internal void GetParameters(
parameters.NumberOfPasses = (long)_opsLimit;
}

internal override unsafe bool TryDeriveBytesCore(
internal override bool TryDeriveBytesCore(
ReadOnlySpan<byte> password,
ReadOnlySpan<byte> salt,
Span<byte> bytes)
Expand All @@ -126,35 +126,30 @@ internal override unsafe bool TryDeriveBytesCore(

const int MinCount = crypto_pwhash_argon2id_BYTES_MIN;
bool min = bytes.Length < MinCount;
byte* temp = stackalloc byte[MinCount];
Span<byte> temp = stackalloc byte[MinCount];

try
{
fixed (byte* @in = password)
fixed (byte* salt_ = salt)
fixed (byte* @out = bytes)
{
int error = crypto_pwhash_argon2id(
min ? temp : @out,
(ulong)(min ? MinCount : bytes.Length),
(sbyte*)@in,
(ulong)password.Length,
salt_,
_opsLimit,
_memLimit,
crypto_pwhash_argon2id_ALG_ARGON2ID13);

if (min)
{
Unsafe.CopyBlockUnaligned(@out, temp, (uint)bytes.Length);
}
int error = crypto_pwhash_argon2id(
min ? temp : bytes,
(ulong)(min ? temp : bytes).Length,
MemoryMarshal.Cast<byte, sbyte>(password),
(ulong)password.Length,
salt,
_opsLimit,
_memLimit,
crypto_pwhash_argon2id_ALG_ARGON2ID13);

return error == 0;
if (min)
{
temp[..bytes.Length].CopyTo(bytes);
}

return error == 0;
}
finally
{
Unsafe.InitBlockUnaligned(temp, 0, MinCount);
System.Security.Cryptography.CryptographicOperations.ZeroMemory(temp);
}
}

Expand Down
Loading

0 comments on commit bb6cc6c

Please sign in to comment.