finished session encrypt/decrypt implementation

This commit is contained in:
Kevin Froman 2020-05-31 17:48:58 -05:00
parent 915653b035
commit ccf406a0b3
3 changed files with 60 additions and 13 deletions

View File

@ -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
);
}
}

View File

@ -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();
}
}
}

View File

@ -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);