Skip to content

Commit

Permalink
feat(crypto): ✨ add SecretBox related APIs for encrypt and decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonboukheir committed Sep 25, 2023
1 parent 6642e99 commit a6f7ea0
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs
Original file line number Diff line number Diff line change
@@ -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<byte>(Key key)
{
return new Span<byte>(key.bytes, SizeBytes);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Key.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs
Original file line number Diff line number Diff line change
@@ -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<byte>(Mac mac)
{
return new Span<byte>(mac.bytes, SizeBytes);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Mac.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs
Original file line number Diff line number Diff line change
@@ -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<byte>(Nonce nonce)
{
return new Span<byte>(nonce.bytes, SizeBytes);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox+Nonce.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs
Original file line number Diff line number Diff line change
@@ -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<byte> cipher,
ReadOnlySpan<byte> message,
Key* key,
out Nonce nonce
)
{
nonce = Random.Bytes<Nonce>();
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<byte> message,
ReadOnlySpan<byte> 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
);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Algorand.Unity.Crypto/SecretBox/SecretBox.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6f7ea0

Please sign in to comment.