コード例 #1
0
        public static void TestMethods()
        {
            SymmetricKey key = new SymmetricKey(128);

            Assert.That(key.GetBytes().Length, Is.EqualTo(16), "The default key length is 128 bits.");
            Assert.That(key.GetBytes(), Is.Not.EquivalentTo(new byte[16]), "A random key cannot be expected to be all zeros.");

            SymmetricKey specifiedKey = new SymmetricKey(key.GetBytes());

            Assert.That(specifiedKey.GetBytes(), Is.EquivalentTo(key.GetBytes()), "The specified key should contain the bits given to it.");
        }
コード例 #2
0
        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)));
        }
コード例 #3
0
        public void TestMethods()
        {
            SymmetricKey key  = new SymmetricKey(128);
            HMAC         hmac = New <AxCryptHMACSHA1>().Initialize(key);

            Assert.That(hmac.Key(), Is.EquivalentTo(key.GetBytes()), "Ensure that we're using the specified key.");
        }
コード例 #4
0
        public byte[] UnwrapMasterKey(SymmetricKey keyEncryptingKey, byte fileVersionMajor)
        {
            if (keyEncryptingKey == null)
            {
                throw new ArgumentNullException("keyEncryptingKey");
            }

            byte[]       wrappedKeyData         = GetKeyData();
            Salt         salt                   = Salt;
            SymmetricKey masterKeyEncryptingKey = keyEncryptingKey;

            if (fileVersionMajor <= 1)
            {
                // Due to a bug in 1.1 and earlier we only used a truncated part of the key and salt :-(
                // Compensate for this here. Users should be warned if FileVersionMajor <= 1 .
                byte[] badKey = new byte[masterKeyEncryptingKey.Size / 8];
                Array.Copy(keyEncryptingKey.GetBytes(), 0, badKey, 0, 4);
                masterKeyEncryptingKey = new SymmetricKey(badKey);

                byte[] badSalt = new byte[salt.Length];
                Array.Copy(salt.GetBytes(), 0, badSalt, 0, 4);
                salt = new Salt(badSalt);
            }

            KeyWrap keyWrap = new KeyWrap(salt, KeyWrapIterations, KeyWrapMode.AxCrypt);

            byte[] unwrappedKeyData = keyWrap.Unwrap(Resolve.CryptoFactory.Legacy.CreateCrypto(masterKeyEncryptingKey, null, 0), wrappedKeyData);
            return(unwrappedKeyData);
        }
コード例 #5
0
        public void TestUnwrap()
        {
            byte[]  unwrapped;
            KeyWrap keyWrap = new KeyWrap(6, KeyWrapMode.Specification);

            unwrapped = keyWrap.Unwrap(new V1AesCrypto(new V1Aes128CryptoFactory(), _keyEncryptingKey, SymmetricIV.Zero128), _wrapped);

            Assert.That(unwrapped, Is.EquivalentTo(_keyData.GetBytes()), "Unwrapped the wrong data");
        }
コード例 #6
0
        public void TestWrapAndUnwrapAxCryptMode()
        {
            SymmetricKey keyToWrap         = new SymmetricKey(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
            Salt         salt              = new Salt(new byte[] { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 });
            long         keyWrapIterations = 12345;

            byte[]  wrapped;
            KeyWrap keyWrap = new KeyWrap(salt, keyWrapIterations, KeyWrapMode.AxCrypt);

            wrapped = keyWrap.Wrap(new V1AesCrypto(new V1Aes128CryptoFactory(), _keyEncryptingKey, SymmetricIV.Zero128), keyToWrap);
            byte[] unwrapped;
            keyWrap   = new KeyWrap(salt, keyWrapIterations, KeyWrapMode.AxCrypt);
            unwrapped = keyWrap.Unwrap(new V1AesCrypto(new V1Aes128CryptoFactory(), _keyEncryptingKey, SymmetricIV.Zero128), wrapped);

            Assert.That(unwrapped, Is.EquivalentTo(keyToWrap.GetBytes()), "The unwrapped data should be equal to original.");
        }
コード例 #7
0
        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]));
        }
コード例 #8
0
        public static void TestConstructorFromKeyUsingKeyWrap128TestVectors()
        {
            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 == (16 + 16)))).Returns(_keyData128.GetBytes());
            TypeMap.Register.Singleton <IRandomGenerator>(() => mock.Object);

            V2KeyWrapHeaderBlock header = new V2KeyWrapHeaderBlock(new V2Aes128CryptoFactory(), _keyEncryptingKey128, 6);

            byte[] bytes   = header.GetDataBlockBytes();
            byte[] wrapped = new byte[24];
            Array.Copy(bytes, 0, wrapped, 0, wrapped.Length);

            Assert.That(wrapped, Is.EquivalentTo(_wrapped128));
        }