public async Task TestAddingSingleV2AsymmetricKeyWrap() { EncryptionParameters encryptionParameters = new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("allan")); IAsymmetricPublicKey publicKey = New <IAsymmetricFactory>().CreatePublicKey(Resources.PublicKey1); await encryptionParameters.AddAsync(new UserPublicKey[] { new UserPublicKey(EmailAddress.Parse("*****@*****.**"), publicKey), }); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(encryptionParameters, 1000); IEnumerable <V2AsymmetricKeyWrapHeaderBlock> wraps = documentHeaders.Headers.HeaderBlocks.OfType <V2AsymmetricKeyWrapHeaderBlock>(); Assert.That(wraps.Count(), Is.EqualTo(1), "There should be one V2AsymmetricKeyWrapHeaderBlock found."); V2AsymmetricKeyWrapHeaderBlock block1 = wraps.First(); ICryptoFactory cryptoFactory = Resolve.CryptoFactory.Create(encryptionParameters.CryptoId); IAsymmetricPrivateKey privateKey1 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey1); block1.SetPrivateKey(cryptoFactory, privateKey1); ICrypto cryptoFromAsymmetricKey = block1.Crypto(0); V2KeyWrapHeaderBlock symmetricKeyWrap = documentHeaders.Headers.HeaderBlocks.OfType <V2KeyWrapHeaderBlock>().First(); ICrypto cryptoFromSymmetricKey = cryptoFactory.CreateCrypto(symmetricKeyWrap.MasterKey, symmetricKeyWrap.MasterIV, 0); Assert.That(cryptoFromAsymmetricKey.Key, Is.EqualTo(cryptoFromSymmetricKey.Key), "The keys from Asymmetric and Symmetric should be equal."); IAsymmetricPrivateKey privateKey2 = New <IAsymmetricFactory>().CreatePrivateKey(Resources.PrivateKey2); block1.SetPrivateKey(cryptoFactory, privateKey2); ICrypto cryptoFromAsymmetricKey1WithKey2 = block1.Crypto(0); Assert.That(cryptoFromAsymmetricKey1WithKey2, Is.Null, "There should be no valid key set and thus no ICrypto instance returned."); }
public void TestHeadersPropertyGetter() { V2KeyWrapHeaderBlock keyWrap = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), new V2DerivedKey(new Passphrase("Key"), 256), 256); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(keyWrap); Assert.That(documentHeaders.Headers.HeaderBlocks.Count, Is.EqualTo(0)); }
public static void TestClone() { TypeMap.Register.Singleton <IRandomGenerator>(() => new FakeRandomGenerator()); IDerivedKey keyEncryptingKey = new V2DerivedKey(new Passphrase("secret"), new Salt(256), 100, 256); V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), keyEncryptingKey, 125); V2KeyWrapHeaderBlock clone = (V2KeyWrapHeaderBlock)header.Clone(); Assert.That(header.GetDataBlockBytes(), Is.EquivalentTo(clone.GetDataBlockBytes())); }
public static void TestMasterIVWithWrongKeyEncryptingCrypto() { TypeMap.Register.Singleton <IRandomGenerator>(() => new FakeRandomGenerator()); IDerivedKey keyEncryptingKey = new V2DerivedKey(new Passphrase("secret"), new Salt(256), 100, 256); V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), keyEncryptingKey, 125); header.SetDerivedKey(new V2Aes256CryptoFactory(), new V2DerivedKey(new Passphrase("another secret"), 256)); SymmetricIV iv = header.MasterIV; Assert.That(iv, Is.Null); }
public static void TestUnwrapMasterKeyAndIV256WithNonzeroRandomNumbers() { TypeMap.Register.Singleton <IRandomGenerator>(() => new FakeRandomGenerator()); IDerivedKey keyEncryptingKey = new V2DerivedKey(new Passphrase("secret"), new Salt(256), 100, 256); V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), keyEncryptingKey, 125); SymmetricKey key = header.MasterKey; Assert.That(key.GetBytes(), Is.EquivalentTo(ByteSequence(key.GetBytes()[0], key.Size / 8))); SymmetricIV iv = header.MasterIV; Assert.That(iv.GetBytes(), Is.EquivalentTo(ByteSequence(iv.GetBytes()[0], iv.Length))); }
public static void TestConstructorFromKeyUsingKeyWrap256TestVectors() { var mock = new Mock <IRandomGenerator>(); mock.Setup <byte[]>(x => x.Generate(It.Is <int>(v => v == 248))).Returns(new byte[248]); mock.Setup <byte[]>(x => x.Generate(It.Is <int>(v => v == (32 + 16)))).Returns(_keyData256); TypeMap.Register.Singleton <IRandomGenerator>(() => mock.Object); V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), _keyEncryptingKey256, 6); byte[] bytes = header.GetDataBlockBytes(); byte[] wrapped = new byte[40]; Array.Copy(bytes, 0, wrapped, 0, wrapped.Length); Assert.That(wrapped, Is.EquivalentTo(_wrapped256)); }
public static void TestConstructorFromBytes() { byte[] datablock = new byte[248]; for (int i = 247; i >= 0; --i) { datablock[247 - i] = (byte)i; } V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(datablock); Assert.That(header.GetDataBlockBytes(), Is.EquivalentTo(datablock)); header = null; Assert.Throws <ArgumentException>(() => header = new V2KeyWrapHeaderBlock(new byte[247])); Assert.Throws <ArgumentNullException>(() => header = new V2KeyWrapHeaderBlock(null)); Assert.That(header, Is.Null); }
public static void TestUnwrapMasterKeyAndIV256WithZeroRandomNumbers() { var mock = new Mock <IRandomGenerator>(); mock.Setup <byte[]>(x => x.Generate(It.IsAny <int>())).Returns <int>(v => new byte[v]); TypeMap.Register.Singleton <IRandomGenerator>(() => mock.Object); IDerivedKey keyEncryptingKey = new V2DerivedKey(new Passphrase("secret"), new Salt(256), 100, 256); V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), keyEncryptingKey, 250); SymmetricKey key = header.MasterKey; Assert.That(key.GetBytes(), Is.EquivalentTo(new byte[32])); SymmetricIV iv = header.MasterIV; Assert.That(iv.GetBytes(), Is.EquivalentTo(new byte[16])); }
/// <summary> /// Loads an AxCrypt file from the specified reader. After this, the reader is positioned to /// read encrypted data. /// </summary> /// <param name="passphrase">The passphrase.</param> /// <param name="cryptoId">The crypto identifier.</param> /// <param name="reader">The reader.</param> /// <param name="headers">The headers.</param> /// <returns> /// True if the key was valid, false if it was wrong. /// </returns> private bool Load(Passphrase passphrase, Guid cryptoId, AxCryptReader reader, Headers headers) { ResetState(); if (cryptoId == new V1Aes128CryptoFactory().CryptoId) { return(PassphraseIsValid); } _reader = reader; CryptoFactory = Resolve.CryptoFactory.Create(cryptoId); V2KeyWrapHeaderBlock keyWrap = headers.FindHeaderBlock <V2KeyWrapHeaderBlock>(); IDerivedKey key = CryptoFactory.RestoreDerivedKey(passphrase, keyWrap.DerivationSalt, keyWrap.DerivationIterations); keyWrap.SetDerivedKey(CryptoFactory, key); DocumentHeaders = new V2DocumentHeaders(keyWrap); PassphraseIsValid = DocumentHeaders.Load(headers); Properties = EncryptedProperties.Create(this); return(PassphraseIsValid); }
public void TestUnknownEncryptedHeader() { Headers headers = new Headers(); IDerivedKey key = new V2DerivedKey(new Passphrase("A key"), 256); headers.HeaderBlocks.Add(new PreambleHeaderBlock()); headers.HeaderBlocks.Add(new VersionHeaderBlock(new byte[] { 4, 0, 2, 0, 0 })); V2KeyWrapHeaderBlock wrapHeader = new V2KeyWrapHeaderBlock(new V2Aes256CryptoFactory(), key, 10); headers.HeaderBlocks.Add(wrapHeader); headers.HeaderBlocks.Add(new FileInfoEncryptedHeaderBlock(new byte[0])); headers.HeaderBlocks.Add(new V2CompressionEncryptedHeaderBlock(new byte[1])); headers.HeaderBlocks.Add(new V2UnicodeFileNameInfoEncryptedHeaderBlock(new byte[0])); headers.HeaderBlocks.Add(new UnknownEncryptedHeaderBlock(new byte[0])); headers.HeaderBlocks.Add(new DataHeaderBlock()); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(wrapHeader); Assert.Throws <InternalErrorException>(() => documentHeaders.Load(headers)); }
public void TestLoadWithInvalidPassphrase() { Headers headers = new Headers(); V2Aes256CryptoFactory cryptoFactory = new V2Aes256CryptoFactory(); headers.HeaderBlocks.Add(new PreambleHeaderBlock()); headers.HeaderBlocks.Add(new VersionHeaderBlock(new byte[] { 4, 0, 2, 0, 0 })); V2KeyWrapHeaderBlock originalKeyWrapBlock = new V2KeyWrapHeaderBlock(cryptoFactory, new V2DerivedKey(new Passphrase("RealKey"), 256), 10); V2KeyWrapHeaderBlock headerKeyWrapBlock = new V2KeyWrapHeaderBlock(originalKeyWrapBlock.GetDataBlockBytes()); headers.HeaderBlocks.Add(headerKeyWrapBlock); headers.HeaderBlocks.Add(new FileInfoEncryptedHeaderBlock(new byte[0])); headers.HeaderBlocks.Add(new V2CompressionEncryptedHeaderBlock(new byte[1])); headers.HeaderBlocks.Add(new V2UnicodeFileNameInfoEncryptedHeaderBlock(new byte[0])); headers.HeaderBlocks.Add(new DataHeaderBlock()); IDerivedKey key; key = cryptoFactory.RestoreDerivedKey(new Passphrase("WrongKey"), headerKeyWrapBlock.DerivationSalt, headerKeyWrapBlock.DerivationIterations); headerKeyWrapBlock.SetDerivedKey(cryptoFactory, key); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(headerKeyWrapBlock); Assert.That(documentHeaders.Load(headers), Is.False); key = cryptoFactory.RestoreDerivedKey(new Passphrase("AnotherWrongKey"), headerKeyWrapBlock.DerivationSalt, headerKeyWrapBlock.DerivationIterations); headerKeyWrapBlock.SetDerivedKey(cryptoFactory, key); documentHeaders = new V2DocumentHeaders(headerKeyWrapBlock); Assert.That(documentHeaders.Load(headers), Is.False); key = cryptoFactory.RestoreDerivedKey(new Passphrase("RealKey"), headerKeyWrapBlock.DerivationSalt, headerKeyWrapBlock.DerivationIterations); headerKeyWrapBlock.SetDerivedKey(cryptoFactory, key); documentHeaders = new V2DocumentHeaders(headerKeyWrapBlock); Assert.That(documentHeaders.Load(headers), Is.True); }
public static void TestGetCryptoFromHeaders(CryptoImplementation cryptoImplementation) { SetupAssembly.AssemblySetupCrypto(cryptoImplementation); Headers headers = new Headers(); V2DocumentHeaders documentHeaders = new V2DocumentHeaders(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 10); using (V2HmacStream <MemoryStream> stream = V2HmacStream.Create <MemoryStream>(new V2HmacCalculator(new SymmetricKey(new byte[0])), new MemoryStream())) { documentHeaders.WriteStartWithHmac(stream); stream.Flush(); stream.Chained.Position = 0; using (V2AxCryptReader reader = new V2AxCryptReader(new LookAheadStream(stream.Chained))) { while (reader.Read()) { if (reader.CurrentItemType == AxCryptItemType.HeaderBlock) { headers.HeaderBlocks.Add(reader.CurrentHeaderBlock); } } SymmetricKey dataEncryptingKey = documentHeaders.Headers.FindHeaderBlock <V2KeyWrapHeaderBlock>().MasterKey; V2KeyWrapHeaderBlock keyWrap = headers.FindHeaderBlock <V2KeyWrapHeaderBlock>(); IDerivedKey key = new V2Aes256CryptoFactory().RestoreDerivedKey(new Passphrase("passphrase"), keyWrap.DerivationSalt, keyWrap.DerivationIterations); keyWrap.SetDerivedKey(new V2Aes256CryptoFactory(), key); Assert.That(dataEncryptingKey, Is.EqualTo(keyWrap.MasterKey)); key = new V2Aes256CryptoFactory().RestoreDerivedKey(new Passphrase("wrong"), keyWrap.DerivationSalt, keyWrap.DerivationIterations); keyWrap.SetDerivedKey(new V2Aes256CryptoFactory(), key); Assert.That(dataEncryptingKey, Is.Not.EqualTo(keyWrap.MasterKey)); } } }