Example #1
0
        public void ReplacePaymentSecretTest()
        {
            // Arrange
            string kdfeIdentifier = ",.-4key12344";
            string password       = "******";

            KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry(kdfeIdentifier);
            CommonSecretsContainer     csc  = new CommonSecretsContainer(kdfe);

            PaymentCard add1 = ContentGenerator.GenerateRandomPaymentCard();
            PaymentCard add2 = ContentGenerator.GenerateRandomPaymentCard();

            PaymentCard replace1 = ContentGenerator.GenerateRandomPaymentCard();
            PaymentCard replace2 = ContentGenerator.GenerateRandomPaymentCard();

            // Act
            var addResultSuccess1 = csc.AddPaymentCardSecret(password, add1, kdfeIdentifier);
            var addResultSuccess2 = csc.AddPaymentCardSecret(kdfe.GeneratePasswordBytes(password), add2, kdfeIdentifier);

            var replaceResultSuccess1 = csc.ReplacePaymentCardSecret(0, password, replace1, kdfeIdentifier);
            var replaceResultSuccess2 = csc.ReplacePaymentCardSecret(1, kdfe.GeneratePasswordBytes(password), replace2, kdfeIdentifier, SymmetricEncryptionAlgorithm.ChaCha20);

            var replaceResultFailure1 = csc.ReplacePaymentCardSecret(0, password, null, kdfeIdentifier);
            var replaceResultFailure2 = csc.ReplacePaymentCardSecret(0, password, ContentGenerator.GenerateRandomPaymentCard(), "not existing");
            var replaceResultFailure3 = csc.ReplacePaymentCardSecret(0, "", ContentGenerator.GenerateRandomPaymentCard(), kdfeIdentifier);
            var replaceResultFailure4 = csc.ReplacePaymentCardSecret(-1, password, replace1, kdfeIdentifier);
            var replaceResultFailure5 = csc.ReplacePaymentCardSecret(2, password, replace1, kdfeIdentifier);

            // Assert
            Assert.AreNotEqual(add1.GetTitle(), replace1.GetTitle(), "Make sure that random content do not match!");
            Assert.AreNotEqual(add2.GetTitle(), replace2.GetTitle(), "Make sure that random content do not match!");

            Assert.AreEqual(replace1.GetTitle(), csc.paymentCardSecrets[0].GetTitle(kdfe.GeneratePasswordBytes(password)));
            Assert.AreEqual(replace2.GetTitle(), csc.paymentCardSecrets[1].GetTitle(kdfe.GeneratePasswordBytes(password)));

            Assert.IsTrue(addResultSuccess1.success);
            Assert.AreEqual("", addResultSuccess1.possibleError);

            Assert.IsTrue(addResultSuccess2.success);
            Assert.AreEqual("", addResultSuccess2.possibleError);

            Assert.IsFalse(replaceResultFailure1.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure1.possibleError));

            Assert.IsFalse(replaceResultFailure2.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure2.possibleError));

            Assert.IsFalse(replaceResultFailure3.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure3.possibleError));

            Assert.IsFalse(replaceResultFailure4.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure4.possibleError));

            Assert.IsFalse(replaceResultFailure5.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure5.possibleError));

            Assert.AreEqual(2, csc.paymentCardSecrets.Count);
        }
