예제 #1
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));
        }
예제 #2
0
        /**
         * create a standard MAC based on a block cipher with the size of the
         * MAC been given in bits.
         * <p/>
         * Note: the size of the MAC must be at least 24 bits (FIPS Publication 81),
         * or 16 bits if being used as a data authenticator (FIPS Publication 113),
         * and in general should be less than the size of the block cipher as it reduces
         * the chance of an exhaustive attack (see Handbook of Applied Cryptography).
         *
         * @param cipher        the cipher to be used as the basis of the MAC generation.
         * @param macSizeInBits the size of the MAC in bits, must be a multiple of 8 and @lt;= 128.
         */
        public CMac(
            BlockCipherBase cipher,
            int macSizeInBits)
        {
            if ((macSizeInBits % 8) != 0)
            {
                throw new ArgumentException("MAC size must be multiple of 8");
            }

            if (macSizeInBits > (cipher.BlockSize * 8))
            {
                throw new ArgumentException(
                          "MAC size must be less or equal to "
                          + (cipher.BlockSize * 8));
            }

            if (cipher.BlockSize != 8 && cipher.BlockSize != 16)
            {
                throw new ArgumentException(
                          "Block size must be either 64 or 128 bits");
            }

            this._cipher     = new CbcBlockCipher(cipher);
            this._outputSize = macSizeInBits / 8;

            _zeroes    = new byte[cipher.BlockSize];
            _nullCbcIv = new byte[cipher.BlockSize];

            _buf    = new byte[cipher.BlockSize];
            _bufOff = 0;

            _mac = new byte[this._outputSize];
        }
예제 #3
0
 /// <summary>
 /// Instantiates a CFB mode block cipher wrapper.
 /// </summary>
 /// <param name="cipher">Cipher to be used as the basis for the feedback mode.</param>
 /// <param name="feedbackSize">
 /// Bytes copied in feedback mechanism per block.
 /// Defaults to full block, but other values can be used to produce a self-synchronising
 /// stream cipher, such as CFB-8 (1 byte feedback - slow).
 /// </param>
 public CfbBlockCipher(BlockCipherBase cipher, int?feedbackSize = null)
     : base(BlockCipherMode.Cfb, cipher)
 {
     _feedbackSize = feedbackSize ?? cipher.BlockSize;
     _cfbV         = new byte[cipher.BlockSize];
     _cfbOutV      = new byte[cipher.BlockSize];
 }
예제 #4
0
 /*		*
  * Constructs a Poly1305 MAC, using a 128 bit block cipher.
  */
 public Poly1305Mac(BlockCipherBase cipher)
 {
     if (cipher.BlockSize != BLOCK_SIZE)
     {
         throw new ArgumentException("Poly1305 requires a 128 bit block cipher.");
     }
     this.cipher = cipher;
 }
예제 #5
0
        /// <summary>
        ///     Implements a mode of operation on top of an existing block cipher.
        /// </summary>
        /// <param name="cipher">The block cipher to implement this mode of operation on top of.</param>
        /// <param name="modeEnum">The mode of operation to implement.</param>
        /// <returns>
        ///     A <see cref="BlockCipherBase" /> object implementing the relevant mode of operation,
        ///     overlaying the supplied symmetric block cipher.
        /// </returns>
        public static BlockCipherModeBase OverlayBlockCipherWithMode(BlockCipherBase cipher, BlockCipherMode modeEnum)
        {
            Contract.Requires <ArgumentNullException>(cipher != null);
            Contract.Requires(modeEnum != BlockCipherMode.None, "Cannot instantiate null mode of operation.");

            BlockCipherModeBase cipherMode = ModeInstantiatorsBlock[modeEnum](cipher);

            return(cipherMode);
        }
예제 #6
0
 public CbcBlockCipher(BlockCipherBase cipher)
     : base(BlockCipherMode.Cbc, cipher)
 {
     this._cbcV     = new byte[CipherBlockSize];
     this._cbcNextV = new byte[CipherBlockSize];
 }
예제 #7
0
 /**
  * create a standard MAC based on a CBC block cipher (64 or 128 bit block).
  * This will produce an authentication code the length of the block size
  * of the cipher.
  *
  * @param cipher the cipher to be used as the basis of the MAC generation.
  */
 public CMac(
     BlockCipherBase cipher)
     : this(cipher, cipher.BlockSize * 8)
 {
 }
예제 #8
0
 public OfbBlockCipher(BlockCipherBase cipher)
     : base(BlockCipherMode.Ofb, cipher)
 {
     this._ofbV    = new byte[CipherBlockSize];
     this._ofbOutV = new byte[CipherBlockSize];
 }
예제 #9
0
 public Poly1305Mac()
 {
     this.cipher = null;
 }
예제 #10
0
 public CtrBlockCipher(BlockCipherBase cipher) : base(BlockCipherMode.Ctr, cipher)
 {
     _counter    = new byte[CipherBlockSize];
     _counterOut = new byte[CipherBlockSize];
 }