Skip to content

Commit c4c2672

Browse files
committed
feat(crypto): add ChaCha20 Encrypt and Decrypt functions
these work in all platforms except for webgl, where they will fail. A libsodium js library is needed to get these to work for that platform.
1 parent 2f9d722 commit c4c2672

26 files changed

+513
-160
lines changed

Algorand.Unity.Package/Packages/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dependencies": {
3-
"com.unity.burst": "1.8.4",
4-
"com.unity.ide.rider": "3.0.21",
3+
"com.unity.burst": "1.8.7",
4+
"com.unity.ide.rider": "3.0.24",
55
"com.unity.ide.visualstudio": "2.0.18",
66
"com.unity.ide.vscode": "1.2.5",
77
"com.unity.mobile.android-logcat": "1.3.2",
88
"com.unity.test-framework": "1.1.33",
99
"com.unity.test-framework.performance": "3.0.0-pre.2",
10-
"com.unity.testtools.codecoverage": "1.2.3",
10+
"com.unity.testtools.codecoverage": "1.2.4",
1111
"com.unity.toolchain.macos-x86_64-linux-x86_64": "2.0.4",
1212
"com.unity.toolchain.win-x86_64-linux-x86_64": "2.0.4",
1313
"com.unity.ugui": "1.0.0",

Algorand.Unity.Package/Packages/packages-lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"url": "https://package.openupm.com"
1919
},
2020
"com.unity.burst": {
21-
"version": "1.8.4",
21+
"version": "1.8.7",
2222
"depth": 0,
2323
"source": "registry",
2424
"dependencies": {
@@ -45,7 +45,7 @@
4545
"url": "https://packages.unity.com"
4646
},
4747
"com.unity.ide.rider": {
48-
"version": "3.0.21",
48+
"version": "3.0.24",
4949
"depth": 0,
5050
"source": "registry",
5151
"dependencies": {
@@ -135,7 +135,7 @@
135135
"url": "https://packages.unity.com"
136136
},
137137
"com.unity.testtools.codecoverage": {
138-
"version": "1.2.3",
138+
"version": "1.2.4",
139139
"depth": 0,
140140
"source": "registry",
141141
"dependencies": {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
m_EditorVersion: 2022.2.17f1
2-
m_EditorVersionWithRevision: 2022.2.17f1 (54cb9bda89c4)
1+
m_EditorVersion: 2022.3.4f1
2+
m_EditorVersionWithRevision: 2022.3.4f1 (35713cd46cd7)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Algorand.Unity.Crypto.Tests")]

Runtime/Algorand.Unity.Crypto/AssemblyInfo.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Algorand.Unity.Crypto/ChaCha20Poly1305.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Algorand.Unity.LowLevel;
4+
5+
namespace Algorand.Unity.Crypto
6+
{
7+
public static partial class ChaCha20Poly1305
8+
{
9+
[Serializable]
10+
[StructLayout(LayoutKind.Explicit, Size = Size)]
11+
public struct AuthTag : IByteArray, IEquatable<AuthTag>
12+
{
13+
public const int Size = 16;
14+
15+
[FieldOffset(0)]
16+
internal unsafe fixed ulong buffer[Size / 8];
17+
18+
public int Length => Size;
19+
20+
public byte this[int index]
21+
{
22+
get => this.GetByteAt(index);
23+
set => this.SetByteAt(index, value);
24+
}
25+
26+
public unsafe void* GetUnsafePtr()
27+
{
28+
fixed (ulong* b = buffer)
29+
{
30+
return b;
31+
}
32+
}
33+
34+
public bool Equals(AuthTag other)
35+
{
36+
return ByteArray.Equals(this, other);
37+
}
38+
}
39+
}
40+
}

Runtime/Algorand.Unity.Crypto/ChaCha20Poly1305/ChaCha20Poly1305+AuthTag.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Algorand.Unity.LowLevel;
2+
using Unity.Collections;
3+
using Unity.Collections.LowLevel.Unsafe;
4+
5+
namespace Algorand.Unity.Crypto
6+
{
7+
public static partial class ChaCha20Poly1305
8+
{
9+
public enum DecryptionError
10+
{
11+
None = 0,
12+
VerificationFailed = -1
13+
}
14+
15+
public static unsafe DecryptionError Decrypt<TCipher>(
16+
NativeList<byte> message,
17+
TCipher cipher,
18+
Key key,
19+
Nonce nonce
20+
)
21+
where TCipher : struct, IByteArray
22+
{
23+
message.Length = cipher.Length - AuthTag.Size;
24+
var errorCode = sodium.crypto_aead_chacha20poly1305_ietf_decrypt(
25+
message.GetUnsafePtr(),
26+
out var messageLength,
27+
null,
28+
cipher.GetUnsafePtr(),
29+
(ulong)cipher.Length,
30+
null,
31+
0,
32+
nonce.GetUnsafePtr(),
33+
key.GetUnsafePtr()
34+
);
35+
message.Length = (int)messageLength;
36+
return (DecryptionError)errorCode;
37+
}
38+
}
39+
}

Runtime/Algorand.Unity.Crypto/ChaCha20Poly1305/ChaCha20Poly1305+Decrypt.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)