From bbeaa837a995176ee845f990b19e0c8bd25da917 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 9 May 2020 05:26:38 -0500 Subject: [PATCH] finished standard authenticated pubkey encryption, but no decryption yet --- tests/Curve25519Test.cs | 52 +++++++++++++++++++++++ tests/KeyGenTest.cs | 1 + treasurechest/chestcrypto/bytecombiner.cs | 13 ++++++ treasurechest/chestcrypto/curve25519.cs | 19 +++++++++ treasurechest/chestcrypto/keygen.cs | 9 +--- 5 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 tests/Curve25519Test.cs create mode 100644 treasurechest/chestcrypto/bytecombiner.cs create mode 100644 treasurechest/chestcrypto/curve25519.cs diff --git a/tests/Curve25519Test.cs b/tests/Curve25519Test.cs new file mode 100644 index 0000000..8c1cc09 --- /dev/null +++ b/tests/Curve25519Test.cs @@ -0,0 +1,52 @@ +using NUnit.Framework; +using chestcrypto; +using System; +using System.Text; +using Sodium; + +namespace Curve25519Tests +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestCurve25519Encrypt() + { + var alice = PublicKeyBox.GenerateKeyPair(); + var bob = PublicKeyBox.GenerateKeyPair(); + string message = "Hello World"; + byte[] message_bytes = UTF8Encoding.UTF8.GetBytes(message); + byte[] encrypted_with_nonce = chestcrypto.Curve25519.encrypt(alice.PrivateKey, bob.PublicKey, message_bytes); + byte[] used_nonce = new byte[24]; + + byte[] encrypted_without_nonce = new byte[encrypted_with_nonce.Length - 24]; + + int counter = 0; + Console.WriteLine(encrypted_without_nonce.Length); + for (int i = 24; i < encrypted_with_nonce.Length; i++){ + //Console.WriteLine(counter); + encrypted_without_nonce[counter] = encrypted_with_nonce[i]; + counter += 1; + } + + for (int i = 0; i < chestcrypto.Curve25519.NONCE_BYTE_AMOUNT; i++){ + //Console.WriteLine(i); + used_nonce[i] = encrypted_with_nonce[i]; + } + for (int i = 0; i < chestcrypto.Curve25519.NONCE_BYTE_AMOUNT; i++){ + if (used_nonce[i] != encrypted_with_nonce[i]){ + Assert.Fail(); + } + } + byte[] decrypted = PublicKeyBox.Open(encrypted_without_nonce, used_nonce, bob.PrivateKey, alice.PublicKey); + if (!Encoding.UTF8.GetString(decrypted, 0, decrypted.Length).Equals(message)){ + Console.WriteLine(Encoding.UTF8.GetString(decrypted, 0, decrypted.Length)); + Assert.Fail(); + } + } + } +} \ No newline at end of file diff --git a/tests/KeyGenTest.cs b/tests/KeyGenTest.cs index f3ededf..1be3b17 100644 --- a/tests/KeyGenTest.cs +++ b/tests/KeyGenTest.cs @@ -23,5 +23,6 @@ namespace tests } Assert.Pass(); } + } } \ No newline at end of file diff --git a/treasurechest/chestcrypto/bytecombiner.cs b/treasurechest/chestcrypto/bytecombiner.cs new file mode 100644 index 0000000..94f23cc --- /dev/null +++ b/treasurechest/chestcrypto/bytecombiner.cs @@ -0,0 +1,13 @@ +using System; + +namespace chestcrypto{ + internal class ByteCombiner{ + internal static byte[] Combine(byte[] first, byte[] second) + { + byte[] bytes = new byte[first.Length + second.Length]; + Buffer.BlockCopy(first, 0, bytes, 0, first.Length); + Buffer.BlockCopy(second, 0, bytes, first.Length, second.Length); + return bytes; + } + } +} \ No newline at end of file diff --git a/treasurechest/chestcrypto/curve25519.cs b/treasurechest/chestcrypto/curve25519.cs new file mode 100644 index 0000000..83637a9 --- /dev/null +++ b/treasurechest/chestcrypto/curve25519.cs @@ -0,0 +1,19 @@ +using Sodium; + +namespace chestcrypto { + public class Curve25519{ + + public static int NONCE_BYTE_AMOUNT = 24; + public static byte[] encrypt(byte[] privkey, byte[] pubkey, byte[] message){ + byte[] nonce = Sodium.PublicKeyBox.GenerateNonce(); + return ByteCombiner.Combine + (nonce, + Sodium.PublicKeyBox.Create( + message, + nonce, + privkey, + pubkey + )); + } + } +} \ No newline at end of file diff --git a/treasurechest/chestcrypto/keygen.cs b/treasurechest/chestcrypto/keygen.cs index ff853b0..5c644dc 100644 --- a/treasurechest/chestcrypto/keygen.cs +++ b/treasurechest/chestcrypto/keygen.cs @@ -21,18 +21,11 @@ internal class Curve25519KeyGenerator{ namespace chestcrypto{ public class PrivateKeyGenerator{ - private static byte[] Combine(byte[] first, byte[] second) - { - byte[] bytes = new byte[first.Length + second.Length]; - Buffer.BlockCopy(first, 0, bytes, 0, first.Length); - Buffer.BlockCopy(second, 0, bytes, first.Length, second.Length); - return bytes; - } public static byte[] generate() { byte[] ed25519 = Ed25519KeyGenerator.generator(); byte[] curve25519 = Curve25519KeyGenerator.generator(); - byte[] key = Combine(ed25519, curve25519); + byte[] key = chestcrypto.ByteCombiner.Combine(ed25519, curve25519); Array.Clear(ed25519, 0, ed25519.Length); Array.Clear(curve25519, 0, curve25519.Length); return key;