diff --git a/src/Cryptography/Aegis128L.cs b/src/Cryptography/Aegis128L.cs index fbeeed96..3e469958 100644 --- a/src/Cryptography/Aegis128L.cs +++ b/src/Cryptography/Aegis128L.cs @@ -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 nonce, ReadOnlySpan associatedData, @@ -75,25 +75,19 @@ 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() @@ -101,7 +95,7 @@ internal override int GetSeedSize() return crypto_aead_aegis128l_KEYBYTES; } - private protected unsafe override bool DecryptCore( + private protected override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan associatedData, @@ -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( diff --git a/src/Cryptography/Aegis256.cs b/src/Cryptography/Aegis256.cs index 1a8acfa8..9a1dbdc5 100644 --- a/src/Cryptography/Aegis256.cs +++ b/src/Cryptography/Aegis256.cs @@ -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 nonce, ReadOnlySpan associatedData, @@ -75,25 +75,19 @@ 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() @@ -101,7 +95,7 @@ internal override int GetSeedSize() return crypto_aead_aegis256_KEYBYTES; } - private protected unsafe override bool DecryptCore( + private protected override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan associatedData, @@ -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( diff --git a/src/Cryptography/Aes256Gcm.cs b/src/Cryptography/Aes256Gcm.cs index d91bb41e..a95a68cf 100644 --- a/src/Cryptography/Aes256Gcm.cs +++ b/src/Cryptography/Aes256Gcm.cs @@ -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 nonce, ReadOnlySpan associatedData, @@ -100,25 +100,19 @@ 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() @@ -126,7 +120,7 @@ internal override int GetSeedSize() return crypto_aead_aes256gcm_KEYBYTES; } - private protected unsafe override bool DecryptCore( + private protected override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan associatedData, @@ -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( diff --git a/src/Cryptography/Argon2id.cs b/src/Cryptography/Argon2id.cs index c919195a..f99b643a 100644 --- a/src/Cryptography/Argon2id.cs +++ b/src/Cryptography/Argon2id.cs @@ -1,6 +1,6 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; using static Interop.Libsodium; @@ -117,7 +117,7 @@ internal void GetParameters( parameters.NumberOfPasses = (long)_opsLimit; } - internal override unsafe bool TryDeriveBytesCore( + internal override bool TryDeriveBytesCore( ReadOnlySpan password, ReadOnlySpan salt, Span bytes) @@ -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 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(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); } } diff --git a/src/Cryptography/Blake2b.cs b/src/Cryptography/Blake2b.cs index 6efccd9f..580872d0 100644 --- a/src/Cryptography/Blake2b.cs +++ b/src/Cryptography/Blake2b.cs @@ -48,79 +48,64 @@ public Blake2b(int hashSize) : base( } } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalHashState state, Span hash) { Debug.Assert(hash.Length >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(hash.Length <= crypto_generichash_blake2b_BYTES_MAX); - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - fixed (byte* @out = hash) - { - int error = crypto_generichash_blake2b_final( - state_, - @out, - (nuint)hash.Length); + int error = crypto_generichash_blake2b_final( + ref state.blake2b, + hash, + (nuint)hash.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void InitializeCore( + internal override void InitializeCore( out IncrementalHashState state) { Debug.Assert(HashSize >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(HashSize <= crypto_generichash_blake2b_BYTES_MAX); - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - { - int error = crypto_generichash_blake2b_init( - state_, - IntPtr.Zero, - 0, - (nuint)HashSize); + int error = crypto_generichash_blake2b_init( + ref state.blake2b, + IntPtr.Zero, + 0, + (nuint)HashSize); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalHashState state, ReadOnlySpan data) { - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - fixed (byte* @in = data) - { - int error = crypto_generichash_blake2b_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_generichash_blake2b_update( + ref state.blake2b, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void HashCore( + private protected override void HashCore( ReadOnlySpan data, Span hash) { Debug.Assert(hash.Length >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(hash.Length <= crypto_generichash_blake2b_BYTES_MAX); - fixed (byte* @out = hash) - fixed (byte* @in = data) - { - int error = crypto_generichash_blake2b( - @out, - (nuint)hash.Length, - @in, - (ulong)data.Length, - IntPtr.Zero, - 0); - - Debug.Assert(error == 0); - } + int error = crypto_generichash_blake2b( + hash, + (nuint)hash.Length, + data, + (ulong)data.Length, + IntPtr.Zero, + 0); + + Debug.Assert(error == 0); } private static void SelfTest() diff --git a/src/Cryptography/Blake2bMac.cs b/src/Cryptography/Blake2bMac.cs index 9b362c7a..4c53313b 100644 --- a/src/Cryptography/Blake2bMac.cs +++ b/src/Cryptography/Blake2bMac.cs @@ -74,23 +74,19 @@ internal override void CreateKey( keyHandle = SecureMemoryHandle.CreateFrom(seed); } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalMacState state, Span mac) { Debug.Assert(mac.Length >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(mac.Length <= crypto_generichash_blake2b_BYTES_MAX); - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - fixed (byte* @out = mac) - { - int error = crypto_generichash_blake2b_final( - state_, - @out, - (nuint)mac.Length); + int error = crypto_generichash_blake2b_final( + ref state.blake2b, + mac, + (nuint)mac.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override int GetSeedSize() @@ -98,7 +94,7 @@ internal override int GetSeedSize() return KeySize; } - internal unsafe override void InitializeCore( + internal override void InitializeCore( SecureMemoryHandle keyHandle, out IncrementalMacState state) { @@ -107,16 +103,13 @@ internal unsafe override void InitializeCore( Debug.Assert(MacSize >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(MacSize <= crypto_generichash_blake2b_BYTES_MAX); - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - { - int error = crypto_generichash_blake2b_init( - state_, - keyHandle, - (nuint)keyHandle.Size, - (nuint)MacSize); + int error = crypto_generichash_blake2b_init( + ref state.blake2b, + keyHandle, + (nuint)keyHandle.Size, + (nuint)MacSize); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override bool TryExportKey( @@ -149,23 +142,19 @@ internal override bool TryImportKey( }; } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalMacState state, ReadOnlySpan data) { - fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b) - fixed (byte* @in = data) - { - int error = crypto_generichash_blake2b_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_generichash_blake2b_update( + ref state.blake2b, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void MacCore( + private protected override void MacCore( SecureMemoryHandle keyHandle, ReadOnlySpan data, Span mac) @@ -175,19 +164,15 @@ private protected unsafe override void MacCore( Debug.Assert(mac.Length >= crypto_generichash_blake2b_BYTES_MIN); Debug.Assert(mac.Length <= crypto_generichash_blake2b_BYTES_MAX); - fixed (byte* @out = mac) - fixed (byte* @in = data) - { - int error = crypto_generichash_blake2b( - @out, - (nuint)mac.Length, - @in, - (ulong)data.Length, - keyHandle, - (nuint)keyHandle.Size); - - Debug.Assert(error == 0); - } + int error = crypto_generichash_blake2b( + mac, + (nuint)mac.Length, + data, + (ulong)data.Length, + keyHandle, + (nuint)keyHandle.Size); + + Debug.Assert(error == 0); } private static void SelfTest() diff --git a/src/Cryptography/ChaCha20Poly1305.cs b/src/Cryptography/ChaCha20Poly1305.cs index b457d316..37097aeb 100644 --- a/src/Cryptography/ChaCha20Poly1305.cs +++ b/src/Cryptography/ChaCha20Poly1305.cs @@ -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 nonce, ReadOnlySpan associatedData, @@ -75,25 +75,19 @@ private protected unsafe override void EncryptCore( Debug.Assert(nonce.Length == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); Debug.Assert(ciphertext.Length == plaintext.Length + crypto_aead_chacha20poly1305_ietf_ABYTES); - fixed (byte* c = ciphertext) - fixed (byte* m = plaintext) - fixed (byte* ad = associatedData) - fixed (byte* n = nonce) - { - int error = crypto_aead_chacha20poly1305_ietf_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_chacha20poly1305_ietf_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() @@ -101,7 +95,7 @@ internal override int GetSeedSize() return crypto_aead_chacha20poly1305_ietf_KEYBYTES; } - private protected unsafe override bool DecryptCore( + private protected override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan associatedData, @@ -112,27 +106,21 @@ private protected unsafe override bool DecryptCore( Debug.Assert(nonce.Length == crypto_aead_chacha20poly1305_ietf_NPUBBYTES); Debug.Assert(plaintext.Length == ciphertext.Length - crypto_aead_chacha20poly1305_ietf_ABYTES); - fixed (byte* m = plaintext) - fixed (byte* c = ciphertext) - fixed (byte* ad = associatedData) - fixed (byte* n = nonce) - { - int error = crypto_aead_chacha20poly1305_ietf_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_chacha20poly1305_ietf_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( diff --git a/src/Cryptography/Ed25519.cs b/src/Cryptography/Ed25519.cs index 8aa0e2b8..0431b235 100644 --- a/src/Cryptography/Ed25519.cs +++ b/src/Cryptography/Ed25519.cs @@ -77,7 +77,7 @@ public Ed25519() : base( } } - internal override unsafe void CreateKey( + internal override void CreateKey( ReadOnlySpan seed, out SecureMemoryHandle keyHandle, out PublicKey? publicKey) @@ -92,13 +92,12 @@ internal override unsafe void CreateKey( publicKey = new PublicKey(this); keyHandle = SecureMemoryHandle.Create(crypto_sign_ed25519_SECRETKEYBYTES); - fixed (PublicKeyBytes* pk = publicKey) - fixed (byte* seed_ = seed) - { - int error = crypto_sign_ed25519_seed_keypair(pk, keyHandle, seed_); + int error = crypto_sign_ed25519_seed_keypair( + ref publicKey.GetPinnableReference(), + keyHandle, + seed); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override int GetSeedSize() @@ -106,7 +105,7 @@ internal override int GetSeedSize() return crypto_sign_ed25519_SEEDBYTES; } - private protected unsafe override void SignCore( + private protected override void SignCore( SecureMemoryHandle keyHandle, ReadOnlySpan data, Span signature) @@ -114,19 +113,15 @@ private protected unsafe override void SignCore( Debug.Assert(keyHandle.Size == crypto_sign_ed25519_SECRETKEYBYTES); Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (byte* sig = signature) - fixed (byte* m = data) - { - int error = crypto_sign_ed25519_detached( - sig, - out ulong signatureLength, - m, - (ulong)data.Length, - keyHandle); - - Debug.Assert(error == 0); - Debug.Assert((ulong)signature.Length == signatureLength); - } + int error = crypto_sign_ed25519_detached( + signature, + out ulong siglen, + data, + (ulong)data.Length, + keyHandle); + + Debug.Assert(error == 0); + Debug.Assert((ulong)signature.Length == siglen); } internal override bool TryExportKey( @@ -196,7 +191,7 @@ internal override bool TryImportPublicKey( }; } - private protected unsafe override bool VerifyCore( + private protected override bool VerifyCore( ref readonly PublicKeyBytes publicKeyBytes, ReadOnlySpan data, ReadOnlySpan signature) @@ -208,18 +203,13 @@ private protected unsafe override bool VerifyCore( Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (byte* sig = signature) - fixed (byte* m = data) - fixed (PublicKeyBytes* pk = &publicKeyBytes) - { - int error = crypto_sign_ed25519_verify_detached( - sig, - m, - (ulong)data.Length, - pk); + int error = crypto_sign_ed25519_verify_detached( + signature, + data, + (ulong)data.Length, + in publicKeyBytes); - return error == 0; - } + return error == 0; } private static void SelfTest() diff --git a/src/Cryptography/Ed25519ph.cs b/src/Cryptography/Ed25519ph.cs index b145d093..037e487b 100644 --- a/src/Cryptography/Ed25519ph.cs +++ b/src/Cryptography/Ed25519ph.cs @@ -52,7 +52,7 @@ public Ed25519ph() : base( } } - internal override unsafe void CreateKey( + internal override void CreateKey( ReadOnlySpan seed, out SecureMemoryHandle keyHandle, out PublicKey? publicKey) @@ -67,13 +67,12 @@ internal override unsafe void CreateKey( publicKey = new PublicKey(this); keyHandle = SecureMemoryHandle.Create(crypto_sign_ed25519_SECRETKEYBYTES); - fixed (PublicKeyBytes* pk = publicKey) - fixed (byte* seed_ = seed) - { - int error = crypto_sign_ed25519_seed_keypair(pk, keyHandle, seed_); + int error = crypto_sign_ed25519_seed_keypair( + ref publicKey.GetPinnableReference(), + keyHandle, + seed); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override int GetSeedSize() @@ -81,7 +80,7 @@ internal override int GetSeedSize() return crypto_sign_ed25519_SEEDBYTES; } - private protected unsafe override void SignCore( + private protected override void SignCore( SecureMemoryHandle keyHandle, ReadOnlySpan data, Span signature) @@ -89,31 +88,27 @@ private protected unsafe override void SignCore( Debug.Assert(keyHandle.Size == crypto_sign_ed25519_SECRETKEYBYTES); Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (byte* sig = signature) - fixed (byte* m = data) - { - crypto_sign_ed25519ph_state state; + crypto_sign_ed25519ph_state state; - crypto_sign_ed25519ph_init( - &state); + crypto_sign_ed25519ph_init( + ref state); - crypto_sign_ed25519ph_update( - &state, - m, - (ulong)data.Length); + crypto_sign_ed25519ph_update( + ref state, + data, + (ulong)data.Length); - int error = crypto_sign_ed25519ph_final_create( - &state, - sig, - out ulong signatureLength, - keyHandle); + int error = crypto_sign_ed25519ph_final_create( + ref state, + signature, + out ulong siglen, + keyHandle); - Debug.Assert(error == 0); - Debug.Assert((ulong)signature.Length == signatureLength); - } + Debug.Assert(error == 0); + Debug.Assert((ulong)signature.Length == siglen); } - private protected unsafe override bool VerifyCore( + private protected override bool VerifyCore( ref readonly PublicKeyBytes publicKeyBytes, ReadOnlySpan data, ReadOnlySpan signature) @@ -125,57 +120,46 @@ private protected unsafe override bool VerifyCore( Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (byte* sig = signature) - fixed (byte* m = data) - fixed (PublicKeyBytes* pk = &publicKeyBytes) - { - crypto_sign_ed25519ph_state state; + crypto_sign_ed25519ph_state state; - crypto_sign_ed25519ph_init( - &state); + crypto_sign_ed25519ph_init( + ref state); - crypto_sign_ed25519ph_update( - &state, - m, - (ulong)data.Length); + crypto_sign_ed25519ph_update( + ref state, + data, + (ulong)data.Length); - int error = crypto_sign_ed25519ph_final_verify( - &state, - sig, - pk); + int error = crypto_sign_ed25519ph_final_verify( + ref state, + signature, + in publicKeyBytes); - return error == 0; - } + return error == 0; } - internal unsafe override void InitializeCore( + internal override void InitializeCore( out IncrementalSignatureState state) { - fixed (crypto_sign_ed25519ph_state* state_ = &state.ed25519ph) - { - int error = crypto_sign_ed25519ph_init(state_); + int error = crypto_sign_ed25519ph_init( + ref state.ed25519ph); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalSignatureState state, ReadOnlySpan data) { - fixed (crypto_sign_ed25519ph_state* state_ = &state.ed25519ph) - fixed (byte* @in = data) - { - int error = crypto_sign_ed25519ph_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_sign_ed25519ph_update( + ref state.ed25519ph, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void FinalSignCore( + internal override void FinalSignCore( ref IncrementalSignatureState state, SecureMemoryHandle keyHandle, Span signature) @@ -183,21 +167,17 @@ internal unsafe override void FinalSignCore( Debug.Assert(keyHandle.Size == crypto_sign_ed25519_SECRETKEYBYTES); Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (crypto_sign_ed25519ph_state* state_ = &state.ed25519ph) - fixed (byte* sig = signature) - { - int error = crypto_sign_ed25519ph_final_create( - state_, - sig, - out ulong signatureLength, - keyHandle); - - Debug.Assert(error == 0); - Debug.Assert((ulong)signature.Length == signatureLength); - } + int error = crypto_sign_ed25519ph_final_create( + ref state.ed25519ph, + signature, + out ulong siglen, + keyHandle); + + Debug.Assert(error == 0); + Debug.Assert((ulong)signature.Length == siglen); } - internal unsafe override bool FinalVerifyCore( + internal override bool FinalVerifyCore( ref IncrementalSignatureState state, ref readonly PublicKeyBytes publicKeyBytes, ReadOnlySpan signature) @@ -209,17 +189,12 @@ internal unsafe override bool FinalVerifyCore( Debug.Assert(signature.Length == crypto_sign_ed25519_BYTES); - fixed (crypto_sign_ed25519ph_state* state_ = &state.ed25519ph) - fixed (byte* sig = signature) - fixed (PublicKeyBytes* pk = &publicKeyBytes) - { - int error = crypto_sign_ed25519ph_final_verify( - state_, - sig, - pk); + int error = crypto_sign_ed25519ph_final_verify( + ref state.ed25519ph, + signature, + in publicKeyBytes); - return error == 0; - } + return error == 0; } internal override bool TryExportKey( @@ -287,7 +262,7 @@ private static void SelfTest() (crypto_sign_ed25519_publickeybytes() != crypto_sign_ed25519_PUBLICKEYBYTES) || (crypto_sign_ed25519_secretkeybytes() != crypto_sign_ed25519_SECRETKEYBYTES) || (crypto_sign_ed25519_seedbytes() != crypto_sign_ed25519_SEEDBYTES) || - (crypto_sign_ed25519ph_statebytes() != (nuint)Unsafe.SizeOf())) + (crypto_sign_ed25519ph_statebytes() != (nuint)Unsafe.SizeOf())) { throw Error.InvalidOperation_InitializationFailed(); } diff --git a/src/Cryptography/Formatting/Ed25519PrivateKeyFormatter.cs b/src/Cryptography/Formatting/Ed25519PrivateKeyFormatter.cs index a5924e61..8b208e67 100644 --- a/src/Cryptography/Formatting/Ed25519PrivateKeyFormatter.cs +++ b/src/Cryptography/Formatting/Ed25519PrivateKeyFormatter.cs @@ -9,7 +9,7 @@ internal sealed class Ed25519PrivateKeyFormatter(byte[] blobHeader) : PrivateKey crypto_sign_ed25519_SEEDBYTES, blobHeader) { - protected unsafe override void Deserialize( + protected override void Deserialize( ReadOnlySpan span, out SecureMemoryHandle? keyHandle, out PublicKeyBytes publicKeyBytes) @@ -22,29 +22,28 @@ protected unsafe override void Deserialize( Debug.Assert(span.Length == crypto_sign_ed25519_SEEDBYTES); keyHandle = SecureMemoryHandle.Create(crypto_sign_ed25519_SECRETKEYBYTES); + publicKeyBytes = new PublicKeyBytes(); - fixed (PublicKeyBytes* pk = &publicKeyBytes) - fixed (byte* seed_ = span) - { - int error = crypto_sign_ed25519_seed_keypair(pk, keyHandle, seed_); + int error = crypto_sign_ed25519_seed_keypair( + ref publicKeyBytes, + keyHandle, + span); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - protected unsafe override void Serialize( + protected override void Serialize( SecureMemoryHandle keyHandle, Span span) { Debug.Assert(keyHandle.Size == crypto_sign_ed25519_SECRETKEYBYTES); Debug.Assert(span.Length == crypto_sign_ed25519_SEEDBYTES); - fixed (byte* seed_ = span) - { - int error = crypto_sign_ed25519_sk_to_seed(seed_, keyHandle); + int error = crypto_sign_ed25519_sk_to_seed( + span, + keyHandle); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } } } diff --git a/src/Cryptography/Formatting/Ed25519PublicKeyFormatter.cs b/src/Cryptography/Formatting/Ed25519PublicKeyFormatter.cs index 70c55bc3..65cd6425 100644 --- a/src/Cryptography/Formatting/Ed25519PublicKeyFormatter.cs +++ b/src/Cryptography/Formatting/Ed25519PublicKeyFormatter.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; using static Interop.Libsodium; namespace NSec.Cryptography.Formatting @@ -13,28 +12,19 @@ protected override void Deserialize( ReadOnlySpan span, out PublicKeyBytes publicKeyBytes) { - if (Unsafe.SizeOf() != crypto_sign_ed25519_PUBLICKEYBYTES) - { - throw Error.InvalidOperation_InternalError(); - } - Debug.Assert(span.Length == crypto_sign_ed25519_PUBLICKEYBYTES); - Unsafe.CopyBlockUnaligned(ref Unsafe.As(ref publicKeyBytes), ref Unsafe.AsRef(in span.GetPinnableReference()), crypto_sign_ed25519_PUBLICKEYBYTES); + publicKeyBytes = new PublicKeyBytes(); + span.CopyTo(publicKeyBytes); } protected override void Serialize( ref readonly PublicKeyBytes publicKeyBytes, Span span) { - if (Unsafe.SizeOf() != crypto_sign_ed25519_PUBLICKEYBYTES) - { - throw Error.InvalidOperation_InternalError(); - } - Debug.Assert(span.Length == crypto_sign_ed25519_PUBLICKEYBYTES); - Unsafe.CopyBlockUnaligned(ref span.GetPinnableReference(), ref Unsafe.As(ref Unsafe.AsRef(in publicKeyBytes)), crypto_sign_ed25519_PUBLICKEYBYTES); + publicKeyBytes[..crypto_sign_ed25519_PUBLICKEYBYTES].CopyTo(span); } } } diff --git a/src/Cryptography/Formatting/X25519PrivateKeyFormatter.cs b/src/Cryptography/Formatting/X25519PrivateKeyFormatter.cs index 741ace34..46663ffb 100644 --- a/src/Cryptography/Formatting/X25519PrivateKeyFormatter.cs +++ b/src/Cryptography/Formatting/X25519PrivateKeyFormatter.cs @@ -9,7 +9,7 @@ internal sealed class X25519PrivateKeyFormatter(byte[] blobHeader) : PrivateKeyF crypto_scalarmult_curve25519_SCALARBYTES, blobHeader) { - protected override unsafe void Deserialize( + protected override void Deserialize( ReadOnlySpan span, out SecureMemoryHandle? keyHandle, out PublicKeyBytes publicKeyBytes) @@ -22,14 +22,14 @@ protected override unsafe void Deserialize( Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); keyHandle = SecureMemoryHandle.CreateFrom(span); + publicKeyBytes = new PublicKeyBytes(); - fixed (PublicKeyBytes* q = &publicKeyBytes) - { - int error = crypto_scalarmult_curve25519_base(q, keyHandle); + int error = crypto_scalarmult_curve25519_base( + ref publicKeyBytes, + keyHandle); - Debug.Assert(error == 0); - Debug.Assert((((byte*)q)[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); - } + Debug.Assert(error == 0); + Debug.Assert((publicKeyBytes[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); } protected override void Serialize( diff --git a/src/Cryptography/Formatting/X25519PublicKeyFormatter.cs b/src/Cryptography/Formatting/X25519PublicKeyFormatter.cs index 214be90e..f38f36ef 100644 --- a/src/Cryptography/Formatting/X25519PublicKeyFormatter.cs +++ b/src/Cryptography/Formatting/X25519PublicKeyFormatter.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; using static Interop.Libsodium; namespace NSec.Cryptography.Formatting @@ -13,29 +12,22 @@ protected override void Deserialize( ReadOnlySpan span, out PublicKeyBytes publicKeyBytes) { - if (Unsafe.SizeOf() != crypto_scalarmult_curve25519_SCALARBYTES) - { - throw Error.InvalidOperation_InternalError(); - } - Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); - Unsafe.CopyBlockUnaligned(ref Unsafe.As(ref publicKeyBytes), ref Unsafe.AsRef(in span.GetPinnableReference()), crypto_scalarmult_curve25519_SCALARBYTES); - Unsafe.Add(ref Unsafe.As(ref publicKeyBytes), crypto_scalarmult_curve25519_SCALARBYTES - 1) &= 0x7F; + publicKeyBytes = new PublicKeyBytes(); + span.CopyTo(publicKeyBytes); + publicKeyBytes[crypto_scalarmult_curve25519_SCALARBYTES - 1] &= 0x7F; + + Debug.Assert((publicKeyBytes[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); } protected override void Serialize( ref readonly PublicKeyBytes publicKeyBytes, Span span) { - if (Unsafe.SizeOf() != crypto_scalarmult_curve25519_SCALARBYTES) - { - throw Error.InvalidOperation_InternalError(); - } - Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); - Unsafe.CopyBlockUnaligned(ref span.GetPinnableReference(), ref Unsafe.As(ref Unsafe.AsRef(in publicKeyBytes)), crypto_scalarmult_curve25519_SCALARBYTES); + publicKeyBytes[..crypto_scalarmult_curve25519_SCALARBYTES].CopyTo(span); } } } diff --git a/src/Cryptography/HmacSha256.cs b/src/Cryptography/HmacSha256.cs index 75c86e24..585234eb 100644 --- a/src/Cryptography/HmacSha256.cs +++ b/src/Cryptography/HmacSha256.cs @@ -74,27 +74,21 @@ internal override void CreateKey( keyHandle = SecureMemoryHandle.CreateFrom(seed); } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalMacState state, Span mac) { Debug.Assert(mac.Length <= crypto_auth_hmacsha256_BYTES); - byte* temp = stackalloc byte[crypto_auth_hmacsha256_BYTES]; + Span temp = stackalloc byte[crypto_auth_hmacsha256_BYTES]; - fixed (crypto_auth_hmacsha256_state* state_ = &state.hmacsha256) - { - int error = crypto_auth_hmacsha256_final( - state_, - temp); + int error = crypto_auth_hmacsha256_final( + ref state.hmacsha256, + temp); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); - fixed (byte* @out = mac) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)mac.Length); - } + temp[..mac.Length].CopyTo(mac); } internal override int GetSeedSize() @@ -102,21 +96,18 @@ internal override int GetSeedSize() return KeySize; } - internal unsafe override void InitializeCore( + internal override void InitializeCore( SecureMemoryHandle keyHandle, out IncrementalMacState state) { Debug.Assert(keyHandle.Size == crypto_hash_sha256_BYTES); - fixed (crypto_auth_hmacsha256_state* state_ = &state.hmacsha256) - { - int error = crypto_auth_hmacsha256_init( - state_, - keyHandle, - (nuint)keyHandle.Size); + int error = crypto_auth_hmacsha256_init( + ref state.hmacsha256, + keyHandle, + (nuint)keyHandle.Size); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override bool TryExportKey( @@ -149,23 +140,19 @@ internal override bool TryImportKey( }; } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalMacState state, ReadOnlySpan data) { - fixed (crypto_auth_hmacsha256_state* state_ = &state.hmacsha256) - fixed (byte* @in = data) - { - int error = crypto_auth_hmacsha256_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_auth_hmacsha256_update( + ref state.hmacsha256, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void MacCore( + private protected override void MacCore( SecureMemoryHandle keyHandle, ReadOnlySpan data, Span mac) @@ -173,31 +160,24 @@ private protected unsafe override void MacCore( Debug.Assert(keyHandle.Size == crypto_hash_sha256_BYTES); Debug.Assert(mac.Length <= crypto_auth_hmacsha256_BYTES); - byte* temp = stackalloc byte[crypto_auth_hmacsha512_BYTES]; - - fixed (byte* @in = data) - { - crypto_auth_hmacsha256_state state; + Span temp = stackalloc byte[crypto_auth_hmacsha256_BYTES]; + crypto_auth_hmacsha256_state state; - crypto_auth_hmacsha256_init( - &state, - keyHandle, - (nuint)keyHandle.Size); + crypto_auth_hmacsha256_init( + ref state, + keyHandle, + (nuint)keyHandle.Size); - crypto_auth_hmacsha256_update( - &state, - @in, - (ulong)data.Length); + crypto_auth_hmacsha256_update( + ref state, + data, + (ulong)data.Length); - crypto_auth_hmacsha256_final( - &state, - temp); - } + crypto_auth_hmacsha256_final( + ref state, + temp); - fixed (byte* @out = mac) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)mac.Length); - } + temp[..mac.Length].CopyTo(mac); } private static void SelfTest() diff --git a/src/Cryptography/HmacSha512.cs b/src/Cryptography/HmacSha512.cs index 2a230200..435a1264 100644 --- a/src/Cryptography/HmacSha512.cs +++ b/src/Cryptography/HmacSha512.cs @@ -77,27 +77,21 @@ internal override void CreateKey( keyHandle = SecureMemoryHandle.CreateFrom(seed); } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalMacState state, Span mac) { Debug.Assert(mac.Length <= crypto_auth_hmacsha512_BYTES); - byte* temp = stackalloc byte[crypto_auth_hmacsha512_BYTES]; + Span temp = stackalloc byte[crypto_auth_hmacsha512_BYTES]; - fixed (crypto_auth_hmacsha512_state* state_ = &state.hmacsha512) - { - int error = crypto_auth_hmacsha512_final( - state_, - temp); + int error = crypto_auth_hmacsha512_final( + ref state.hmacsha512, + temp); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); - fixed (byte* @out = mac) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)mac.Length); - } + temp[..mac.Length].CopyTo(mac); } internal override int GetSeedSize() @@ -105,21 +99,18 @@ internal override int GetSeedSize() return KeySize; } - internal unsafe override void InitializeCore( + internal override void InitializeCore( SecureMemoryHandle keyHandle, out IncrementalMacState state) { Debug.Assert(keyHandle.Size == crypto_hash_sha512_BYTES); - fixed (crypto_auth_hmacsha512_state* state_ = &state.hmacsha512) - { - int error = crypto_auth_hmacsha512_init( - state_, - keyHandle, - (nuint)keyHandle.Size); + int error = crypto_auth_hmacsha512_init( + ref state.hmacsha512, + keyHandle, + (nuint)keyHandle.Size); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } internal override bool TryExportKey( @@ -152,23 +143,19 @@ internal override bool TryImportKey( }; } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalMacState state, ReadOnlySpan data) { - fixed (crypto_auth_hmacsha512_state* state_ = &state.hmacsha512) - fixed (byte* @in = data) - { - int error = crypto_auth_hmacsha512_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_auth_hmacsha512_update( + ref state.hmacsha512, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void MacCore( + private protected override void MacCore( SecureMemoryHandle keyHandle, ReadOnlySpan data, Span mac) @@ -176,31 +163,24 @@ private protected unsafe override void MacCore( Debug.Assert(keyHandle.Size == crypto_hash_sha512_BYTES); Debug.Assert(mac.Length <= crypto_auth_hmacsha512_BYTES); - byte* temp = stackalloc byte[crypto_auth_hmacsha512_BYTES]; - - fixed (byte* @in = data) - { - crypto_auth_hmacsha512_state state; + Span temp = stackalloc byte[crypto_auth_hmacsha512_BYTES]; + crypto_auth_hmacsha512_state state; - crypto_auth_hmacsha512_init( - &state, - keyHandle, - (nuint)keyHandle.Size); + crypto_auth_hmacsha512_init( + ref state, + keyHandle, + (nuint)keyHandle.Size); - crypto_auth_hmacsha512_update( - &state, - @in, - (ulong)data.Length); + crypto_auth_hmacsha512_update( + ref state, + data, + (ulong)data.Length); - crypto_auth_hmacsha512_final( - &state, - temp); - } + crypto_auth_hmacsha512_final( + ref state, + temp); - fixed (byte* @out = mac) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)mac.Length); - } + temp[..mac.Length].CopyTo(mac); } private static void SelfTest() diff --git a/src/Cryptography/PublicKey.cs b/src/Cryptography/PublicKey.cs index 4b25d12d..1aea6249 100644 --- a/src/Cryptography/PublicKey.cs +++ b/src/Cryptography/PublicKey.cs @@ -1,7 +1,7 @@ using System; using System.ComponentModel; using System.Diagnostics; -using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using static Interop.Libsodium; namespace NSec.Cryptography @@ -65,10 +65,6 @@ public static bool TryImport( public bool Equals( PublicKey? other) { - if (Unsafe.SizeOf() != 8 * sizeof(uint)) - { - throw Error.InvalidOperation_InternalError(); - } if (other == this) { return true; @@ -78,17 +74,8 @@ public bool Equals( return false; } - ref byte x = ref Unsafe.As(ref _bytes); - ref byte y = ref Unsafe.As(ref other._bytes); - - return Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 0 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 0 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 1 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 1 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 2 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 2 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 3 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 3 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 4 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 4 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 5 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 5 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 6 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 6 * sizeof(uint))) - && Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 7 * sizeof(uint))) == Unsafe.ReadUnaligned(ref Unsafe.Add(ref y, 7 * sizeof(uint))); + ReadOnlySpan bytes = _bytes; + return bytes.SequenceEqual(other._bytes); } [EditorBrowsable(EditorBrowsableState.Never)] @@ -122,24 +109,8 @@ public int GetExportBlobSize( public override int GetHashCode() { - if (Unsafe.SizeOf() != 8 * sizeof(uint)) - { - throw Error.InvalidOperation_InternalError(); - } - - ref byte x = ref Unsafe.As(ref _bytes); - uint hashCode = unchecked((uint)_algorithm.GetHashCode()); - - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 0 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 1 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 2 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 3 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 4 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 5 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 6 * sizeof(uint)))); - hashCode = unchecked(hashCode * 0xA5555529 + Unsafe.ReadUnaligned(ref Unsafe.Add(ref x, 7 * sizeof(uint)))); - - return unchecked((int)hashCode); + ReadOnlySpan values = MemoryMarshal.Cast(_bytes); + return HashCode.Combine(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7]); } [EditorBrowsable(EditorBrowsableState.Never)] diff --git a/src/Cryptography/Scrypt.cs b/src/Cryptography/Scrypt.cs index 1323247e..1e730b45 100644 --- a/src/Cryptography/Scrypt.cs +++ b/src/Cryptography/Scrypt.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; using System.Threading; using static Interop.Libsodium; @@ -101,7 +100,7 @@ internal void GetParameters( parameters.Parallelization = (int)_p; } - internal override unsafe bool TryDeriveBytesCore( + internal override bool TryDeriveBytesCore( ReadOnlySpan password, ReadOnlySpan salt, Span bytes) @@ -110,36 +109,31 @@ internal override unsafe bool TryDeriveBytesCore( const int MinCount = crypto_pwhash_scryptsalsa208sha256_BYTES_MIN; bool min = bytes.Length < MinCount; - byte* temp = stackalloc byte[MinCount]; + Span temp = stackalloc byte[MinCount]; try { - fixed (byte* @in = password) - fixed (byte* salt_ = salt) - fixed (byte* @out = bytes) - { - int error = crypto_pwhash_scryptsalsa208sha256_ll( - @in, - (nuint)password.Length, - salt_, - (nuint)salt.Length, - _n, - _r, - _p, - min ? temp : @out, - (nuint)(min ? MinCount : bytes.Length)); - - if (min) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)bytes.Length); - } + int error = crypto_pwhash_scryptsalsa208sha256_ll( + password, + (nuint)password.Length, + salt, + (nuint)salt.Length, + _n, + _r, + _p, + min ? temp : bytes, + (nuint)(min ? temp : bytes).Length); - 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); } } diff --git a/src/Cryptography/Sha256.cs b/src/Cryptography/Sha256.cs index 6b51d8c9..56fa193d 100644 --- a/src/Cryptography/Sha256.cs +++ b/src/Cryptography/Sha256.cs @@ -49,67 +49,52 @@ public Sha256(int hashSize) : base( } } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalHashState state, Span hash) { Debug.Assert(hash.Length == crypto_hash_sha256_BYTES); - fixed (crypto_hash_sha256_state* state_ = &state.sha256) - fixed (byte* @out = hash) - { - int error = crypto_hash_sha256_final( - state_, - @out); + int error = crypto_hash_sha256_final( + ref state.sha256, + hash); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void InitializeCore( + internal override void InitializeCore( out IncrementalHashState state) { - fixed (crypto_hash_sha256_state* state_ = &state.sha256) - { - int error = crypto_hash_sha256_init( - state_); + int error = crypto_hash_sha256_init( + ref state.sha256); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalHashState state, ReadOnlySpan data) { - fixed (crypto_hash_sha256_state* state_ = &state.sha256) - fixed (byte* @in = data) - { - int error = crypto_hash_sha256_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_hash_sha256_update( + ref state.sha256, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void HashCore( + private protected override void HashCore( ReadOnlySpan data, Span hash) { Debug.Assert(hash.Length == crypto_hash_sha256_BYTES); - fixed (byte* @out = hash) - fixed (byte* @in = data) - { - int error = crypto_hash_sha256( - @out, - @in, - (ulong)data.Length); + int error = crypto_hash_sha256( + hash, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } private static void SelfTest() diff --git a/src/Cryptography/Sha512.cs b/src/Cryptography/Sha512.cs index 10665189..082ba90b 100644 --- a/src/Cryptography/Sha512.cs +++ b/src/Cryptography/Sha512.cs @@ -52,79 +52,60 @@ public Sha512(int hashSize) : base( } } - internal unsafe override void FinalizeCore( + internal override void FinalizeCore( ref IncrementalHashState state, Span hash) { Debug.Assert(hash.Length <= crypto_hash_sha512_BYTES); - byte* temp = stackalloc byte[crypto_hash_sha512_BYTES]; + Span temp = stackalloc byte[crypto_hash_sha512_BYTES]; - fixed (crypto_hash_sha512_state* state_ = &state.sha512) - { - int error = crypto_hash_sha512_final( - state_, - temp); + int error = crypto_hash_sha512_final( + ref state.sha512, + temp); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); - fixed (byte* @out = hash) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)hash.Length); - } + temp[..hash.Length].CopyTo(hash); } - internal unsafe override void InitializeCore( + internal override void InitializeCore( out IncrementalHashState state) { - fixed (crypto_hash_sha512_state* state_ = &state.sha512) - { - int error = crypto_hash_sha512_init( - state_); + int error = crypto_hash_sha512_init( + ref state.sha512); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - internal unsafe override void UpdateCore( + internal override void UpdateCore( ref IncrementalHashState state, ReadOnlySpan data) { - fixed (crypto_hash_sha512_state* state_ = &state.sha512) - fixed (byte* @in = data) - { - int error = crypto_hash_sha512_update( - state_, - @in, - (ulong)data.Length); + int error = crypto_hash_sha512_update( + ref state.sha512, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void HashCore( + private protected override void HashCore( ReadOnlySpan data, Span hash) { Debug.Assert(hash.Length <= crypto_hash_sha512_BYTES); - byte* temp = stackalloc byte[crypto_hash_sha512_BYTES]; + Span temp = stackalloc byte[crypto_hash_sha512_BYTES]; - fixed (byte* @in = data) - { - int error = crypto_hash_sha512( - temp, - @in, - (ulong)data.Length); + int error = crypto_hash_sha512( + temp, + data, + (ulong)data.Length); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); - fixed (byte* @out = hash) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)hash.Length); - } + temp[..hash.Length].CopyTo(hash); } private static void SelfTest() diff --git a/src/Cryptography/X25519.cs b/src/Cryptography/X25519.cs index 5d5ab262..cfd4b296 100644 --- a/src/Cryptography/X25519.cs +++ b/src/Cryptography/X25519.cs @@ -73,7 +73,7 @@ public X25519() : base( } } - internal override unsafe void CreateKey( + internal override void CreateKey( ReadOnlySpan seed, out SecureMemoryHandle keyHandle, out PublicKey? publicKey) @@ -88,13 +88,12 @@ internal override unsafe void CreateKey( publicKey = new PublicKey(this); keyHandle = SecureMemoryHandle.CreateFrom(seed); - fixed (PublicKeyBytes* q = publicKey) - { - int error = crypto_scalarmult_curve25519_base(q, keyHandle); + int error = crypto_scalarmult_curve25519_base( + ref publicKey.GetPinnableReference(), + keyHandle); - Debug.Assert(error == 0); - Debug.Assert((((byte*)q)[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); - } + Debug.Assert(error == 0); + Debug.Assert((publicKey.GetPinnableReference()[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); } internal override int GetSeedSize() @@ -102,7 +101,7 @@ internal override int GetSeedSize() return crypto_scalarmult_curve25519_SCALARBYTES; } - private protected unsafe override bool AgreeCore( + private protected override bool AgreeCore( SecureMemoryHandle keyHandle, ref readonly PublicKeyBytes otherPartyPublicKey, out SecureMemoryHandle? sharedSecretHandle) @@ -116,12 +115,12 @@ private protected unsafe override bool AgreeCore( sharedSecretHandle = SecureMemoryHandle.Create(crypto_scalarmult_curve25519_BYTES); - fixed (PublicKeyBytes* p = &otherPartyPublicKey) - { - int error = crypto_scalarmult_curve25519(sharedSecretHandle, keyHandle, p); + int error = crypto_scalarmult_curve25519( + sharedSecretHandle, + keyHandle, + in otherPartyPublicKey); - return error == 0; - } + return error == 0; } internal override bool TryExportKey( diff --git a/src/Cryptography/XChaCha20Poly1305.cs b/src/Cryptography/XChaCha20Poly1305.cs index 80c8528e..2a171b1f 100644 --- a/src/Cryptography/XChaCha20Poly1305.cs +++ b/src/Cryptography/XChaCha20Poly1305.cs @@ -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 nonce, ReadOnlySpan associatedData, @@ -75,25 +75,19 @@ private protected unsafe override void EncryptCore( Debug.Assert(nonce.Length == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); Debug.Assert(ciphertext.Length == plaintext.Length + crypto_aead_xchacha20poly1305_ietf_ABYTES); - fixed (byte* c = ciphertext) - fixed (byte* m = plaintext) - fixed (byte* ad = associatedData) - fixed (byte* n = nonce) - { - int error = crypto_aead_xchacha20poly1305_ietf_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_xchacha20poly1305_ietf_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() @@ -101,7 +95,7 @@ internal override int GetSeedSize() return crypto_aead_xchacha20poly1305_ietf_KEYBYTES; } - private protected unsafe override bool DecryptCore( + private protected override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan associatedData, @@ -112,27 +106,21 @@ private protected unsafe override bool DecryptCore( Debug.Assert(nonce.Length == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES); Debug.Assert(plaintext.Length == ciphertext.Length - crypto_aead_xchacha20poly1305_ietf_ABYTES); - fixed (byte* m = plaintext) - fixed (byte* c = ciphertext) - fixed (byte* ad = associatedData) - fixed (byte* n = nonce) - { - int error = crypto_aead_xchacha20poly1305_ietf_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_xchacha20poly1305_ietf_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( diff --git a/src/Experimental/AnsiX963KdfSha256.cs b/src/Experimental/AnsiX963KdfSha256.cs index 5636dbc2..78a160ba 100644 --- a/src/Experimental/AnsiX963KdfSha256.cs +++ b/src/Experimental/AnsiX963KdfSha256.cs @@ -1,7 +1,6 @@ using System; using System.Buffers.Binary; using System.Diagnostics; -using System.Runtime.CompilerServices; using NSec.Cryptography; using static Interop.Libsodium; @@ -31,7 +30,7 @@ public AnsiX963KdfSha256() : base( { } - private protected unsafe override void DeriveBytesCore( + private protected override void DeriveBytesCore( ReadOnlySpan inputKeyingMaterial, ReadOnlySpan salt, ReadOnlySpan info, @@ -39,46 +38,52 @@ private protected unsafe override void DeriveBytesCore( { Debug.Assert(salt.IsEmpty); - byte* temp = stackalloc byte[crypto_hash_sha256_BYTES]; + Span temp = stackalloc byte[crypto_hash_sha256_BYTES]; + int offset = 0; + uint counter = 0; + int chunkSize; try { - fixed (byte* ikm = inputKeyingMaterial) - fixed (byte* @in = info) - fixed (byte* @out = bytes) + crypto_hash_sha256_state initialState; + + crypto_hash_sha256_init( + ref initialState); + + crypto_hash_sha256_update( + ref initialState, + inputKeyingMaterial, + (ulong)inputKeyingMaterial.Length); + + while ((chunkSize = Math.Min(bytes.Length - offset, crypto_hash_sha256_BYTES)) > 0) { - int offset = 0; - uint counter = 0; - int chunkSize; + counter++; - crypto_hash_sha256_state initialState; - crypto_hash_sha256_init(&initialState); - crypto_hash_sha256_update(&initialState, ikm, (ulong)inputKeyingMaterial.Length); + uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; - while ((chunkSize = bytes.Length - offset) > 0) - { - counter++; + crypto_hash_sha256_state state = initialState; - uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; + crypto_hash_sha256_update( + ref state, + in counterBigEndian, + sizeof(uint)); - crypto_hash_sha256_state state = initialState; - crypto_hash_sha256_update(&state, (byte*)&counterBigEndian, sizeof(uint)); - crypto_hash_sha256_update(&state, @in, (ulong)info.Length); - crypto_hash_sha256_final(&state, temp); + crypto_hash_sha256_update( + ref state, + info, + (ulong)info.Length); - if (chunkSize > crypto_hash_sha256_BYTES) - { - chunkSize = crypto_hash_sha256_BYTES; - } + crypto_hash_sha256_final( + ref state, + temp); - Unsafe.CopyBlockUnaligned(@out + offset, temp, (uint)chunkSize); - offset += chunkSize; - } + temp[..chunkSize].CopyTo(bytes[offset..]); + offset += chunkSize; } } finally { - Unsafe.InitBlockUnaligned(temp, 0, crypto_hash_sha256_BYTES); + System.Security.Cryptography.CryptographicOperations.ZeroMemory(temp); } } } diff --git a/src/Experimental/ChaCha20.cs b/src/Experimental/ChaCha20.cs index 44d798a3..61ab7a20 100644 --- a/src/Experimental/ChaCha20.cs +++ b/src/Experimental/ChaCha20.cs @@ -38,7 +38,7 @@ internal override int GetSeedSize() return crypto_stream_chacha20_ietf_KEYBYTES; } - private protected unsafe override void GeneratePseudoRandomStreamCore( + private protected override void GeneratePseudoRandomStreamCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, Span bytes) @@ -46,20 +46,16 @@ private protected unsafe override void GeneratePseudoRandomStreamCore( Debug.Assert(keyHandle.Size == crypto_stream_chacha20_ietf_KEYBYTES); Debug.Assert(nonce.Length == crypto_stream_chacha20_ietf_NONCEBYTES); - fixed (byte* c = bytes) - fixed (byte* n = nonce) - { - int error = crypto_stream_chacha20_ietf( - c, - (ulong)bytes.Length, - n, - keyHandle); + int error = crypto_stream_chacha20_ietf( + bytes, + (ulong)bytes.Length, + nonce, + keyHandle); - Debug.Assert(error == 0); - } + Debug.Assert(error == 0); } - private protected unsafe override void XOrCore( + private protected override void XOrCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan input, @@ -69,22 +65,17 @@ private protected unsafe override void XOrCore( Debug.Assert(nonce.Length == crypto_stream_chacha20_ietf_NONCEBYTES); Debug.Assert(output.Length == input.Length); - fixed (byte* c = output) - fixed (byte* m = input) - fixed (byte* n = nonce) - { - int error = crypto_stream_chacha20_ietf_xor( - c, - m, - (ulong)input.Length, - n, - keyHandle); - - Debug.Assert(error == 0); - } + int error = crypto_stream_chacha20_ietf_xor( + output, + input, + (ulong)input.Length, + nonce, + keyHandle); + + Debug.Assert(error == 0); } - private protected unsafe override void XOrICCore( + private protected override void XOrICCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan input, @@ -95,20 +86,15 @@ private protected unsafe override void XOrICCore( Debug.Assert(nonce.Length == crypto_stream_chacha20_ietf_NONCEBYTES); Debug.Assert(output.Length == input.Length); - fixed (byte* c = output) - fixed (byte* m = input) - fixed (byte* n = nonce) - { - int error = crypto_stream_chacha20_ietf_xor_ic( - c, - m, - (ulong)input.Length, - n, - ic, - keyHandle); - - Debug.Assert(error == 0); - } + int error = crypto_stream_chacha20_ietf_xor_ic( + output, + input, + (ulong)input.Length, + nonce, + ic, + keyHandle); + + Debug.Assert(error == 0); } internal override bool TryExportKey( diff --git a/src/Experimental/ConcatKdfHmacSha256.cs b/src/Experimental/ConcatKdfHmacSha256.cs index dc7dea0c..7b287172 100644 --- a/src/Experimental/ConcatKdfHmacSha256.cs +++ b/src/Experimental/ConcatKdfHmacSha256.cs @@ -1,6 +1,5 @@ using System; using System.Buffers.Binary; -using System.Runtime.CompilerServices; using NSec.Cryptography; using static Interop.Libsodium; @@ -32,53 +31,60 @@ public ConcatKdfHmacSha256() : base( { } - private protected unsafe override void DeriveBytesCore( + private protected override void DeriveBytesCore( ReadOnlySpan inputKeyingMaterial, ReadOnlySpan salt, ReadOnlySpan info, Span bytes) { - byte* temp = stackalloc byte[crypto_auth_hmacsha256_BYTES]; + Span temp = stackalloc byte[crypto_auth_hmacsha256_BYTES]; + int offset = 0; + uint counter = 0; + int chunkSize; try { - fixed (byte* ikm = inputKeyingMaterial) - fixed (byte* key = salt) - fixed (byte* @in = info) - fixed (byte* @out = bytes) + crypto_auth_hmacsha256_state initialState; + + crypto_auth_hmacsha256_init( + ref initialState, + salt, + (nuint)salt.Length); + + while ((chunkSize = Math.Min(bytes.Length - offset, crypto_auth_hmacsha256_BYTES)) > 0) { - int offset = 0; - uint counter = 0; - int chunkSize; + counter++; + + uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; - crypto_auth_hmacsha256_state initialState; - crypto_auth_hmacsha256_init(&initialState, key, (nuint)salt.Length); + crypto_auth_hmacsha256_state state = initialState; - while ((chunkSize = bytes.Length - offset) > 0) - { - counter++; + crypto_auth_hmacsha256_update( + ref state, + in counterBigEndian, + sizeof(uint)); - uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; + crypto_auth_hmacsha256_update( + ref state, + inputKeyingMaterial, + (ulong)inputKeyingMaterial.Length); - crypto_auth_hmacsha256_state state = initialState; - crypto_auth_hmacsha256_update(&state, (byte*)&counterBigEndian, sizeof(uint)); - crypto_auth_hmacsha256_update(&state, ikm, (ulong)inputKeyingMaterial.Length); - crypto_auth_hmacsha256_update(&state, @in, (ulong)info.Length); - crypto_auth_hmacsha256_final(&state, temp); + crypto_auth_hmacsha256_update( + ref state, + info, + (ulong)info.Length); - if (chunkSize > crypto_auth_hmacsha256_BYTES) - { - chunkSize = crypto_auth_hmacsha256_BYTES; - } + crypto_auth_hmacsha256_final( + ref state, + temp); - Unsafe.CopyBlockUnaligned(@out + offset, temp, (uint)chunkSize); - offset += chunkSize; - } + temp[..chunkSize].CopyTo(bytes[offset..]); + offset += chunkSize; } } finally { - Unsafe.InitBlockUnaligned(temp, 0, crypto_auth_hmacsha256_BYTES); + System.Security.Cryptography.CryptographicOperations.ZeroMemory(temp); } } } diff --git a/src/Experimental/ConcatKdfSha256.cs b/src/Experimental/ConcatKdfSha256.cs index 5de8e082..c8d172ff 100644 --- a/src/Experimental/ConcatKdfSha256.cs +++ b/src/Experimental/ConcatKdfSha256.cs @@ -1,7 +1,6 @@ using System; using System.Buffers.Binary; using System.Diagnostics; -using System.Runtime.CompilerServices; using NSec.Cryptography; using static Interop.Libsodium; @@ -33,7 +32,7 @@ public ConcatKdfSha256() : base( { } - private protected unsafe override void DeriveBytesCore( + private protected override void DeriveBytesCore( ReadOnlySpan inputKeyingMaterial, ReadOnlySpan salt, ReadOnlySpan info, @@ -41,46 +40,52 @@ private protected unsafe override void DeriveBytesCore( { Debug.Assert(salt.IsEmpty); - byte* temp = stackalloc byte[crypto_hash_sha256_BYTES]; + Span temp = stackalloc byte[crypto_hash_sha256_BYTES]; + int offset = 0; + uint counter = 0; + int chunkSize; try { - fixed (byte* ikm = inputKeyingMaterial) - fixed (byte* @in = info) - fixed (byte* @out = bytes) + crypto_hash_sha256_state initialState; + + crypto_hash_sha256_init( + ref initialState); + + while ((chunkSize = Math.Min(bytes.Length - offset, crypto_hash_sha256_BYTES)) > 0) { - int offset = 0; - uint counter = 0; - int chunkSize; + counter++; + + uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; - crypto_hash_sha256_state initialState; - crypto_hash_sha256_init(&initialState); + crypto_hash_sha256_state state = initialState; - while ((chunkSize = bytes.Length - offset) > 0) - { - counter++; + crypto_hash_sha256_update( + ref state, + in counterBigEndian, + sizeof(uint)); - uint counterBigEndian = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(counter) : counter; + crypto_hash_sha256_update( + ref state, + inputKeyingMaterial, + (ulong)inputKeyingMaterial.Length); - crypto_hash_sha256_state state = initialState; - crypto_hash_sha256_update(&state, (byte*)&counterBigEndian, sizeof(uint)); - crypto_hash_sha256_update(&state, ikm, (ulong)inputKeyingMaterial.Length); - crypto_hash_sha256_update(&state, @in, (ulong)info.Length); - crypto_hash_sha256_final(&state, temp); + crypto_hash_sha256_update( + ref state, + info, + (ulong)info.Length); - if (chunkSize > crypto_hash_sha256_BYTES) - { - chunkSize = crypto_hash_sha256_BYTES; - } + crypto_hash_sha256_final( + ref state, + temp); - Unsafe.CopyBlockUnaligned(@out + offset, temp, (uint)chunkSize); - offset += chunkSize; - } + temp[..chunkSize].CopyTo(bytes[offset..]); + offset += chunkSize; } } finally { - Unsafe.InitBlockUnaligned(temp, 0, crypto_auth_hmacsha256_BYTES); + System.Security.Cryptography.CryptographicOperations.ZeroMemory(temp); } } } diff --git a/src/Experimental/KeyConverter.cs b/src/Experimental/KeyConverter.cs index 487e6a22..6c321b41 100644 --- a/src/Experimental/KeyConverter.cs +++ b/src/Experimental/KeyConverter.cs @@ -37,17 +37,11 @@ public static Key ConvertPrivateKey( Span seed = stackalloc byte[crypto_scalarmult_curve25519_BYTES]; try { - unsafe - { - fixed (byte* buf = seed) - { - int error = crypto_sign_ed25519_sk_to_curve25519(buf, key.Handle); + int error = crypto_sign_ed25519_sk_to_curve25519(seed, key.Handle); - if (error != 0) - { - throw Error.InvalidOperation_InternalError(); - } - } + if (error != 0) + { + throw Error.InvalidOperation_InternalError(); } algorithm.CreateKey(seed, out keyHandle, out publicKey); @@ -92,18 +86,13 @@ public static PublicKey ConvertPublicKey( PublicKey newPublicKey = new(algorithm); - unsafe - { - fixed (PublicKeyBytes* curve25519_pk = newPublicKey) - fixed (PublicKeyBytes* ed25519_pk = publicKey) - { - int error = crypto_sign_ed25519_pk_to_curve25519(curve25519_pk, ed25519_pk); + int error = crypto_sign_ed25519_pk_to_curve25519( + ref newPublicKey.GetPinnableReference(), + in publicKey.GetPinnableReference()); - if (error != 0) - { - throw Error.InvalidOperation_InternalError(); - } - } + if (error != 0) + { + throw Error.InvalidOperation_InternalError(); } return newPublicKey; diff --git a/src/Experimental/PasswordBased/Argon2i.cs b/src/Experimental/PasswordBased/Argon2i.cs index b0e44f3a..92cd84b1 100644 --- a/src/Experimental/PasswordBased/Argon2i.cs +++ b/src/Experimental/PasswordBased/Argon2i.cs @@ -1,6 +1,6 @@ using System; using System.Diagnostics; -using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; using NSec.Cryptography; using static Interop.Libsodium; @@ -104,7 +104,7 @@ internal void GetParameters( parameters.NumberOfPasses = (long)_opsLimit; } - internal override unsafe bool TryDeriveBytesCore( + internal override bool TryDeriveBytesCore( ReadOnlySpan password, ReadOnlySpan salt, Span bytes) @@ -113,35 +113,30 @@ internal override unsafe bool TryDeriveBytesCore( const int MinCount = crypto_pwhash_argon2i_BYTES_MIN; bool min = bytes.Length < MinCount; - byte* temp = stackalloc byte[MinCount]; + Span temp = stackalloc byte[MinCount]; try { - fixed (byte* @in = password) - fixed (byte* salt_ = salt) - fixed (byte* @out = bytes) - { - int error = crypto_pwhash_argon2i( - min ? temp : @out, - (ulong)(min ? MinCount : bytes.Length), - (sbyte*)@in, - (ulong)password.Length, - salt_, - _opsLimit, - _memLimit, - crypto_pwhash_argon2i_ALG_ARGON2I13); - - if (min) - { - Unsafe.CopyBlockUnaligned(@out, temp, (uint)bytes.Length); - } + int error = crypto_pwhash_argon2i( + min ? temp : bytes, + (ulong)(min ? temp : bytes).Length, + MemoryMarshal.Cast(password), + (ulong)password.Length, + salt, + _opsLimit, + _memLimit, + crypto_pwhash_argon2i_ALG_ARGON2I13); - 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); } } diff --git a/src/Experimental/PasswordBased/Pbkdf2HmacSha256.cs b/src/Experimental/PasswordBased/Pbkdf2HmacSha256.cs index c2a43d02..ae2ea987 100644 --- a/src/Experimental/PasswordBased/Pbkdf2HmacSha256.cs +++ b/src/Experimental/PasswordBased/Pbkdf2HmacSha256.cs @@ -46,7 +46,7 @@ internal void GetParameters( parameters.IterationCount = _c; } - internal override unsafe bool TryDeriveBytesCore( + internal override bool TryDeriveBytesCore( ReadOnlySpan password, ReadOnlySpan salt, Span bytes) diff --git a/src/Experimental/RandomGenerator.cs b/src/Experimental/RandomGenerator.cs index 8b0ccaba..6ba1ed88 100644 --- a/src/Experimental/RandomGenerator.cs +++ b/src/Experimental/RandomGenerator.cs @@ -198,7 +198,7 @@ private protected abstract void GenerateBytesCore( internal sealed class System : RandomGenerator { - private protected unsafe override void GenerateBytesCore( + private protected override void GenerateBytesCore( Span bytes) { global::System.Security.Cryptography.RandomNumberGenerator.Fill(bytes); diff --git a/src/Experimental/Sodium/NaclXSalsa20Poly1305.cs b/src/Experimental/Sodium/NaclXSalsa20Poly1305.cs index 6615dfb7..0707d82c 100644 --- a/src/Experimental/Sodium/NaclXSalsa20Poly1305.cs +++ b/src/Experimental/Sodium/NaclXSalsa20Poly1305.cs @@ -37,7 +37,7 @@ internal override void CreateKey( keyHandle = SecureMemoryHandle.CreateFrom(seed); } - internal unsafe override void EncryptCore( + internal override void EncryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan plaintext, @@ -47,19 +47,14 @@ internal unsafe override void EncryptCore( Debug.Assert(nonce.Length == crypto_secretbox_xsalsa20poly1305_NONCEBYTES); Debug.Assert(ciphertext.Length == crypto_secretbox_xsalsa20poly1305_MACBYTES + plaintext.Length); - fixed (byte* c = ciphertext) - fixed (byte* m = plaintext) - fixed (byte* n = nonce) - { - int error = crypto_secretbox_easy( - c, - m, - (ulong)plaintext.Length, - n, - keyHandle); - - Debug.Assert(error == 0); - } + int error = crypto_secretbox_easy( + ciphertext, + plaintext, + (ulong)plaintext.Length, + nonce, + keyHandle); + + Debug.Assert(error == 0); } internal override int GetSeedSize() @@ -67,7 +62,7 @@ internal override int GetSeedSize() return crypto_secretbox_xsalsa20poly1305_KEYBYTES; } - internal unsafe override bool DecryptCore( + internal override bool DecryptCore( SecureMemoryHandle keyHandle, ReadOnlySpan nonce, ReadOnlySpan ciphertext, @@ -77,21 +72,16 @@ internal unsafe override bool DecryptCore( Debug.Assert(nonce.Length == crypto_secretbox_xsalsa20poly1305_NONCEBYTES); Debug.Assert(plaintext.Length == ciphertext.Length - crypto_secretbox_xsalsa20poly1305_MACBYTES); - fixed (byte* m = plaintext) - fixed (byte* c = ciphertext) - fixed (byte* n = nonce) - { - int error = crypto_secretbox_open_easy( - m, - c, - (ulong)ciphertext.Length, - n, - keyHandle); + int error = crypto_secretbox_open_easy( + plaintext, + ciphertext, + (ulong)ciphertext.Length, + nonce, + keyHandle); - // TODO: clear plaintext if decryption fails + // TODO: clear plaintext if decryption fails - return error == 0; - } + return error == 0; } internal override bool TryExportKey( diff --git a/src/Interop/Interop.Aead.Aegis128L.cs b/src/Interop/Interop.Aead.Aegis128L.cs index f19e9b33..74fadc93 100644 --- a/src/Interop/Interop.Aead.Aegis128L.cs +++ b/src/Interop/Interop.Aead.Aegis128L.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,40 +11,46 @@ internal static partial class Libsodium internal const int crypto_aead_aegis128l_NPUBBYTES = 16; internal const int crypto_aead_aegis128l_NSECBYTES = 0; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis128l_abytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis128l_abytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aegis128l_decrypt( - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aegis128l_decrypt( + Span m, out ulong mlen_p, - byte* nsec, - byte* c, + IntPtr nsec, + ReadOnlySpan c, ulong clen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* npub, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aegis128l_encrypt( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aegis128l_encrypt( + Span c, out ulong clen_p, - byte* m, + ReadOnlySpan m, ulong mlen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* nsec, - byte* npub, + IntPtr nsec, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis128l_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis128l_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis128l_npubbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis128l_npubbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis128l_nsecbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis128l_nsecbytes(); } } diff --git a/src/Interop/Interop.Aead.Aegis256.cs b/src/Interop/Interop.Aead.Aegis256.cs index 4b22d0bb..4f3756d7 100644 --- a/src/Interop/Interop.Aead.Aegis256.cs +++ b/src/Interop/Interop.Aead.Aegis256.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,40 +11,46 @@ internal static partial class Libsodium internal const int crypto_aead_aegis256_NPUBBYTES = 32; internal const int crypto_aead_aegis256_NSECBYTES = 0; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis256_abytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis256_abytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aegis256_decrypt( - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aegis256_decrypt( + Span m, out ulong mlen_p, - byte* nsec, - byte* c, + IntPtr nsec, + ReadOnlySpan c, ulong clen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* npub, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aegis256_encrypt( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aegis256_encrypt( + Span c, out ulong clen_p, - byte* m, + ReadOnlySpan m, ulong mlen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* nsec, - byte* npub, + IntPtr nsec, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis256_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis256_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis256_npubbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis256_npubbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aegis256_nsecbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aegis256_nsecbytes(); } } diff --git a/src/Interop/Interop.Aead.Aes256Gcm.cs b/src/Interop/Interop.Aead.Aes256Gcm.cs index 4f9f3c13..c0ccb382 100644 --- a/src/Interop/Interop.Aead.Aes256Gcm.cs +++ b/src/Interop/Interop.Aead.Aes256Gcm.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,43 +11,50 @@ internal static partial class Libsodium internal const int crypto_aead_aes256gcm_NPUBBYTES = 12; internal const int crypto_aead_aes256gcm_NSECBYTES = 0; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aes256gcm_abytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aes256gcm_abytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aes256gcm_decrypt( - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aes256gcm_decrypt( + Span m, out ulong mlen_p, - byte* nsec, - byte* c, + IntPtr nsec, + ReadOnlySpan c, ulong clen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* npub, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_aes256gcm_encrypt( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aes256gcm_encrypt( + Span c, out ulong clen_p, - byte* m, + ReadOnlySpan m, ulong mlen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* nsec, - byte* npub, + IntPtr nsec, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int crypto_aead_aes256gcm_is_available(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_aes256gcm_is_available(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aes256gcm_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aes256gcm_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aes256gcm_npubbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aes256gcm_npubbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_aes256gcm_nsecbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_aes256gcm_nsecbytes(); } } diff --git a/src/Interop/Interop.Aead.ChaCha20Poly1305.cs b/src/Interop/Interop.Aead.ChaCha20Poly1305.cs index ca777e85..807128f8 100644 --- a/src/Interop/Interop.Aead.ChaCha20Poly1305.cs +++ b/src/Interop/Interop.Aead.ChaCha20Poly1305.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,40 +11,46 @@ internal static partial class Libsodium internal const int crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12; internal const int crypto_aead_chacha20poly1305_ietf_NSECBYTES = 0; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_chacha20poly1305_ietf_abytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_chacha20poly1305_ietf_abytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_chacha20poly1305_ietf_decrypt( - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_chacha20poly1305_ietf_decrypt( + Span m, out ulong mlen_p, - byte* nsec, - byte* c, + IntPtr nsec, + ReadOnlySpan c, ulong clen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* npub, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_chacha20poly1305_ietf_encrypt( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_chacha20poly1305_ietf_encrypt( + Span c, out ulong clen_p, - byte* m, + ReadOnlySpan m, ulong mlen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* nsec, - byte* npub, + IntPtr nsec, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_chacha20poly1305_ietf_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_chacha20poly1305_ietf_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_chacha20poly1305_ietf_npubbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_chacha20poly1305_ietf_npubbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_chacha20poly1305_ietf_nsecbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_chacha20poly1305_ietf_nsecbytes(); } } diff --git a/src/Interop/Interop.Aead.XChaCha20Poly1305.cs b/src/Interop/Interop.Aead.XChaCha20Poly1305.cs index 67f084d6..2425e4ae 100644 --- a/src/Interop/Interop.Aead.XChaCha20Poly1305.cs +++ b/src/Interop/Interop.Aead.XChaCha20Poly1305.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,40 +11,46 @@ internal static partial class Libsodium internal const int crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24; internal const int crypto_aead_xchacha20poly1305_ietf_NSECBYTES = 0; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_xchacha20poly1305_ietf_abytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_xchacha20poly1305_ietf_abytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_xchacha20poly1305_ietf_decrypt( - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_xchacha20poly1305_ietf_decrypt( + Span m, out ulong mlen_p, - byte* nsec, - byte* c, + IntPtr nsec, + ReadOnlySpan c, ulong clen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* npub, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_aead_xchacha20poly1305_ietf_encrypt( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_aead_xchacha20poly1305_ietf_encrypt( + Span c, out ulong clen_p, - byte* m, + ReadOnlySpan m, ulong mlen, - byte* ad, + ReadOnlySpan ad, ulong adlen, - byte* nsec, - byte* npub, + IntPtr nsec, + ReadOnlySpan npub, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_xchacha20poly1305_ietf_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_xchacha20poly1305_ietf_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_xchacha20poly1305_ietf_npubbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_xchacha20poly1305_ietf_npubbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_aead_xchacha20poly1305_ietf_nsecbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_aead_xchacha20poly1305_ietf_nsecbytes(); } } diff --git a/src/Interop/Interop.Auth.HmacSha256.cs b/src/Interop/Interop.Auth.HmacSha256.cs index 5843d02b..bc521776 100644 --- a/src/Interop/Interop.Auth.HmacSha256.cs +++ b/src/Interop/Interop.Auth.HmacSha256.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,36 +9,50 @@ internal static partial class Libsodium internal const int crypto_auth_hmacsha256_BYTES = 32; internal const int crypto_auth_hmacsha256_KEYBYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha256_bytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha256_final( - crypto_auth_hmacsha256_state* state, - byte* @out); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha256_init( - crypto_auth_hmacsha256_state* state, - byte* key, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha256_bytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha256_final( + ref crypto_auth_hmacsha256_state state, + Span @out); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha256_init( + ref crypto_auth_hmacsha256_state state, + ReadOnlySpan key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha256_init( - crypto_auth_hmacsha256_state* state, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha256_init( + ref crypto_auth_hmacsha256_state state, SecureMemoryHandle key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha256_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha256_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha256_statebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha256_statebytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha256_update( + ref crypto_auth_hmacsha256_state state, + ReadOnlySpan @in, + ulong inlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha256_update( - crypto_auth_hmacsha256_state* state, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha256_update( + ref crypto_auth_hmacsha256_state state, + in uint @in, ulong inlen); [StructLayout(LayoutKind.Explicit, Size = 208)] diff --git a/src/Interop/Interop.Auth.HmacSha512.cs b/src/Interop/Interop.Auth.HmacSha512.cs index fa42e81a..d9469d81 100644 --- a/src/Interop/Interop.Auth.HmacSha512.cs +++ b/src/Interop/Interop.Auth.HmacSha512.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,36 +9,43 @@ internal static partial class Libsodium internal const int crypto_auth_hmacsha512_BYTES = 64; internal const int crypto_auth_hmacsha512_KEYBYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha512_bytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha512_final( - crypto_auth_hmacsha512_state* state, - byte* @out); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha512_init( - crypto_auth_hmacsha512_state* state, - byte* key, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha512_bytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha512_final( + ref crypto_auth_hmacsha512_state state, + Span @out); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha512_init( + ref crypto_auth_hmacsha512_state state, + ReadOnlySpan key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha512_init( - crypto_auth_hmacsha512_state* state, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha512_init( + ref crypto_auth_hmacsha512_state state, SecureMemoryHandle key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha512_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha512_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_auth_hmacsha512_statebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_auth_hmacsha512_statebytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_auth_hmacsha512_update( - crypto_auth_hmacsha512_state* state, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_auth_hmacsha512_update( + ref crypto_auth_hmacsha512_state state, + ReadOnlySpan @in, ulong inlen); [StructLayout(LayoutKind.Explicit, Size = 416)] diff --git a/src/Interop/Interop.Core.cs b/src/Interop/Interop.Core.cs index 64dfc2fd..7004f0aa 100644 --- a/src/Interop/Interop.Core.cs +++ b/src/Interop/Interop.Core.cs @@ -1,15 +1,18 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop { internal static partial class Libsodium { - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int sodium_init(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int sodium_init(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int sodium_set_misuse_handler( + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static unsafe partial int sodium_set_misuse_handler( delegate* unmanaged[Cdecl] handler); } } diff --git a/src/Interop/Interop.GenericHash.Blake2b.cs b/src/Interop/Interop.GenericHash.Blake2b.cs index 543f47a3..abccc3b3 100644 --- a/src/Interop/Interop.GenericHash.Blake2b.cs +++ b/src/Interop/Interop.GenericHash.Blake2b.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -12,69 +13,82 @@ internal static partial class Libsodium internal const int crypto_generichash_blake2b_KEYBYTES_MAX = 64; internal const int crypto_generichash_blake2b_KEYBYTES_MIN = 16; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b( - byte* @out, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b( + Span @out, nuint outlen, - byte* @in, + ReadOnlySpan @in, ulong inlen, IntPtr key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b( - byte* @out, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b( + Span @out, nuint outlen, - byte* @in, + ReadOnlySpan @in, ulong inlen, SecureMemoryHandle key, nuint keylen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_bytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_bytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_bytes_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_bytes_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_bytes_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_bytes_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b_final( - crypto_generichash_blake2b_state* state, - byte* @out, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b_final( + ref crypto_generichash_blake2b_state state, + Span @out, nuint outlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b_init( - crypto_generichash_blake2b_state* state, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b_init( + ref crypto_generichash_blake2b_state state, IntPtr key, nuint keylen, nuint outlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b_init( - crypto_generichash_blake2b_state* state, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b_init( + ref crypto_generichash_blake2b_state state, SecureMemoryHandle key, nuint keylen, nuint outlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_keybytes_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_keybytes_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_keybytes_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_keybytes_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_generichash_blake2b_statebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_generichash_blake2b_statebytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_generichash_blake2b_update( - crypto_generichash_blake2b_state* state, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_generichash_blake2b_update( + ref crypto_generichash_blake2b_state state, + ReadOnlySpan @in, ulong inlen); [StructLayout(LayoutKind.Explicit, Size = 384)] diff --git a/src/Interop/Interop.Hash.Sha256.cs b/src/Interop/Interop.Hash.Sha256.cs index 3840011a..c147de68 100644 --- a/src/Interop/Interop.Hash.Sha256.cs +++ b/src/Interop/Interop.Hash.Sha256.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -7,31 +8,44 @@ internal static partial class Libsodium { internal const int crypto_hash_sha256_BYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha256( - byte* @out, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha256( + Span @out, + ReadOnlySpan @in, ulong inlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_hash_sha256_bytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha256_final( - crypto_hash_sha256_state* state, - byte* @out); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha256_init( - crypto_hash_sha256_state* state); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_hash_sha256_statebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_hash_sha256_bytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha256_final( + ref crypto_hash_sha256_state state, + Span @out); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha256_init( + ref crypto_hash_sha256_state state); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_hash_sha256_statebytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha256_update( + ref crypto_hash_sha256_state state, + ReadOnlySpan @in, + ulong inlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha256_update( - crypto_hash_sha256_state* state, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha256_update( + ref crypto_hash_sha256_state state, + in uint @in, ulong inlen); [StructLayout(LayoutKind.Explicit, Size = 104)] diff --git a/src/Interop/Interop.Hash.Sha512.cs b/src/Interop/Interop.Hash.Sha512.cs index 8f3f8cc8..64d43588 100644 --- a/src/Interop/Interop.Hash.Sha512.cs +++ b/src/Interop/Interop.Hash.Sha512.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -7,31 +8,37 @@ internal static partial class Libsodium { internal const int crypto_hash_sha512_BYTES = 64; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha512( - byte* @out, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha512( + Span @out, + ReadOnlySpan @in, ulong inlen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_hash_sha512_bytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha512_final( - crypto_hash_sha512_state* state, - byte* @out); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha512_init( - crypto_hash_sha512_state* state); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_hash_sha512_statebytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_hash_sha512_update( - crypto_hash_sha512_state* state, - byte* @in, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_hash_sha512_bytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha512_final( + ref crypto_hash_sha512_state state, + Span @out); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha512_init( + ref crypto_hash_sha512_state state); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_hash_sha512_statebytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_hash_sha512_update( + ref crypto_hash_sha512_state state, + ReadOnlySpan @in, ulong inlen); [StructLayout(LayoutKind.Explicit, Size = 208)] diff --git a/src/Interop/Interop.PublicKeyBytes.cs b/src/Interop/Interop.PublicKeyBytes.cs index ab924402..de406308 100644 --- a/src/Interop/Interop.PublicKeyBytes.cs +++ b/src/Interop/Interop.PublicKeyBytes.cs @@ -1,12 +1,13 @@ -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; internal static partial class Interop { internal static partial class Libsodium { - [StructLayout(LayoutKind.Explicit, Size = 32)] - internal readonly struct PublicKeyBytes + [InlineArray(32)] + internal struct PublicKeyBytes { + private byte _element0; } } } diff --git a/src/Interop/Interop.Pwhash.Argon2i.cs b/src/Interop/Interop.Pwhash.Argon2i.cs index 64f93301..5b15b3cb 100644 --- a/src/Interop/Interop.Pwhash.Argon2i.cs +++ b/src/Interop/Interop.Pwhash.Argon2i.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -7,44 +8,53 @@ internal static partial class Libsodium { internal const int crypto_pwhash_argon2i_ALG_ARGON2I13 = 1; internal const int crypto_pwhash_argon2i_BYTES_MIN = 16; - internal const long crypto_pwhash_argon2i_MEMLIMIT_MIN = 8192; + internal const int crypto_pwhash_argon2i_MEMLIMIT_MIN = 8192; internal const long crypto_pwhash_argon2i_OPSLIMIT_MAX = 4294967295; - internal const long crypto_pwhash_argon2i_OPSLIMIT_MIN = 3; + internal const int crypto_pwhash_argon2i_OPSLIMIT_MIN = 3; internal const int crypto_pwhash_argon2i_SALTBYTES = 16; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_pwhash_argon2i( - byte* @out, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_pwhash_argon2i( + Span @out, ulong outlen, - sbyte* passwd, + ReadOnlySpan passwd, ulong passwdlen, - byte* salt, + ReadOnlySpan salt, ulong opslimit, nuint memlimit, int alg); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int crypto_pwhash_argon2i_alg_argon2i13(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_pwhash_argon2i_alg_argon2i13(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_bytes_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_bytes_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_bytes_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_bytes_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_memlimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_memlimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_memlimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_memlimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_opslimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_opslimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_opslimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_opslimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2i_saltbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2i_saltbytes(); } } diff --git a/src/Interop/Interop.Pwhash.Argon2id.cs b/src/Interop/Interop.Pwhash.Argon2id.cs index 499f8bae..caf0ed9f 100644 --- a/src/Interop/Interop.Pwhash.Argon2id.cs +++ b/src/Interop/Interop.Pwhash.Argon2id.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -7,44 +8,53 @@ internal static partial class Libsodium { internal const int crypto_pwhash_argon2id_ALG_ARGON2ID13 = 2; internal const int crypto_pwhash_argon2id_BYTES_MIN = 16; - internal const long crypto_pwhash_argon2id_MEMLIMIT_MIN = 8192; + internal const int crypto_pwhash_argon2id_MEMLIMIT_MIN = 8192; internal const long crypto_pwhash_argon2id_OPSLIMIT_MAX = 4294967295; - internal const long crypto_pwhash_argon2id_OPSLIMIT_MIN = 1; + internal const int crypto_pwhash_argon2id_OPSLIMIT_MIN = 1; internal const int crypto_pwhash_argon2id_SALTBYTES = 16; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_pwhash_argon2id( - byte* @out, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_pwhash_argon2id( + Span @out, ulong outlen, - sbyte* passwd, + ReadOnlySpan passwd, ulong passwdlen, - byte* salt, + ReadOnlySpan salt, ulong opslimit, nuint memlimit, int alg); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int crypto_pwhash_argon2id_alg_argon2id13(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_pwhash_argon2id_alg_argon2id13(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_bytes_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_bytes_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_bytes_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_bytes_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_memlimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_memlimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_memlimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_memlimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_opslimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_opslimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_opslimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_opslimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_argon2id_saltbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_argon2id_saltbytes(); } } diff --git a/src/Interop/Interop.Pwhash.Scrypt.cs b/src/Interop/Interop.Pwhash.Scrypt.cs index ff968116..97c8f862 100644 --- a/src/Interop/Interop.Pwhash.Scrypt.cs +++ b/src/Interop/Interop.Pwhash.Scrypt.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -6,42 +7,50 @@ internal static partial class Interop internal static partial class Libsodium { internal const int crypto_pwhash_scryptsalsa208sha256_BYTES_MIN = 16; - internal const long crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = 16777216; + internal const int crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN = 16777216; internal const long crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX = 4294967295; - internal const long crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = 32768; + internal const int crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN = 32768; internal const int crypto_pwhash_scryptsalsa208sha256_SALTBYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_bytes_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_bytes_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_bytes_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_bytes_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_pwhash_scryptsalsa208sha256_ll( - byte* passwd, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_pwhash_scryptsalsa208sha256_ll( + ReadOnlySpan passwd, nuint passwdlen, - byte* salt, + ReadOnlySpan salt, nuint saltlen, ulong N, uint r, uint p, - byte* buf, + Span buf, nuint buflen); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_memlimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_memlimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_memlimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_memlimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_opslimit_max(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_opslimit_max(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_opslimit_min(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_opslimit_min(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_pwhash_scryptsalsa208sha256_saltbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_pwhash_scryptsalsa208sha256_saltbytes(); } } diff --git a/src/Interop/Interop.Scalarmult.X25519.cs b/src/Interop/Interop.Scalarmult.X25519.cs index cf9f91af..f338ddd7 100644 --- a/src/Interop/Interop.Scalarmult.X25519.cs +++ b/src/Interop/Interop.Scalarmult.X25519.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,21 +9,25 @@ internal static partial class Libsodium internal const int crypto_scalarmult_curve25519_BYTES = 32; internal const int crypto_scalarmult_curve25519_SCALARBYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_scalarmult_curve25519( + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_scalarmult_curve25519( SecureMemoryHandle q, SecureMemoryHandle n, - PublicKeyBytes* p); + in PublicKeyBytes p); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_scalarmult_curve25519_base( - PublicKeyBytes* q, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_scalarmult_curve25519_base( + ref PublicKeyBytes q, SecureMemoryHandle n); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_scalarmult_curve25519_bytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_scalarmult_curve25519_bytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_scalarmult_curve25519_scalarbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_scalarmult_curve25519_scalarbytes(); } } diff --git a/src/Interop/Interop.SecretBox.XSalsa20Poly1305.cs b/src/Interop/Interop.SecretBox.XSalsa20Poly1305.cs index 5a179811..62c43353 100644 --- a/src/Interop/Interop.SecretBox.XSalsa20Poly1305.cs +++ b/src/Interop/Interop.SecretBox.XSalsa20Poly1305.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,32 +11,38 @@ internal static partial class Libsodium internal const int crypto_secretbox_xsalsa20poly1305_MACBYTES = 16; internal const int crypto_secretbox_xsalsa20poly1305_NONCEBYTES = 24; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_secretbox_easy( - byte* c, - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_secretbox_easy( + Span c, + ReadOnlySpan m, ulong mlen, - byte* n, + ReadOnlySpan n, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_secretbox_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_secretbox_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_secretbox_macbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_secretbox_macbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_secretbox_noncebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_secretbox_noncebytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_secretbox_open_easy( - byte* m, - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_secretbox_open_easy( + Span m, + ReadOnlySpan c, ulong clen, - byte* n, + ReadOnlySpan n, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr crypto_secretbox_primitive(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial IntPtr crypto_secretbox_primitive(); } } diff --git a/src/Interop/Interop.Sign.Ed25519.cs b/src/Interop/Interop.Sign.Ed25519.cs index e576e250..aa70bae5 100644 --- a/src/Interop/Interop.Sign.Ed25519.cs +++ b/src/Interop/Interop.Sign.Ed25519.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -10,78 +11,93 @@ internal static partial class Libsodium internal const int crypto_sign_ed25519_SECRETKEYBYTES = (32 + 32); internal const int crypto_sign_ed25519_SEEDBYTES = 32; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_sign_ed25519_bytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_sign_ed25519_bytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_detached( - byte* sig, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_detached( + Span sig, out ulong siglen_p, - byte* m, + ReadOnlySpan m, ulong mlen, SecureMemoryHandle sk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_pk_to_curve25519( - PublicKeyBytes* curve25519_pk, - PublicKeyBytes* ed25519_pk); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_pk_to_curve25519( + ref PublicKeyBytes curve25519_pk, + in PublicKeyBytes ed25519_pk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_sign_ed25519_publickeybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_sign_ed25519_publickeybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_sign_ed25519_secretkeybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_sign_ed25519_secretkeybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_seed_keypair( - PublicKeyBytes* pk, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_seed_keypair( + ref PublicKeyBytes pk, SecureMemoryHandle sk, - byte* seed); + ReadOnlySpan seed); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_sign_ed25519_seedbytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_sign_ed25519_seedbytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_sk_to_curve25519( - byte* curve25519_sk, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_sk_to_curve25519( + Span curve25519_sk, SecureMemoryHandle ed25519_sk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_sk_to_seed( - byte* seed, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_sk_to_seed( + Span seed, SecureMemoryHandle sk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519_verify_detached( - byte* sig, - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519_verify_detached( + ReadOnlySpan sig, + ReadOnlySpan m, ulong mlen, - PublicKeyBytes* pk); + in PublicKeyBytes pk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519ph_final_create( - crypto_sign_ed25519ph_state* state, - byte* sig, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519ph_final_create( + ref crypto_sign_ed25519ph_state state, + Span sig, out ulong siglen_p, SecureMemoryHandle sk); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519ph_final_verify( - crypto_sign_ed25519ph_state* state, - byte* sig, - PublicKeyBytes* pk); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519ph_init( - crypto_sign_ed25519ph_state* state); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_sign_ed25519ph_statebytes(); - - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_sign_ed25519ph_update( - crypto_sign_ed25519ph_state* state, - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519ph_final_verify( + ref crypto_sign_ed25519ph_state state, + ReadOnlySpan sig, + in PublicKeyBytes pk); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519ph_init( + ref crypto_sign_ed25519ph_state state); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_sign_ed25519ph_statebytes(); + + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_sign_ed25519ph_update( + ref crypto_sign_ed25519ph_state state, + ReadOnlySpan m, ulong mlen); [StructLayout(LayoutKind.Explicit, Size = 208)] diff --git a/src/Interop/Interop.Stream.ChaCha20.cs b/src/Interop/Interop.Stream.ChaCha20.cs index fb77a4c5..6191bd27 100644 --- a/src/Interop/Interop.Stream.ChaCha20.cs +++ b/src/Interop/Interop.Stream.ChaCha20.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -8,33 +9,38 @@ internal static partial class Libsodium internal const int crypto_stream_chacha20_ietf_KEYBYTES = 32; internal const int crypto_stream_chacha20_ietf_NONCEBYTES = 12; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_stream_chacha20_ietf( - byte* c, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_stream_chacha20_ietf( + Span c, ulong clen, - byte* n, + ReadOnlySpan n, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_stream_chacha20_ietf_keybytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_stream_chacha20_ietf_keybytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern nuint crypto_stream_chacha20_ietf_noncebytes(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial nuint crypto_stream_chacha20_ietf_noncebytes(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_stream_chacha20_ietf_xor( - byte* c, - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_stream_chacha20_ietf_xor( + Span c, + ReadOnlySpan m, ulong mlen, - byte* n, + ReadOnlySpan n, SecureMemoryHandle k); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static unsafe extern int crypto_stream_chacha20_ietf_xor_ic( - byte* c, - byte* m, + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int crypto_stream_chacha20_ietf_xor_ic( + Span c, + ReadOnlySpan m, ulong mlen, - byte* n, + ReadOnlySpan n, uint ic, SecureMemoryHandle k); } diff --git a/src/Interop/Interop.Utils.cs b/src/Interop/Interop.Utils.cs index 6f1e1bbd..4f79d0c7 100644 --- a/src/Interop/Interop.Utils.cs +++ b/src/Interop/Interop.Utils.cs @@ -1,16 +1,19 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop { internal static partial class Libsodium { - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern void sodium_free( + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial void sodium_free( IntPtr ptr); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern SecureMemoryHandle sodium_malloc( + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial SecureMemoryHandle sodium_malloc( nuint size); } } diff --git a/src/Interop/Interop.Version.cs b/src/Interop/Interop.Version.cs index 2bc5c7d3..a362451f 100644 --- a/src/Interop/Interop.Version.cs +++ b/src/Interop/Interop.Version.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; internal static partial class Interop @@ -9,13 +10,16 @@ internal static partial class Libsodium internal const int SODIUM_LIBRARY_VERSION_MINOR = 1; internal const string SODIUM_VERSION_STRING = "1.0.19"; - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int sodium_library_version_major(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int sodium_library_version_major(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern int sodium_library_version_minor(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial int sodium_library_version_minor(); - [DllImport(Libraries.Libsodium, CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr sodium_version_string(); + [LibraryImport(Libraries.Libsodium)] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + internal static partial IntPtr sodium_version_string(); } } diff --git a/src/Interop/Interop.yaml b/src/Interop/Interop.yaml index fd4f6148..69769783 100644 --- a/src/Interop/Interop.yaml +++ b/src/Interop/Interop.yaml @@ -15,8 +15,8 @@ Interop.Aead.Aegis128L.cs: - crypto_aead_aegis128l_NSECBYTES functions: - crypto_aead_aegis128l_abytes - - crypto_aead_aegis128l_decrypt (out ulong mlen_p, SecureMemoryHandle k) - - crypto_aead_aegis128l_encrypt (out ulong clen_p, SecureMemoryHandle k) + - crypto_aead_aegis128l_decrypt (out ulong mlen_p, IntPtr nsec, SecureMemoryHandle k) + - crypto_aead_aegis128l_encrypt (out ulong clen_p, IntPtr nsec, SecureMemoryHandle k) - crypto_aead_aegis128l_keybytes - crypto_aead_aegis128l_npubbytes - crypto_aead_aegis128l_nsecbytes @@ -30,8 +30,8 @@ Interop.Aead.Aegis256.cs: - crypto_aead_aegis256_NSECBYTES functions: - crypto_aead_aegis256_abytes - - crypto_aead_aegis256_decrypt (out ulong mlen_p, SecureMemoryHandle k) - - crypto_aead_aegis256_encrypt (out ulong clen_p, SecureMemoryHandle k) + - crypto_aead_aegis256_decrypt (out ulong mlen_p, IntPtr nsec, SecureMemoryHandle k) + - crypto_aead_aegis256_encrypt (out ulong clen_p, IntPtr nsec, SecureMemoryHandle k) - crypto_aead_aegis256_keybytes - crypto_aead_aegis256_npubbytes - crypto_aead_aegis256_nsecbytes @@ -45,8 +45,8 @@ Interop.Aead.Aes256Gcm.cs: - crypto_aead_aes256gcm_NSECBYTES functions: - crypto_aead_aes256gcm_abytes - - crypto_aead_aes256gcm_decrypt (out ulong mlen_p, SecureMemoryHandle k) - - crypto_aead_aes256gcm_encrypt (out ulong clen_p, SecureMemoryHandle k) + - crypto_aead_aes256gcm_decrypt (out ulong mlen_p, IntPtr nsec, SecureMemoryHandle k) + - crypto_aead_aes256gcm_encrypt (out ulong clen_p, IntPtr nsec, SecureMemoryHandle k) - crypto_aead_aes256gcm_is_available - crypto_aead_aes256gcm_keybytes - crypto_aead_aes256gcm_npubbytes @@ -61,8 +61,8 @@ Interop.Aead.ChaCha20Poly1305.cs: - crypto_aead_chacha20poly1305_ietf_NSECBYTES functions: - crypto_aead_chacha20poly1305_ietf_abytes - - crypto_aead_chacha20poly1305_ietf_decrypt (out ulong mlen_p, SecureMemoryHandle k) - - crypto_aead_chacha20poly1305_ietf_encrypt (out ulong clen_p, SecureMemoryHandle k) + - crypto_aead_chacha20poly1305_ietf_decrypt (out ulong mlen_p, IntPtr nsec, SecureMemoryHandle k) + - crypto_aead_chacha20poly1305_ietf_encrypt (out ulong clen_p, IntPtr nsec, SecureMemoryHandle k) - crypto_aead_chacha20poly1305_ietf_keybytes - crypto_aead_chacha20poly1305_ietf_npubbytes - crypto_aead_chacha20poly1305_ietf_nsecbytes @@ -76,8 +76,8 @@ Interop.Aead.XChaCha20Poly1305.cs: - crypto_aead_xchacha20poly1305_ietf_NSECBYTES functions: - crypto_aead_xchacha20poly1305_ietf_abytes - - crypto_aead_xchacha20poly1305_ietf_decrypt (out ulong mlen_p, SecureMemoryHandle k) - - crypto_aead_xchacha20poly1305_ietf_encrypt (out ulong clen_p, SecureMemoryHandle k) + - crypto_aead_xchacha20poly1305_ietf_decrypt (out ulong mlen_p, IntPtr nsec, SecureMemoryHandle k) + - crypto_aead_xchacha20poly1305_ietf_encrypt (out ulong clen_p, IntPtr nsec, SecureMemoryHandle k) - crypto_aead_xchacha20poly1305_ietf_keybytes - crypto_aead_xchacha20poly1305_ietf_npubbytes - crypto_aead_xchacha20poly1305_ietf_nsecbytes @@ -95,6 +95,7 @@ Interop.Auth.HmacSha256.cs: - crypto_auth_hmacsha256_keybytes - crypto_auth_hmacsha256_statebytes - crypto_auth_hmacsha256_update + - crypto_auth_hmacsha256_update (in uint in) structs: - crypto_auth_hmacsha256_state @@ -181,6 +182,7 @@ Interop.Hash.Sha256.cs: - crypto_hash_sha256_init - crypto_hash_sha256_statebytes - crypto_hash_sha256_update + - crypto_hash_sha256_update (in uint in) structs: - crypto_hash_sha256_state @@ -209,9 +211,9 @@ Interop.Pwhash.Argon2i.cs: constants: - crypto_pwhash_argon2i_ALG_ARGON2I13 - crypto_pwhash_argon2i_BYTES_MIN - - crypto_pwhash_argon2i_MEMLIMIT_MIN (long) - - crypto_pwhash_argon2i_OPSLIMIT_MAX (long) - - crypto_pwhash_argon2i_OPSLIMIT_MIN (long) + - crypto_pwhash_argon2i_MEMLIMIT_MIN + - crypto_pwhash_argon2i_OPSLIMIT_MAX + - crypto_pwhash_argon2i_OPSLIMIT_MIN - crypto_pwhash_argon2i_SALTBYTES functions: - crypto_pwhash_argon2i @@ -229,9 +231,9 @@ Interop.Pwhash.Argon2id.cs: constants: - crypto_pwhash_argon2id_ALG_ARGON2ID13 - crypto_pwhash_argon2id_BYTES_MIN - - crypto_pwhash_argon2id_MEMLIMIT_MIN (long) - - crypto_pwhash_argon2id_OPSLIMIT_MAX (long) - - crypto_pwhash_argon2id_OPSLIMIT_MIN (long) + - crypto_pwhash_argon2id_MEMLIMIT_MIN + - crypto_pwhash_argon2id_OPSLIMIT_MAX + - crypto_pwhash_argon2id_OPSLIMIT_MIN - crypto_pwhash_argon2id_SALTBYTES functions: - crypto_pwhash_argon2id @@ -248,9 +250,9 @@ Interop.Pwhash.Scrypt.cs: include: include/sodium/crypto_pwhash_scryptsalsa208sha256.h constants: - crypto_pwhash_scryptsalsa208sha256_BYTES_MIN - - crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN (long) - - crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX (long) - - crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN (long) + - crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN + - crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX + - crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN - crypto_pwhash_scryptsalsa208sha256_SALTBYTES functions: - crypto_pwhash_scryptsalsa208sha256_ll @@ -268,8 +270,8 @@ Interop.Scalarmult.X25519.cs: - crypto_scalarmult_curve25519_BYTES - crypto_scalarmult_curve25519_SCALARBYTES functions: - - crypto_scalarmult_curve25519 (SecureMemoryHandle q, SecureMemoryHandle n, PublicKeyBytes* p) - - crypto_scalarmult_curve25519_base (PublicKeyBytes* q, SecureMemoryHandle n) + - crypto_scalarmult_curve25519 (SecureMemoryHandle q, SecureMemoryHandle n, in PublicKeyBytes p) + - crypto_scalarmult_curve25519_base (ref PublicKeyBytes q, SecureMemoryHandle n) - crypto_scalarmult_curve25519_bytes - crypto_scalarmult_curve25519_scalarbytes @@ -315,16 +317,16 @@ Interop.Sign.Ed25519.cs: functions: - crypto_sign_ed25519_bytes - crypto_sign_ed25519_detached (out ulong siglen_p, SecureMemoryHandle sk) - - crypto_sign_ed25519_pk_to_curve25519 (PublicKeyBytes* curve25519_pk, PublicKeyBytes* ed25519_pk) + - crypto_sign_ed25519_pk_to_curve25519 (ref PublicKeyBytes curve25519_pk, in PublicKeyBytes ed25519_pk) - crypto_sign_ed25519_publickeybytes - crypto_sign_ed25519_secretkeybytes - - crypto_sign_ed25519_seed_keypair (PublicKeyBytes* pk, SecureMemoryHandle sk) + - crypto_sign_ed25519_seed_keypair (ref PublicKeyBytes pk, SecureMemoryHandle sk) - crypto_sign_ed25519_seedbytes - crypto_sign_ed25519_sk_to_curve25519 (SecureMemoryHandle ed25519_sk) - crypto_sign_ed25519_sk_to_seed (SecureMemoryHandle sk) - - crypto_sign_ed25519_verify_detached (PublicKeyBytes* pk) + - crypto_sign_ed25519_verify_detached (in PublicKeyBytes pk) - crypto_sign_ed25519ph_final_create (out ulong siglen_p, SecureMemoryHandle sk) - - crypto_sign_ed25519ph_final_verify (PublicKeyBytes* pk) + - crypto_sign_ed25519ph_final_verify (in PublicKeyBytes pk) - crypto_sign_ed25519ph_init - crypto_sign_ed25519ph_statebytes - crypto_sign_ed25519ph_update @@ -379,7 +381,7 @@ Interop.Runtime.cs: Interop.Utils.cs: include: include/sodium/utils.h functions: - - sodium_free (IntPtr ptr) + - sodium_free - sodium_malloc (SecureMemoryHandle) Interop.Version.cs: