public void AddContactSecretTest() { // Arrange string kdfeIdentifier = "somefile122344"; string password = "******"; KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry(kdfeIdentifier); CommonSecretsContainer csc = new CommonSecretsContainer(kdfe); // Act var addResultSuccess1 = csc.AddContactSecret(password, ContentGenerator.GenerateRandomContact(), kdfeIdentifier); var addResultSuccess2 = csc.AddContactSecret(kdfe.GeneratePasswordBytes(password), ContentGenerator.GenerateRandomContact(), kdfeIdentifier); var addResultFailure1 = csc.AddContactSecret(password, null, kdfeIdentifier); var addResultFailure2 = csc.AddContactSecret(password, ContentGenerator.GenerateRandomContact(), "not existing"); var addResultFailure3 = csc.AddContactSecret("", ContentGenerator.GenerateRandomContact(), 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.contactSecrets.Count); }
public void CanBeDecryptedWithDerivedPasswordTest() { byte[] derivedKey1 = new byte[16] { 111, 222, 31, 47, 75, 168, 78, 13, 61, 118, 221, 18, 213, 104, 15, 16 }; byte[] derivedKey2 = new byte[16] { 111, 222, 31, 47, 75, 168, 78, 13, 61, 118, 221, 18, 213, 104, 15, 15 }; byte[] initialCounter = new byte[] { 0xa7, 0xb1, 0xcb, 0xcd, 0xaa, 0xc5, 0xd3, 0xb5, 0x58, 0x51, 0x91, 0x2b, 0x33, 0xfd, 0xfe, 0xff }; SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter); SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR); string keyIdentifier = "primary"; // Act ContactSecret contactSecret = new ContactSecret(ContentGenerator.GenerateRandomContact(), keyIdentifier, skaAES_CTR, derivedKey1); // Assert Assert.IsTrue(contactSecret.CanBeDecryptedWithDerivedPassword(derivedKey1)); Assert.IsFalse(contactSecret.CanBeDecryptedWithDerivedPassword(null)); Assert.IsFalse(contactSecret.CanBeDecryptedWithDerivedPassword(new byte[] {})); Assert.IsFalse(contactSecret.CanBeDecryptedWithDerivedPassword(derivedKey2)); }
public void ReplaceContactSecretTest() { // Arrange string kdfeIdentifier = ",.-4somefile12344"; string password = "******"; KeyDerivationFunctionEntry kdfe = KeyDerivationFunctionEntry.CreateHMACSHA256KeyDerivationFunctionEntry(kdfeIdentifier); CommonSecretsContainer csc = new CommonSecretsContainer(kdfe); Contact add1 = ContentGenerator.GenerateRandomContact(); Contact add2 = ContentGenerator.GenerateRandomContact(); Contact replace1 = ContentGenerator.GenerateRandomContact(); Contact replace2 = ContentGenerator.GenerateRandomContact(); // Act var addResultSuccess1 = csc.AddContactSecret(password, add1, kdfeIdentifier); var addResultSuccess2 = csc.AddContactSecret(kdfe.GeneratePasswordBytes(password), add2, kdfeIdentifier); var replaceResultSuccess1 = csc.ReplaceContactSecret(0, password, replace1, kdfeIdentifier); var replaceResultSuccess2 = csc.ReplaceContactSecret(1, kdfe.GeneratePasswordBytes(password), replace2, kdfeIdentifier, SymmetricEncryptionAlgorithm.ChaCha20); var replaceResultFailure1 = csc.ReplaceContactSecret(0, password, null, kdfeIdentifier); var replaceResultFailure2 = csc.ReplaceContactSecret(0, password, ContentGenerator.GenerateRandomContact(), "not existing"); var replaceResultFailure3 = csc.ReplaceContactSecret(0, "", ContentGenerator.GenerateRandomContact(), kdfeIdentifier); var replaceResultFailure4 = csc.ReplaceContactSecret(-1, password, replace1, kdfeIdentifier); var replaceResultFailure5 = csc.ReplaceContactSecret(2, password, replace1, kdfeIdentifier); // Assert Assert.AreNotEqual(add1.GetFirstName(), replace1.GetFirstName(), "Make sure that random content do not match!"); Assert.AreNotEqual(add2.GetFirstName(), replace2.GetFirstName(), "Make sure that random content do not match!"); Assert.AreEqual(replace1.GetFirstName(), csc.contactSecrets[0].GetFirstName(kdfe.GeneratePasswordBytes(password))); Assert.AreEqual(replace2.GetFirstName(), csc.contactSecrets[1].GetFirstName(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.contactSecrets.Count); }
public void GetKeyIdentifierTest() { // Arrange byte[] derivedKey = new byte[16] { 111, 222, 31, 47, 75, 168, 78, 83, 91, 110, 221, 18, 213, 104, 15, 16 }; byte[] initialCounter = new byte[] { 0xa0, 0xb1, 0xcb, 0xcd, 0xaa, 0xc5, 0xd3, 0xb5, 0x58, 0x59, 0x15, 0x2b, 0x33, 0xfd, 0xfe, 0xff }; SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter); SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR); string keyIdentifier = "primary"; // Act ContactSecret contactSecret = new ContactSecret(ContentGenerator.GenerateRandomContact(), keyIdentifier, skaAES_CTR, derivedKey); // Assert Assert.AreEqual(keyIdentifier, contactSecret.GetKeyIdentifier()); }
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])); } }
public void SetSingleEntriesTest() { // Arrange byte[] derivedKey = new byte[16] { 1, 2, 3, 4, 3, 6, 4, 8, 9, 13, 11, 12, 13, 140, 15, 16 }; byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xd6, 0xf7, 0xf8, 0xf9, 0xfa, 0xf1, 0xfc, 0xfd, 0xfe, 0xff }; SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter); SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR); string firstName = "Super2110"; string lastName = "Aweso2me3220"; string middleName = "Mega4330"; string namePrefix = "Sirest"; string nameSuffix = "IIIXII"; string nickname = "MegaDragon13"; string company = "EverDragons COTYERT1"; string jobTitle = "Mi3d dragon"; string department = "13rd Cave"; string[] emails = { "*****@*****.**", "*****@*****.**" }; string[] emailDescriptions = { "work", "home" }; string[] phoneNumbers = { "1234-123-123", "2344-234-234" }; string[] phoneNumberDescriptions = { "work", "hotel" }; string country = "drago4nland II"; string streetAddress = "dragon street 122"; string streetAddressAdditional = "no addition"; string postalCode = "112345"; string city = "dragvoncave"; string poBox = "no po box"; string birthday = "11-09-1697"; string relationship = "single!"; string notes = "Very awesome dragon again"; string[] websites = { "https://dacoolastdragons4life.com", "https://nicevalleyvaults.net" }; Contact c1 = new Contact(ContentGenerator.GenerateRandomContact()); ContactSecret cs = new ContactSecret(c1, "does not matter", skaAES_CTR, derivedKey); // Act cs.SetFirstName(firstName, derivedKey); cs.SetLastName(lastName, derivedKey); cs.SetMiddleName(middleName, derivedKey); cs.SetNamePrefix(namePrefix, derivedKey); cs.SetNameSuffix(nameSuffix, derivedKey); cs.SetNickname(nickname, derivedKey); cs.SetCompany(company, derivedKey); cs.SetJobTitle(jobTitle, derivedKey); cs.SetDepartment(department, derivedKey); cs.SetEmailsAndDescriptions(emails, emailDescriptions, derivedKey); cs.SetPhoneNumbersAndDescriptions(phoneNumbers, phoneNumberDescriptions, derivedKey); cs.SetCountry(country, derivedKey); cs.SetStreetAddress(streetAddress, derivedKey); cs.SetStreetAddressAdditional(streetAddressAdditional, derivedKey); cs.SetPostalCode(postalCode, derivedKey); cs.SetCity(city, derivedKey); cs.SetPOBox(poBox, derivedKey); cs.SetBirthday(birthday, derivedKey); cs.SetRelationship(relationship, derivedKey); cs.SetNotes(notes, derivedKey); cs.SetWebsites(websites, derivedKey); // Assert Assert.AreEqual(firstName, cs.GetFirstName(derivedKey)); Assert.AreEqual(lastName, cs.GetLastName(derivedKey)); Assert.AreEqual(middleName, cs.GetMiddleName(derivedKey)); Assert.AreEqual(namePrefix, cs.GetNamePrefix(derivedKey)); Assert.AreEqual(nameSuffix, cs.GetNameSuffix(derivedKey)); Assert.AreEqual(nickname, cs.GetNickname(derivedKey)); Assert.AreEqual(company, cs.GetCompany(derivedKey)); Assert.AreEqual(jobTitle, cs.GetJobTitle(derivedKey)); Assert.AreEqual(department, cs.GetDepartment(derivedKey)); CollectionAssert.AreEqual(emails, cs.GetEmails(derivedKey)); CollectionAssert.AreEqual(emailDescriptions, cs.GetEmailDescriptions(derivedKey)); CollectionAssert.AreEqual(phoneNumbers, cs.GetPhoneNumbers(derivedKey)); CollectionAssert.AreEqual(phoneNumberDescriptions, cs.GetPhoneNumberDescriptions(derivedKey)); Assert.AreEqual(country, cs.GetCountry(derivedKey)); Assert.AreEqual(streetAddress, cs.GetStreetAddress(derivedKey)); Assert.AreEqual(streetAddressAdditional, cs.GetStreetAddressAdditional(derivedKey)); Assert.AreEqual(postalCode, cs.GetPostalCode(derivedKey)); Assert.AreEqual(city, cs.GetCity(derivedKey)); Assert.AreEqual(poBox, cs.GetPOBox(derivedKey)); Assert.AreEqual(birthday, cs.GetBirthday(derivedKey)); Assert.AreEqual(relationship, cs.GetRelationship(derivedKey)); Assert.AreEqual(notes, cs.GetNotes(derivedKey)); CollectionAssert.AreEqual(websites, cs.GetWebsites(derivedKey)); }