Beispiel #1
0
        public void InitializeKey([ReadOnlyArray()] byte[] key, int offset)
        {
            uint i, c, d, t, s, shifts;

            c = CipherUtil.GetIntLE(key, offset + 0);
            d = CipherUtil.GetIntLE(key, offset + 4);

            t  = ((d >> 4) ^ c) & 0x0f0f0f0f;
            c ^= t;
            d ^= t << 4;
            t  = (((c << (16 - (-2))) ^ c) & 0xcccc0000);
            c  = c ^ t ^ (t >> (16 - (-2)));
            t  = (((d << (16 - (-2))) ^ d) & 0xcccc0000);
            d  = d ^ t ^ (t >> (16 - (-2)));
            t  = ((d >> 1) ^ c) & 0x55555555;
            c ^= t;
            d ^= t << 1;
            t  = ((c >> 8) ^ d) & 0x00ff00ff;
            d ^= t;
            c ^= t << 8;
            t  = ((d >> 1) ^ c) & 0x55555555;
            c ^= t;
            d ^= t << 1;

            d = ((d & 0xff) << 16) | (d & 0xff00) |
                ((d >> 16) & 0xff) | ((c >> 4) & 0xf000000);
            c     &= 0x0fffffff;
            shifts = 0x7efc;

            for (i = 0; i < 16; i++)
            {
                if ((shifts & 1) != 0)
                {
                    c = ((c >> 2) | (c << 26));
                    d = ((d >> 2) | (d << 26));
                }
                else
                {
                    c = ((c >> 1) | (c << 27));
                    d = ((d >> 1) | (d << 27));
                }
                shifts >>= 1;
                c       &= 0x0fffffff;
                d       &= 0x0fffffff;

                s = SKB[0, (c) & 0x3f] |
                    SKB[1, ((c >> 6) & 0x03) | ((c >> 7) & 0x3c)] |
                    SKB[2, ((c >> 13) & 0x0f) | ((c >> 14) & 0x30)] |
                    SKB[3, ((c >> 20) & 0x01) | ((c >> 21) & 0x06) | ((c >> 22) & 0x38)];

                t = SKB[4, (d) & 0x3f] |
                    SKB[5, ((d >> 7) & 0x03) | ((d >> 8) & 0x3c)] |
                    SKB[6, (d >> 15) & 0x3f] |
                    SKB[7, ((d >> 21) & 0x0f) | ((d >> 22) & 0x30)];

                _key[i * 2]       = ((t << 16) | (s & 0xffff));
                s                 = ((s >> 16) | (t & 0xffff0000));
                _key[(i * 2) + 1] = (s << 4) | (s >> 28);
            }
        }
Beispiel #2
0
        public void BlockDecrypt([ReadOnlyArray()] byte[] input, int inOffset, [WriteOnlyArray()] byte[] output, int outOffset)
        {
            uint t;
            int  i;

            uint[] lr = new uint[2];

            lr[0] = CipherUtil.GetIntLE(input, inOffset);
            lr[1] = CipherUtil.GetIntLE(input, inOffset + 4);

            initPerm(lr);

            t     = (lr[1] << 1) | (lr[1] >> 31);
            lr[1] = (lr[0] << 1) | (lr[0] >> 31);
            lr[0] = t;

            for (i = 30; i > 0; i -= 4)
            {
                desCipher1(lr, i);
                desCipher2(lr, i - 2);
            }

            lr[0] = (lr[0] >> 1) | (lr[0] << 31);
            lr[1] = (lr[1] >> 1) | (lr[1] << 31);

            finalPerm(lr);

            CipherUtil.PutIntLE(lr[0], output, outOffset);
            CipherUtil.PutIntLE(lr[1], output, outOffset + 4);
        }
Beispiel #3
0
        public void initializeKey([ReadOnlyArray()] byte[] key)
        {
            int  i, j, len = key.Length;
            uint temp;

            Array.Copy(blowfish_pbox, 0, P, 0, 18);
            Array.Copy(blowfish_sbox, 0, S0, 0, 256);
            Array.Copy(blowfish_sbox, 256, S1, 0, 256);
            Array.Copy(blowfish_sbox, 512, S2, 0, 256);
            Array.Copy(blowfish_sbox, 768, S3, 0, 256);

            for (j = 0, i = 0; i < 16 + 2; i++)
            {
                temp = (((uint)(key[j]) << 24) |
                        ((uint)(key[(j + 1) % len]) << 16) |
                        ((uint)(key[(j + 2) % len]) << 8) |
                        ((uint)(key[(j + 3) % len])));
                P[i] = P[i] ^ temp;
                j    = (j + 4) % len;
            }

            byte[] LR = new byte[8];

            for (i = 0; i < 16 + 2; i += 2)
            {
                blockEncrypt(LR, 0, LR, 0);
                P[i]     = CipherUtil.GetIntBE(LR, 0);
                P[i + 1] = CipherUtil.GetIntBE(LR, 4);
            }

            for (j = 0; j < 256; j += 2)
            {
                blockEncrypt(LR, 0, LR, 0);
                S0[j]     = CipherUtil.GetIntBE(LR, 0);
                S0[j + 1] = CipherUtil.GetIntBE(LR, 4);
            }
            for (j = 0; j < 256; j += 2)
            {
                blockEncrypt(LR, 0, LR, 0);
                S1[j]     = CipherUtil.GetIntBE(LR, 0);
                S1[j + 1] = CipherUtil.GetIntBE(LR, 4);
            }
            for (j = 0; j < 256; j += 2)
            {
                blockEncrypt(LR, 0, LR, 0);
                S2[j]     = CipherUtil.GetIntBE(LR, 0);
                S2[j + 1] = CipherUtil.GetIntBE(LR, 4);
            }
            for (j = 0; j < 256; j += 2)
            {
                blockEncrypt(LR, 0, LR, 0);
                S3[j]     = CipherUtil.GetIntBE(LR, 0);
                S3[j + 1] = CipherUtil.GetIntBE(LR, 4);
            }
        }
