what-the-bank/sources/org/bouncycastle/crypto/engines/RC564Engine.java

139 lines
4.8 KiB
Java

package org.bouncycastle.crypto.engines;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.RC5Parameters;
/* loaded from: classes6.dex */
public class RC564Engine implements BlockCipher {
private static final long P64 = -5196783011329398165L;
private static final long Q64 = -7046029254386353131L;
private static final int bytesPerWord = 8;
private static final int wordSize = 64;
private boolean forEncryption;
private int _noRounds = 12;
private long[] _S = null;
private long rotateLeft(long j, long j2) {
long j3 = j2 & 63;
int i = (int) j3;
return (j << i) | (j >>> ((int) (64 - j3)));
}
private long rotateRight(long j, long j2) {
long j3 = j2 & 63;
int i = (int) j3;
return (j >>> i) | (j << ((int) (64 - j3)));
}
@Override // org.bouncycastle.crypto.BlockCipher
public int getBlockSize() {
return 16;
}
@Override // org.bouncycastle.crypto.BlockCipher
public void reset() {
}
@Override // org.bouncycastle.crypto.BlockCipher
public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
return this.forEncryption ? encryptBlock(bArr, i, bArr2, i2) : decryptBlock(bArr, i, bArr2, i2);
}
@Override // org.bouncycastle.crypto.BlockCipher
public void init(boolean z, CipherParameters cipherParameters) {
if (!(cipherParameters instanceof RC5Parameters)) {
StringBuilder sb = new StringBuilder("invalid parameter passed to RC564 init - ");
sb.append(cipherParameters.getClass().getName());
throw new IllegalArgumentException(sb.toString());
}
RC5Parameters rC5Parameters = (RC5Parameters) cipherParameters;
this.forEncryption = z;
this._noRounds = rC5Parameters.getRounds();
setKey(rC5Parameters.getKey());
}
@Override // org.bouncycastle.crypto.BlockCipher
public String getAlgorithmName() {
return "RC5-64";
}
private void wordToBytes(long j, byte[] bArr, int i) {
for (int i2 = 0; i2 < 8; i2++) {
bArr[i2 + i] = (byte) j;
j >>>= 8;
}
}
private void setKey(byte[] bArr) {
long[] jArr;
int length = (bArr.length + 7) / 8;
long[] jArr2 = new long[length];
for (int i = 0; i != bArr.length; i++) {
int i2 = i / 8;
jArr2[i2] = jArr2[i2] + ((bArr[i] & UnsignedBytes.MAX_VALUE) << ((i % 8) << 3));
}
long[] jArr3 = new long[(this._noRounds + 1) << 1];
this._S = jArr3;
jArr3[0] = -5196783011329398165L;
int i3 = 1;
while (true) {
jArr = this._S;
if (i3 >= jArr.length) {
break;
}
jArr[i3] = jArr[i3 - 1] - 7046029254386353131L;
i3++;
}
int length2 = length > jArr.length ? length * 3 : jArr.length * 3;
long j = 0;
long j2 = 0;
int i4 = 0;
int i5 = 0;
for (int i6 = 0; i6 < length2; i6++) {
long[] jArr4 = this._S;
j = rotateLeft(jArr4[i4] + j + j2, 3L);
jArr4[i4] = j;
j2 = rotateLeft(jArr2[i5] + j + j2, j2 + j);
jArr2[i5] = j2;
i4 = (i4 + 1) % this._S.length;
i5 = (i5 + 1) % length;
}
}
private int encryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
long bytesToWord = bytesToWord(bArr, i) + this._S[0];
long bytesToWord2 = bytesToWord(bArr, i + 8) + this._S[1];
for (int i3 = 1; i3 <= this._noRounds; i3++) {
int i4 = i3 << 1;
bytesToWord = rotateLeft(bytesToWord ^ bytesToWord2, bytesToWord2) + this._S[i4];
bytesToWord2 = rotateLeft(bytesToWord2 ^ bytesToWord, bytesToWord) + this._S[i4 + 1];
}
wordToBytes(bytesToWord, bArr2, i2);
wordToBytes(bytesToWord2, bArr2, i2 + 8);
return 16;
}
private int decryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) {
long bytesToWord = bytesToWord(bArr, i);
long bytesToWord2 = bytesToWord(bArr, i + 8);
for (int i3 = this._noRounds; i3 > 0; i3--) {
int i4 = i3 << 1;
bytesToWord2 = rotateRight(bytesToWord2 - this._S[i4 + 1], bytesToWord) ^ bytesToWord;
bytesToWord = rotateRight(bytesToWord - this._S[i4], bytesToWord2) ^ bytesToWord2;
}
wordToBytes(bytesToWord - this._S[0], bArr2, i2);
wordToBytes(bytesToWord2 - this._S[1], bArr2, i2 + 8);
return 16;
}
private long bytesToWord(byte[] bArr, int i) {
long j = 0;
for (int i2 = 7; i2 >= 0; i2--) {
j = (j << 8) + (bArr[i2 + i] & UnsignedBytes.MAX_VALUE);
}
return j;
}
}