Exemple #1
0
        public KeySchedule(byte[] rawkey, BlockCipherDirections function, bool ignoreParity)
        {
            Schedule = new uint[16, 4];
            Function = function;
            if (!ignoreParity && !rawkey.AllBytesHaveOddParity())
            {
                throw new ArgumentException("Supplied key does not have odd partiy");
            }

            long pcct = 0;
            long pcdt = 0;

            long[] pc2out = new long[2];
            for (int idx = 0; idx < 28; idx++)
            {
                pcct |= rawkey.GetKeyBit(pc1c[idx]);
                pcdt |= rawkey.GetKeyBit(pc1d[idx]);
                if (idx < 27)
                {
                    pcct <<= 1;
                    pcdt <<= 1;
                }
            }

            /* Now, pcct and pcdt have the values on which we can apply
             * pc2 and select the key bits, storing them in pc2out[0] and
             * pc2out[1]. The high order [pc2out[0]] bits all come from
             * pcct, and the low order from pcdt. */

            for (int round = 0; round < NUMBER_OF_ROUNDS; round++)
            {
                pcct = lrot28(pcct);
                pcdt = lrot28(pcdt);
                if (shiftsked[round] == 2)
                {   /* this round needs another shift */
                    pcct = lrot28(pcct);
                    pcdt = lrot28(pcdt);
                }
                pc2out[0]  = pc2otab[0, pcct >> 21];
                pc2out[0] |= pc2otab[1, 0X7F & (pcct >> 14)];
                pc2out[0] |= pc2otab[2, 0X7F & (pcct >> 7)];
                pc2out[0] |= pc2otab[3, 0X7F & pcct];

                pc2out[1]  = pc2otab[4, pcdt >> 21];
                pc2out[1] |= pc2otab[5, 0X7F & (pcdt >> 14)];
                pc2out[1] |= pc2otab[6, 0X7F & (pcdt >> 7)];
                pc2out[1] |= pc2otab[7, 0X7F & pcdt];
                int workingRound = function == BlockCipherDirections.Decrypt ? 15 - round : round;

                Schedule[workingRound, 0]  = (uint)(0XFC00 & (pc2out[0] >> 8));
                Schedule[workingRound, 0] |= (uint)(0XFC & (pc2out[0] >> 4));
                Schedule[workingRound, 1]  = (uint)(0XFC00 & (pc2out[1] >> 8));
                Schedule[workingRound, 1] |= (uint)(0XFC & (pc2out[1] >> 4));

                Schedule[workingRound, 2]  = (uint)(0XFC00 & (pc2out[0] >> 2));
                Schedule[workingRound, 2] |= (uint)(0XFC & (pc2out[0] << 2));
                Schedule[workingRound, 3]  = (uint)(0XFC00 & (pc2out[1] >> 2));
                Schedule[workingRound, 3] |= (uint)(0XFC & (pc2out[1] << 2));
            }
        }
Exemple #2
0
        public void Init(IBlockCipherEngineParameters param)
        {
            if (param == null)
            {
                throw new ArgumentException($"{nameof(param)} is null.");
            }
            if (param.Key == null)
            {
                throw new ArgumentException($"{nameof(param.Key)} is null.");
            }

            var keyLengthInBits = param.Key.Length * _bitsInByte;

            if (!_validKeyLengths.Contains(keyLengthInBits))
            {
                throw new ArgumentOutOfRangeException(nameof(param.Key));
            }

            _useInverseCipher = param.UseInverseCipherMode;

            if (_key == null)
            {
                _key       = param.Key;
                _direction = param.Direction;
                PopulateKeySchedule();
            }
            else if (!param.Key.SequenceEqual(_key) || param.Direction != _direction)
            {
                _key       = param.Key;
                _direction = param.Direction;
                PopulateKeySchedule();
            }
        }
Exemple #3
0
 public CounterModeBlockCipherParameters(
     BlockCipherDirections direction,
     BitString key,
     BitString payload,
     BitString result,
     bool useInverseCipherMode = false)
     : base(direction, key, payload, useInverseCipherMode)
 {
     Result = result;
 }
Exemple #4
0
 /// <summary>
 /// Construction for modes requiring use of an IV
 /// </summary>
 /// <param name="direction"></param>
 /// <param name="iv"></param>
 /// <param name="key"></param>
 /// <param name="payload"></param>
 /// <param name="useInverseCipherMode"></param>
 public ModeBlockCipherParameters(
     BlockCipherDirections direction,
     BitString iv,
     BitString key,
     BitString payload,
     bool useInverseCipherMode = false
     )
     : this(direction, key, payload, useInverseCipherMode)
 {
     Iv = iv;
 }