Beispiel #4
0
        public void EncryptCBC([ReadOnlyArray()] byte[] input, int inputOffset, int inputLen, [WriteOnlyArray()] byte[] output, int outputOffset)
        {
            int nBlocks = inputLen / 8;

            for (int bc = 0; bc < nBlocks; bc++)
            {
                CipherUtil.BlockXor(input, inputOffset, 8, _iv, 0);
                BlockEncrypt(_iv, 0, output, outputOffset);
                Array.Copy(output, outputOffset, _iv, 0, 8);
                inputOffset  += 8;
                outputOffset += 8;
            }
        }
Beispiel #5
0
        public void encryptCBC([ReadOnlyArray()] byte[] input, int inputOffset, int inputLen, [WriteOnlyArray()] byte[] output, int outputOffset)
        {
            int nBlocks = inputLen / BLOCK_SIZE;

            for (int bc = 0; bc < nBlocks; bc++)
            {
                CipherUtil.BlockXor(input, inputOffset, BLOCK_SIZE, IV, 0);
                blockEncrypt(IV, 0, output, outputOffset);
                Array.Copy(output, outputOffset, IV, 0, BLOCK_SIZE);
                inputOffset  += BLOCK_SIZE;
                outputOffset += BLOCK_SIZE;
            }
        }
Beispiel #6
0
        public void encryptCBC([ReadOnlyArray()] byte[] input, int inputOffset, int inputLen, [WriteOnlyArray()] byte[] output, int outputOffset)
        {
            int block_size = GetBlockSize();
            int nBlocks    = inputLen / block_size;

            for (int bc = 0; bc < nBlocks; bc++)
            {
                CipherUtil.BlockXor(input, inputOffset, block_size, _IV, 0);
                blockEncrypt(_IV, 0, output, outputOffset);
                Array.Copy(output, outputOffset, _IV, 0, block_size);
                inputOffset  += block_size;
                outputOffset += block_size;
            }
        }
Beispiel #7
0
        public void encryptCTR([ReadOnlyArray()] byte[] input, int inputOffset, int inputLen, [WriteOnlyArray()] byte[] output, int outputOffset)
        {
            int block_size = GetBlockSize();
            int nBlocks    = inputLen / block_size;

            byte[] tmpBlk = new byte[block_size];
            for (int bc = 0; bc < nBlocks; bc++)
            {
                blockEncrypt(_IV, 0, tmpBlk, 0);
                CipherUtil.BlockXor(input, inputOffset, block_size, tmpBlk, 0);
                Array.Copy(tmpBlk, 0, output, outputOffset, block_size);
                incrementIV();
                inputOffset  += block_size;
                outputOffset += block_size;
            }
        }
Beispiel #8
0
        public void blockDecrypt([ReadOnlyArray()] byte[] input, int inOffset, [WriteOnlyArray()] byte[] output, int outOffset)
        {
            uint L, R;

            L = CipherUtil.GetIntBE(input, inOffset);
            R = CipherUtil.GetIntBE(input, inOffset + 4);

            L ^= P[17];
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[16]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[15]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[14]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[13]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[12]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[11]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[10]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[9]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[8]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[7]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[6]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[5]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[4]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[3]);
            R ^= ((((S0[(int)((L >> 24) & 0xff)] + S1[(int)((L >> 16) & 0xff)]) ^
                    S2[(int)((L >> 8) & 0xff)]) + S3[(int)(L & 0xff)]) ^ P[2]);
            L ^= ((((S0[(int)((R >> 24) & 0xff)] + S1[(int)((R >> 16) & 0xff)]) ^
                    S2[(int)((R >> 8) & 0xff)]) + S3[(int)(R & 0xff)]) ^ P[1]);
            R ^= P[0];

            CipherUtil.PutIntBE(R, output, outOffset);
            CipherUtil.PutIntBE(L, output, outOffset + 4);
        }