示例#1
0
        public static uint h(ITwofishMDS mds, uint x, uint[] L)
        {
            int k = L.Length;

            uint[] y = Word32Bits.ToUintBytes(x);

            if (k == 4)
            {
                y[0] = (uint)q1(y[0]) ^ Word32Bits.GetByte(L[3], 0);
                y[1] = (uint)q0(y[1]) ^ Word32Bits.GetByte(L[3], 1);
                y[2] = (uint)q0(y[2]) ^ Word32Bits.GetByte(L[3], 2);
                y[3] = (uint)q1(y[3]) ^ Word32Bits.GetByte(L[3], 3);
            }
            if (k >= 3)
            {
                y[0] = (uint)q1(y[0]) ^ Word32Bits.GetByte(L[2], 0);
                y[1] = (uint)q1(y[1]) ^ Word32Bits.GetByte(L[2], 1);
                y[2] = (uint)q0(y[2]) ^ Word32Bits.GetByte(L[2], 2);
                y[3] = (uint)q0(y[3]) ^ Word32Bits.GetByte(L[2], 3);
            }
            y[0] = (uint)(q0(q0(y[0]) ^ Word32Bits.GetByte(L[1], 0)) ^ Word32Bits.GetByte(L[0], 0));
            y[1] = (uint)(q0(q1(y[1]) ^ Word32Bits.GetByte(L[1], 1)) ^ Word32Bits.GetByte(L[0], 1));
            y[2] = (uint)(q1(q0(y[2]) ^ Word32Bits.GetByte(L[1], 2)) ^ Word32Bits.GetByte(L[0], 2));
            y[3] = (uint)(q1(q1(y[3]) ^ Word32Bits.GetByte(L[1], 3)) ^ Word32Bits.GetByte(L[0], 3));

            return(mds.Multiply(y));
        }
示例#2
0
 private uint[] Rounds(ITwofishMDS mds, uint[] K)
 {
     for (int round = 0; round < 16; round++)
     {
         K = Round(mds, K, round);
         K = SwapWords(K);
     }
     return(K);
 }
示例#3
0
        private uint[] Round(ITwofishMDS mds, uint[] K, int round)
        {
            uint F0 = TwofishFunction.h(mds, K[0], _key.SBox);
            uint F1 = TwofishFunction.h(mds, Word32Bits.RotateLeft(K[1], 8), _key.SBox);

            K[2] ^= F0 + F1 + _key.K[2 * round + 8];
            K[2]  = Word32Bits.RotateRight(K[2], 1);
            K[3]  = Word32Bits.RotateLeft(K[3], 1) ^ (F0 + 2 * F1 + _key.K[2 * round + 9]);
            return(K);
        }
示例#4
0
 public Twofish(ITwofishKey key, ITwofishMDS mds) : base(key.RawBytes)
 {
     _key = key;
     _mds = mds;
 }
示例#5
0
 public Twofish(byte[] key) : base(key)
 {
     _key = new TwofishKey(key);
     _mds = new TwofishMDS();
 }