Example #2
0
        public void AddNoteSecretTest()
        {
            // Arrange
            string kdfeIdentifier           = "somethinghere";
            string password                 = "******";
            KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry(kdfeIdentifier);
            CommonSecretsContainer     csc  = new CommonSecretsContainer(kdfe);

            // Act
            var addResultSuccess1 = csc.AddNoteSecret(password, ContentGenerator.GenerateRandomNote(), kdfeIdentifier);
            var addResultSuccess2 = csc.AddNoteSecret(kdfe.GeneratePasswordBytes(password), ContentGenerator.GenerateRandomNote(), kdfeIdentifier);

            var addResultFailure1 = csc.AddNoteSecret(password, null, kdfeIdentifier);
            var addResultFailure2 = csc.AddNoteSecret(password, ContentGenerator.GenerateRandomNote(), "not existing");
            var addResultFailure3 = csc.AddNoteSecret("", ContentGenerator.GenerateRandomNote(), kdfeIdentifier);

            // Assert
            Assert.IsTrue(addResultSuccess1.success);
            Assert.AreEqual("", addResultSuccess1.possibleError);

            Assert.IsTrue(addResultSuccess2.success);
            Assert.AreEqual("", addResultSuccess2.possibleError);

            Assert.IsFalse(addResultFailure1.success);
            Assert.IsFalse(string.IsNullOrEmpty(addResultFailure1.possibleError));

            Assert.IsFalse(addResultFailure2.success);
            Assert.IsFalse(string.IsNullOrEmpty(addResultFailure2.possibleError));

            Assert.IsFalse(addResultFailure3.success);
            Assert.IsFalse(string.IsNullOrEmpty(addResultFailure3.possibleError));

            Assert.AreEqual(2, csc.noteSecrets.Count);
        }
        public void GeneratePasswordBytesTest()
        {
            // Arrange
            byte[] salt1 = Encoding.UTF8.GetBytes("saltSALTsaltSALTsaltSALTsaltSALTsalt");
            KeyDerivationFunctionEntry kdfe1 = new KeyDerivationFunctionEntry(KeyDerivationPrf.HMACSHA256, salt1, 4096, 25, "master_key");

            byte[] bytesShouldBe1 = new byte[25] {
                0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, 0x1c
            };

            byte[] salt2 = Encoding.UTF8.GetBytes("saltKEYbcTcXHCBxtjD");
            KeyDerivationFunctionEntry kdfe2 = new KeyDerivationFunctionEntry(KeyDerivationPrf.HMACSHA512, salt2, 100000, 64, "master_key");

            byte[] bytesShouldBe2 = new byte[64] {
                0xAC, 0xCD, 0xCD, 0x87, 0x98, 0xAE, 0x5C, 0xD8, 0x58, 0x04, 0x73, 0x90, 0x15, 0xEF, 0x2A, 0x11, 0xE3, 0x25, 0x91, 0xB7, 0xB7, 0xD1, 0x6F, 0x76, 0x81, 0x9B, 0x30, 0xB0, 0xD4, 0x9D, 0x80, 0xE1, 0xAB, 0xEA, 0x6C, 0x98, 0x22, 0xB8, 0x0A, 0x1F, 0xDF, 0xE4, 0x21, 0xE2, 0x6F, 0x56, 0x03, 0xEC, 0xA8, 0xA4, 0x7A, 0x64, 0xC9, 0xA0, 0x04, 0xFB, 0x5A, 0xF8, 0x22, 0x9F, 0x76, 0x2F, 0xF4, 0x1F
            };

            // Act
            byte[] bytes1 = kdfe1.GeneratePasswordBytes("passwordPASSWORDpassword");
            byte[] bytes2 = kdfe2.GeneratePasswordBytes("passDATAb00AB7YxDTT");

            // Assert
            CollectionAssert.AreEqual(bytesShouldBe1, bytes1);
            CollectionAssert.AreEqual(bytesShouldBe2, bytes2);
        }
