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.KDFFeedbackParameters; import org.bouncycastle.crypto.params.KeyParameter; /* loaded from: classes6.dex */ public class KDFFeedbackBytesGenerator implements MacDerivationFunction { private static final BigInteger INTEGER_MAX = BigInteger.valueOf(2147483647L); private static final BigInteger TWO = BigInteger.valueOf(2); private byte[] fixedInputData; private int generatedBytes; private final int h; private byte[] ios; private byte[] iv; 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 KDFFeedbackParameters)) { throw new IllegalArgumentException("Wrong type of arguments given"); } KDFFeedbackParameters kDFFeedbackParameters = (KDFFeedbackParameters) derivationParameters; this.prf.init(new KeyParameter(kDFFeedbackParameters.getKI())); this.fixedInputData = kDFFeedbackParameters.getFixedInputData(); int r = kDFFeedbackParameters.getR(); this.ios = new byte[r / 8]; int i = Integer.MAX_VALUE; if (kDFFeedbackParameters.useCounter()) { BigInteger multiply = TWO.pow(r).multiply(BigInteger.valueOf(this.h)); if (multiply.compareTo(INTEGER_MAX) != 1) { i = multiply.intValue(); } } this.maxSizeExcl = i; this.iv = kDFFeedbackParameters.getIV(); this.useCounter = kDFFeedbackParameters.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.iv; mac.update(bArr, 0, bArr.length); } else { Mac mac2 = this.prf; byte[] bArr2 = this.k; mac2.update(bArr2, 0, bArr2.length); } if (this.useCounter) { int i = (this.generatedBytes / this.h) + 1; byte[] bArr3 = this.ios; int length = bArr3.length; if (length != 1) { if (length != 2) { if (length != 3) { if (length != 4) { throw new IllegalStateException("Unsupported size of counter i"); } bArr3[0] = (byte) (i >>> 24); } bArr3[bArr3.length - 3] = (byte) (i >>> 16); } bArr3[bArr3.length - 2] = (byte) (i >>> 8); } bArr3[bArr3.length - 1] = (byte) i; this.prf.update(bArr3, 0, bArr3.length); } Mac mac3 = this.prf; byte[] bArr4 = this.fixedInputData; mac3.update(bArr4, 0, bArr4.length); this.prf.doFinal(this.k, 0); } public KDFFeedbackBytesGenerator(Mac mac) { this.prf = mac; int macSize = mac.getMacSize(); this.h = macSize; this.k = new byte[macSize]; } }