diff --git a/session.txt b/session.txt index fc8cfd3..ef857d3 100644 --- a/session.txt +++ b/session.txt @@ -3,13 +3,5 @@ Session(private master key, public master peer key, bool strict, int timePerKey) List pubkeys List privkeys -public: -encrypt() -decrypt() -deleteExpired() -byte[] SessionMessage(byte[] data) - -getNewKey() - diff --git a/tests/session/testSession.cs b/tests/session/testSession.cs new file mode 100644 index 0000000..c885e4c --- /dev/null +++ b/tests/session/testSession.cs @@ -0,0 +1,73 @@ +using NUnit.Framework; +using System; +using System.Linq; +using chestcrypto.session; +using chestcrypto.exceptions; +using Sodium; + +namespace sessionTests +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + public long getFutureTime(int seconds){ + return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds; + } + + [Test] + public void TestSessionAddValidPublic(){ + byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey; + byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey; + byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey; + Session session = new Session(privateK, publicK, true); + session.addPublic(newK, getFutureTime(61)); + Assert.IsTrue(Enumerable.SequenceEqual(newK, session.getLatestPublicKey())); + } + + [Test] + public void TestSessionAddPublicInvalidTime(){ + byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey; + byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey; + byte[] newK = PublicKeyBox.GenerateKeyPair().PublicKey; + Session session = new Session(privateK, publicK, true); + try{ + session.addPublic(newK, getFutureTime(-1)); + } + catch(System.ArgumentOutOfRangeException){ + return; + } + Assert.Fail(); + } + + [Test] + public void TestSessionConstructor() + { + byte[] publicK = PublicKeyBox.GenerateKeyPair().PublicKey; + byte[] privateK = PublicKeyBox.GenerateKeyPair().PrivateKey; + Session session = new Session(privateK, publicK, true); + byte[] invalid = {0, 0, 0}; + + try{ + new Session(invalid, publicK, true); + } + catch(InvalidKeyLength){ + goto secondAssert; + } + Assert.Fail(); + secondAssert: + try{ + new Session(privateK, invalid, true); + } + catch(InvalidKeyLength){ + return; + } + Assert.Fail(); + } + + + } +} \ No newline at end of file diff --git a/treasurechest/chestcrypto/session/message.cs b/treasurechest/chestcrypto/session/message.cs new file mode 100644 index 0000000..e69de29 diff --git a/treasurechest/chestcrypto/session/session.cs b/treasurechest/chestcrypto/session/session.cs new file mode 100644 index 0000000..607a76a --- /dev/null +++ b/treasurechest/chestcrypto/session/session.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System; +using chestcrypto.exceptions; + +namespace chestcrypto{ + + namespace session{ + + internal class Session{ + + // Create List of tuples(time, byte[]) + // Where the tuple contains a time stamp for expiry and a ed25519 key + private List<(long, byte[])> ourPrivateKeys; + private List<(long, byte[])> theirPublicKeys; + + private byte[] ourMasterPrivateKey; + private byte[] theirMasterPublicKey; + private bool strictMode; + private const int minimumKeyExpireSeconds = 60; + + public Session(byte[] masterPrivate, byte[] masterPublic, bool strictMode){ + if(masterPrivate.Length != 32 | masterPublic.Length != 32){throw new InvalidKeyLength();} + ourMasterPrivateKey = masterPrivate; + theirMasterPublicKey = masterPublic; + this.strictMode = strictMode; + ourPrivateKeys = new List<(long, byte[])>(); + theirPublicKeys = new List<(long, byte[])>(); + + } + + public void addPublic(byte[] publicKey, long timestamp){ + if (timestamp < DateTimeOffset.UtcNow.ToUnixTimeSeconds() + minimumKeyExpireSeconds){ + throw new ArgumentOutOfRangeException(); + } + theirPublicKeys.Add((timestamp, publicKey)); + } + public byte[] getLatestPublicKey(){return theirPublicKeys[theirPublicKeys.Count - 1].Item2;} + + + } + + } + +} \ No newline at end of file diff --git a/treasurechest/keyring/keyring.cs b/treasurechest/keyring/keyring.cs index b9a60a2..97858f4 100644 --- a/treasurechest/keyring/keyring.cs +++ b/treasurechest/keyring/keyring.cs @@ -7,7 +7,7 @@ namespace keyring{ public class KeyRing { - private List publicIdentities; + //private List publicIdentities; private List privateIdentities; public KeyRing(){