Example #4
0
        public static int Benchmark(double howLongToRunInMilliseconds, int iterations)
        {
            int returnValue = 0;

            byte[] salt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            KeyDerivationFunctionEntry kdfe = new KeyDerivationFunctionEntry(KeyDerivationPrf.HMACSHA256, salt, iterations, 32, "Does not matter");

            int valueToIncrease = 100000;

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            while (stopwatch.Elapsed.TotalMilliseconds < howLongToRunInMilliseconds)
            {
                kdfe.GeneratePasswordBytes(valueToIncrease.ToString());
                valueToIncrease++;
                returnValue++;

                kdfe.GeneratePasswordBytes(valueToIncrease.ToString());
                valueToIncrease++;
                returnValue++;

                kdfe.GeneratePasswordBytes(valueToIncrease.ToString());
                valueToIncrease++;
                returnValue++;

                kdfe.GeneratePasswordBytes(valueToIncrease.ToString());
                valueToIncrease++;
                returnValue++;

                kdfe.GeneratePasswordBytes(valueToIncrease.ToString());
                valueToIncrease++;
                returnValue++;
            }
            stopwatch.Stop();

            return(returnValue);
        }
        public void CreateHMACSHA256KeyDerivationFunctionEntryTest()
        {
            // Arrange
            KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry("does not matter");
            string password = "******";

            // Act
            byte[] derivedPassword = kdfe.GeneratePasswordBytes(password);

            // Assert
            Assert.GreaterOrEqual(kdfe.iterations, KeyDerivationFunctionEntry.iterationsMin);
            Assert.AreEqual(32, derivedPassword.Length);
            Assert.Greater(CalculateEntropy.ShannonEntropy(derivedPassword), 4.0);
        }
        private void CreateNewCommonSecrets(KeyDerivationFunctionEntry kdfe, string password)
        {
            this.derivedPasswords.Clear();
            this.derivedPasswords[kdfe.GetKeyIdentifier()] = kdfe.GeneratePasswordBytes(password);

            this.csc = new CommonSecretsContainer(kdfe);

            LoginInformation demoLogin = new LoginInformation("Demo login", "https://localhost", "*****@*****.**", "Dragon", "gwWTY#ยค&%36", "This login will expire in 2030", new byte[] {}, "Samples", "Samples\tDemo");

            (bool successAddLoginInformation, string possibleErrorAddLoginInformation) = this.csc.AddLoginInformationSecret(password, demoLogin, kdfe.GetKeyIdentifier());

            if (!successAddLoginInformation)
            {
                //MessageBox.Show($"Error when adding demo secret: {possibleErrorAddLoginInformation}", "Error");
                return;
            }

            Note demoNote = new Note("Sample topic", "You can easily create notes");

            (bool successAddNote, string possibleErrorAddNote) = this.csc.AddNoteSecret(password, demoNote, kdfe.GetKeyIdentifier());

            if (!successAddNote)
            {
                //MessageBox.Show($"Error when adding demo note: {possibleErrorAddNote}", "Error");
                return;
            }

            // Update UI lists
            this.GenerateLoginSimplifiedsFromCommonSecrets();
            this.GenerateNoteSimplifiedsFromCommonSecrets();
            this.GenerateFileSimplifiedsFromCommonSecrets();

            this.isModified = true;
            this.UpdateMainTitle(untitledTempName);

            OnPropertyChanged(nameof(this.IsSaveEnabled));

            // Change UI
            OnPropertyChanged(nameof(this.TabsVisibility));
            OnPropertyChanged(nameof(this.WizardVisibility));
        }
