what-the-bank/sources/org/bouncycastle/crypto/generators/KDFDoublePipelineIterationB...

133 lines
4.9 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.generators;
import java.math.BigInteger;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.DerivationParameters;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.MacDerivationFunction;
import org.bouncycastle.crypto.params.KDFDoublePipelineIterationParameters;
import org.bouncycastle.crypto.params.KeyParameter;
/* loaded from: classes6.dex */
public class KDFDoublePipelineIterationBytesGenerator implements MacDerivationFunction {
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(2147483647L);
private static final BigInteger TWO = BigInteger.valueOf(2);
private byte[] a;
private byte[] fixedInputData;
private int generatedBytes;
private final int h;
private byte[] ios;
private byte[] k;
private int maxSizeExcl;
private final Mac prf;
private boolean useCounter;
@Override // org.bouncycastle.crypto.DerivationFunction
public void init(DerivationParameters derivationParameters) {
if (!(derivationParameters instanceof KDFDoublePipelineIterationParameters)) {
throw new IllegalArgumentException("Wrong type of arguments given");
}
KDFDoublePipelineIterationParameters kDFDoublePipelineIterationParameters = (KDFDoublePipelineIterationParameters) derivationParameters;
this.prf.init(new KeyParameter(kDFDoublePipelineIterationParameters.getKI()));
this.fixedInputData = kDFDoublePipelineIterationParameters.getFixedInputData();
int r = kDFDoublePipelineIterationParameters.getR();
this.ios = new byte[r / 8];
int i = Integer.MAX_VALUE;
if (kDFDoublePipelineIterationParameters.useCounter()) {
BigInteger multiply = TWO.pow(r).multiply(BigInteger.valueOf(this.h));
if (multiply.compareTo(INTEGER_MAX) != 1) {
i = multiply.intValue();
}
}
this.maxSizeExcl = i;
this.useCounter = kDFDoublePipelineIterationParameters.useCounter();
this.generatedBytes = 0;
}
@Override // org.bouncycastle.crypto.MacDerivationFunction
public Mac getMac() {
return this.prf;
}
@Override // org.bouncycastle.crypto.DerivationFunction
public int generateBytes(byte[] bArr, int i, int i2) throws DataLengthException, IllegalArgumentException {
int i3 = this.generatedBytes;
int i4 = i3 + i2;
if (i4 < 0 || i4 >= this.maxSizeExcl) {
StringBuilder sb = new StringBuilder("Current KDFCTR may only be used for ");
sb.append(this.maxSizeExcl);
sb.append(" bytes");
throw new DataLengthException(sb.toString());
}
if (i3 % this.h == 0) {
generateNext();
}
int i5 = this.generatedBytes;
int i6 = this.h;
int i7 = i5 % i6;
int min = Math.min(i6 - i7, i2);
System.arraycopy(this.k, i7, bArr, i, min);
this.generatedBytes += min;
int i8 = i2 - min;
while (true) {
i += min;
if (i8 <= 0) {
return i2;
}
generateNext();
min = Math.min(this.h, i8);
System.arraycopy(this.k, 0, bArr, i, min);
this.generatedBytes += min;
i8 -= min;
}
}
private void generateNext() {
if (this.generatedBytes == 0) {
Mac mac = this.prf;
byte[] bArr = this.fixedInputData;
mac.update(bArr, 0, bArr.length);
this.prf.doFinal(this.a, 0);
} else {
Mac mac2 = this.prf;
byte[] bArr2 = this.a;
mac2.update(bArr2, 0, bArr2.length);
this.prf.doFinal(this.a, 0);
}
Mac mac3 = this.prf;
byte[] bArr3 = this.a;
mac3.update(bArr3, 0, bArr3.length);
if (this.useCounter) {
int i = (this.generatedBytes / this.h) + 1;
byte[] bArr4 = this.ios;
int length = bArr4.length;
if (length != 1) {
if (length != 2) {
if (length != 3) {
if (length != 4) {
throw new IllegalStateException("Unsupported size of counter i");
}
bArr4[0] = (byte) (i >>> 24);
}
bArr4[bArr4.length - 3] = (byte) (i >>> 16);
}
bArr4[bArr4.length - 2] = (byte) (i >>> 8);
}
bArr4[bArr4.length - 1] = (byte) i;
this.prf.update(bArr4, 0, bArr4.length);
}
Mac mac4 = this.prf;
byte[] bArr5 = this.fixedInputData;
mac4.update(bArr5, 0, bArr5.length);
this.prf.doFinal(this.k, 0);
}
public KDFDoublePipelineIterationBytesGenerator(Mac mac) {
this.prf = mac;
int macSize = mac.getMacSize();
this.h = macSize;
this.a = new byte[macSize];
this.k = new byte[macSize];
}
}