public ECB() { super(new TwofishEngine()); } }
public TwofishEngine() { // calculate the MDS matrix int[] m1 = new int[2]; int[] mX = new int[2]; int[] mY = new int[2]; int j; for (int i=0; i< MAX_KEY_BITS ; i++) { j = P[0][i] & 0xff; m1[0] = j; mX[0] = Mx_X(j) & 0xff; mY[0] = Mx_Y(j) & 0xff; j = P[1][i] & 0xff; m1[1] = j; mX[1] = Mx_X(j) & 0xff; mY[1] = Mx_Y(j) & 0xff; gMDS0[i] = m1[P_00] | mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24; gMDS1[i] = mY[P_10] | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24; gMDS2[i] = mX[P_20] | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24; gMDS3[i] = mX[P_30] | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24; } }
k32e[i] = BytesTo32Bits(key, p); k32o[i] = BytesTo32Bits(key, p+4); sBoxKeys[k64Cnt-1-i] = RS_MDS_Encode(k32e[i], k32o[i]); A = F32(q, k32e); B = F32(q+SK_BUMP, k32o); B = B << 8 | B >>> 24; A += B; gSBox[i*2] = gMDS0[(P[P_01][b0] & 0xff) ^ b0(k0)]; gSBox[i*2+1] = gMDS1[(P[P_11][b1] & 0xff) ^ b1(k0)]; gSBox[i*2+0x200] = gMDS2[(P[P_21][b2] & 0xff) ^ b2(k0)]; gSBox[i*2+0x201] = gMDS3[(P[P_31][b3] & 0xff) ^ b3(k0)]; break; case 0: // 256 bits of key b0 = (P[P_04][b0] & 0xff) ^ b0(k3); b1 = (P[P_14][b1] & 0xff) ^ b1(k3); b2 = (P[P_24][b2] & 0xff) ^ b2(k3); b3 = (P[P_34][b3] & 0xff) ^ b3(k3); b0 = (P[P_03][b0] & 0xff) ^ b0(k2); b1 = (P[P_13][b1] & 0xff) ^ b1(k2); b2 = (P[P_23][b2] & 0xff) ^ b2(k2); b3 = (P[P_33][b3] & 0xff) ^ b3(k2); [(P[P_02][b0] & 0xff) ^ b0(k1)] & 0xff) ^ b0(k0)]; gSBox[i*2+1] = gMDS1[(P[P_11] [(P[P_12][b1] & 0xff) ^ b1(k1)] & 0xff) ^ b1(k0)];
int dstIndex) int x2 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[OUTPUT_WHITEN]; int x3 = BytesTo32Bits(src, srcIndex+4) ^ gSubKeys[OUTPUT_WHITEN + 1]; int x0 = BytesTo32Bits(src, srcIndex+8) ^ gSubKeys[OUTPUT_WHITEN + 2]; int x1 = BytesTo32Bits(src, srcIndex+12) ^ gSubKeys[OUTPUT_WHITEN + 3]; for (int r = 0; r< ROUNDS ; r +=2) t0 = Fe32_0(x2); t1 = Fe32_3(x3); x1 ^= t0 + 2*t1 + gSubKeys[k--]; x0 = (x0 << 1 | x0 >>> 31) ^ (t0 + t1 + gSubKeys[k--]); x1 = x1 >>>1 | x1 << 31; t0 = Fe32_0(x0); t1 = Fe32_3(x1); x3 ^= t0 + 2*t1 + gSubKeys[k--]; x2 = (x2 << 1 | x2 >>> 31) ^ (t0 + t1 + gSubKeys[k--]); Bits32ToBytes(x0 ^ gSubKeys[INPUT_WHITEN], dst, dstIndex); Bits32ToBytes(x1 ^ gSubKeys[INPUT_WHITEN + 1], dst, dstIndex + 4); Bits32ToBytes(x2 ^ gSubKeys[INPUT_WHITEN + 2], dst, dstIndex + 8); Bits32ToBytes(x3 ^ gSubKeys[INPUT_WHITEN + 3], dst, dstIndex + 12);
private int Mx_X(int x) { return x ^ LFSR2(x); } // 5B
k32e[i] = BytesTo32Bits(key, p); k32o[i] = BytesTo32Bits(key, p+4); sBoxKeys[k64Cnt-1-i] = RS_MDS_Encode(k32e[i], k32o[i]); A = F32(q, k32e); B = F32(q+SK_BUMP, k32o); B = B << 8 | B >>> 24; A += B; gSBox[i*2] = gMDS0[(P[P_01][b0] & 0xff) ^ b0(k0)]; gSBox[i*2+1] = gMDS1[(P[P_11][b1] & 0xff) ^ b1(k0)]; gSBox[i*2+0x200] = gMDS2[(P[P_21][b2] & 0xff) ^ b2(k0)]; gSBox[i*2+0x201] = gMDS3[(P[P_31][b3] & 0xff) ^ b3(k0)]; break; case 0: // 256 bits of key b0 = (P[P_04][b0] & 0xff) ^ b0(k3); b1 = (P[P_14][b1] & 0xff) ^ b1(k3); b2 = (P[P_24][b2] & 0xff) ^ b2(k3); b3 = (P[P_34][b3] & 0xff) ^ b3(k3); b0 = (P[P_03][b0] & 0xff) ^ b0(k2); b1 = (P[P_13][b1] & 0xff) ^ b1(k2); b2 = (P[P_23][b2] & 0xff) ^ b2(k2); b3 = (P[P_33][b3] & 0xff) ^ b3(k2); [(P[P_02][b0] & 0xff) ^ b0(k1)] & 0xff) ^ b0(k0)]; gSBox[i*2+1] = gMDS1[(P[P_11] [(P[P_12][b1] & 0xff) ^ b1(k1)] & 0xff) ^ b1(k0)];
int dstIndex) int x0 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[INPUT_WHITEN]; int x1 = BytesTo32Bits(src, srcIndex + 4) ^ gSubKeys[INPUT_WHITEN + 1]; int x2 = BytesTo32Bits(src, srcIndex + 8) ^ gSubKeys[INPUT_WHITEN + 2]; int x3 = BytesTo32Bits(src, srcIndex + 12) ^ gSubKeys[INPUT_WHITEN + 3]; for (int r = 0; r < ROUNDS; r +=2) t0 = Fe32_0(x0); t1 = Fe32_3(x1); x2 ^= t0 + t1 + gSubKeys[k++]; x2 = x2 >>>1 | x2 << 31; x3 = (x3 << 1 | x3 >>> 31) ^ (t0 + 2*t1 + gSubKeys[k++]); t0 = Fe32_0(x2); t1 = Fe32_3(x3); x0 ^= t0 + t1 + gSubKeys[k++]; x0 = x0 >>>1 | x0 << 31; Bits32ToBytes(x2 ^ gSubKeys[OUTPUT_WHITEN], dst, dstIndex); Bits32ToBytes(x3 ^ gSubKeys[OUTPUT_WHITEN + 1], dst, dstIndex + 4); Bits32ToBytes(x0 ^ gSubKeys[OUTPUT_WHITEN + 2], dst, dstIndex + 8); Bits32ToBytes(x1 ^ gSubKeys[OUTPUT_WHITEN + 3], dst, dstIndex + 12);
private int Mx_X(int x) { return x ^ LFSR2(x); } // 5B
int dstIndex) int x2 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[OUTPUT_WHITEN]; int x3 = BytesTo32Bits(src, srcIndex+4) ^ gSubKeys[OUTPUT_WHITEN + 1]; int x0 = BytesTo32Bits(src, srcIndex+8) ^ gSubKeys[OUTPUT_WHITEN + 2]; int x1 = BytesTo32Bits(src, srcIndex+12) ^ gSubKeys[OUTPUT_WHITEN + 3]; for (int r = 0; r< ROUNDS ; r +=2) t0 = Fe32_0(x2); t1 = Fe32_3(x3); x1 ^= t0 + 2*t1 + gSubKeys[k--]; x0 = (x0 << 1 | x0 >>> 31) ^ (t0 + t1 + gSubKeys[k--]); x1 = x1 >>>1 | x1 << 31; t0 = Fe32_0(x0); t1 = Fe32_3(x1); x3 ^= t0 + 2*t1 + gSubKeys[k--]; x2 = (x2 << 1 | x2 >>> 31) ^ (t0 + t1 + gSubKeys[k--]); Bits32ToBytes(x0 ^ gSubKeys[INPUT_WHITEN], dst, dstIndex); Bits32ToBytes(x1 ^ gSubKeys[INPUT_WHITEN + 1], dst, dstIndex + 4); Bits32ToBytes(x2 ^ gSubKeys[INPUT_WHITEN + 2], dst, dstIndex + 8); Bits32ToBytes(x3 ^ gSubKeys[INPUT_WHITEN + 3], dst, dstIndex + 12);
public BlockCipher get() { return new TwofishEngine(); } });
public TwofishEngine() { // calculate the MDS matrix int[] m1 = new int[2]; int[] mX = new int[2]; int[] mY = new int[2]; int j; for (int i=0; i< MAX_KEY_BITS ; i++) { j = P[0][i] & 0xff; m1[0] = j; mX[0] = Mx_X(j) & 0xff; mY[0] = Mx_Y(j) & 0xff; j = P[1][i] & 0xff; m1[1] = j; mX[1] = Mx_X(j) & 0xff; mY[1] = Mx_Y(j) & 0xff; gMDS0[i] = m1[P_00] | mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24; gMDS1[i] = mY[P_10] | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24; gMDS2[i] = mX[P_20] | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24; gMDS3[i] = mX[P_30] | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24; } }
int dstIndex) int x0 = BytesTo32Bits(src, srcIndex) ^ gSubKeys[INPUT_WHITEN]; int x1 = BytesTo32Bits(src, srcIndex + 4) ^ gSubKeys[INPUT_WHITEN + 1]; int x2 = BytesTo32Bits(src, srcIndex + 8) ^ gSubKeys[INPUT_WHITEN + 2]; int x3 = BytesTo32Bits(src, srcIndex + 12) ^ gSubKeys[INPUT_WHITEN + 3]; for (int r = 0; r < ROUNDS; r +=2) t0 = Fe32_0(x0); t1 = Fe32_3(x1); x2 ^= t0 + t1 + gSubKeys[k++]; x2 = x2 >>>1 | x2 << 31; x3 = (x3 << 1 | x3 >>> 31) ^ (t0 + 2*t1 + gSubKeys[k++]); t0 = Fe32_0(x2); t1 = Fe32_3(x3); x0 ^= t0 + t1 + gSubKeys[k++]; x0 = x0 >>>1 | x0 << 31; Bits32ToBytes(x2 ^ gSubKeys[OUTPUT_WHITEN], dst, dstIndex); Bits32ToBytes(x3 ^ gSubKeys[OUTPUT_WHITEN + 1], dst, dstIndex + 4); Bits32ToBytes(x0 ^ gSubKeys[OUTPUT_WHITEN + 2], dst, dstIndex + 8); Bits32ToBytes(x1 ^ gSubKeys[OUTPUT_WHITEN + 3], dst, dstIndex + 12);
public OldPBEWithSHAAndTwofish() { super(new CBCBlockCipher(new TwofishEngine()), OLD_PKCS12, SHA1, 256, 128); } }
public Poly1305() { super(new org.spongycastle.crypto.macs.Poly1305(new TwofishEngine())); } }
public PBEWithSHA() { super(new CBCBlockCipher(new TwofishEngine()), PKCS12, SHA1, 256, 16); } }
public Poly1305() { super(new org.spongycastle.crypto.macs.Poly1305(new TwofishEngine())); } }
public Twofish_CFB8() { super(new CFBBlockCipher(new TwofishEngine(), 8), 128); } }
public OldPBEWithSHAAndTwofish() { super(new CBCBlockCipher(new TwofishEngine()), OLD_PKCS12, SHA1, 256, 128); } }