Exemple #1
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()));
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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 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);
        }