Example #7
0
        public void ReplaceNoteSecretTest()
        {
            // Arrange
            string kdfeIdentifier           = "somet!%&hinghere";
            string password                 = "******";
            KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry(kdfeIdentifier);
            CommonSecretsContainer     csc  = new CommonSecretsContainer(kdfe);

            Note add1 = ContentGenerator.GenerateRandomNote();
            Note add2 = ContentGenerator.GenerateRandomNote();

            Note replace1 = ContentGenerator.GenerateRandomNote();
            Note replace2 = ContentGenerator.GenerateRandomNote();

            // Act
            var addResultSuccess1 = csc.AddNoteSecret(password, add1, kdfeIdentifier);
            var addResultSuccess2 = csc.AddNoteSecret(kdfe.GeneratePasswordBytes(password), add2, kdfeIdentifier);

            var replaceResultSuccess1 = csc.ReplaceNoteSecret(0, password, replace1, kdfeIdentifier);
            var replaceResultSuccess2 = csc.ReplaceNoteSecret(1, kdfe.GeneratePasswordBytes(password), replace2, kdfeIdentifier);

            var replaceResultFailure1 = csc.ReplaceNoteSecret(0, password, null, kdfeIdentifier);
            var replaceResultFailure2 = csc.ReplaceNoteSecret(0, password, ContentGenerator.GenerateRandomNote(), "not existing");
            var replaceResultFailure3 = csc.ReplaceNoteSecret(0, "", ContentGenerator.GenerateRandomNote(), kdfeIdentifier);
            var replaceResultFailure4 = csc.ReplaceNoteSecret(-1, password, replace1, kdfeIdentifier);
            var replaceResultFailure5 = csc.ReplaceNoteSecret(2, password, replace1, kdfeIdentifier);

            // Assert
            Assert.AreNotEqual(add1.GetNoteTitle(), replace1.GetNoteTitle(), "Make sure that random content do not match!");
            Assert.AreNotEqual(add2.GetNoteTitle(), replace2.GetNoteTitle(), "Make sure that random content do not match!");

            Assert.AreEqual(replace1.GetNoteTitle(), csc.noteSecrets[0].GetNoteTitle(kdfe.GeneratePasswordBytes(password)));
            Assert.AreEqual(replace2.GetNoteTitle(), csc.noteSecrets[1].GetNoteTitle(kdfe.GeneratePasswordBytes(password)));

            Assert.IsTrue(addResultSuccess1.success);
            Assert.AreEqual("", addResultSuccess1.possibleError);

            Assert.IsTrue(addResultSuccess2.success);
            Assert.AreEqual("", addResultSuccess2.possibleError);

            Assert.IsTrue(replaceResultSuccess1.success);
            Assert.AreEqual("", replaceResultSuccess1.possibleError);

            Assert.IsTrue(replaceResultSuccess2.success);
            Assert.AreEqual("", replaceResultSuccess2.possibleError);

            Assert.IsFalse(replaceResultFailure1.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure1.possibleError));

            Assert.IsFalse(replaceResultFailure2.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure2.possibleError));

            Assert.IsFalse(replaceResultFailure3.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure3.possibleError));

            Assert.IsFalse(replaceResultFailure4.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure4.possibleError));

            Assert.IsFalse(replaceResultFailure5.success);
            Assert.IsFalse(string.IsNullOrEmpty(replaceResultFailure5.possibleError));

            Assert.AreEqual(2, csc.noteSecrets.Count);
        }
        public void RoundTripComplexTest()
        {
            // Arrange
            CommonSecretsContainer csc = new CommonSecretsContainer();

            string password = "******";

            byte[]                initialCounter1  = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
            SettingsAES_CTR       settingsAES_CTR1 = new SettingsAES_CTR(initialCounter1);
            SymmetricKeyAlgorithm skaAES           = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR1);

            KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry("does not matter");

            int loginsAmount       = 12;
            int loginsSecretAmount = 14;

            int notesAmount       = 17;
            int notesSecretAmount = 11;

            int filesAmount       = 5;
            int filesSecretAmount = 3;

            int contactAmount       = 4;
            int contactSecretAmount = 2;

            int paymentAmount       = 3;
            int paymentSecretAmount = 7;

            // Act
            byte[] derivedPassword = kdfe.GeneratePasswordBytes(password);

            csc.keyDerivationFunctionEntries.Add(kdfe);

            for (int i = 0; i < loginsAmount; i++)
            {
                csc.loginInformations.Add(ContentGenerator.GenerateRandomLoginInformation());
            }

            for (int i = 0; i < loginsSecretAmount; i++)
            {
                csc.loginInformationSecrets.Add(new LoginInformationSecret(ContentGenerator.GenerateRandomLoginInformation(), kdfe.GetKeyIdentifier(), skaAES, derivedPassword));
            }

            for (int i = 0; i < notesAmount; i++)
            {
                csc.notes.Add(ContentGenerator.GenerateRandomNote());
            }

            for (int i = 0; i < notesSecretAmount; i++)
            {
                csc.noteSecrets.Add(new NoteSecret(ContentGenerator.GenerateRandomNote(), kdfe.GetKeyIdentifier(), skaAES, derivedPassword));
            }

            for (int i = 0; i < filesAmount; i++)
            {
                csc.files.Add(ContentGenerator.GenerateRandomFileEntry());
            }

            for (int i = 0; i < filesSecretAmount; i++)
            {
                csc.fileSecrets.Add(new FileEntrySecret(ContentGenerator.GenerateRandomFileEntry(), kdfe.GetKeyIdentifier(), skaAES, derivedPassword));
            }

            for (int i = 0; i < contactAmount; i++)
            {
                csc.contacts.Add(ContentGenerator.GenerateRandomContact());
            }

            for (int i = 0; i < contactSecretAmount; i++)
            {
                csc.contactSecrets.Add(new ContactSecret(ContentGenerator.GenerateRandomContact(), kdfe.GetKeyIdentifier(), skaAES, derivedPassword));
            }

            for (int i = 0; i < paymentAmount; i++)
            {
                csc.paymentCards.Add(ContentGenerator.GenerateRandomPaymentCard());
            }

            for (int i = 0; i < paymentSecretAmount; i++)
            {
                csc.paymentCardSecrets.Add(new PaymentCardSecret(ContentGenerator.GenerateRandomPaymentCard(), kdfe.GetKeyIdentifier(), skaAES, derivedPassword));
            }

            string json = JsonSerializer.Serialize(csc, serializerOptions);

            CommonSecretsContainer cscDeserialized = JsonSerializer.Deserialize <CommonSecretsContainer>(json);

            // Assert
            Assert.AreEqual(1, csc.keyDerivationFunctionEntries.Count);
            Assert.AreEqual(1, cscDeserialized.keyDerivationFunctionEntries.Count);
            Assert.IsTrue(ComparisonHelper.AreKeyDerivationFunctionEntriesEqual(csc.keyDerivationFunctionEntries[0], cscDeserialized.keyDerivationFunctionEntries[0]));

            Assert.AreEqual(loginsAmount, csc.loginInformations.Count);
            Assert.AreEqual(loginsAmount, cscDeserialized.loginInformations.Count);
            for (int i = 0; i < loginsAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreLoginInformationsEqual(csc.loginInformations[i], cscDeserialized.loginInformations[i]));
            }

            Assert.AreEqual(loginsSecretAmount, csc.loginInformationSecrets.Count);
            Assert.AreEqual(loginsSecretAmount, cscDeserialized.loginInformationSecrets.Count);
            for (int i = 0; i < loginsSecretAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreLoginInformationSecretsEqual(csc.loginInformationSecrets[i], cscDeserialized.loginInformationSecrets[i]));
            }


            Assert.AreEqual(notesAmount, csc.notes.Count);
            Assert.AreEqual(notesAmount, cscDeserialized.notes.Count);
            for (int i = 0; i < notesAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreNotesEqual(csc.notes[i], cscDeserialized.notes[i]));
            }

            Assert.AreEqual(notesSecretAmount, csc.noteSecrets.Count);
            Assert.AreEqual(notesSecretAmount, cscDeserialized.noteSecrets.Count);
            for (int i = 0; i < notesSecretAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreNotesSecretEqual(csc.noteSecrets[i], cscDeserialized.noteSecrets[i]));
            }


            Assert.AreEqual(filesAmount, csc.files.Count);
            Assert.AreEqual(filesAmount, cscDeserialized.files.Count);
            for (int i = 0; i < filesAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreFileEntriesEqual(csc.files[i], cscDeserialized.files[i]));
            }

            Assert.AreEqual(filesSecretAmount, csc.fileSecrets.Count);
            Assert.AreEqual(filesSecretAmount, cscDeserialized.fileSecrets.Count);
            for (int i = 0; i < filesSecretAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreFileEntrySecretsEqual(csc.fileSecrets[i], cscDeserialized.fileSecrets[i]));
            }


            Assert.AreEqual(contactAmount, csc.contacts.Count);
            Assert.AreEqual(contactAmount, cscDeserialized.contacts.Count);
            for (int i = 0; i < contactAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreContactsEqual(csc.contacts[i], cscDeserialized.contacts[i]));
            }

            Assert.AreEqual(contactSecretAmount, csc.contactSecrets.Count);
            Assert.AreEqual(contactSecretAmount, cscDeserialized.contactSecrets.Count);
            for (int i = 0; i < contactSecretAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.AreContactSecretsEqual(csc.contactSecrets[i], cscDeserialized.contactSecrets[i]));
            }


            Assert.AreEqual(paymentAmount, csc.paymentCards.Count);
            Assert.AreEqual(paymentAmount, cscDeserialized.paymentCards.Count);
            for (int i = 0; i < paymentAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.ArePaymentCardsEqual(csc.paymentCards[i], cscDeserialized.paymentCards[i]));
            }

            Assert.AreEqual(paymentSecretAmount, csc.paymentCardSecrets.Count);
            Assert.AreEqual(paymentSecretAmount, cscDeserialized.paymentCardSecrets.Count);
            for (int i = 0; i < paymentSecretAmount; i++)
            {
                Assert.IsTrue(ComparisonHelper.ArePaymentCardSecretsEqual(csc.paymentCardSecrets[i], cscDeserialized.paymentCardSecrets[i]));
            }
        }