From a6f7ea0d8783e8edc73a07d17156878fbe759dfa Mon Sep 17 00:00:00 2001 From: Jason Elie Bou Kheir <5115126+jasonboukheir@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:57:49 -0700 Subject: [PATCH] feat(crypto): :sparkles: add `SecretBox` related APIs for encrypt and decrypt --- .../SecretBox/SecretBox+Key.cs | 21 ++++++ .../SecretBox/SecretBox+Key.cs.meta | 11 +++ .../SecretBox/SecretBox+Mac.cs | 21 ++++++ .../SecretBox/SecretBox+Mac.cs.meta | 11 +++ .../SecretBox/SecretBox+Nonce.cs | 21 ++++++ .../SecretBox/SecretBox+Nonce.cs.meta | 11 +++ .../SecretBox/SecretBox.cs | 71 +++++++++++++++++++ .../SecretBox/SecretBox.cs.meta | 11 +++ 8 files changed, 178 insertions(+) create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs create mode 100644 Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs.meta diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs new file mode 100644 index 000000000..12f1915c1 --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs @@ -0,0 +1,21 @@ +using System; +using System.Runtime.InteropServices; + +namespace Algorand.Unity.Crypto +{ + public static partial class SecretBox + { + [StructLayout(LayoutKind.Explicit, Size = SizeBytes)] + public unsafe struct Key + { + public const int SizeBytes = sodium.crypto_secretbox_KEYBYTES; + + [FieldOffset(0)] public fixed byte bytes[SizeBytes]; + + public static implicit operator Span(Key key) + { + return new Span(key.bytes, SizeBytes); + } + } + } +} diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta new file mode 100644 index 000000000..a718aa6cc --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 54f0c3d4cc4754662ba3e9173fc6b263 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs new file mode 100644 index 000000000..bbd0d96ac --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs @@ -0,0 +1,21 @@ +using System; +using System.Runtime.InteropServices; + +namespace Algorand.Unity.Crypto +{ + public static partial class SecretBox + { + [StructLayout(LayoutKind.Explicit, Size = SizeBytes)] + public unsafe struct Mac + { + public const int SizeBytes = sodium.crypto_secretbox_MACBYTES; + + [FieldOffset(0)] public fixed byte bytes[SizeBytes]; + + public static implicit operator Span(Mac mac) + { + return new Span(mac.bytes, SizeBytes); + } + } + } +} diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta new file mode 100644 index 000000000..efd8512b7 --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e1ed30b2380bf48ae938f01f36ccb5c7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs new file mode 100644 index 000000000..969cd3d58 --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs @@ -0,0 +1,21 @@ +using System; +using System.Runtime.InteropServices; + +namespace Algorand.Unity.Crypto +{ + public static partial class SecretBox + { + [StructLayout(LayoutKind.Explicit, Size = SizeBytes)] + public unsafe struct Nonce + { + public const int SizeBytes = sodium.crypto_secretbox_NONCEBYTES; + + [FieldOffset(0)] public fixed byte bytes[SizeBytes]; + + public static implicit operator Span(Nonce nonce) + { + return new Span(nonce.bytes, SizeBytes); + } + } + } +} diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta new file mode 100644 index 000000000..484e37e59 --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 156715622084241058ac0449040c8c63 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs new file mode 100644 index 000000000..952928629 --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs @@ -0,0 +1,71 @@ +using System; + +namespace Algorand.Unity.Crypto +{ + public static partial class SecretBox + { + public enum EncryptError + { + Error = -1, + None + } + + public enum DecryptError + { + Error = -1, + None + } + + public static int CipherLength(int messageLength) + { + return messageLength + Mac.SizeBytes; + } + + public static int MessageLength(int cipherLength) + { + return cipherLength - Mac.SizeBytes; + } + + public static unsafe EncryptError Encrypt( + Span cipher, + ReadOnlySpan message, + Key* key, + out Nonce nonce + ) + { + nonce = Random.Bytes(); + fixed (byte* noncePtr = nonce.bytes) + fixed (byte* cipherPtr = cipher) + fixed (byte* messagePtr = message) + { + return (EncryptError)sodium.crypto_secretbox_easy( + cipherPtr, + messagePtr, + (ulong)message.Length, + noncePtr, + key + ); + } + } + + public static unsafe DecryptError Decrypt( + Span message, + ReadOnlySpan cipher, + Key* key, + Nonce nonce + ) + { + fixed (byte* messagePtr = message) + fixed (byte* cipherPtr = cipher) + { + return (DecryptError)sodium.crypto_secretbox_open_easy( + messagePtr, + cipherPtr, + (ulong)cipher.Length, + nonce.bytes, + key + ); + } + } + } +} diff --git a/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs.meta b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs.meta new file mode 100644 index 000000000..09d128a5c --- /dev/null +++ b/Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 476821147e0474a489f90ad7ed7efec3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: