Ejemplo n.º 1
0
        /// <summary>
        ///     Initialises a stream cipher from cipher configuration DTO. Used by constructor.
        /// </summary>
        private static ICipherWrapper InitStreamCipher(bool encrypting, CipherConfiguration config, byte[] key, out int maxDelta)
        {
            var streamConfigWrapper = new StreamCipherConfigurationWrapper(config);

            if (key.Length != streamConfigWrapper.KeySizeBytes)
            {
                throw new ArgumentException("Key is not of the length declared in the cipher configuration.",
                                            "key");
            }

            StreamCipherEngine streamCipher;

            try {
                streamCipher = CipherFactory.CreateStreamCipher(streamConfigWrapper.GetStreamCipher());
                streamCipher.Init(encrypting, key, streamConfigWrapper.GetNonce());
            } catch (Exception e) {
                throw new ConfigurationInvalidException("Configuration of stream cipher is invalid.",
                                                        e.InnerException);
            }

            // This should always be 0, but we'll do it anyway...
            maxDelta = Athena.Cryptography.StreamCiphers[streamCipher.Identity].MaximumOutputSizeDifference(encrypting);

            return(new StreamCipherWrapper(encrypting, streamCipher, strideIncreaseFactor: 2));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Initialises a block cipher from cipher configuration DTO. Used by constructor.
        /// </summary>
        private static ICipherWrapper InitBlockCipher(bool encrypting, CipherConfiguration config, byte[] key, out int maxDelta)
        {
            var blockConfigWrapper = new BlockCipherConfigurationWrapper(config);

            if (key.Length != blockConfigWrapper.KeySizeBytes)
            {
                throw new ArgumentException("Key is not of the length declared in the cipher configuration.",
                                            "key");
            }

            BlockCipherBase blockCipherPrimitive = CipherFactory.CreateBlockCipher(blockConfigWrapper.GetBlockCipher(),
                                                                                   blockConfigWrapper.GetBlockSizeBits());
            // Overlay the cipher with the mode of operation
            BlockCipherModeBase blockCipher;

            try {
                blockCipher = CipherFactory.OverlayBlockCipherWithMode(blockCipherPrimitive, blockConfigWrapper.Mode);
            } catch (Exception e) {
                throw new ConfigurationInvalidException(
                          "Configuration of block cipher mode of operation is invalid.", e.InnerException);
            }
            IBlockCipherPadding padding     = null;
            BlockCipherPadding  paddingEnum = blockConfigWrapper.GetPadding();

            if (paddingEnum != BlockCipherPadding.None)
            {
                padding = CipherFactory.CreatePadding(paddingEnum);
                padding.Init(StratCom.EntropySupplier);
            }

            maxDelta = Athena.Cryptography.BlockCiphers[blockCipherPrimitive.Identity].MaximumOutputSizeDifference(encrypting);

            blockCipher.Init(encrypting, key, blockConfigWrapper.GetInitialisationVector());
            return(new BlockCipherWrapper(encrypting, blockCipher, padding));
        }