camelliaF2() private static method

private static camelliaF2 ( uint s, uint skey, int keyoff ) : void
s uint
skey uint
keyoff int
return void
Exemplo n.º 1
0
 private int processBlock192or256(byte[] input, int inOff, byte[] output, int outOff)
 {
     for (int i = 0; i < 4; i++)
     {
         this.state[i]  = CamelliaEngine.bytes2uint(input, inOff + i * 4);
         this.state[i] ^= this.kw[i];
     }
     CamelliaEngine.camelliaF2(this.state, this.subkey, 0);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 4);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 8);
     CamelliaEngine.camelliaFLs(this.state, this.ke, 0);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 12);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 16);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 20);
     CamelliaEngine.camelliaFLs(this.state, this.ke, 4);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 24);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 28);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 32);
     CamelliaEngine.camelliaFLs(this.state, this.ke, 8);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 36);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 40);
     CamelliaEngine.camelliaF2(this.state, this.subkey, 44);
     this.state[2] ^= this.kw[4];
     this.state[3] ^= this.kw[5];
     this.state[0] ^= this.kw[6];
     this.state[1] ^= this.kw[7];
     CamelliaEngine.uint2bytes(this.state[2], output, outOff);
     CamelliaEngine.uint2bytes(this.state[3], output, outOff + 4);
     CamelliaEngine.uint2bytes(this.state[0], output, outOff + 8);
     CamelliaEngine.uint2bytes(this.state[1], output, outOff + 12);
     return(16);
 }
Exemplo n.º 2
0
        private void setKey(bool forEncryption, byte[] key)
        {
            uint[] array  = new uint[8];
            uint[] array2 = new uint[4];
            uint[] array3 = new uint[4];
            uint[] array4 = new uint[4];
            int    num    = key.Length;

            if (num != 16)
            {
                if (num != 24)
                {
                    if (num != 32)
                    {
                        throw new ArgumentException("key sizes are only 16/24/32 bytes.");
                    }
                    array[0]       = CamelliaEngine.bytes2uint(key, 0);
                    array[1]       = CamelliaEngine.bytes2uint(key, 4);
                    array[2]       = CamelliaEngine.bytes2uint(key, 8);
                    array[3]       = CamelliaEngine.bytes2uint(key, 12);
                    array[4]       = CamelliaEngine.bytes2uint(key, 16);
                    array[5]       = CamelliaEngine.bytes2uint(key, 20);
                    array[6]       = CamelliaEngine.bytes2uint(key, 24);
                    array[7]       = CamelliaEngine.bytes2uint(key, 28);
                    this._keyIs128 = false;
                }
                else
                {
                    array[0]       = CamelliaEngine.bytes2uint(key, 0);
                    array[1]       = CamelliaEngine.bytes2uint(key, 4);
                    array[2]       = CamelliaEngine.bytes2uint(key, 8);
                    array[3]       = CamelliaEngine.bytes2uint(key, 12);
                    array[4]       = CamelliaEngine.bytes2uint(key, 16);
                    array[5]       = CamelliaEngine.bytes2uint(key, 20);
                    array[6]       = ~array[4];
                    array[7]       = ~array[5];
                    this._keyIs128 = false;
                }
            }
            else
            {
                this._keyIs128 = true;
                array[0]       = CamelliaEngine.bytes2uint(key, 0);
                array[1]       = CamelliaEngine.bytes2uint(key, 4);
                array[2]       = CamelliaEngine.bytes2uint(key, 8);
                array[3]       = CamelliaEngine.bytes2uint(key, 12);
                array[4]       = (array[5] = (array[6] = (array[7] = 0u)));
            }
            for (int i = 0; i < 4; i++)
            {
                array2[i] = (array[i] ^ array[i + 4]);
            }
            CamelliaEngine.camelliaF2(array2, CamelliaEngine.SIGMA, 0);
            for (int j = 0; j < 4; j++)
            {
                array2[j] ^= array[j];
            }
            CamelliaEngine.camelliaF2(array2, CamelliaEngine.SIGMA, 4);
            if (this._keyIs128)
            {
                if (forEncryption)
                {
                    this.kw[0] = array[0];
                    this.kw[1] = array[1];
                    this.kw[2] = array[2];
                    this.kw[3] = array[3];
                    CamelliaEngine.roldq(15, array, 0, this.subkey, 4);
                    CamelliaEngine.roldq(30, array, 0, this.subkey, 12);
                    CamelliaEngine.roldq(15, array, 0, array4, 0);
                    this.subkey[18] = array4[2];
                    this.subkey[19] = array4[3];
                    CamelliaEngine.roldq(17, array, 0, this.ke, 4);
                    CamelliaEngine.roldq(17, array, 0, this.subkey, 24);
                    CamelliaEngine.roldq(17, array, 0, this.subkey, 32);
                    this.subkey[0] = array2[0];
                    this.subkey[1] = array2[1];
                    this.subkey[2] = array2[2];
                    this.subkey[3] = array2[3];
                    CamelliaEngine.roldq(15, array2, 0, this.subkey, 8);
                    CamelliaEngine.roldq(15, array2, 0, this.ke, 0);
                    CamelliaEngine.roldq(15, array2, 0, array4, 0);
                    this.subkey[16] = array4[0];
                    this.subkey[17] = array4[1];
                    CamelliaEngine.roldq(15, array2, 0, this.subkey, 20);
                    CamelliaEngine.roldqo32(34, array2, 0, this.subkey, 28);
                    CamelliaEngine.roldq(17, array2, 0, this.kw, 4);
                    return;
                }
                this.kw[4] = array[0];
                this.kw[5] = array[1];
                this.kw[6] = array[2];
                this.kw[7] = array[3];
                CamelliaEngine.decroldq(15, array, 0, this.subkey, 28);
                CamelliaEngine.decroldq(30, array, 0, this.subkey, 20);
                CamelliaEngine.decroldq(15, array, 0, array4, 0);
                this.subkey[16] = array4[0];
                this.subkey[17] = array4[1];
                CamelliaEngine.decroldq(17, array, 0, this.ke, 0);
                CamelliaEngine.decroldq(17, array, 0, this.subkey, 8);
                CamelliaEngine.decroldq(17, array, 0, this.subkey, 0);
                this.subkey[34] = array2[0];
                this.subkey[35] = array2[1];
                this.subkey[32] = array2[2];
                this.subkey[33] = array2[3];
                CamelliaEngine.decroldq(15, array2, 0, this.subkey, 24);
                CamelliaEngine.decroldq(15, array2, 0, this.ke, 4);
                CamelliaEngine.decroldq(15, array2, 0, array4, 0);
                this.subkey[18] = array4[2];
                this.subkey[19] = array4[3];
                CamelliaEngine.decroldq(15, array2, 0, this.subkey, 12);
                CamelliaEngine.decroldqo32(34, array2, 0, this.subkey, 4);
                CamelliaEngine.roldq(17, array2, 0, this.kw, 0);
                return;
            }
            else
            {
                for (int k = 0; k < 4; k++)
                {
                    array3[k] = (array2[k] ^ array[k + 4]);
                }
                CamelliaEngine.camelliaF2(array3, CamelliaEngine.SIGMA, 8);
                if (forEncryption)
                {
                    this.kw[0] = array[0];
                    this.kw[1] = array[1];
                    this.kw[2] = array[2];
                    this.kw[3] = array[3];
                    CamelliaEngine.roldqo32(45, array, 0, this.subkey, 16);
                    CamelliaEngine.roldq(15, array, 0, this.ke, 4);
                    CamelliaEngine.roldq(17, array, 0, this.subkey, 32);
                    CamelliaEngine.roldqo32(34, array, 0, this.subkey, 44);
                    CamelliaEngine.roldq(15, array, 4, this.subkey, 4);
                    CamelliaEngine.roldq(15, array, 4, this.ke, 0);
                    CamelliaEngine.roldq(30, array, 4, this.subkey, 24);
                    CamelliaEngine.roldqo32(34, array, 4, this.subkey, 36);
                    CamelliaEngine.roldq(15, array2, 0, this.subkey, 8);
                    CamelliaEngine.roldq(30, array2, 0, this.subkey, 20);
                    this.ke[8]  = array2[1];
                    this.ke[9]  = array2[2];
                    this.ke[10] = array2[3];
                    this.ke[11] = array2[0];
                    CamelliaEngine.roldqo32(49, array2, 0, this.subkey, 40);
                    this.subkey[0] = array3[0];
                    this.subkey[1] = array3[1];
                    this.subkey[2] = array3[2];
                    this.subkey[3] = array3[3];
                    CamelliaEngine.roldq(30, array3, 0, this.subkey, 12);
                    CamelliaEngine.roldq(30, array3, 0, this.subkey, 28);
                    CamelliaEngine.roldqo32(51, array3, 0, this.kw, 4);
                    return;
                }
                this.kw[4] = array[0];
                this.kw[5] = array[1];
                this.kw[6] = array[2];
                this.kw[7] = array[3];
                CamelliaEngine.decroldqo32(45, array, 0, this.subkey, 28);
                CamelliaEngine.decroldq(15, array, 0, this.ke, 4);
                CamelliaEngine.decroldq(17, array, 0, this.subkey, 12);
                CamelliaEngine.decroldqo32(34, array, 0, this.subkey, 0);
                CamelliaEngine.decroldq(15, array, 4, this.subkey, 40);
                CamelliaEngine.decroldq(15, array, 4, this.ke, 8);
                CamelliaEngine.decroldq(30, array, 4, this.subkey, 20);
                CamelliaEngine.decroldqo32(34, array, 4, this.subkey, 8);
                CamelliaEngine.decroldq(15, array2, 0, this.subkey, 36);
                CamelliaEngine.decroldq(30, array2, 0, this.subkey, 24);
                this.ke[2] = array2[1];
                this.ke[3] = array2[2];
                this.ke[0] = array2[3];
                this.ke[1] = array2[0];
                CamelliaEngine.decroldqo32(49, array2, 0, this.subkey, 4);
                this.subkey[46] = array3[0];
                this.subkey[47] = array3[1];
                this.subkey[44] = array3[2];
                this.subkey[45] = array3[3];
                CamelliaEngine.decroldq(30, array3, 0, this.subkey, 32);
                CamelliaEngine.decroldq(30, array3, 0, this.subkey, 16);
                CamelliaEngine.roldqo32(51, array3, 0, this.kw, 0);
                return;
            }
        }