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

183 lines
6.7 KiB
Java

package org.bouncycastle.crypto.engines;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
/* loaded from: classes6.dex */
public class HC256Engine implements StreamCipher {
private boolean initialised;
private byte[] iv;
private byte[] key;
private int[] p = new int[1024];
private int[] q = new int[1024];
private int cnt = 0;
private byte[] buf = new byte[4];
private int idx = 0;
private static int rotateRight(int i, int i2) {
return (i >>> i2) | (i << (-i2));
}
@Override // org.bouncycastle.crypto.StreamCipher
public byte returnByte(byte b) {
return (byte) (b ^ getByte());
}
@Override // org.bouncycastle.crypto.StreamCipher
public void reset() {
init();
}
@Override // org.bouncycastle.crypto.StreamCipher
public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException {
if (!this.initialised) {
StringBuilder sb = new StringBuilder();
sb.append(getAlgorithmName());
sb.append(" not initialised");
throw new IllegalStateException(sb.toString());
}
if (i + i2 > bArr.length) {
throw new DataLengthException("input buffer too short");
}
if (i3 + i2 > bArr2.length) {
throw new OutputLengthException("output buffer too short");
}
for (int i4 = 0; i4 < i2; i4++) {
bArr2[i3 + i4] = (byte) (bArr[i + i4] ^ getByte());
}
return i2;
}
@Override // org.bouncycastle.crypto.StreamCipher
public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
CipherParameters cipherParameters2;
if (cipherParameters instanceof ParametersWithIV) {
ParametersWithIV parametersWithIV = (ParametersWithIV) cipherParameters;
this.iv = parametersWithIV.getIV();
cipherParameters2 = parametersWithIV.getParameters();
} else {
this.iv = new byte[0];
cipherParameters2 = cipherParameters;
}
if (!(cipherParameters2 instanceof KeyParameter)) {
StringBuilder sb = new StringBuilder("Invalid parameter passed to HC256 init - ");
sb.append(cipherParameters.getClass().getName());
throw new IllegalArgumentException(sb.toString());
}
this.key = ((KeyParameter) cipherParameters2).getKey();
init();
this.initialised = true;
}
@Override // org.bouncycastle.crypto.StreamCipher
public String getAlgorithmName() {
return "HC-256";
}
private int step() {
int i;
int i2;
int i3 = this.cnt;
int i4 = i3 & 1023;
if (i3 < 1024) {
int[] iArr = this.p;
int i5 = iArr[(i4 - 3) & 1023];
int i6 = iArr[(i4 - 1023) & 1023];
int i7 = iArr[i4];
int i8 = iArr[(i4 - 10) & 1023];
int rotateRight = rotateRight(i5, 10);
int rotateRight2 = rotateRight(i6, 23);
int[] iArr2 = this.q;
iArr[i4] = i7 + i8 + (rotateRight2 ^ rotateRight) + iArr2[(i5 ^ i6) & 1023];
int[] iArr3 = this.p;
int i9 = iArr3[(i4 - 12) & 1023];
i = iArr2[i9 & 255] + iArr2[((i9 >> 8) & 255) + 256] + iArr2[((i9 >> 16) & 255) + 512] + iArr2[(i9 >>> 24) + 768];
i2 = iArr3[i4];
} else {
int[] iArr4 = this.q;
int i10 = iArr4[(i4 - 3) & 1023];
int i11 = iArr4[(i4 - 1023) & 1023];
int i12 = iArr4[i4];
int i13 = iArr4[(i4 - 10) & 1023];
int rotateRight3 = rotateRight(i10, 10);
int rotateRight4 = rotateRight(i11, 23);
int[] iArr5 = this.p;
iArr4[i4] = i12 + i13 + (rotateRight4 ^ rotateRight3) + iArr5[(i10 ^ i11) & 1023];
int[] iArr6 = this.q;
int i14 = iArr6[(i4 - 12) & 1023];
i = iArr5[i14 & 255] + iArr5[((i14 >> 8) & 255) + 256] + iArr5[((i14 >> 16) & 255) + 512] + iArr5[(i14 >>> 24) + 768];
i2 = iArr6[i4];
}
this.cnt = (this.cnt + 1) & 2047;
return i2 ^ i;
}
private void init() {
byte[] bArr = this.key;
if (bArr.length != 32 && bArr.length != 16) {
throw new IllegalArgumentException("The key must be 128/256 bits long");
}
if (this.iv.length < 16) {
throw new IllegalArgumentException("The IV must be at least 128 bits long");
}
if (bArr.length != 32) {
byte[] bArr2 = new byte[32];
System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
byte[] bArr3 = this.key;
System.arraycopy(bArr3, 0, bArr2, 16, bArr3.length);
this.key = bArr2;
}
byte[] bArr4 = this.iv;
if (bArr4.length < 32) {
byte[] bArr5 = new byte[32];
System.arraycopy(bArr4, 0, bArr5, 0, bArr4.length);
byte[] bArr6 = this.iv;
System.arraycopy(bArr6, 0, bArr5, bArr6.length, 32 - bArr6.length);
this.iv = bArr5;
}
this.idx = 0;
this.cnt = 0;
int[] iArr = new int[2560];
for (int i = 0; i < 32; i++) {
int i2 = i >> 2;
iArr[i2] = iArr[i2] | ((this.key[i] & UnsignedBytes.MAX_VALUE) << ((i & 3) << 3));
}
for (int i3 = 0; i3 < 32; i3++) {
int i4 = (i3 >> 2) + 8;
iArr[i4] = iArr[i4] | ((this.iv[i3] & UnsignedBytes.MAX_VALUE) << ((i3 & 3) << 3));
}
for (int i5 = 16; i5 < 2560; i5++) {
int i6 = iArr[i5 - 2];
int i7 = iArr[i5 - 15];
iArr[i5] = ((rotateRight(i6, 19) ^ rotateRight(i6, 17)) ^ (i6 >>> 10)) + iArr[i5 - 7] + ((rotateRight(i7, 18) ^ rotateRight(i7, 7)) ^ (i7 >>> 3)) + iArr[i5 - 16] + i5;
}
System.arraycopy(iArr, 512, this.p, 0, 1024);
System.arraycopy(iArr, 1536, this.q, 0, 1024);
for (int i8 = 0; i8 < 4096; i8++) {
step();
}
this.cnt = 0;
}
private byte getByte() {
if (this.idx == 0) {
int step = step();
byte[] bArr = this.buf;
bArr[0] = (byte) step;
bArr[1] = (byte) (step >> 8);
bArr[2] = (byte) (step >> 16);
bArr[3] = (byte) (step >>> 24);
}
byte[] bArr2 = this.buf;
int i = this.idx;
byte b = bArr2[i];
this.idx = (i + 1) & 3;
return b;
}
}