using NUnit.Framework; using ServiceShared.Crypto; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace ServiceOutsideTests.Crypto { public class Curve25519 { [Test] public void SharedKeys() { KeyPair aliceKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); KeyPair bobKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); byte[] aliceSharedKey = aliceKey.GetSharedKey(bobKey.PublicKey); byte[] bobSharedKey = bobKey.GetSharedKey(aliceKey.PublicKey); Assert.AreEqual(aliceSharedKey, bobSharedKey); } [Test] public void AliceSendsToBob() { KeyPair aliceKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); KeyPair bobKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); byte[] aliceSharedKey = aliceKey.GetSharedKey(bobKey.PublicKey); byte[] bobSharedKey = bobKey.GetSharedKey(aliceKey.PublicKey); string key = "a dasdas djakl jdklas jdlkjakldhsajkdhsakdhkajhdkjgahkdgsagdas gdj sadag jdsaj gdg jasdagj dgsa gdsa dsga dgsja gdjagdgasj gjhdga gdja"; string input = "Hello World"; string encrypted = ServiceShared.Crypto.AES.Encrypt(input, aliceSharedKey); string decrypted = ServiceShared.Crypto.AES.Decrypt(encrypted, bobSharedKey); Assert.AreEqual(input, decrypted); } [Test] public void BobSendsToAlice() { KeyPair aliceKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); KeyPair bobKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); byte[] aliceSharedKey = aliceKey.GetSharedKey(bobKey.PublicKey); byte[] bobSharedKey = bobKey.GetSharedKey(aliceKey.PublicKey); string key = "a dasdas djakl jdklas jdlkjakldhsajkdhsakdhkajhdkjgahkdgsagdas gdj sadag jdsaj gdg jasdagj dgsa gdsa dsga dgsja gdjagdgasj gjhdga gdja"; string input = "Hello World"; string encrypted = ServiceShared.Crypto.AES.Encrypt(input, bobSharedKey); string decrypted = ServiceShared.Crypto.AES.Decrypt(encrypted, aliceSharedKey); Assert.AreEqual(input, decrypted); } [Test] public void EncryptDecrypt() { KeyPair aliceKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); KeyPair bobKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); byte[] aliceSharedKey = aliceKey.GetSharedKey(bobKey.PublicKey); byte[] bobSharedKey = bobKey.GetSharedKey(aliceKey.PublicKey); for (int i = 0; i < 1000; i++) { int KeySize = new Random().Next(1, 4096); byte[] random = new byte[KeySize]; RandomNumberGenerator.Fill(random); if(i%2 == 0) { string input = Encoding.UTF8.GetString(random); string encrypted = ServiceShared.Crypto.AES.Encrypt(input, aliceSharedKey); string decrypted = ServiceShared.Crypto.AES.Decrypt(encrypted, bobSharedKey); Assert.AreEqual(input, decrypted); } else { string input = Encoding.UTF8.GetString(random); string encrypted = ServiceShared.Crypto.AES.Encrypt(input, bobSharedKey); string decrypted = ServiceShared.Crypto.AES.Decrypt(encrypted, aliceSharedKey); Assert.AreEqual(input, decrypted); } } } [Test] public void BobVerifiesAlicasSignature() { KeyPair aliceKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); KeyPair bobKey = ServiceShared.Crypto.Curve25519.GenerateKeyPair(); string message = "a dasdas djakl jdklas jdlkjakldhsajkdhsakdhkajhdkjgahkdgsagdas gdj sadag jdsaj gdg jasdagj dgsa gdsa dsga dgsja gdjagdgasj gjhdga gdjaa dasdas djakl jdklas jdlkjakldhsajkdhsakdhk"; string aliceSignature = aliceKey.GetSignature(message); Assert.IsTrue(bobKey.VerifySignature(message, aliceSignature, aliceKey.SigningPublicKey)); } [Test] public void FixedKeys() { byte[] bobDefinedPrivateKey = Convert.FromBase64String("sl+zJ6XehkmHhr9GUgdtNGItpBrWy2y9zqUNkxQ+2nc="); byte[] bobPrivate = ServiceShared.Crypto.Curve25519.ClampPrivateKey(bobDefinedPrivateKey); byte[] bobPublic = ServiceShared.Crypto.Curve25519.GetPublicKey(bobPrivate); string alicePublicKeyBase64 = "Q6eZkHPevsEB7cpoMRQ4TY0ggjdB4etV+X6aOqqeymc="; byte[] alicePublicKey = Convert.FromBase64String(alicePublicKeyBase64); byte[] bobSharedSecret = ServiceShared.Crypto.Curve25519.GetSharedSecret(bobPrivate, alicePublicKey); string bobSharedSecretBase64 = Convert.ToBase64String(bobSharedSecret); byte[] bobSharedKey = HKDF.DeriveKey(HashAlgorithmName.SHA512, bobSharedSecret, 32); string bobSharedKeyBase64 = Convert.ToBase64String(bobSharedKey); string bobSecretMessage = "XhK7GpN6JqbFgSl4h2J6kjpwEi+aFGzTPDehrdF/qvEupBUHYPJI9zOWPhbWBi4IZ2i9"; string bobSecretMessageDecrypted = ServiceShared.Crypto.AES.Decrypt(bobSecretMessage, bobSharedKey); Console.WriteLine("Bob Private Key: " + Convert.ToBase64String(bobPrivate)); Console.WriteLine("Bob Public Key: " + Convert.ToBase64String(bobPublic)); Console.WriteLine("Alice Public Key: " + Convert.ToBase64String(alicePublicKey)); Console.WriteLine("Bob Shared Secret: " + Convert.ToBase64String(bobSharedSecret)); Console.WriteLine("Bob Shared Key(Base64): " + Convert.ToBase64String(bobSharedKey)); Console.WriteLine("Bob Shared Key(ASCII): " + Encoding.ASCII.GetString(bobSharedKey)); Console.WriteLine("Bob Secret Message(Clearly): " + bobSecretMessageDecrypted); Console.WriteLine("Alice HMAC: " + ServiceShared.Crypto.SHA512.HMAC(bobSecretMessage, bobSharedKey)); Assert.IsTrue(ServiceShared.Crypto.SHA512.isValidAuthenticationCode(ServiceShared.Crypto.SHA512.HMAC(bobSecretMessage, bobSharedKey), bobSecretMessage, bobSharedKey)); } [Test] public void VerifyFixedSignature() { string strPrivateKey = "+jkrpn+/3akv6I1AubmC4SX/+ivUNtuOwSynX56lKbw="; string strPublicKey = "XNeY4FNb9GvGP2UA4Qm3luwM5fGmuE1uBESHSoV6DK4="; byte[] privateKey = Convert.FromBase64String(strPrivateKey); byte[] publicKey = Convert.FromBase64String(strPublicKey); byte[] message = Encoding.UTF8.GetBytes("Signature Message"); byte[] signature = ServiceShared.Crypto.Ed25519.Signature(message, privateKey, publicKey); string base64Signature = "4KVCEjEqGajal6LsUOeOeA6NobyPmo0fiRiyCGQ3IcMZtAVUZjOfrkbOubtvL66ARNP5EnrWXHWtP6PpyqeGCg=="; // Convert.ToBase64String(signature); byte[] signatureBytes = Convert.FromBase64String(base64Signature); bool verified = ServiceShared.Crypto.Ed25519.CheckValid(signatureBytes, message, publicKey); Assert.IsTrue(verified); } } }