Example #1
0
        /// <summary>
        /// Instantiate a transformation
        /// </summary>
        /// <param name="key">The key</param>
        /// <param name="iv">Initial Vector, or null for a zero vector.</param>
        public V1AesCrypto(ICryptoFactory factory, SymmetricKey key, SymmetricIV iv)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (key.Size != 128)
            {
                throw new ArgumentException("Key length is invalid.");
            }
            using (SymmetricAlgorithm algorithm = CreateRawAlgorithm())
            {
                iv = iv ?? new SymmetricIV(new byte[algorithm.BlockSize / 8]);
                if (iv.Length != algorithm.BlockSize / 8)
                {
                    throw new ArgumentException("The IV length must be the same as the algorithm block length.");
                }
            }

            Key = key;
            _iv = iv;
        }
        public V2DocumentHeaders(EncryptionParameters encryptionParameters, long keyWrapIterations)
        {
            if (encryptionParameters == null)
            {
                throw new ArgumentNullException("encryptionParameters");
            }

            _headers = new Headers();

            _headers.HeaderBlocks.Add(new PreambleHeaderBlock());
            _headers.HeaderBlocks.Add(new VersionHeaderBlock(_version));

            ICryptoFactory       cryptoFactory    = Resolve.CryptoFactory.Create(encryptionParameters.CryptoId);
            IDerivedKey          keyEncryptingKey = cryptoFactory.CreateDerivedKey(encryptionParameters.Passphrase);
            V2KeyWrapHeaderBlock keyWrap          = new V2KeyWrapHeaderBlock(cryptoFactory, keyEncryptingKey, keyWrapIterations);

            _headers.HeaderBlocks.Add(keyWrap);
            _keyStreamFactory = keyWrap;

            foreach (UserPublicKey publicKey in encryptionParameters.PublicKeys)
            {
                _headers.HeaderBlocks.Add(new V2AsymmetricKeyWrapHeaderBlock(publicKey, keyWrap.MasterKey, keyWrap.MasterIV));
            }
            _headers.HeaderBlocks.Add(new V2AsymmetricRecipientsEncryptedHeaderBlock(GetHeaderCrypto(HeaderBlockType.AsymmetricRecipients))
            {
                Recipients = new Recipients(encryptionParameters.PublicKeys)
            });
            _headers.HeaderBlocks.Add(new FileInfoEncryptedHeaderBlock(GetHeaderCrypto(HeaderBlockType.FileInfo)));
            _headers.HeaderBlocks.Add(new V2CompressionEncryptedHeaderBlock(GetHeaderCrypto(HeaderBlockType.Compression)));
            _headers.HeaderBlocks.Add(new V2UnicodeFileNameInfoEncryptedHeaderBlock(GetHeaderCrypto(HeaderBlockType.UnicodeFileNameInfo)));
            _headers.HeaderBlocks.Add(new V2AlgorithmVerifierEncryptedHeaderBlock(GetHeaderCrypto(HeaderBlockType.AlgorithmVerifier)));
            _headers.HeaderBlocks.Add(new DataHeaderBlock());

            SetDataEncryptingCryptoForEncryptedHeaderBlocks(_headers.HeaderBlocks);
        }
        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.");
        }
Example #4
0
        public V2KeyWrapHeaderBlock(ICryptoFactory cryptoFactory, IDerivedKey keyEncryptingKey, long keyWrapIterations)
            : this(Resolve.RandomGenerator.Generate(DATABLOCK_LENGTH))
        {
            _cryptoFactory    = cryptoFactory;
            _keyEncryptingKey = keyEncryptingKey;

            Initialize(keyWrapIterations);
        }
            public WrapIterator(Guid cryptoId)
            {
                ICryptoFactory factory = Resolve.CryptoFactory.Create(cryptoId);

                _dummyCrypto = factory.CreateCrypto(factory.CreateDerivedKey(new Passphrase("A dummy passphrase")).DerivedKey, null, 0);
                _dummySalt   = new Salt(_dummyCrypto.Key.Size);
                _dummyKey    = new SymmetricKey(_dummyCrypto.Key.Size);
            }
        /// <summary>
        /// Instantiate a thumb print
        /// </summary>
        /// <param name="passphrase">The passphrase to thumbprint.</param>
        /// <param name="salt">The salt to use.</param>
        public SymmetricKeyThumbprint(Passphrase passphrase, Salt salt, long keyWrapIterations)
        {
            if (passphrase == null)
            {
                throw new ArgumentNullException("passphrase");
            }
            if (salt == null)
            {
                throw new ArgumentNullException("salt");
            }

            ICryptoFactory factory = Resolve.CryptoFactory.Minimum;
            ICrypto        crypto  = factory.CreateCrypto(factory.RestoreDerivedKey(passphrase, salt, CryptoFactory.DerivationIterations).DerivedKey, null, 0);
            KeyWrap        keyWrap = new KeyWrap(salt, keyWrapIterations, KeyWrapMode.Specification);

            byte[] wrap = keyWrap.Wrap(crypto, crypto.Key);

            _bytes = wrap.Reduce(6);
        }
 public V1AxCryptDocument()
 {
     CryptoFactory        = new V1Aes128CryptoFactory();
     AsymmetricRecipients = new UserPublicKey[0];
 }
Example #8
0
 public void SetDerivedKey(ICryptoFactory cryptoFactory, IDerivedKey keyEncryptingKey)
 {
     _cryptoFactory    = cryptoFactory;
     _keyEncryptingKey = keyEncryptingKey;
     _unwrappedKeyData = null;
 }
Example #9
0
 /// <summary>
 /// Sets the private key.
 /// </summary>
 /// <param name="privateKey">The private key.</param>
 public void SetPrivateKey(ICryptoFactory cryptoFactory, IAsymmetricPrivateKey privateKey)
 {
     _cryptoFactory      = cryptoFactory;
     _privateKey         = privateKey;
     _decryptedDataBlock = null;
 }
Example #10
0
 public static void SetCurrent(ICryptoFactory cryptoFactory)
 {
     _currentCryptoFactory = cryptoFactory;
 }
Example #11
0
 public CryptoService(ICryptoFactory factory)
 {
     _factory = factory;
 }