From 2162178cb7e209e9f060748842e1c4782a2ab852 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 17 Aug 2024 21:33:23 -0400 Subject: refactor: cipher utils update and simplify --- .../src/NoscryptExtensions.cs | 162 --------------------- 1 file changed, 162 deletions(-) (limited to 'wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptExtensions.cs') diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptExtensions.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptExtensions.cs index ccae190..e96ff96 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptExtensions.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NoscryptExtensions.cs @@ -24,168 +24,6 @@ namespace VNLib.Utils.Cryptography.Noscrypt public static class NoscryptExtensions { - public static void EncryptNip44( - this INostrCrypto lib, - ref readonly NCSecretKey secretKey, - ref readonly NCPublicKey publicKey, - ReadOnlySpan nonce32, - ReadOnlySpan plainText, - Span hmackKeyOut32, - Span cipherText - ) - { - ArgumentNullException.ThrowIfNull(lib); - - //Chacha requires the output buffer to be at-least the size of the input buffer - ArgumentOutOfRangeException.ThrowIfGreaterThan(plainText.Length, cipherText.Length, nameof(plainText)); - - //Nonce must be exactly 32 bytes - ArgumentOutOfRangeException.ThrowIfNotEqual(nonce32.Length, NC_ENCRYPTION_NONCE_SIZE, nameof(nonce32)); - - ArgumentOutOfRangeException.ThrowIfNotEqual(hmackKeyOut32.Length, NC_HMAC_KEY_SIZE, nameof(hmackKeyOut32)); - - //Encrypt data, use the plaintext buffer size as the data size - lib.EncryptNip44( - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: in MemoryMarshal.GetReference(nonce32), - plainText: in MemoryMarshal.GetReference(plainText), - cipherText: ref MemoryMarshal.GetReference(cipherText), - size: (uint)plainText.Length, - hmacKeyOut32: ref MemoryMarshal.GetReference(hmackKeyOut32) - ); - } - - public static unsafe void EncryptNip44( - this INostrCrypto lib, - ref NCSecretKey secretKey, - ref NCPublicKey publicKey, - void* nonce32, - void* hmacKeyOut32, - void* plainText, - void* cipherText, - uint size - ) - { - ArgumentNullException.ThrowIfNull(plainText); - ArgumentNullException.ThrowIfNull(cipherText); - ArgumentNullException.ThrowIfNull(nonce32); - - //Spans are easer to forward references from pointers without screwing up arguments - lib.EncryptNip44( - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: in Unsafe.AsRef(nonce32), - plainText: in Unsafe.AsRef(plainText), - cipherText: ref Unsafe.AsRef(cipherText), - size: size, - hmacKeyOut32: ref Unsafe.AsRef(hmacKeyOut32) - ); - } - - - public static void DecryptNip44( - this INostrCrypto lib, - ref readonly NCSecretKey secretKey, - ref readonly NCPublicKey publicKey, - ReadOnlySpan nonce32, - ReadOnlySpan cipherText, - Span plainText - ) - { - ArgumentNullException.ThrowIfNull(lib); - - //Chacha requires the output buffer to be at-least the size of the input buffer - ArgumentOutOfRangeException.ThrowIfGreaterThan(cipherText.Length, plainText.Length, nameof(cipherText)); - - //Nonce must be exactly 32 bytes - ArgumentOutOfRangeException.ThrowIfNotEqual(nonce32.Length, 32, nameof(nonce32)); - - //Decrypt data, use the ciphertext buffer size as the data size - lib.DecryptNip44( - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: in MemoryMarshal.GetReference(nonce32), - cipherText: in MemoryMarshal.GetReference(cipherText), - plainText: ref MemoryMarshal.GetReference(plainText), - size: (uint)cipherText.Length - ); - } - - public static unsafe void DecryptNip44( - this INostrCrypto lib, - ref readonly NCSecretKey secretKey, - ref readonly NCPublicKey publicKey, - void* nonce32, - void* cipherText, - void* plainText, - uint size - ) - { - ArgumentNullException.ThrowIfNull(nonce32); - ArgumentNullException.ThrowIfNull(cipherText); - ArgumentNullException.ThrowIfNull(plainText); - - //Spans are easer to forward references from pointers without screwing up arguments - DecryptNip44( - lib: lib, - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: new Span(nonce32, NC_ENCRYPTION_NONCE_SIZE), - cipherText: new Span(cipherText, (int)size), - plainText: new Span(plainText, (int)size) - ); - } - - public static bool VerifyMac( - this INostrCrypto lib, - ref readonly NCSecretKey secretKey, - ref readonly NCPublicKey publicKey, - ReadOnlySpan nonce32, - ReadOnlySpan mac32, - ReadOnlySpan payload - ) - { - ArgumentNullException.ThrowIfNull(lib); - ArgumentOutOfRangeException.ThrowIfZero(payload.Length, nameof(payload)); - ArgumentOutOfRangeException.ThrowIfNotEqual(nonce32.Length, NC_ENCRYPTION_NONCE_SIZE, nameof(nonce32)); - ArgumentOutOfRangeException.ThrowIfNotEqual(mac32.Length, NC_ENCRYPTION_MAC_SIZE, nameof(mac32)); - - //Verify the HMAC - return lib.VerifyMac( - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: in MemoryMarshal.GetReference(nonce32), - mac32: in MemoryMarshal.GetReference(mac32), - payload: in MemoryMarshal.GetReference(payload), - payloadSize: (uint)payload.Length - ); - } - - public static unsafe bool VerifyMac( - this INostrCrypto lib, - ref readonly NCSecretKey secretKey, - ref readonly NCPublicKey publicKey, - void* nonce32, - void* mac32, - void* payload, - uint payloadSize - ) - { - ArgumentNullException.ThrowIfNull(nonce32); - ArgumentNullException.ThrowIfNull(mac32); - ArgumentNullException.ThrowIfNull(payload); - - return lib.VerifyMac( - secretKey: in secretKey, - publicKey: in publicKey, - nonce32: in Unsafe.AsRef(nonce32), - mac32: in Unsafe.AsRef(mac32), - payload: in Unsafe.AsRef(payload), - payloadSize: payloadSize - ); - } - public static void SignData( this INostrCrypto lib, ref readonly NCSecretKey secKey, -- cgit