118 lines
4.3 KiB
Java
118 lines
4.3 KiB
Java
package org.bouncycastle.crypto.engines;
|
|
|
|
import com.google.common.base.Ascii;
|
|
import com.google.common.primitives.UnsignedBytes;
|
|
import org.bouncycastle.crypto.BlockCipher;
|
|
import org.bouncycastle.crypto.CipherParameters;
|
|
import org.bouncycastle.crypto.DataLengthException;
|
|
import org.bouncycastle.crypto.OutputLengthException;
|
|
import org.bouncycastle.crypto.params.KeyParameter;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class TEAEngine implements BlockCipher {
|
|
private static final int block_size = 8;
|
|
private static final int d_sum = -957401312;
|
|
private static final int delta = -1640531527;
|
|
private static final int rounds = 32;
|
|
private int _a;
|
|
private int _b;
|
|
private int _c;
|
|
private int _d;
|
|
private boolean _forEncryption;
|
|
private boolean _initialised = false;
|
|
|
|
@Override // org.bouncycastle.crypto.BlockCipher
|
|
public int getBlockSize() {
|
|
return 8;
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.BlockCipher
|
|
public void reset() {
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.BlockCipher
|
|
public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
|
|
if (!this._initialised) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(getAlgorithmName());
|
|
sb.append(" not initialised");
|
|
throw new IllegalStateException(sb.toString());
|
|
}
|
|
if (i + 8 > bArr.length) {
|
|
throw new DataLengthException("input buffer too short");
|
|
}
|
|
if (i2 + 8 <= bArr2.length) {
|
|
return this._forEncryption ? encryptBlock(bArr, i, bArr2, i2) : decryptBlock(bArr, i, bArr2, i2);
|
|
}
|
|
throw new OutputLengthException("output buffer too short");
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.BlockCipher
|
|
public void init(boolean z, CipherParameters cipherParameters) {
|
|
if (!(cipherParameters instanceof KeyParameter)) {
|
|
StringBuilder sb = new StringBuilder("invalid parameter passed to TEA init - ");
|
|
sb.append(cipherParameters.getClass().getName());
|
|
throw new IllegalArgumentException(sb.toString());
|
|
}
|
|
this._forEncryption = z;
|
|
this._initialised = true;
|
|
setKey(((KeyParameter) cipherParameters).getKey());
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.BlockCipher
|
|
public String getAlgorithmName() {
|
|
return "TEA";
|
|
}
|
|
|
|
private void unpackInt(int i, byte[] bArr, int i2) {
|
|
bArr[i2] = (byte) (i >>> 24);
|
|
bArr[i2 + 1] = (byte) (i >>> 16);
|
|
bArr[i2 + 2] = (byte) (i >>> 8);
|
|
bArr[i2 + 3] = (byte) i;
|
|
}
|
|
|
|
private void setKey(byte[] bArr) {
|
|
if (bArr.length != 16) {
|
|
throw new IllegalArgumentException("Key size must be 128 bits.");
|
|
}
|
|
this._a = bytesToInt(bArr, 0);
|
|
this._b = bytesToInt(bArr, 4);
|
|
this._c = bytesToInt(bArr, 8);
|
|
this._d = bytesToInt(bArr, 12);
|
|
}
|
|
|
|
private int encryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
|
|
int bytesToInt = bytesToInt(bArr, i);
|
|
int bytesToInt2 = bytesToInt(bArr, i + 4);
|
|
int i3 = bytesToInt;
|
|
int i4 = 0;
|
|
for (int i5 = 0; i5 != 32; i5++) {
|
|
i4 -= 1640531527;
|
|
i3 += (((bytesToInt2 << 4) + this._a) ^ (bytesToInt2 + i4)) ^ ((bytesToInt2 >>> 5) + this._b);
|
|
bytesToInt2 += (((i3 << 4) + this._c) ^ (i3 + i4)) ^ ((i3 >>> 5) + this._d);
|
|
}
|
|
unpackInt(i3, bArr2, i2);
|
|
unpackInt(bytesToInt2, bArr2, i2 + 4);
|
|
return 8;
|
|
}
|
|
|
|
private int decryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
|
|
int bytesToInt = bytesToInt(bArr, i);
|
|
int bytesToInt2 = bytesToInt(bArr, i + 4);
|
|
int i3 = d_sum;
|
|
for (int i4 = 0; i4 != 32; i4++) {
|
|
bytesToInt2 -= (((bytesToInt << 4) + this._c) ^ (bytesToInt + i3)) ^ ((bytesToInt >>> 5) + this._d);
|
|
bytesToInt -= (((bytesToInt2 << 4) + this._a) ^ (bytesToInt2 + i3)) ^ ((bytesToInt2 >>> 5) + this._b);
|
|
i3 += 1640531527;
|
|
}
|
|
unpackInt(bytesToInt, bArr2, i2);
|
|
unpackInt(bytesToInt2, bArr2, i2 + 4);
|
|
return 8;
|
|
}
|
|
|
|
private int bytesToInt(byte[] bArr, int i) {
|
|
byte b = bArr[i];
|
|
return (bArr[i + 3] & UnsignedBytes.MAX_VALUE) | ((bArr[i + 1] & UnsignedBytes.MAX_VALUE) << 16) | (b << Ascii.CAN) | ((bArr[i + 2] & UnsignedBytes.MAX_VALUE) << 8);
|
|
}
|
|
}
|