From ccf406a0b344a9ed82518b400bf95d337aa18117 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 31 May 2020 17:48:58 -0500 Subject: [PATCH] finished session encrypt/decrypt implementation --- tests/session/testSessionEncrypt.cs | 56 ++++++++++++++----- .../chestcrypto/session/crypto/encrypt.cs | 15 +++++ treasurechest/chestcrypto/session/session.cs | 2 + 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/tests/session/testSessionEncrypt.cs b/tests/session/testSessionEncrypt.cs index 761aa23..7afc5c6 100644 --- a/tests/session/testSessionEncrypt.cs +++ b/tests/session/testSessionEncrypt.cs @@ -20,21 +20,51 @@ namespace sessionTestEncrypt public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;} [Test] - public void TestEncrypt(){ - var pair1 = PublicKeyBox.GenerateKeyPair(); - byte[] publicK = pair1.PublicKey; - byte[] privateK = pair1.PrivateKey; - var pair = PublicKeyBox.GenerateKeyPair(); - byte[] privKey = pair.PrivateKey; - byte[] pubKey = pair.PublicKey; + public void TestDecrypt(){ + var us = PublicKeyBox.GenerateKeyPair(); + var them = PublicKeyBox.GenerateKeyPair(); byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend"); - Session session = new Session(privateK, publicK, true, 5); - session.setMinimumKeyExpireSeconds(10); - session.setMessageDelay((long) 25); - session.addPublic(pubKey, getFutureTime(100)); + Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); + var ourNew = PublicKeyBox.GenerateKeyPair(); + session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); + byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); + Assert.AreEqual( + SessionCrypto.decrypt(session, encrypted), + message + ); + } + + [Test] + public void TestDecryptOlderKey(){ + var us = PublicKeyBox.GenerateKeyPair(); + var them = PublicKeyBox.GenerateKeyPair(); + byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend"); + Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); + var ourNew = PublicKeyBox.GenerateKeyPair(); + var ourNew2 = PublicKeyBox.GenerateKeyPair(); + session.addPrivate(ourNew.PrivateKey, getFutureTime(1000)); + byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message); + session.addPrivate(ourNew2.PrivateKey, getFutureTime(1005)); + Assert.AreEqual( + SessionCrypto.decrypt(session, encrypted), + message + ); + } + + [Test] + public void TestEncrypt(){ + // Test ephemeral encrypt + var us = PublicKeyBox.GenerateKeyPair(); + var them = PublicKeyBox.GenerateKeyPair(); + var ephemeral = PublicKeyBox.GenerateKeyPair(); + byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend"); + Session session = new Session(us.PrivateKey, them.PublicKey, true, 5); + session.addPublic(ephemeral.PublicKey, getFutureTime(1000)); byte[] encrypted = SessionCrypto.encrypt(session, message); - byte[] decrypted = Curve25519.decrypt(privKey, publicK, encrypted); - Assert.AreEqual(decrypted, message); + Assert.AreEqual( + Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted), + message + ); } } diff --git a/treasurechest/chestcrypto/session/crypto/encrypt.cs b/treasurechest/chestcrypto/session/crypto/encrypt.cs index 7dfdcf7..20e36d2 100644 --- a/treasurechest/chestcrypto/session/crypto/encrypt.cs +++ b/treasurechest/chestcrypto/session/crypto/encrypt.cs @@ -13,6 +13,21 @@ namespace chestcrypto.session.crypto{ return Curve25519.encrypt(privateKey, publicKey, message); } + public static byte[] decrypt(Session activeSession, byte[] ciphertext){ + byte[] publicKey = activeSession.getTheirMasterPublic(); + byte[] decrypted; + byte[] privateKey; + foreach (var privKey in activeSession.getAllPrivateKeys()){ + try{ + privateKey = privKey.Item2; + decrypted = Curve25519.decrypt(privateKey, publicKey, ciphertext); + return decrypted; + } + catch(System.Security.Cryptography.CryptographicException){} + } + throw new System.Security.Cryptography.CryptographicException(); + } + } } \ No newline at end of file diff --git a/treasurechest/chestcrypto/session/session.cs b/treasurechest/chestcrypto/session/session.cs index 24d7e0a..068da4b 100644 --- a/treasurechest/chestcrypto/session/session.cs +++ b/treasurechest/chestcrypto/session/session.cs @@ -99,6 +99,8 @@ namespace chestcrypto{ return key.Item2; } + public (long, byte[])[] getAllPrivateKeys(){return ourPrivateKeys.ToArray();} + public void addPrivate(byte[] privateKey, long timestamp){ validateKeyLength(privateKey); validateTimestamp(timestamp);