From 562bba216e8991a42d1fac80eec88897868bb659 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 12 May 2020 03:06:13 -0500 Subject: [PATCH] Implemented DoublePrivateKey --- tests/doublekeyPrivateTest.cs | 76 +++++++++++++++++++ ...oublekeyTest.cs => doublekeyPublicTest.cs} | 36 ++++++++- .../chestcrypto/doublekey/private.cs | 33 +++++++- treasurechest/chestcrypto/doublekey/public.cs | 7 +- 4 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 tests/doublekeyPrivateTest.cs rename tests/{doublekeyTest.cs => doublekeyPublicTest.cs} (58%) diff --git a/tests/doublekeyPrivateTest.cs b/tests/doublekeyPrivateTest.cs new file mode 100644 index 0000000..4845494 --- /dev/null +++ b/tests/doublekeyPrivateTest.cs @@ -0,0 +1,76 @@ +using NUnit.Framework; +using chestcrypto; +using System; +using Sodium; + +namespace DoubleKeyPrivateTests +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestDoublePrivateKeyThrowsOnBadLoad() + { + byte[] invalid = {0}; + byte[] invalid2 = new byte[33]; + for (int i = 0; i < invalid2.Length; i++){ + invalid2[i] = 1; + } + bool success = false; + try{ + new chestcrypto.DoublePrivateKey(invalid); + success = true; + } + catch (chestcrypto.InvalidDoubleKeyException){ + Console.WriteLine("Throws properly for too small array size"); + } + if (success){ + Assert.Fail(); + } + + try{ + new chestcrypto.DoublePrivateKey(invalid2); + success = true; + } + catch (chestcrypto.InvalidDoubleKeyException){ + Console.WriteLine("Throws properly for too large array size"); + } + if (success){ + Assert.Fail(); + } + } + + [Test] + public void TestDoubleKeyLoad() + { + // Test that the combined key loader loads both constructors with the same results + byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PrivateKey; + byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PrivateKey; + + byte[] combinedKey = new byte[signingKey.Length + encryptionKey.Length]; + Buffer.BlockCopy(signingKey, 0, combinedKey, 0, signingKey.Length); + Buffer.BlockCopy(encryptionKey, 0, combinedKey, signingKey.Length, encryptionKey.Length); + + DoublePrivateKey combinedLoad = new chestcrypto.DoublePrivateKey(combinedKey); + DoublePrivateKey parameterLoad = new chestcrypto.DoublePrivateKey(signingKey, encryptionKey); + if (combinedLoad.getRawDouble().Length != parameterLoad.getRawDouble().Length){ + Assert.Fail(); + } + if (combinedLoad.getRawDouble().Length != 96){ + Assert.Fail(); + } + for (int i = 0; i < combinedLoad.getRawDouble().Length; i++){ + if (combinedLoad.getRawDouble()[i] != parameterLoad.getRawDouble()[i]){ + Assert.Fail(); + } + if (combinedLoad.getRawDouble()[i] != combinedKey[i]){ + Assert.Fail(); + } + } + } + } +} \ No newline at end of file diff --git a/tests/doublekeyTest.cs b/tests/doublekeyPublicTest.cs similarity index 58% rename from tests/doublekeyTest.cs rename to tests/doublekeyPublicTest.cs index 62e5f77..d40b9df 100644 --- a/tests/doublekeyTest.cs +++ b/tests/doublekeyPublicTest.cs @@ -3,7 +3,7 @@ using chestcrypto; using System; using Sodium; -namespace DoubleKeyTests +namespace DoubleKeyPublicTests { public class Tests { @@ -13,7 +13,39 @@ namespace DoubleKeyTests } [Test] - public void TestDoublePublicKey() + public void TestDoublePublicKeyThrowsOnBadLoad() + { + byte[] invalid = {0}; + byte[] invalid2 = new byte[33]; + for (int i = 0; i < invalid2.Length; i++){ + invalid2[i] = 1; + } + bool success = false; + try{ + new chestcrypto.DoublePublicKey(invalid); + success = true; + } + catch (chestcrypto.InvalidDoubleKeyException){ + Console.WriteLine("Throws properly for too small array size"); + } + if (success){ + Assert.Fail(); + } + + try{ + new chestcrypto.DoublePublicKey(invalid2); + success = true; + } + catch (chestcrypto.InvalidDoubleKeyException){ + Console.WriteLine("Throws properly for too large array size"); + } + if (success){ + Assert.Fail(); + } + } + + [Test] + public void TestDoublePublicKeyLoad() { // Test that the combined key loader loads both constructors with the same results byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey; diff --git a/treasurechest/chestcrypto/doublekey/private.cs b/treasurechest/chestcrypto/doublekey/private.cs index b964ad3..6b09973 100644 --- a/treasurechest/chestcrypto/doublekey/private.cs +++ b/treasurechest/chestcrypto/doublekey/private.cs @@ -2,13 +2,38 @@ namespace chestcrypto{ public class DoublePrivateKey{ - private byte[] signingprivateKey; - public byte[] encryptprivateKey; + private byte[] signingPrivateKey = new byte[64]; + private byte[] encryptPrivateKey = new byte[32]; + private string signingPrivateKeyString; + private string encryptPrivateKeyString; + + public byte[] getRawDouble(){ + return ByteCombiner.Combine(signingPrivateKey, encryptPrivateKey); + } public DoublePrivateKey(byte[] sign, byte[] encrypt){ - signingprivateKey = sign; - encryptprivateKey = encrypt; + if (sign.Length != 64){ + throw new InvalidDoubleKeyException("Signing private key must be 64 bytes in length."); + } + if (encrypt.Length != 32){ + throw new InvalidDoubleKeyException("Signing private key must be 32 bytes in length."); + } + signingPrivateKey = sign; + encryptPrivateKey = encrypt; + } + + public DoublePrivateKey(byte[] combinedKey){ + if (combinedKey.Length != 96){ + throw new InvalidDoubleKeyException("Invalid key length, must be 96 bytes in length"); + } + for (int i = 0; i < combinedKey.Length; i++){ + if (i < 64){ + signingPrivateKey[i] = combinedKey[i]; + continue; + } + encryptPrivateKey[i - 64] = combinedKey[i]; + } } } diff --git a/treasurechest/chestcrypto/doublekey/public.cs b/treasurechest/chestcrypto/doublekey/public.cs index 97b6a42..1d70181 100644 --- a/treasurechest/chestcrypto/doublekey/public.cs +++ b/treasurechest/chestcrypto/doublekey/public.cs @@ -14,13 +14,18 @@ namespace chestcrypto{ public DoublePublicKey(byte[] sign, byte[] encrypt){ + // Construct double key from two separate byte arrays + if (sign.Length != 32 || encrypt.Length != 32){ + throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); + } signingPublicKey = sign; encryptPublicKey = encrypt; } public DoublePublicKey(byte[] joinedKey){ + // Construct double key from one bytearray if (joinedKey.Length != 64){ - throw new InvalidDoubleKeyException(); + throw new InvalidDoubleKeyException("Invalid length, both keys should be 32 bytes"); } for (int i = 0; i < joinedKey.Length; i++){ if (i < 32){