From 1aeb224be8cdc75d71627107e5d195277755c3cc Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 25 May 2020 00:05:49 -0500 Subject: [PATCH] added entropy calculator --- cli/ui/interfaces/encrypt.cs | 3 ++ tests/kdf/symmetricKDFTest.cs | 0 tests/testEntropy.cs | 31 ++++++++++++++++ treasurechest/EntropyCal.cs | 37 +++++++++++++++++++ .../key-derivation/deriveSymmetric.cs | 26 +++++++++++++ .../symmetric-crypto/encryptwithpassphrase.cs | 19 ++++++++++ 6 files changed, 116 insertions(+) create mode 100644 tests/kdf/symmetricKDFTest.cs create mode 100644 tests/testEntropy.cs create mode 100644 treasurechest/EntropyCal.cs create mode 100644 treasurechest/chestcrypto/key-derivation/deriveSymmetric.cs create mode 100644 treasurechest/chestcrypto/symmetric-crypto/encryptwithpassphrase.cs diff --git a/cli/ui/interfaces/encrypt.cs b/cli/ui/interfaces/encrypt.cs index f9e2f1c..a58c199 100644 --- a/cli/ui/interfaces/encrypt.cs +++ b/cli/ui/interfaces/encrypt.cs @@ -2,6 +2,7 @@ using System.IO; using System; using treasurechest.STDIOWrapper; using getpass; +using ShannonEntropyCal; namespace treasurechestCLI{ @@ -11,6 +12,7 @@ namespace treasurechestCLI{ int counter = 1; string message; string passphrase; + string encrypted; translations.Strings strings = new translations.Strings(); @@ -52,6 +54,7 @@ namespace treasurechestCLI{ continue; } passphrase = GetPass.getPass(strings.PASSPHRASE); + encrypted = } else if (choice == encryptMenuOptions.Length){ break; diff --git a/tests/kdf/symmetricKDFTest.cs b/tests/kdf/symmetricKDFTest.cs new file mode 100644 index 0000000..e69de29 diff --git a/tests/testEntropy.cs b/tests/testEntropy.cs new file mode 100644 index 0000000..6acb391 --- /dev/null +++ b/tests/testEntropy.cs @@ -0,0 +1,31 @@ +using NUnit.Framework; +using ShannonEntropyCal; +using System; + +namespace entropytests +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestShannonEntropyLow() + { + string low = "abc123"; + if (EntropyCal.EntropyValue(low) > 3.0){ + Assert.Fail(); + } + } + + [Test] + public void TestShannonEntropyHigh() + { + string high = "ý¼¸²>æ{£¤@TçKA¥£åKPk.rPoSo}fÑú½§rêÆÀðke(9/¹©ÔRqTãîý`Çóè°T²þµ)ÁÄÒÙr7éijÈ·Ñø{.8'ü*=Å.ôþSø&ÏßP9D}\"û+îÏæ¼aZ-'ûÐмÊZh5³ÒD®/YÙ¤(a·]Ðf"; + Assert.IsTrue(EntropyCal.EntropyValue(high) > 6.3); + } + + } +} \ No newline at end of file diff --git a/treasurechest/EntropyCal.cs b/treasurechest/EntropyCal.cs new file mode 100644 index 0000000..3887112 --- /dev/null +++ b/treasurechest/EntropyCal.cs @@ -0,0 +1,37 @@ +// Taken from https://github.com/Kaynn-Cahya/Shannon-Entropy with no license given. +// Since this was published on nuget, i am assuming the author is ok with it being used in open source +using System; +using System.Collections.Generic; +using System.Linq; + +// Test: testEntropy.cs +namespace ShannonEntropyCal +{ + public class EntropyCal + { + + public static double EntropyValue(string message) + { + Dictionary K = message.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count()); + double EntropyValue = 0; + foreach (var character in K) + { + double PR = character.Value / (double) message.Length; + EntropyValue -= PR * Math.Log(PR, 2); + } + return EntropyValue; + } + + public static double EntropyBits(string message) + { + Dictionary K = message.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count()); + double EntropyValue = 0; + foreach (var character in K) + { + double PR = character.Value / (double) message.Length; + EntropyValue -= PR * Math.Log(PR, 2); + } + return Math.Ceiling(EntropyValue) * message.Length; + } + } +} diff --git a/treasurechest/chestcrypto/key-derivation/deriveSymmetric.cs b/treasurechest/chestcrypto/key-derivation/deriveSymmetric.cs new file mode 100644 index 0000000..1ae637c --- /dev/null +++ b/treasurechest/chestcrypto/key-derivation/deriveSymmetric.cs @@ -0,0 +1,26 @@ + +using System.Text; +using Sodium; + +namespace chestcrypto{ + + namespace kdf{ + public class DeterministicSymmetricKey{ + // Test + public static byte[] generate(string passphrase, bool extraSensitive=false){ + var nonce = SecretBox.GenerateNonce(); + int strength = 2; + if (extraSensitive){ + strength = 3; + } + return PasswordHash.ArgonHashBinary(Encoding.UTF8.GetBytes(passphrase), // Passphrase converted to bytes + PasswordHash.ArgonGenerateSalt(), // Salt + strength, strength, + 32); + } + + } + + } + +} diff --git a/treasurechest/chestcrypto/symmetric-crypto/encryptwithpassphrase.cs b/treasurechest/chestcrypto/symmetric-crypto/encryptwithpassphrase.cs new file mode 100644 index 0000000..9b912ff --- /dev/null +++ b/treasurechest/chestcrypto/symmetric-crypto/encryptwithpassphrase.cs @@ -0,0 +1,19 @@ +using Sodium; +using System; +using chestcrypto.kdf; +namespace treasurechest{ + + namespace symmetric{ + + public class EncryptWithPassphrase{ + /* Class name is somewhat misleading as we actually derive a key from a string pass and use the key for secret key crypto*/ + public static byte[] encrypt(byte[] data, string passphrase, bool extraSensitive = false){ + byte[] key = DeterministicSymmetricKey.generate(passphrase, extraSensitive); + return SecretBox.Create(data, SecretBox.GenerateNonce(), key); + } + + } + + } + +} \ No newline at end of file