what-the-bank/sources/org/bouncycastle/crypto/generators/KDFCounterBytesGenerator.java

114 lines
4.2 KiB
Java
Raw 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.KDFCounterParameters;
import org.bouncycastle.crypto.params.KeyParameter;
/* loaded from: classes6.dex */
public class KDFCounterBytesGenerator implements MacDerivationFunction {
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(2147483647L);
private static final BigInteger TWO = BigInteger.valueOf(2);
private byte[] fixedInputDataCtrPrefix;
private byte[] fixedInputData_afterCtr;
private int generatedBytes;
private final int h;
private byte[] ios;
private byte[] k;
private int maxSizeExcl;
private final Mac prf;
@Override // org.bouncycastle.crypto.DerivationFunction
public void init(DerivationParameters derivationParameters) {
if (!(derivationParameters instanceof KDFCounterParameters)) {
throw new IllegalArgumentException("Wrong type of arguments given");
}
KDFCounterParameters kDFCounterParameters = (KDFCounterParameters) derivationParameters;
this.prf.init(new KeyParameter(kDFCounterParameters.getKI()));
this.fixedInputDataCtrPrefix = kDFCounterParameters.getFixedInputDataCounterPrefix();
this.fixedInputData_afterCtr = kDFCounterParameters.getFixedInputDataCounterSuffix();
int r = kDFCounterParameters.getR();
this.ios = new byte[r / 8];
BigInteger multiply = TWO.pow(r).multiply(BigInteger.valueOf(this.h));
this.maxSizeExcl = multiply.compareTo(INTEGER_MAX) == 1 ? Integer.MAX_VALUE : multiply.intValue();
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() {
int i = (this.generatedBytes / this.h) + 1;
byte[] bArr = this.ios;
int length = bArr.length;
if (length != 1) {
if (length != 2) {
if (length != 3) {
if (length != 4) {
throw new IllegalStateException("Unsupported size of counter i");
}
bArr[0] = (byte) (i >>> 24);
}
bArr[bArr.length - 3] = (byte) (i >>> 16);
}
bArr[bArr.length - 2] = (byte) (i >>> 8);
}
bArr[bArr.length - 1] = (byte) i;
Mac mac = this.prf;
byte[] bArr2 = this.fixedInputDataCtrPrefix;
mac.update(bArr2, 0, bArr2.length);
Mac mac2 = this.prf;
byte[] bArr3 = this.ios;
mac2.update(bArr3, 0, bArr3.length);
Mac mac3 = this.prf;
byte[] bArr4 = this.fixedInputData_afterCtr;
mac3.update(bArr4, 0, bArr4.length);
this.prf.doFinal(this.k, 0);
}
public KDFCounterBytesGenerator(Mac mac) {
this.prf = mac;
int macSize = mac.getMacSize();
this.h = macSize;
this.k = new byte[macSize];
}
}