Exemple #5
0
 public TDESContext(TDESKeys keys, BlockCipherDirections function)
 {
     Schedule = new List <KeySchedule>();
     Function = function;
     for (int keyIdx = 0; keyIdx < keys.Keys.Count; keyIdx++)
     {
         var workingFunction = GetWorkingFunction(keyIdx);
         var keySched        = new KeySchedule(keys.Keys[keyIdx], workingFunction, true);
         Schedule.Add(keySched);
     }
 }
Exemple #6
0
 /// <summary>
 /// Construction for modes that do not require an IV
 /// </summary>
 /// <param name="direction"></param>
 /// <param name="key"></param>
 /// <param name="payload"></param>
 /// <param name="useInverseCipherMode"></param>
 public ModeBlockCipherParameters(
     BlockCipherDirections direction,
     BitString key,
     BitString payload,
     bool useInverseCipherMode = false
     )
 {
     Direction            = direction;
     Key                  = key;
     Payload              = payload;
     UseInverseCipherMode = useInverseCipherMode;
 }
Exemple #7
0
        public void ShouldReturnEncrypResponseWith100Count(int keySize, BlockCipherDirections direction)
        {
            BitString iv      = new BitString(128);
            BitString key     = new BitString(keySize);
            BitString payload = new BitString(128);

            var p = new ModeBlockCipherParameters(
                direction,
                iv,
                key,
                payload
                );

            var result = _subject.ProcessMonteCarloTest(p);

            Assert.AreEqual(100, result.Response.Count);
        }
        public AeadModeBlockCipherParameters(
            BlockCipherDirections direction,
            BitString iv,
            BitString key,
            BitString payload,
            BitString additionalAuthenticatedData,
            BitString tag,
            bool useInverseCipherMode = false
            ) : this(direction, iv, key, payload, additionalAuthenticatedData, tag.BitLength, useInverseCipherMode)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            Tag = tag;
        }
 public AeadModeBlockCipherParameters(
     BlockCipherDirections direction,
     BitString iv,
     BitString key,
     BitString payload,
     BitString additionalAuthenticatedData,
     int tagLength,
     bool useInverseCipherMode = false
     )
 {
     Direction = direction;
     Iv        = iv;
     Key       = key;
     Payload   = payload;
     AdditionalAuthenticatedData = additionalAuthenticatedData;
     TagLength            = tagLength;
     UseInverseCipherMode = useInverseCipherMode;
 }
Exemple #10
0
        public void ShouldRunEncryptOperation100000TimesForTestCase(int keySize, BlockCipherDirections direction)
        {
            BitString iv      = new BitString(128);
            BitString key     = new BitString(keySize);
            BitString payload = new BitString(128);

            var p = new ModeBlockCipherParameters(
                direction,
                iv,
                key,
                payload
                );

            var result = _subject.ProcessMonteCarloTest(p);

            Assert.IsTrue(result.Success, nameof(result.Success));
            _algo.Verify(v => v.ProcessPayload(
                             It.IsAny <ModeBlockCipherParameters>()),
                         Times.Exactly(100000),
                         nameof(_algo.Object.ProcessPayload)
                         );
        }
Exemple #11
0
        public void ShouldReturnErrorMessageOnErrorEncrypt(BlockCipherDirections direction)
        {
            string error = "Algo failure!";

            BitString iv      = new BitString(128);
            BitString key     = new BitString(128);
            BitString payload = new BitString(128);

            _algo
            .Setup(s => s.ProcessPayload(It.IsAny <ModeBlockCipherParameters>()))
            .Throws(new Exception(error));

            var p = new ModeBlockCipherParameters(
                direction,
                iv,
                key,
                payload
                );

            var result = _subject.ProcessMonteCarloTest(p);

            Assert.IsFalse(result.Success, nameof(result.Success));
            Assert.AreEqual(error, result.ErrorMessage, nameof(result.ErrorMessage));
        }
 public EngineInitParameters(BlockCipherDirections direction, byte[] key, bool useInverseCipherMode = false)
 {
     Direction            = direction;
     Key                  = key;
     UseInverseCipherMode = useInverseCipherMode;
 }