diff --git a/cli/cli.csproj b/cli/cli.csproj index d224f3c..65ee111 100644 --- a/cli/cli.csproj +++ b/cli/cli.csproj @@ -5,13 +5,14 @@ netcoreapp3.1 - - - + + + + - - + + diff --git a/cli/translations/spanish.cs b/cli/translations/spanish.cs index efd79ec..da50405 100644 --- a/cli/translations/spanish.cs +++ b/cli/translations/spanish.cs @@ -11,12 +11,13 @@ namespace treasurechestCLI { stringInst.EXIT = "Salir de la aplicación"; stringInst.MAIN_MENU_ENCRYPT = "Encriptar"; stringInst.MAIN_MENU_DECRYPT = "Desencriptar"; + stringInst.MAIN_MENU_KEYRING = "Gestionar contactos"; stringInst.INVALID_OPTION = "Opción inválida"; stringInst.MAIN_MENU_SELECT_INTEGER = "Ingrese un número entero desde el menú"; stringInst.RETURN_TO_PREVIOUS_MENU = "Menú anterior"; stringInst.ENCRYPT_MENU_ENCRYPT_MESSAGE = "Cifrar texto"; stringInst.ENCRYPT_MENU_ENCRYPT_FILE = "Cifrar archivo"; - stringInst.ENCRYPT_MENU_USE_PASSPHRASE = "Usar frase de contraseña"; + stringInst.ENCRYPT_MENU_USE_PASSPHRASE = "Use mnemotécnico"; stringInst.ENCRYPT_MENU_USE_PUBKEY = "Usar clave pública"; stringInst.ENTER_MESSAGE_UNTIL_DONE = "Ingrese su mensaje y termine con -q en una nueva línea."; stringInst.PASSPHRASE = "Frase de contraseña"; diff --git a/cli/translations/text.cs b/cli/translations/text.cs index 10e93ed..762afac 100644 --- a/cli/translations/text.cs +++ b/cli/translations/text.cs @@ -13,6 +13,7 @@ namespace treasurechestCLI { public string EXIT; public string MAIN_MENU_ENCRYPT; public string MAIN_MENU_DECRYPT; + public string MAIN_MENU_KEYRING; public string INVALID_OPTION; public string MAIN_MENU_SELECT_INTEGER; public string RETURN_TO_PREVIOUS_MENU; @@ -37,12 +38,13 @@ namespace treasurechestCLI { EXIT = "Exit application"; MAIN_MENU_ENCRYPT = "Encrypt"; MAIN_MENU_DECRYPT = "Decrypt"; + MAIN_MENU_KEYRING = "Manage contacts"; INVALID_OPTION = "Invalid option"; MAIN_MENU_SELECT_INTEGER = "Enter an integer from the menu"; RETURN_TO_PREVIOUS_MENU = "Previous menu"; ENCRYPT_MENU_ENCRYPT_MESSAGE = "Encrypt text"; ENCRYPT_MENU_ENCRYPT_FILE = "Encrypt file"; - ENCRYPT_MENU_USE_PASSPHRASE = "Use passphrase"; + ENCRYPT_MENU_USE_PASSPHRASE = "Use mnemonic"; ENCRYPT_MENU_USE_PUBKEY = "Use public key"; ENTER_MESSAGE_UNTIL_DONE = "Enter your message and finish with -q on a new line."; PASSPHRASE = "Passphrase"; diff --git a/cli/ui/interfaces/encrypt.cs b/cli/ui/interfaces/encrypt/encrypt.cs similarity index 64% rename from cli/ui/interfaces/encrypt.cs rename to cli/ui/interfaces/encrypt/encrypt.cs index 9a45874..6d95fa5 100644 --- a/cli/ui/interfaces/encrypt.cs +++ b/cli/ui/interfaces/encrypt/encrypt.cs @@ -4,7 +4,7 @@ using System.Text; using Sodium; using niceware; -using chestcrypto.simplepack; +using simplepack; using chestcrypto.symmetric; using treasurechest.STDIOWrapper; @@ -12,12 +12,30 @@ using treasurechest.STDIOWrapper; namespace treasurechestCLI{ internal class EncryptMessageInterface{ + private static void EncryptWithMnemonic(){ + byte[] key = new byte[32]; // Key has to be 32 bytes in size + byte[] message; // Plaintext + string encrypted; // Ciphertext will be encoded with SimplePack. + try { + message = UTF8Encoding.UTF8.GetBytes(GetMessage.getTypedMessage()); + } + catch(System.NullReferenceException){ + return; + } + SimplePack packer = new SimplePack("treasure chest-message ", " end treasure chest message."); + + key = SecretBox.GenerateKey(); + encrypted = packer.encode(Symmetric.encrypt(message, key)); + STDIO.O(encrypted); + foreach (string word in Niceware.ToPassphrase(key)){ + Console.Write(word + " "); + } + STDIO.O(""); + } + public static void EncryptMessage(){ int choice = 0; int counter = 1; - byte[] key = new byte[32]; - byte[] message; - string encrypted; translations.Strings strings = new translations.Strings(); @@ -52,23 +70,12 @@ namespace treasurechestCLI{ choice = encryptMenuOptions.Length; } if (choice == 1){ - try { - message = UTF8Encoding.UTF8.GetBytes(GetMessage.getTypedMessage()); - } - catch(System.NullReferenceException){ - continue; - } - - key = SecretBox.GenerateKey(); - encrypted = SimplePack.pack(Symmetric.encrypt(message, key)); - STDIO.O(encrypted); - foreach (string word in Niceware.ToPassphrase(key)){ - Console.Write(word + " "); - } - STDIO.O(""); + EncryptWithMnemonic(); + } + else if (choice == 2){ } - else if (choice == encryptMenuOptions.Length){ + else if (choice == 3){ break; } diff --git a/cli/ui/interfaces/keyentry.cs b/cli/ui/interfaces/keyentry.cs new file mode 100644 index 0000000..8b36d5a --- /dev/null +++ b/cli/ui/interfaces/keyentry.cs @@ -0,0 +1,5 @@ +namespace treasurechestCLI{ + + + +} \ No newline at end of file diff --git a/cli/ui/menus/KeyRingMenu.cs b/cli/ui/menus/KeyRingMenu.cs new file mode 100644 index 0000000..e69de29 diff --git a/cli/ui/menus/MainMenu.cs b/cli/ui/menus/MainMenu.cs index dc6ef92..0229e20 100644 --- a/cli/ui/menus/MainMenu.cs +++ b/cli/ui/menus/MainMenu.cs @@ -15,7 +15,7 @@ namespace treasurechestCLI internal void showMenu(){ translations.Strings strings = new translations.Strings(); - string[] mainMenuOptions = {strings.MAIN_MENU_ENCRYPT, strings.MAIN_MENU_DECRYPT, strings.EXIT}; + string[] mainMenuOptions = {strings.MAIN_MENU_ENCRYPT, strings.MAIN_MENU_DECRYPT, strings.MAIN_MENU_KEYRING, strings.EXIT}; STDIO.O(strings.WELCOME); int counter = 1; int choice = 0; diff --git a/tests/identity/testPrivate.cs b/tests/identity/testPrivate.cs new file mode 100644 index 0000000..43dad68 --- /dev/null +++ b/tests/identity/testPrivate.cs @@ -0,0 +1,63 @@ +using NUnit.Framework; +using chestcrypto.identity; +using chestcrypto; +using System; +using System.Linq; +using Sodium; + +namespace PrivateIndentityTest +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestPrivateIdentityGetDoublePrivateKey(){ + 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); + + PrivateIdentity iden = new PrivateIdentity(combinedLoad, "Picard"); + + Assert.IsTrue(Enumerable.SequenceEqual(iden.getPrivateKey().getRawDouble(), combinedLoad.getRawDouble())); + + } + + [Test] + public void TestPrivateIdentityConstructor() + { + 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); + + PrivateIdentity iden = new PrivateIdentity(combinedLoad, "Picard"); + Assert.AreEqual(iden.getName(), "Picard"); + Assert.AreEqual(iden.getNote(), ""); + + PrivateIdentity iden2 = new PrivateIdentity(combinedLoad, "Picard2", "test"); + Assert.AreEqual(iden2.getName(), "Picard2"); + Assert.AreEqual(iden2.getNote(), "test"); + } + + [Test] + public void TestPrivateIdenToPublic() + { + + } + + + } +} \ No newline at end of file diff --git a/tests/keyring/add.cs b/tests/keyring/add.cs deleted file mode 100644 index 2b570a4..0000000 --- a/tests/keyring/add.cs +++ /dev/null @@ -1,83 +0,0 @@ -using NUnit.Framework; -using System; -using System.IO; -using Sodium; -using System.Collections.Generic; -using keyring; -using chestcrypto; -using chestcrypto.exceptions; - -namespace KeyRingTests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void TestKeyRingStoreNoDupe(){ - string tempFile = Path.GetTempFileName(); - - DoublePublicKey getKey(){ - KeyRing keyRing = new KeyRing(); - byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey; - byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey; - - 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); - DoublePublicKey combo = new DoublePublicKey(signingKey, encryptionKey); - return combo; - } - DoublePublicKey combo = getKey(); - KeyRing keyRing = new KeyRing(); - keyRing.addPublicKey(combo); - try{ - keyRing.addPublicKey(combo); - Assert.Fail(); - } - catch(DuplicateIdentityException){ - - } - - List storedKeys = keyRing.getIdentityPublicKeys(); - if (storedKeys.Count != 1){ - Assert.Fail(); - } - } - - [Test] - public void TestKeyRingStore() - { - string tempFile = Path.GetTempFileName(); - KeyRing keyRing = new KeyRing(); - byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey; - byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey; - - 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); - DoublePublicKey combo = new DoublePublicKey(signingKey, encryptionKey); - keyRing.addPublicKey(combo); - - List storedKeys = keyRing.getIdentityPublicKeys(); - bool success = false; - storedKeys.ForEach(delegate(byte[] key) - { - for (int x = 0; x < combinedKey.Length; x++){ - if (combinedKey[x] == key[x]){ - success = true; - continue; - } - success = false; - } - - }); - if (! success){ - Assert.Fail(); - } - } - } -} \ No newline at end of file diff --git a/tests/keyring/deletePublic.cs b/tests/keyring/deletePublic.cs deleted file mode 100644 index bcaf643..0000000 --- a/tests/keyring/deletePublic.cs +++ /dev/null @@ -1,45 +0,0 @@ -using NUnit.Framework; -using System; -using System.IO; -using Sodium; -using System.Collections.Generic; -using keyring; -using chestcrypto; -using chestcrypto.exceptions; - -namespace KeyRingDeletePublicTests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void TestDeleteIdentityByPublicKey(){ - string tempFile = Path.GetTempFileName(); - - DoublePublicKey getKey(){ - KeyRing keyRing = new KeyRing(); - byte[] signingKey = PublicKeyAuth.GenerateKeyPair().PublicKey; - byte[] encryptionKey = PublicKeyBox.GenerateKeyPair().PublicKey; - - 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); - DoublePublicKey combo = new DoublePublicKey(signingKey, encryptionKey); - return combo; - } - DoublePublicKey combo = getKey(); - KeyRing keyRing = new KeyRing(); - keyRing.addPublicKey(combo); - Assert.IsTrue(keyRing.getIdentityCount() == 1); - keyRing.removeIdentityByPubkey(combo); - Assert.IsTrue(keyRing.getIdentityCount() == 0); - - - } - - } -} \ No newline at end of file diff --git a/tests/simplepackTest.cs b/tests/simplepackTest.cs deleted file mode 100644 index 3b6c9bf..0000000 --- a/tests/simplepackTest.cs +++ /dev/null @@ -1,49 +0,0 @@ -using NUnit.Framework; -using chestcrypto; -using System; - -namespace simplepackTests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void TestPackUnpackString() - { - string message = "hello world"; - string packed; - packed = chestcrypto.simplepack.SimplePack.pack(message); - Assert.AreEqual(message, chestcrypto.simplepack.SimplePack.unpack(packed)); - } - [Test] - public void TestPackUnpackBytes() - { - byte[] message = System.Text.Encoding.UTF8.GetBytes("hello world"); - string packed; - packed = chestcrypto.simplepack.SimplePack.pack(message); - Assert.AreEqual(message, chestcrypto.simplepack.SimplePack.unpack(packed)); - } - [Test] - public void TestPackUnpackInvalid(){ - byte[] message = System.Text.Encoding.UTF8.GetBytes("hello world"); - string packed; - packed = chestcrypto.simplepack.SimplePack.pack(message).Remove(1); - bool success = false; - try{ - chestcrypto.simplepack.SimplePack.unpack(packed); - } - catch(chestcrypto.exceptions.InvalidSimplePackMessage){ - success = true; - } - if (! success){ - Assert.Fail(); - } - } - - - } -} \ No newline at end of file diff --git a/treasurechest/chestcrypto/identity/ephemeralkey.cs b/treasurechest/chestcrypto/identity/ephemeralkey.cs deleted file mode 100644 index 0194027..0000000 --- a/treasurechest/chestcrypto/identity/ephemeralkey.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace chestcrypto{ - - namespace identity{ - internal class EphemeralKey{ - - private int epochTime; - private int secondsToExpire; - private byte[] key = new byte[32]; - private bool isPrivate; - - private Identity identity; - public EphemeralKey(Identity identity, byte[] key, int secondsToExpire){ - - } - - - } - } - -} \ No newline at end of file diff --git a/treasurechest/chestcrypto/identity/exceptions.cs b/treasurechest/chestcrypto/identity/exceptions.cs deleted file mode 100644 index c8ee2ea..0000000 --- a/treasurechest/chestcrypto/identity/exceptions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -namespace chestcrypto{ - - namespace exceptions{ - public class NoIdentityException : Exception - { - public NoIdentityException() - { - } - - public NoIdentityException(string message) - : base(message) - { - } - - public NoIdentityException(string message, Exception inner) - : base(message, inner) - { - } - } - } - -} \ No newline at end of file diff --git a/treasurechest/chestcrypto/identity/identity.cs b/treasurechest/chestcrypto/identity/identity.cs deleted file mode 100644 index c69a20a..0000000 --- a/treasurechest/chestcrypto/identity/identity.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Collections.Generic; - -namespace chestcrypto{ - - namespace identity { - internal class Identity { - private DoublePrivateKey privateKey; - private DoublePublicKey publicKey; - private bool hasPrivate = false; - - private List ephemeralKeys = new List(); - - public DoublePublicKey getDoublePublicKey(){return publicKey;} - public DoublePrivateKey getDoublePrivateKey(){return privateKey;} - - - public Identity(){} - public Identity (List ephemeralKeys){ - - } - public Identity(DoublePublicKey publicKey){ - this.publicKey = publicKey; - } - public Identity(DoublePrivateKey privateKey){ - this.privateKey = privateKey; - } - public Identity(DoublePrivateKey privateKey, List ephemeralKeys){ - - } - public Identity(DoublePublicKey publicKey, List ephemeralKeys){ - - } - - - } - - } -} \ No newline at end of file diff --git a/treasurechest/identity/private.cs b/treasurechest/identity/private.cs new file mode 100644 index 0000000..0fb8f1b --- /dev/null +++ b/treasurechest/identity/private.cs @@ -0,0 +1,34 @@ +using chestcrypto; + +namespace chestcrypto.identity +{ + + public class PrivateIdentity{ + /* + PrivateIdentity is a wrapper around a DoublePrivateKey providing associated metadata such as alias and note + */ + + private DoublePrivateKey key; + private string name; + private string comment; // human's note + + public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias){ + key = doublePrivateKey; + name = alias; + comment = ""; + } + + public PrivateIdentity(DoublePrivateKey doublePrivateKey, string alias, string note){ + key = doublePrivateKey; + name = alias; + comment = note; + } + + public DoublePrivateKey getPrivateKey(){return key;} + public string getName(){return name;} + public string getNote(){return comment;} + + + } + +} \ No newline at end of file diff --git a/treasurechest/keyring/keyring.cs b/treasurechest/keyring/keyring.cs index 5e9d4c5..b9a60a2 100644 --- a/treasurechest/keyring/keyring.cs +++ b/treasurechest/keyring/keyring.cs @@ -1,72 +1,18 @@ using chestcrypto; -using chestcrypto.identity; using chestcrypto.exceptions; +using chestcrypto.identity; using System.Collections.Generic; namespace keyring{ public class KeyRing { - private string storageFile = null; - private List identities = new List(); + private List publicIdentities; + private List privateIdentities; - private bool identityExists(Identity iden){ - bool success = false; - identities.ForEach(delegate(Identity ident) - { - if (ident.getDoublePublicKey().Equals(iden.getDoublePublicKey())){ - success = true; - return; - } - }); - return success; - } - - internal void removeIdentity(Identity iden){identities.Remove(iden);} - - internal Identity getIdentityInstance(DoublePublicKey key){ - foreach (Identity iden in identities){ - if (iden.getDoublePublicKey().Equals(key)){ - return iden; - } - } - throw new NoIdentityException(); - } - - public KeyRing(string storageFile){ - - } - public KeyRing(){} - - public int getIdentityCount(){return identities.Count;} - - - public List getIdentityPublicKeys(){ - List pubKeys = new List(); - identities.ForEach(delegate(Identity identity){ - pubKeys.Add(identity.getDoublePublicKey().getRawDouble()); - }); - return pubKeys; - } - - public void addPublicKey(DoublePublicKey key){ - // Create an Identity with a public key if it does not exist already - - Identity newIdentity = new Identity(key); - if (identityExists(newIdentity)){ - throw new DuplicateIdentityException("An identity with that public key already exists"); - } - - identities.Add(newIdentity); - - } - - public void addPrivateKey(){ - - } - - public void removeIdentityByPubkey(DoublePublicKey key){ - removeIdentity(getIdentityInstance(key)); + public KeyRing(){ + //publicIdentities = new List(); + privateIdentities = new List(); } } diff --git a/treasurechest/simplepack/exceptions.cs b/treasurechest/simplepack/exceptions.cs deleted file mode 100644 index ffec12c..0000000 --- a/treasurechest/simplepack/exceptions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -namespace chestcrypto{ - - namespace exceptions{ - public class InvalidSimplePackMessage : Exception - { - public InvalidSimplePackMessage() - { - } - - public InvalidSimplePackMessage(string message) - : base(message) - { - } - - public InvalidSimplePackMessage(string message, Exception inner) - : base(message, inner) - { - } - } - } - -} \ No newline at end of file diff --git a/treasurechest/simplepack/pack.cs b/treasurechest/simplepack/pack.cs deleted file mode 100644 index 5c9ba3a..0000000 --- a/treasurechest/simplepack/pack.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Base58Check; - -namespace chestcrypto{ - - namespace simplepack{ - - public class SimplePack{ - - private const string header = "CHEST-MESSAGE "; - private const string footer = " END-CHEST-MESSAGE."; - - // Test simplepackTest.TestPackUnpackBytes - public static string pack(byte[] data){ - return header + Base58CheckEncoding.Encode(data) + footer; - } - // Test simplepackTest.TestPackUnpackString - public static string pack(string data){ - return pack(System.Text.Encoding.UTF8.GetBytes(data)); - } - // Test simplepackTest.TestPackUnpackBytes - public static byte[] unpack(string checkedBase58String){ - if (! checkedBase58String.Contains(header) | ! checkedBase58String.Contains(footer)){ - throw new exceptions.InvalidSimplePackMessage("Message does not have valid header and footer"); - } - string encodedMessage = ""; - for (int i = header.Length; i < checkedBase58String.Length - footer.Length; i++){ - encodedMessage += checkedBase58String[i]; - } - return Base58CheckEncoding.Decode(encodedMessage); - - } - - } - - } - -} diff --git a/treasurechest/treasurechest.csproj b/treasurechest/treasurechest.csproj index e9003db..61f0982 100644 --- a/treasurechest/treasurechest.csproj +++ b/treasurechest/treasurechest.csproj @@ -1,13 +1,13 @@ - netstandard2.0 + netstandard2.1 - - - - + + + +