Esempio n. 1
0
        /// <summary>
        /// Create a configuration for a block cipher.
        /// </summary>
        /// <param name="cipher">Block cipher to use.</param>
        /// <param name="mode">Mode of operation for the cipher.</param>
        /// <param name="padding">Padding scheme to use with the mode, where necessary (e.g. CBC).</param>
        /// <param name="keySize">Key size to use, in bits.</param>
        /// <param name="blockSize">Cipher block size to use, in bits.</param>
        /// <returns>Block cipher configuration DTO.</returns>
        public static CipherConfiguration CreateBlockCipherConfiguration(BlockCipher cipher,
                                                                         BlockCipherMode mode, BlockCipherPadding padding, int?keySize = null, int?blockSize = null)
        {
            var config = new CipherConfiguration {
                Type = CipherType.Block
            };

            // Set the key size
            int keySizeNonNull = keySize ?? Athena.Cryptography.BlockCiphers[cipher].DefaultKeySizeBits;

            if (keySize == null || Athena.Cryptography.BlockCiphers[cipher].AllowableKeySizesBits.Contains(keySizeNonNull))
            {
                config.KeySizeBits = keySizeNonNull;
            }
            else
            {
                throw new CipherKeySizeException(cipher, keySizeNonNull);
            }

            // Set the block size
            int blockSizeNonNull = blockSize ?? Athena.Cryptography.BlockCiphers[cipher].DefaultBlockSizeBits;

            if (blockSize == null ||
                Athena.Cryptography.BlockCiphers[cipher].AllowableBlockSizesBits.Contains(blockSizeNonNull))
            {
                config.BlockSizeBits = blockSizeNonNull;
            }
            else
            {
                throw new BlockSizeException(cipher, blockSizeNonNull);
            }

            // Set the mode
            if (Athena.Cryptography.BlockCipherModes[mode].PaddingRequirement == PaddingRequirement.Always &&
                padding == BlockCipherPadding.None)
            {
                throw new ArgumentException(mode +
                                            " mode must be used with padding or errors will occur when plaintext length is not equal to or a multiple of the block size.");
            }

            config.ModeName    = mode.ToString();
            config.PaddingName = padding.ToString();
            config.CipherName  = cipher.ToString();

            config.InitialisationVector = new byte[config.BlockSizeBits.Value / 8];
            StratCom.EntropySupplier.NextBytes(config.InitialisationVector);

            return(config);
        }
Esempio n. 2
0
 /// <summary>
 ///     Scheme utillised to 'pad' blocks to full size where required.
 ///     What any unused space in a block is filled with.
 ///     Set to empty if using block cipher in streaming mode.
 /// </summary>
 public void SetPadding(BlockCipherPadding value)
 {
     RawConfiguration.PaddingName = value.ToString();
 }