native plugin

fix import flags

Show native state in ToString for NM HUD

Old unity support
This commit is contained in:
Robin Rolf 2024-02-26 23:18:04 +00:00
parent 482bd2615e
commit 4de68fe8f8
39 changed files with 621 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography;
using Mirror.Transports.Encryption.Native;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Agreement;
@ -131,6 +132,7 @@ enum State
* The client does this, since the fin is not acked explicitly, but by receiving data to decrypt
*/
private readonly bool _sendsFirst;
private byte[] _keyRaw;
public EncryptedConnection(EncryptionCredentials credentials,
bool isClient,
@ -363,6 +365,18 @@ private ArraySegment<byte> Encrypt(ArraySegment<byte> plaintext)
#endif
// Resize the static buffer to fit
EnsureSize(ref _tmpCryptBuffer, outSize);
if (AesGCMEncryptionNative.IsSupported)
{
ArraySegment<byte> nativeRes = AesGCMEncryptionNative.Encrypt(_keyRaw, _nonce, plaintext, new ArraySegment<byte>(_tmpCryptBuffer));
if (nativeRes.Count == 0)
{
_error(TransportError.Unexpected, $"Native Encryption failed. Please check STDERR (or editor log) for the error.");
return new ArraySegment<byte>();
}
return nativeRes;
}
int resultLen;
try
{
@ -411,6 +425,16 @@ private ArraySegment<byte> Decrypt(ArraySegment<byte> ciphertext)
#endif
// Resize the static buffer to fit
EnsureSize(ref _tmpCryptBuffer, outSize);
if (AesGCMEncryptionNative.IsSupported)
{
var nativeRes = AesGCMEncryptionNative.Decrypt(_keyRaw, ReceiveNonce, ciphertext, new ArraySegment<byte>(_tmpCryptBuffer));
if (nativeRes.Count == 0)
{
_error(TransportError.Unexpected, $"Native Encryption failed. Please check STDERR (or editor log) for the error.");
return new ArraySegment<byte>();
}
return nativeRes;
}
int resultLen;
try
{
@ -537,12 +561,12 @@ private void CompleteExchange(ArraySegment<byte> remotePubKeyRaw, byte[] salt)
Hkdf.Init(new HkdfParameters(sharedSecret, salt, HkdfInfo));
// Allocate a buffer for the output key
byte[] keyRaw = new byte[KeyLength];
_keyRaw = new byte[KeyLength];
// Generate the output keying material
Hkdf.GenerateBytes(keyRaw, 0, keyRaw.Length);
Hkdf.GenerateBytes(_keyRaw, 0, _keyRaw.Length);
KeyParameter key = new KeyParameter(keyRaw);
KeyParameter key = new KeyParameter(_keyRaw);
// generate a starting nonce
_nonce = GenerateSecureBytes(NonceSize);

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Mirror.Transports.Encryption.Native;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Serialization;
@ -38,6 +39,11 @@ public enum ValidationMode
public string EncryptionPublicKeyFingerprint => _credentials?.PublicKeyFingerprint;
public byte[] EncryptionPublicKey => _credentials?.PublicKeySerialized;
public override string ToString()
{
return $"EncryptionTransport(native: {AesGCMEncryptionNative.IsSupported})";
}
private void ServerRemoveFromPending(EncryptedConnection con)
{
for (int i = 0; i < _serverPendingConnections.Count; i++)

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f4ce017555fd48b7974abd511fd855c1
timeCreated: 1708980635

View File

@ -0,0 +1,81 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using UnityEngine;
namespace Mirror.Transports.Encryption.Native
{
public class AesGCMEncryptionNative
{
[SuppressUnmanagedCodeSecurity]
[DllImport("rusty_mirror_encryption", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern byte is_supported();
[SuppressUnmanagedCodeSecurity]
[DllImport("rusty_mirror_encryption", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern uint aes_gcm_encrypt(
UIntPtr key, uint key_size,
UIntPtr nonce, uint nonce_size,
UIntPtr data, uint data_in_size, uint data_capacity);
[SuppressUnmanagedCodeSecurity]
[DllImport("rusty_mirror_encryption", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern uint aes_gcm_decrypt(
UIntPtr key, uint key_size,
UIntPtr nonce, uint nonce_size,
UIntPtr data, uint data_size);
private static bool _supported;
static AesGCMEncryptionNative()
{
try
{
_supported = is_supported() == 1;
}
catch (Exception e)
{
// TODO: silent?
Debug.LogWarning($"Native AES GCM is not supported: {e}");
}
}
public static bool IsSupported => _supported;
public static unsafe ArraySegment<byte> Encrypt(byte[] key, byte[] nonce, ArraySegment<byte> plaintext, ArraySegment<byte> dataOut)
{
Array.Copy(plaintext.Array, plaintext.Offset, dataOut.Array, dataOut.Offset ,plaintext.Count);
fixed (byte* keyPtr = key)
{
fixed (byte* noncePtr = nonce)
{
fixed (byte* dataPtr = dataOut.Array)
{
UIntPtr data = ((UIntPtr)dataPtr) + dataOut.Offset;
uint resultLength = aes_gcm_encrypt(
(UIntPtr)keyPtr, (uint)key.Length,
(UIntPtr)noncePtr, (uint)nonce.Length,
data, (uint)plaintext.Count, (uint)dataOut.Count);
return new ArraySegment<byte>(dataOut.Array, dataOut.Offset, (int)resultLength);
}
}
}
}
public static unsafe ArraySegment<byte> Decrypt(byte[] key, byte[] nonce, ArraySegment<byte> ciphertext, ArraySegment<byte> dataOut)
{
Array.Copy(ciphertext.Array, ciphertext.Offset, dataOut.Array, dataOut.Offset, ciphertext.Count);
fixed (byte* keyPtr = key)
{
fixed (byte* noncePtr = nonce)
{
fixed (byte* dataPtr = dataOut.Array)
{
UIntPtr data = ((UIntPtr)dataPtr) + dataOut.Offset;
uint resultLength = aes_gcm_decrypt(
(UIntPtr)keyPtr, (uint)key.Length,
(UIntPtr)noncePtr, (uint)nonce.Length,
data, (uint)ciphertext.Count);
return new ArraySegment<byte>(dataOut.Array, dataOut.Offset, (int)resultLength);
}
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 62ce567e6de54179a7e71c6173c07074
timeCreated: 1708980733

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Robin Rolf
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3b3ffcf9d4944ce1ba77dc8853552169
timeCreated: 1708988561

View File

@ -0,0 +1,4 @@
# [RustyMirrorEncryption](https://github.com/imerr/RustyMirrorEncryption)
A tiny native library for hardware accelerated AES-GCM encryption and decryption for the EncryptionTransport in [Mirror](https://github.com/MirrorNetworking/Mirror)
Uses [RustCryptos aes_gcm crate](https://github.com/RustCrypto/AEADs/tree/master/aes-gcm) crate

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d335f0e47b24038886233a1c8622cd3
timeCreated: 1708988530

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b590fe1f91b89f7429f7718e41d5c627
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
fileFormatVersion: 2
guid: b76b42eea65583d48a419394df29a98f
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
: Any
second:
enabled: 0
settings:
Exclude Editor: 0
Exclude Linux64: 1
Exclude OSXUniversal: 0
Exclude Win: 1
Exclude Win64: 1
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 1
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Standalone: Linux64
second:
enabled: 0
settings:
CPU: x86_64
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: ARM64
- first:
Standalone: Win
second:
enabled: 0
settings:
CPU: x86
- first:
Standalone: Win64
second:
enabled: 0
settings:
CPU: x86_64
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 52afa74b55d70ff4b944c270cded1621
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 2549b603494fff7469d56bda37b69109
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 1
settings:
DefaultValueInitialized: true
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 12721f4888703aa46b3d1ccf95574933
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
fileFormatVersion: 2
guid: 27733f6020cceb14c8f496b32d0e7fbd
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ab9fb4d0e4db8104ca0b081fdb5ed39b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
fileFormatVersion: 2
guid: 8b444b5607bfebe45b55f3738101e467
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f8232f87006729a4097558e850130da4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
fileFormatVersion: 2
guid: b49434d36bc0ad54db4779b5d09edb63
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cfeeb87517fcd8d43a1e82b9b2bc0ede
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
fileFormatVersion: 2
guid: f52c61e71c4975e46810869fc35f9bae
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
: Any
second:
enabled: 0
settings:
Exclude Editor: 1
Exclude Linux64: 0
Exclude OSXUniversal: 0
Exclude Win: 0
Exclude Win64: 1
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Standalone: Linux64
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: None
- first:
Standalone: Win
second:
enabled: 1
settings:
CPU: x86
- first:
Standalone: Win64
second:
enabled: 0
settings:
CPU: None
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: de0f8585ba061a54aa46ada04c1eb8ea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
fileFormatVersion: 2
guid: 46bfc9351689e844cad51ad69f5e4827
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a4280b4d4efd76e4cbc4e3cf208c350e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 05c6164d5fd90e54e84a20f6c249a9f6
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 1
settings:
DefaultValueInitialized: true
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e8552804ac328fe4cb6d1eabdc6c16e0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
fileFormatVersion: 2
guid: e998ea81dd4a7db4895f9dd0a7b1b851
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
: Any
second:
enabled: 0
settings:
Exclude Editor: 0
Exclude Linux64: 0
Exclude OSXUniversal: 0
Exclude Win: 1
Exclude Win64: 0
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 1
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Standalone: Linux64
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: None
- first:
Standalone: Win
second:
enabled: 0
settings:
CPU: None
- first:
Standalone: Win64
second:
enabled: 1
settings:
CPU: x86_64
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 229ad37adf003944cbe12c48c0cabd39
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,27 @@
fileFormatVersion: 2
guid: 7ec268e885af5d64ab9519ac0026b5a9
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant: