/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;

public class Twofish {
 public static class Key {
  byte direction;
  int  key_len;
  int num_rounds;
  int key32[];
  int sbox_keys[];
  int sub_keys[];

  public Key(byte direction,int key_len,int num_rounds,int key32[]) {
   this.direction=direction;
   this.key_len=key_len;
   this.num_rounds=num_rounds;
   this.key32=key32;

   sbox_keys=new int[MAX_KEY_BITS/64];
   sub_keys=new int[TOTAL_SUBKEYS];
  }
 };

 public static class Cipher {
  byte mode;
  int iv32[];

  public Cipher(byte mode,int iv32[]) {
   this.mode=mode;
   this.iv32=iv32;
  }
 }

 protected static int num_rounds_array[]={
  0, 16, 16, 16
 };

 private static final int SK_STEP    = 0x02020202;
 private static final int SK_BUMP    = 0x01010101;
 private static final int SK_ROTL    = 9;

 private static final int RS_GF_FDBK = 0x14D;

 private static int RS_rem(int x) {
  int b=(x>>24)&0xff;
  int g2=((b<<1)^((b&0x80)!=0 ? RS_GF_FDBK:0))&0xff;
  int g3=((b>>1)&0x7F)^((b&1)!=0 ? RS_GF_FDBK>>1:0)^g2;
  x=(x<<8)^(g3<<24)^(g2<<16)^(g3<<8)^b;

  return x;
 }

 private static final int MDS_GF_FDBK = 0x169;

 private static int LFSR1(int x) {
  return (x>>1)^((x&0x01)!=0 ? MDS_GF_FDBK/2:0);
 }

 private static int LFSR2(int x) {
  return ((x>>2)^((x&0x02)!=0 ? MDS_GF_FDBK/2:0)^((x&0x01)!=0 ? MDS_GF_FDBK/4:0));
 }

 private static int Mx_1(int x) {
  return x;
 }

 private static int Mx_X(int x) {
  return x^LFSR2(x);
 }

 private static int Mx_Y(int x) {
  return x^LFSR1(x)^LFSR2(x);
 }

 private static int M00(int x) {
  return Mx_1(x);
 }

 private static int M01(int x) {
  return Mx_Y(x);
 }

 private static int M02(int x) {
  return Mx_X(x);
 }

 private static int M03(int x) {
  return Mx_X(x);
 }

 private static int M10(int x) {
  return Mx_X(x);
 }

 private static int M11(int x) {
  return Mx_Y(x);
 }

 private static int M12(int x) {
  return Mx_Y(x);
 }

 private static int M13(int x) {
  return Mx_1(x);
 }

 private static int M20(int x) {
  return Mx_Y(x);
 }

 private static int M21(int x) {
  return Mx_X(x);
 }

 private static int M22(int x) {
  return Mx_1(x);
 }

 private static int M23(int x) {
  return Mx_Y(x);
 }

 private static int M30(int x) {
  return Mx_Y(x);
 }

 private static int M31(int x) {
  return Mx_1(x);
 }

 private static int M32(int x) {
  return Mx_Y(x);
 }

 private static int M33(int x) {
  return Mx_X(x);
 }

 public static final int P_00 = 1;
 public static final int P_01 = 0;
 public static final int P_02 = 0;
 public static final int P_03 = (P_01^1);
 public static final int P_04 = 1;

 public static final int P_10 = 0;
 public static final int P_11 = 0;
 public static final int P_12 = 1;
 public static final int P_13 = (P_11^1);
 public static final int P_14 = 0;

 public static final int P_20 = 1;
 public static final int P_21 = 1;
 public static final int P_22 = 0;
 public static final int P_23 = (P_21^1);
 public static final int P_24 = 0;

 public static final int P_30 = 0;
 public static final int P_31 = 1;
 public static final int P_32 = 1;
 public static final int P_33 = (P_31^1);
 public static final int P_34 = 1;

 private static final int P8x8[][]=new int[][] {
  {0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 
   0x9A, 0x92, 0x80, 0x78, 0xE4, 0xDD, 0xD1, 0x38, 
   0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C, 
   0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 
   0xF2, 0xD0, 0x8B, 0x30, 0x84, 0x54, 0xDF, 0x23, 
   0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82, 
   0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 
   0xA6, 0xEB, 0xA5, 0xBE, 0x16, 0x0C, 0xE3, 0x61, 
   0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B, 
   0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 
   0xE1, 0xE6, 0xBD, 0x45, 0xE2, 0xF4, 0xB6, 0x66, 
   0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7, 
   0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 
   0xEA, 0x77, 0x39, 0xAF, 0x33, 0xC9, 0x62, 0x71, 
   0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8, 
   0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 
   0xA1, 0x1D, 0xAA, 0xED, 0x06, 0x70, 0xB2, 0xD2, 
   0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90, 
   0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 
   0x9E, 0x9C, 0x52, 0x1B, 0x5F, 0x93, 0x0A, 0xEF, 
   0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B, 
   0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 
   0x2A, 0xCE, 0xCB, 0x2F, 0xFC, 0x97, 0x05, 0x7A, 
   0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A, 
   0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 
   0xB8, 0xDA, 0xB0, 0x17, 0x55, 0x1F, 0x8A, 0x7D, 
   0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72, 
   0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 
   0x6E, 0x50, 0xDE, 0x68, 0x65, 0xBC, 0xDB, 0xF8, 
   0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4, 
   0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 
   0x6F, 0x9D, 0x36, 0x42, 0x4A, 0x5E, 0xC1, 0xE0},

  {0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 
   0x4A, 0xD3, 0xE6, 0x6B, 0x45, 0x7D, 0xE8, 0x4B, 
   0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1, 
   0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 
   0x5E, 0xBA, 0xAE, 0x5B, 0x8A, 0x00, 0xBC, 0x9D, 
   0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5, 
   0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 
   0xB2, 0x73, 0x4C, 0x54, 0x92, 0x74, 0x36, 0x51, 
   0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96, 
   0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 
   0x13, 0x95, 0x9C, 0xC7, 0x24, 0x46, 0x3B, 0x70, 
   0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8, 
   0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 
   0x03, 0x6F, 0x08, 0xBF, 0x40, 0xE7, 0x2B, 0xE2, 
   0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9, 
   0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 
   0x66, 0x94, 0xA1, 0x1D, 0x3D, 0xF0, 0xDE, 0xB3, 
   0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E, 
   0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 
   0x81, 0x88, 0xEE, 0x21, 0xC4, 0x1A, 0xEB, 0xD9, 
   0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01, 
   0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 
   0x4F, 0xF2, 0x65, 0x8E, 0x78, 0x5C, 0x58, 0x19, 
   0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64, 
   0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 
   0xCE, 0xE9, 0x68, 0x44, 0xE0, 0x4D, 0x43, 0x69, 
   0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E, 
   0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 
   0x22, 0xC9, 0xC0, 0x9B, 0x89, 0xD4, 0xED, 0xAB, 
   0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9, 
   0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 
   0x16, 0x25, 0x86, 0x56, 0x55, 0x09, 0xBE, 0x91}
 };

 protected static final int DIR_ENCRYPT   = 0;
 protected static final int DIR_DECRYPT   = 1;

 protected static final int MODE_ECB      = 1;
 protected static final int MODE_CBC      = 2;

 protected static final int BLOCK_SIZE    = 128;
 protected static final int MAX_ROUNDS    = 16;
 protected static final int ROUNDS_128    = 16;
 protected static final int ROUNDS_192    = 16;
 protected static final int ROUNDS_256    = 16;
 protected static final int MAX_KEY_BITS  = 256;
 protected static final int MIN_KEY_BITS  = 128;

 protected static final int INPUT_WHITEN  = 0;
 protected static final int OUTPUT_WHITEN = (INPUT_WHITEN+BLOCK_SIZE/32);
 protected static final int ROUND_SUBKEYS = (OUTPUT_WHITEN+BLOCK_SIZE/32);
 protected static final int TOTAL_SUBKEYS = (ROUND_SUBKEYS+2*MAX_ROUNDS);

 private static int b0(int x) {
  return (x)&0xff;
 }

 private static int b1(int x) {
  return (x>>8)&0xff;
 }

 private static int b2(int x) {
  return (x>>16)&0xff;
 }

 private static int b3(int x) {
  return (x>>24)&0xff;
 }

 protected static int ROL(int x,int n) {
  return ((x<<(n&0x1f))|(x>>>(32-(n&0x1f))));
 }

 protected static int ROR(int x,int n) {
  return ((x>>>(n&0x1f))|(x<<(32-(n&0x1f))));
 }

 protected static int Bswap(int x) {
  return x;
 }

 protected static int F32(int x,int k32[],int keylen) {
  int b[]=new int[4];
	
  b[0]=(x)&0xff;
  b[1]=(x>>8)&0xff;
  b[2]=(x>>16)&0xff;
  b[3]=(x>>24)&0xff;

  int val=((keylen+63)/64)&3;

  switch (val) {
   case 0:
    int k32_3=k32[3];
    b[0]=P8x8[P_04][b[0]]^b0(k32_3);
    b[1]=P8x8[P_14][b[1]]^b1(k32_3);
    b[2]=P8x8[P_24][b[2]]^b2(k32_3);
    b[3]=P8x8[P_34][b[3]]^b3(k32_3);
   case 3:
    int k32_2=k32[2];
    b[0]=P8x8[P_03][b[0]]^b0(k32_2);
    b[1]=P8x8[P_13][b[1]]^b1(k32_2);
    b[2]=P8x8[P_23][b[2]]^b2(k32_2);
    b[3]=P8x8[P_33][b[3]]^b3(k32_2);
   case 2:
    int k32_1=k32[1];
    int k32_0=k32[0];
    b[0]=P8x8[P_00][P8x8[P_01][P8x8[P_02][b[0]]^b0(k32_1)]^b0(k32_0)];
    b[1]=P8x8[P_10][P8x8[P_11][P8x8[P_12][b[1]]^b1(k32_1)]^b1(k32_0)];
    b[2]=P8x8[P_20][P8x8[P_21][P8x8[P_22][b[2]]^b2(k32_1)]^b2(k32_0)];
    b[3]=P8x8[P_30][P8x8[P_31][P8x8[P_32][b[3]]^b3(k32_1)]^b3(k32_0)];
  }

  return ((M00(b[0])^M01(b[1])^M02(b[2])^M03(b[3]))&0xff)^
         (((M10(b[0])^M11(b[1])^M12(b[2])^M13(b[3]))&0xff)<<8)^
         (((M20(b[0])^M21(b[1])^M22(b[2])^M23(b[3]))&0xff)<<16)^
         (((M30(b[0])^M31(b[1])^M32(b[2])^M33(b[3]))&0xff)<<24);
 }

 private static int RS_MDS_Encode(int k0,int k1) {
  int i,j,r;

  for (i=r=0;i<2;i++) {
   r^=(i!=0) ? k0:k1;
   for(j=0;j<4;j++) {
    r=RS_rem(r);
   }		
  }

  return r;
 }

 protected static void reKey(Key key) {
  int i,k64_cnt;
  int key_len=key.key_len;

  int subkey_cnt=ROUND_SUBKEYS+2*key.num_rounds;

  int A,B;
  int k32e[]=new int[MAX_KEY_BITS/64];
  int k32o[]=new int[MAX_KEY_BITS/64];

  k64_cnt=(key_len+63)/64;

  for(i=0;i<k64_cnt;i++) {
   k32e[i]=key.key32[2*i];
   k32o[i]=key.key32[2*i+1];

   key.sbox_keys[k64_cnt-1-i]=RS_MDS_Encode(k32e[i],k32o[i]);
  }

  for(i=0;i<subkey_cnt/2;i++) {
   A=F32(i*SK_STEP,k32e,key_len);
   B=F32(i*SK_STEP+SK_BUMP,k32o,key_len);
   B=ROL(B,8);
   key.sub_keys[2*i]=A+B;
   key.sub_keys[2*i+1]=ROL(A+2*B,SK_ROTL);
  }
 }

 public static Key makeKey(byte direction,int key_len,int key_data[]) {
  key_len=(key_len+63)&~63;
  int num_rounds=num_rounds_array[(key_len-1)/64];

  int key32[]=new int[MAX_KEY_BITS/32];

  for(int i=0;i<MAX_KEY_BITS/32;i++) {
   key32[i]=0;
  }

  for(int i=0;i<key_len/32;i++) {
   key32[i]=key_data[i];
  }

  Key key=new Key(direction,key_len,num_rounds,key32);

  reKey(key);

  return key;
 }

 public static Cipher cipherInit(byte mode,int IV[]) {
  int iv32[]=new int[BLOCK_SIZE/32];

  if ((mode!=MODE_ECB)&&(IV!=null)) {
   for(int i=0;i<BLOCK_SIZE/32;i++) {
    iv32[i]=IV[i];   
   }
  }

  return new Cipher(mode,iv32);  
 }

 protected static void blockDecrypt_internal(Cipher cipher,Key key,int block[],int x[]) {
  int rounds=key.num_rounds;

  for(int i=0;i<BLOCK_SIZE/32;i++) {
   x[i]=Bswap(block[i])^key.sub_keys[OUTPUT_WHITEN+i];
  }

  for(int r=rounds-1;r>=0;r--) {
   int t0=F32(x[0],key.sbox_keys,key.key_len);
   int t1=F32(ROL(x[1],8),key.sbox_keys,key.key_len);

   x[2]=ROL(x[2],1);
   x[2]^=t0+t1+key.sub_keys[ROUND_SUBKEYS+2*r];
   x[3]^=t0+2*t1+key.sub_keys[ROUND_SUBKEYS+2*r+1];
   x[3]=ROR(x[3],1);

   if (r!=0) {
    t0=x[0];
    x[0]=x[2];
    x[2]=t0;
	
    t1=x[1];
    x[1]=x[3];
    x[3]=t1;
   }
  }

  for(int i=0;i<BLOCK_SIZE/32;i++) {
   x[i]^=key.sub_keys[INPUT_WHITEN+i];

   if (cipher.mode==MODE_CBC) {
    x[i]^=cipher.iv32[i];

    cipher.iv32[i]=Bswap(block[i]);
   }

   block[i]=Bswap(x[i]);
  }
 }

 public static void blockDecrypt(Cipher cipher,Key key,int input[],int input_len,int out_buffer[]) {
  int block[]=new int[BLOCK_SIZE/32];
  int x[]=new int[BLOCK_SIZE/32];

  int ipos=0;
  int opos=0;

  for(int n=0;n<input_len;n+=BLOCK_SIZE,ipos+=BLOCK_SIZE/32,opos+=BLOCK_SIZE/32) {

   for(int i=0;i<BLOCK_SIZE/32;i++) {
    block[i]=input[ipos+i];
   }

   blockDecrypt_internal(cipher,key,block,x);

   for(int i=0;i<BLOCK_SIZE/32;i++) {
    out_buffer[opos+i]=block[i];
   }
  }
 }

 public static void blockDecrypt(Cipher cipher,Key key,byte input[],int input_len,byte out_buffer[]) {
  int block[]=new int[BLOCK_SIZE/32];
  int x[]=new int[BLOCK_SIZE/32];

  int ipos=0;
  int opos=0;

  for(int n=0;n<input_len;n+=BLOCK_SIZE,ipos+=BLOCK_SIZE/8,opos+=BLOCK_SIZE/8) {
   for(int i=0;i<BLOCK_SIZE/32;i++) {
    int b1=((int)input[ipos+4*i])&0xff;
    int b2=((int)input[ipos+4*i+1])&0xff;
    int b3=((int)input[ipos+4*i+2])&0xff;
    int b4=((int)input[ipos+4*i+3])&0xff;

    block[i]=b1|(b2<<8)|(b3<<16)|(b4<<24);
   }

   blockDecrypt_internal(cipher,key,block,x);

   for(int i=0;i<BLOCK_SIZE/32;i++) {
    byte b1=(byte)(block[i]&0xff);
    byte b2=(byte)(block[i]>>>8);
    byte b3=(byte)(block[i]>>>16);
    byte b4=(byte)(block[i]>>>24);

    out_buffer[opos+4*i]=b1;
    out_buffer[opos+4*i+1]=b2;
    out_buffer[opos+4*i+2]=b3;
    out_buffer[opos+4*i+3]=b4;
   }
  }
 }

 public static void blockDecrypt(Cipher cipher,Key key,byte input[],int ipos,int input_len,byte out_buffer[]) {
  int block[]=new int[BLOCK_SIZE/32];
  int x[]=new int[BLOCK_SIZE/32];

  int opos=ipos;

  for(int n=0;n<input_len;n+=BLOCK_SIZE,ipos+=BLOCK_SIZE/8,opos+=BLOCK_SIZE/8) {
   for(int i=0;i<BLOCK_SIZE/32;i++) {
    int b1=((int)input[ipos+4*i])&0xff;
    int b2=((int)input[ipos+4*i+1])&0xff;
    int b3=((int)input[ipos+4*i+2])&0xff;
    int b4=((int)input[ipos+4*i+3])&0xff;

    block[i]=b1|(b2<<8)|(b3<<16)|(b4<<24);
   }

   blockDecrypt_internal(cipher,key,block,x);

   for(int i=0;i<BLOCK_SIZE/32;i++) {
    byte b1=(byte)(block[i]&0xff);
    byte b2=(byte)(block[i]>>>8);
    byte b3=(byte)(block[i]>>>16);
    byte b4=(byte)(block[i]>>>24);

    out_buffer[opos+4*i]=b1;
    out_buffer[opos+4*i+1]=b2;
    out_buffer[opos+4*i+2]=b3;
    out_buffer[opos+4*i+3]=b4;
   }
  }
 }

 private static int swap32(int i) {
  int i1=(i>>24)&0xff;
  int i2=(i>>16)&0xff;
  int i3=(i>>8)&0xff;
  int i4=(i)&0xff;

  i=(i4<<24)|(i3<<16)|(i2<<8)|i1;

  return i;
 }

 protected static void rewrite_key(Key k) {
  for(int i=0;i<8;i++) {
   k.key32[i]=swap32(k.key32[i]);
  }

  reKey(k);
 }

 public static void decrypt_ecb(byte buf[],int off,int size,int key_data[],int key_size) {
  Cipher c=cipherInit((byte)MODE_ECB,null);
  Key k=makeKey((byte)DIR_DECRYPT,key_size*32,key_data);

  rewrite_key(k);

  blockDecrypt(c,k,buf,off,size*8,buf);
 }

 public static void decrypt_cbc(byte buf[],int size,int key_data[],int key_size,int iv[]) {
  Cipher c=cipherInit((byte)MODE_CBC,iv);
  Key k=makeKey((byte)DIR_DECRYPT,key_size*32,key_data);

  rewrite_key(k);

  blockDecrypt(c,k,buf,size*8,buf);
 }


}
