Skip to content

Repository files navigation

MLAPI.Cryptography

MLAPI.Cryptography is a Unity friendly crypto library to fill the missing features of Unity's Mono runtime framework.

Currently it offers a BigInt, ECDHE, ECDHE_RSA and EllipticCurve implementation. Note that MLAPI.Cryptography is NOT designed to be an extensive crypto library such as NaCL or replace the .NET Framework. It's simply there to fill the missing gaps in the Unity Engine. Behind the scenes, MLAPI.Cryptography will use as much of the avalible .NET surface as possible. An example of this is the ECDHE_RSA implementation which uses MLAPI.Cryptographys BigInt, EllipticCurve and DiffieHellman implementations while it uses .NET's RSA implementation.

ECDHE Usage

// Both create their instancesECDiffieHellmanserverDiffie=newECDiffieHellman();ECDiffieHellmanclientDiffie=newECDiffieHellman();// Exchange publics/* START TRANSMISSION */byte[]serverPublic=serverDiffie.GetPublicKey();byte[]clientPublic=clientDiffie.GetPublicKey();/* END TRANSMISSION */// Calculate sharedbyte[]key1=serverDiffie.GetSharedSecretRaw(clientPublic);byte[]key2=clientDiffie.GetSharedSecretRaw(serverPublic);

ECDHERSA Usage

// Key pairsRSAParametersprivateKey;RSAParameterspublicKey;// Generate keys, you can use X509Certificate2 instead of raw RSA keys.using(RSACryptoServiceProviderrsaGen=newRSACryptoServiceProvider(2048)){privateKey=rsaGen.ExportParameters(true);publicKey=rsaGen.ExportParameters(false);}using(RSACryptoServiceProviderserverRSA=newRSACryptoServiceProvider())using(RSACryptoServiceProviderclientRSA=newRSACryptoServiceProvider()){serverRSA.ImportParameters(privateKey);clientRSA.ImportParameters(publicKey);// Both create their instances, constructor can take certificate instead or RSA key.ECDiffieHellmanRSAserverDiffie=newECDiffieHellmanRSA(serverRSA);ECDiffieHellmanRSAclientDiffie=newECDiffieHellmanRSA(clientRSA);// Exchange publics/* START TRANSMISSION */byte[]serverPublic=serverDiffie.GetSecurePublicPart();byte[]clientPublic=clientDiffie.GetSecurePublicPart();/* END TRANSMISSION */// Calculate sharedbyte[]key1=serverDiffie.GetVerifiedSharedPart(clientPublic);byte[]key2=clientDiffie.GetVerifiedSharedPart(serverPublic);}

Timing Side Channel Attack Prevention

byte[]array1=newbyte[120];byte[]array2=newbyte[120];array[50]=67;// This comparison will take constant time, no matter where the diff is (if any).boolequal=ComparisonUtils.ConstTimeArrayEqual(array1,array2);

About

Unity friendly library for networked cryptography.

Resources

Stars

13 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages