treasurechest/tests/session/testSessionEncrypt.cs

71 lines
2.6 KiB
C#
Raw Normal View History

2020-05-30 08:06:08 +00:00
using NUnit.Framework;
using System;
using System.Linq;
2020-05-30 23:00:49 +00:00
using System.Text;
2020-05-30 08:06:08 +00:00
using chestcrypto.session;
2020-05-30 23:00:49 +00:00
using chestcrypto.session.crypto;
2020-05-30 08:06:08 +00:00
using chestcrypto.exceptions;
2020-05-30 23:44:02 +00:00
using chestcrypto;
2020-05-30 08:06:08 +00:00
using Sodium;
namespace sessionTestEncrypt
{
public class Tests
{
[SetUp]
public void Setup()
{
}
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
[Test]
public void TestDecrypt(){
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();
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
);
}
2020-05-30 08:06:08 +00:00
[Test]
public void TestEncrypt(){
// Test ephemeral encrypt
var us = PublicKeyBox.GenerateKeyPair();
var them = PublicKeyBox.GenerateKeyPair();
var ephemeral = PublicKeyBox.GenerateKeyPair();
2020-05-30 23:00:49 +00:00
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
Session session = new Session(us.PrivateKey, them.PublicKey, true, 5);
session.addPublic(ephemeral.PublicKey, getFutureTime(1000));
2020-05-30 23:00:49 +00:00
byte[] encrypted = SessionCrypto.encrypt(session, message);
Assert.AreEqual(
Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted),
message
);
2020-05-30 08:06:08 +00:00
}
}
}