feat: add E2EE DMs, group chats, local serversettings.json, and security/perf fixes

This commit is contained in:
tyuXX 2026-10-17 13:16:42 +00:00
parent b60b5a0774
commit 3b52de20d1
33 changed files with 5107 additions and 366 deletions

View file

@ -58,4 +58,59 @@ public class CryptoTests
Assert.Equal(64, token2.Length);
Assert.NotEqual(token1, token2);
}
[Fact]
public void E2eeCrypto_KeyPairGeneration_And_SharedSecretAgreement_ShouldMatch()
{
var (pubAlice, privAlice) = E2eeCrypto.GenerateKeyPair();
var (pubBob, privBob) = E2eeCrypto.GenerateKeyPair();
byte[] sharedAlice = E2eeCrypto.DeriveSharedAesKey(privAlice, pubBob);
byte[] sharedBob = E2eeCrypto.DeriveSharedAesKey(privBob, pubAlice);
Assert.Equal(32, sharedAlice.Length);
Assert.Equal(sharedAlice, sharedBob);
}
[Fact]
public void E2eeCrypto_EncryptAndDecryptMessage_ShouldRoundTrip_AndHidePlaintext()
{
var (pubAlice, privAlice) = E2eeCrypto.GenerateKeyPair();
var (pubBob, privBob) = E2eeCrypto.GenerateKeyPair();
var (_, privMallory) = E2eeCrypto.GenerateKeyPair();
string secretText = "Confidential E2EE Direct Message between Alice and Bob!";
string encrypted = E2eeCrypto.EncryptMessage(secretText, privAlice, pubBob);
Assert.True(E2eeCrypto.IsEncryptedPayload(encrypted));
Assert.DoesNotContain("Confidential", encrypted);
// Bob can decrypt using his private key and Alice's public key
string decryptedByBob = E2eeCrypto.DecryptMessage(encrypted, privBob, pubAlice);
Assert.Equal(secretText, decryptedByBob);
// Alice can also decrypt her own sent message from history using her private key and Bob's public key
string decryptedByAlice = E2eeCrypto.DecryptMessage(encrypted, privAlice, pubBob);
Assert.Equal(secretText, decryptedByAlice);
// Mallory cannot decrypt
string decryptedByMallory = E2eeCrypto.DecryptMessage(encrypted, privMallory, pubAlice);
Assert.NotEqual(secretText, decryptedByMallory);
}
[Fact]
public void E2eeCrypto_WrapAndUnwrapPrivateKey_ShouldRestoreExactPrivateKey()
{
var (_, privKey) = E2eeCrypto.GenerateKeyPair();
string wrapped = E2eeCrypto.WrapPrivateKey(privKey, "AlicePassword123!", "Alice");
Assert.StartsWith("WRAP:v1:", wrapped);
Assert.DoesNotContain(privKey, wrapped);
string? unwrapped = E2eeCrypto.UnwrapPrivateKey(wrapped, "AlicePassword123!", "alice");
Assert.Equal(privKey, unwrapped);
string? wrongPassword = E2eeCrypto.UnwrapPrivateKey(wrapped, "WrongPassword", "Alice");
Assert.Null(wrongPassword);
}
}