generated from CareBoo/UPMTemplate-2020
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): ✨ add Sha256 and X25519 APIs, and use Span based APIs
- Loading branch information
1 parent
2d4a515
commit 22cd7fd
Showing
27 changed files
with
380 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 37 additions & 23 deletions
60
Runtime/Algorand.Unity.Crypto/ChaCha20Poly1305/ChaCha20Poly1305+Encrypt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
using Algorand.Unity.LowLevel; | ||
using Unity.Collections; | ||
using Unity.Collections.LowLevel.Unsafe; | ||
using System; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class ChaCha20Poly1305 | ||
{ | ||
public enum EncryptionError | ||
{ | ||
None = 0 | ||
None = 0, | ||
CipherInvalidSize = 1, | ||
} | ||
|
||
public static unsafe EncryptionError Encrypt<TMessage>( | ||
NativeList<byte> cipher, | ||
TMessage message, | ||
/// <summary> | ||
/// Encrypt a message using the ChaCha20Poly1305 algorithm. | ||
/// </summary> | ||
/// <param name="cipher">The encrypted message.</param> | ||
/// <param name="message">The message to encrypt.</param> | ||
/// <param name="key">The symmetric key to encrypt with.</param> | ||
/// <param name="nonce">The 12 byte iv to use to encrypt.</param> | ||
/// <returns></returns> | ||
public static unsafe EncryptionError Encrypt( | ||
Span<byte> cipher, | ||
ReadOnlySpan<byte> message, | ||
Key key, | ||
Nonce nonce | ||
) | ||
where TMessage : struct, IByteArray | ||
{ | ||
cipher.Length = message.Length + AuthTag.Size; | ||
var errorCode = sodium.crypto_aead_chacha20poly1305_ietf_encrypt( | ||
cipher.GetUnsafePtr(), | ||
out var cipherLength, | ||
message.GetUnsafePtr(), | ||
(ulong)message.Length, | ||
null, | ||
0, | ||
null, | ||
nonce.GetUnsafePtr(), | ||
key.GetUnsafePtr() | ||
); | ||
cipher.Length = (int)cipherLength; | ||
nonce += 1; | ||
var cipherLength = message.Length + AuthTag.Size; | ||
if (cipher.Length != cipherLength) | ||
{ | ||
return EncryptionError.CipherInvalidSize; | ||
} | ||
var errorCode = default(int); | ||
|
||
fixed (byte* c = &cipher[0]) | ||
fixed (byte* m = &message[0]) | ||
{ | ||
errorCode = sodium.crypto_aead_chacha20poly1305_ietf_encrypt( | ||
c, | ||
out _, | ||
m, | ||
(ulong)message.Length, | ||
null, | ||
0, | ||
null, | ||
nonce.GetUnsafePtr(), | ||
key.GetUnsafePtr() | ||
); | ||
} | ||
return (EncryptionError)errorCode; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
Runtime/Algorand.Unity.Crypto/Interop/sodium/sodium+CryptoBox.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
internal static unsafe partial class sodium | ||
{ | ||
internal const ulong crypto_box_PUBLICKEYBYTES = 32U; | ||
internal const ulong crypto_box_SECRETKEYBYTES = 32U; | ||
internal const ulong crypto_box_NONCEBYTES = 24U; | ||
|
||
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)] | ||
internal static extern int crypto_box_keypair( | ||
void* pk, | ||
void* sk); | ||
|
||
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)] | ||
internal static extern int crypto_box_seed_keypair( | ||
void* pk, | ||
void* sk, | ||
void* seed); | ||
|
||
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)] | ||
internal static extern int crypto_box_easy( | ||
void* c, | ||
void* m, | ||
ulong mlen, | ||
void* n, | ||
void* pk, | ||
void* sk); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Algorand.Unity.Crypto/Interop/sodium/sodium+CryptoBox.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
Runtime/Algorand.Unity.Crypto/Interop/sodium/sodium+Sha256.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
internal static unsafe partial class sodium | ||
{ | ||
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)] | ||
internal static extern int crypto_hash_sha256( | ||
void* outp, | ||
void* inp, | ||
ulong inlen); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Runtime/Algorand.Unity.Crypto/Interop/sodium/sodium+Sha256.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Algorand.Unity.LowLevel; | ||
|
||
namespace Algorand.Unity.Crypto | ||
{ | ||
[Serializable] | ||
[StructLayout(LayoutKind.Explicit, Size = Size)] | ||
public struct Sha256 : IByteArray, IEquatable<Sha256> | ||
{ | ||
public const int Size = 256 / 8; | ||
|
||
[FieldOffset(0)] | ||
internal unsafe fixed ulong buffer[Size / 8]; | ||
|
||
public int Length => Size; | ||
|
||
public byte this[int index] | ||
{ | ||
get => this.GetByteAt(index); | ||
set => this.SetByteAt(index, value); | ||
} | ||
|
||
public unsafe void* GetUnsafePtr() | ||
{ | ||
fixed (void* b = buffer) | ||
{ | ||
return b; | ||
} | ||
} | ||
|
||
public bool Equals(Sha256 other) | ||
{ | ||
return ByteArray.Equals(this, other); | ||
} | ||
|
||
public unsafe static Sha256 Hash(ReadOnlySpan<byte> message) | ||
{ | ||
fixed (byte* messagePtr = message) | ||
{ | ||
var hash = default(Sha256); | ||
sodium.crypto_hash_sha256(&hash, messagePtr, (ulong)message.Length); | ||
return hash; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Algorand.Unity.Crypto | ||
{ | ||
public static partial class X25519 | ||
{ | ||
public static (PublicKey publicKey, SecretKey secretKey) Keygen() | ||
{ | ||
var publicKey = default(PublicKey); | ||
var secretKey = default(SecretKey); | ||
|
||
unsafe | ||
{ | ||
sodium.crypto_box_keypair(&publicKey, &secretKey); | ||
} | ||
return (publicKey, secretKey); | ||
} | ||
} | ||
} |
Oops, something went wrong.