Ejemplo n.º 1
0
        public IBlockTransformer CreateDecryptor(BlockCipherMode mode, byte[] key, byte[] iv)
        {
            switch (PlatformDetector.GetCurrentPlatform())
            {
            case OperatingPlatform.Windows:
                return(new BCryptAesTransformer(mode, key, iv, false));

            default:
                throw new PlatformNotSupportedException("The current system platform is not supported.");
            }
        }
Ejemplo n.º 2
0
        public static void SetBlockChainingMode(IntPtr hAlgorithmProvider, BlockCipherMode mode)
        {
            string chainingModeValue;

            switch (mode)
            {
            case BlockCipherMode.CBC:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_CBC;
                break;

            case BlockCipherMode.CCM:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_CCM;
                break;

            case BlockCipherMode.CFB:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_CFB;
                break;

            case BlockCipherMode.ECB:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_ECB;
                break;

            case BlockCipherMode.GCM:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_GCM;
                break;

            case BlockCipherMode.Unspecified:
                chainingModeValue = BCryptConstants.BCRYPT_CHAIN_MODE_NA;
                break;

            default:
                throw new ArgumentException("The specified block cipher chaining mode is not recognized.", nameof(mode));
            }

            GCHandle gcChainingModeValue = GCHandle.Alloc(chainingModeValue, GCHandleType.Pinned);

            uint result;

            try
            {
                result = BCryptCore.BCryptSetProperty(hAlgorithmProvider, BCryptConstants.BCRYPT_CHAINING_MODE, gcChainingModeValue.AddrOfPinnedObject(), (ulong)chainingModeValue.Length, 0);
            }
            finally
            {
                gcChainingModeValue.Free();
            }

            if (result != 0)
            {
                throw new SystemException("An error was encountered while setting the cipher chaining mode.");
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private static void SendKeysToNodes(BlockCipherMode mode)
        {
            switch (mode)
            {
            case BlockCipherMode.ECB:
                _socketA.WriteBytes(Secrets.KeyEcb.Encrypt(Secrets.Key3).GetB());
                break;

            case BlockCipherMode.CFB:
                _socketB.WriteBytes(Secrets.KeyCfb.Encrypt(Secrets.Key3).GetB());
                _socketB.WriteBytes(Secrets.Iv.Encrypt(Secrets.Key3).GetB());
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
        }
Ejemplo n.º 5
0
        public ClientBehavior(BlockCipherMode modeChoice)
        {
            this.modeChoice = modeChoice;
            _sender         = GetClientSocket();
            this.modeFinal  = GetFinalMode();
            Debug("Final mode chosen");
            alg = getSecurityAlgorithm();
            return;

            var inputList     = "heyy looka this 1234g5g5asd789a9sd3".SplitInBlocks();
            var encryptedList = alg.EncryptList(inputList);
            var decryptedlist = alg.DecryptList(encryptedList);

            Debug(inputList.ToString());
            Debug(encryptedList.ToString());
            Debug(decryptedlist.ToString());
        }
Ejemplo n.º 6
0
        public BCryptAesTransformer(BlockCipherMode mode, byte[] key, byte[] iv, bool isEncrypting)
        {
            this.mode = mode;

            uint result = BCryptCore.BCryptOpenAlgorithmProvider(out this.hAlgorithmProvider, "AES", null, 0);

            if (result != 0)
            {
                throw new SystemException("An error was encountered while opening the algorithm provider.");
            }

            BCryptHelper.SetBlockChainingMode(this.hAlgorithmProvider, mode);

            result = BCryptCore.BCryptGenerateSymmetricKey(this.hAlgorithmProvider, out this.hKey, null, 0, key, (ulong)key.Length, 0);
            if (result != 0)
            {
                throw new SystemException("An error was encountered while generating a symmetric key.");
            }

            this.iv           = iv;
            this.isEncrypting = isEncrypting;
        }
Ejemplo n.º 7
0
 protected BlockCipher(BlockCipherMode mode) => Mode = mode;
Ejemplo n.º 8
0
 public SecurityAlgorithm(Block key, Block iv, BlockCipherMode mode)
 {
     _key  = key ?? throw new ArgumentNullException(nameof(key));
     _iv   = iv != null || mode != BlockCipherMode.CFB ? iv : throw new ArgumentNullException(nameof(iv));
     _mode = mode;
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Instantiate the block cipher mode of operation.
 /// </summary>
 /// <param name="modeIdentity">Type of the mode of operation. Used to provide and verify configuration.</param>
 /// <param name="cipher">Block cipher to wrap with operation mode.</param>
 /// <param name="blockSize"></param>
 protected BlockCipherModeBase(BlockCipherMode modeIdentity, BlockCipherBase cipher, int?blockSize = null)
 {
     CipherModeIdentity = modeIdentity;
     BlockCipher        = cipher;
     CipherBlockSize    = blockSize ?? cipher.BlockSize;
 }