예제 #1
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            var key = param.Key.ToBytes();

            var engineParam = new EngineInitParameters(param.Direction, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var numberOfBlocks = GetNumberOfBlocks(param.Payload.BitLength);
            var outBuffer      = GetOutputBuffer(param.Payload.BitLength);

            if (param.Direction == BlockCipherDirections.Encrypt)
            {
                Encrypt(param, numberOfBlocks, outBuffer);
            }
            else
            {
                Decrypt(param, numberOfBlocks, outBuffer);
            }

            var output = new BitString(outBuffer);

            return(new SymmetricCipherResult(output.GetMostSignificantBits(param.Payload.BitLength)));
        }
예제 #2
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            if (param.Payload.BitLength / _engine.BlockSizeBits < PARTITIONS)
            {
                throw new ArgumentException($"CBCI mode needs at least {PARTITIONS} blocks of data");
            }

            var payloads = TdesPartitionHelpers.TriPartitionBitString(param.Payload);
            var ivs      = TdesPartitionHelpers.SetupIvs(param.Iv);
            var key      = param.Key.ToBytes();

            var engineParam = new EngineInitParameters(param.Direction, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var outBuffer = GetOutputBuffer(param.Payload.BitLength);

            if (param.Direction == BlockCipherDirections.Encrypt)
            {
                Encrypt(param, payloads, ivs, outBuffer);
            }
            else
            {
                Decrypt(param, payloads, ivs, outBuffer);
            }

            return(new SymmetricCipherResult(
                       new BitString(outBuffer).GetMostSignificantBits(param.Payload.BitLength)
                       ));
        }
예제 #3
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            var key = param.Key.ToBytes();
            var actualBitsToProcess = param.Payload.BitLength;

            param.Payload = BitString.PadToNextByteBoundry(param.Payload);

            // CFB always utilizes engine in encrypt mode
            var engineParam = new EngineInitParameters(BlockCipherDirections.Encrypt, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var numberOfSegments = actualBitsToProcess / _shiftRegisterStrategy.ShiftSize;
            var outBuffer        = GetOutputBuffer(param.Payload.BitLength);

            if (param.Direction == BlockCipherDirections.Encrypt)
            {
                Encrypt(param, numberOfSegments, outBuffer);
            }
            else
            {
                Decrypt(param, numberOfSegments, outBuffer);
            }

            return(new SymmetricCipherResult(
                       new BitString(outBuffer).GetMostSignificantBits(actualBitsToProcess)
                       ));
        }
예제 #4
0
        public SymmetricCipherResult ProcessPayload(IFfxModeBlockCipherParameters param)
        {
            var key = param.Key.ToBytes();

            var engineParam = new EngineInitParameters(param.Direction, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            CheckPayloadRequirements(param.Payload);

            if (param.Direction == BlockCipherDirections.Encrypt)
            {
                return(new SymmetricCipherResult(Encrypt(param)));
            }

            return(new SymmetricCipherResult(Decrypt(param)));
        }
예제 #5
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            var key = param.Key.ToBytes();

            var engineParam = new EngineInitParameters(param.Direction, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var numberOfBlocks = GetNumberOfBlocks(param.Payload.BitLength);
            var outBuffer      = GetOutputBuffer(_engine.BlockSizeBits);

            // Same logic regardless of encrypt/decrypt
            ProcessPayload(param, numberOfBlocks, outBuffer);

            return(new SymmetricCipherResult(
                       new BitString(outBuffer)
                       ));
        }
예제 #6
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            var key = param.Key.ToBytes();

            // OFB always utilizes engine in encrypt mode
            var engineParam = new EngineInitParameters(BlockCipherDirections.Encrypt, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var numberOfBlocks = GetNumberOfBlocks(param.Payload.BitLength);
            var outBuffer      = GetOutputBuffer(param.Payload.BitLength);

            ProcessPayload(param, numberOfBlocks, outBuffer);

            return(new SymmetricCipherResult(
                       new BitString(outBuffer).GetMostSignificantBits(param.Payload.BitLength)
                       ));
        }
예제 #7
0
        public SymmetricCounterResult ExtractIvs(ICounterModeBlockCipherParameters parameters)
        {
            BitString plainText, cipherText;

            if (parameters.Direction == BlockCipherDirections.Encrypt)
            {
                plainText  = parameters.Payload;
                cipherText = parameters.Result;
            }
            else
            {
                plainText  = parameters.Result;
                cipherText = parameters.Payload;
            }

            plainText  = BitString.PadToModulus(plainText, _engine.BlockSizeBits);
            cipherText = BitString.PadToModulus(cipherText, _engine.BlockSizeBits);

            var numberOfBlocks = GetNumberOfBlocks(plainText.BitLength);
            var ivs            = new List <BitString>();
            var engineParam    = new EngineInitParameters(BlockCipherDirections.Decrypt, parameters.Key.ToBytes(), parameters.UseInverseCipherMode);

            _engine.Init(engineParam);

            for (var i = 0; i < numberOfBlocks; i++)
            {
                var blockPt = plainText.MSBSubstring(i * _engine.BlockSizeBits, _engine.BlockSizeBits);
                var blockCt = cipherText.MSBSubstring(i * _engine.BlockSizeBits, _engine.BlockSizeBits);

                var xor       = BitString.XOR(blockPt, blockCt).ToBytes();
                var outBuffer = GetOutputBuffer(blockPt.BitLength);
                _engine.ProcessSingleBlock(xor, outBuffer, 0);

                ivs.Add(new BitString(outBuffer));
            }

            return(new SymmetricCounterResult(parameters.Result, ivs));
        }