예제 #1
0
 private void WriteBlock(uint[] K)
 {
     _byteWriter.WriteBytes(Word32Bits.ToBytes(K[2]));
     _byteWriter.WriteBytes(Word32Bits.ToBytes(K[3]));
     _byteWriter.WriteBytes(Word32Bits.ToBytes(K[0]));
     _byteWriter.WriteBytes(Word32Bits.ToBytes(K[1]));
 }
예제 #2
0
 public void WriteWords(uint[] words)
 {
     foreach (var word in words)
     {
         WriteBytes(Word32Bits.ToBytes(word));
     }
 }
예제 #3
0
 private void ReadWords(byte[] key, uint[] words)
 {
     for (int i = 0; i < key.Length / 4; i++)
     {
         words[i] = Word32Bits.ToUint(key.SubArray(i * 4, i * 4 + 4));
     }
 }
예제 #4
0
        public void ToUint_ConvertsToUint_Converted()
        {
            byte[] bytes    = { 2, 108, 209, 77 };
            uint   excepted = 1305570306;

            Assert.AreEqual(excepted, Word32Bits.ToUint(bytes));
        }
예제 #5
0
        public void RotateRight_RotatesTwoBits_Rotated()
        {
            uint value    = 6;
            uint expected = 2147483649;

            Assert.AreEqual(expected, Word32Bits.RotateRight(value, 2));
        }
예제 #6
0
        public void ToBytes_ConvertsToBytesLittleEndian_Converted()
        {
            uint word = 40685901;

            byte[] excepted = { 2, 108, 209, 77 };
            CollectionAssert.AreEqual(excepted, Word32Bits.ToBytes(word, ValueRepresentation.LittleEndian));
        }
예제 #7
0
        public void RotateRight_RotatesOneBit_Rotated()
        {
            uint value    = 3;
            uint expected = 2147483649;

            Assert.AreEqual(expected, Word32Bits.RotateRight(value));
        }
예제 #8
0
        public void ToBytes_ConvertsToBytes_Converted()
        {
            uint word = 1305570306;

            byte[] excepted = { 2, 108, 209, 77 };
            CollectionAssert.AreEqual(excepted, Word32Bits.ToBytes(word));
        }
예제 #9
0
        public void ToUint_ConvertsToUintLittleEndian_Converted()
        {
            byte[] bytes    = { 2, 108, 209, 77 };
            uint   excepted = 40685901;

            Assert.AreEqual(excepted, Word32Bits.ToUint(bytes, ValueRepresentation.LittleEndian));
        }
예제 #10
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));
        }
예제 #11
0
        public void GetByte_GetsByteFromWord_GotByte()
        {
            uint value = 1305570306;

            Assert.AreEqual(2, Word32Bits.GetByte(value, 0));
            Assert.AreEqual(108, Word32Bits.GetByte(value, 1));
            Assert.AreEqual(209, Word32Bits.GetByte(value, 2));
            Assert.AreEqual(77, Word32Bits.GetByte(value, 3));
        }
예제 #12
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);
        }
예제 #13
0
        private uint ReadWord()
        {
            var bytes = new byte[_bytesPerWord];

            for (int i = 0; i < _bytesPerWord; i++)
            {
                bytes[i] = ReadByte();
            }
            return(Word32Bits.ToUint(bytes));
        }
예제 #14
0
        private uint[] SplitInto32BitWords(byte[] m)
        {
            int BYTES_PER_WORD = 4;
            var M = new uint[m.Length / BYTES_PER_WORD];

            for (int i = 0; i < M.Length; i++)
            {
                M[i] = Word32Bits.ToUint(m.SubArray(i * 4, i * 4 + 4));
            }

            return(M);
        }
예제 #15
0
        public static uint[] Inverse(uint[] x)
        {
            uint x1   = Word32Bits.RotateRight(x[2], 22) ^ x[3] ^ x[1] << 7;
            uint x2   = Word32Bits.RotateRight(x[0], 5) ^ x[1] ^ x[3];
            uint num1 = Word32Bits.RotateRight(x[3], 7);
            uint num2 = Word32Bits.RotateRight(x[1], 1);

            x[3] = num1 ^ x1 ^ x2 << 3;
            x[1] = num2 ^ x2 ^ x1;
            x[2] = Word32Bits.RotateRight(x1, 3);
            x[0] = Word32Bits.RotateRight(x2, 13);
            return(x);
        }
예제 #16
0
        private void CalculateWords(uint[] words)
        {
            for (int i = 8; i < 16; i++)
            {
                words[i] = Word32Bits.RotateLeft((uint)(words[i - 8] ^ words[i - 5] ^ words[i - 3] ^ words[i - 1] ^ PHI ^ (i - 8)), 11);
            }

            Array.Copy(words, 8, words, 0, 8);

            for (int i = 8; i < 132; i++)
            {
                words[i] = Word32Bits.RotateLeft((uint)(words[i - 8] ^ words[i - 5] ^ words[i - 3] ^ words[i - 1] ^ PHI ^ i), 11);
            }
        }
예제 #17
0
 public static uint[] Transform(uint[] x)
 {
     x[0] = Word32Bits.RotateLeft(x[0], 13);
     x[2] = Word32Bits.RotateLeft(x[2], 3);
     x[1] = x[1] ^ x[0] ^ x[2];
     x[3] = x[3] ^ x[2] ^ x[0] << 3;
     x[1] = Word32Bits.RotateLeft(x[1], 1);
     x[3] = Word32Bits.RotateLeft(x[3], 7);
     x[0] = x[0] ^ x[1] ^ x[3];
     x[2] = x[2] ^ x[3] ^ x[1] << 7;
     x[0] = Word32Bits.RotateLeft(x[0], 5);
     x[2] = Word32Bits.RotateLeft(x[2], 22);
     return(x);
 }
예제 #18
0
        private uint[] GetK(uint[] Me, uint[] Mo)
        {
            var  k = new uint[40];
            uint p = 16843009;

            var mds = new TwofishMDS();

            for (int i = 0; i < 20; i++)
            {
                uint A = TwofishFunction.h(mds, (uint)(2 * i * p), Me);
                uint B = Word32Bits.RotateLeft(TwofishFunction.h(mds, (uint)((2 * i + 1) * p), Mo), 8);
                k[2 * i]     = A + B;
                k[2 * i + 1] = Word32Bits.RotateLeft(A + 2 * B, 9);
            }

            return(k);
        }