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

109 lines
3.8 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.generators;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.DerivationFunction;
import org.bouncycastle.crypto.DerivationParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.crypto.params.KeyParameter;
/* loaded from: classes6.dex */
public class HKDFBytesGenerator implements DerivationFunction {
private byte[] currentT;
private int generatedBytes;
private HMac hMacHash;
private int hashLen;
private byte[] info;
@Override // org.bouncycastle.crypto.DerivationFunction
public void init(DerivationParameters derivationParameters) {
HMac hMac;
KeyParameter extract;
if (!(derivationParameters instanceof HKDFParameters)) {
throw new IllegalArgumentException("HKDF parameters required for HKDFBytesGenerator");
}
HKDFParameters hKDFParameters = (HKDFParameters) derivationParameters;
if (hKDFParameters.skipExtract()) {
hMac = this.hMacHash;
extract = new KeyParameter(hKDFParameters.getIKM());
} else {
hMac = this.hMacHash;
extract = extract(hKDFParameters.getSalt(), hKDFParameters.getIKM());
}
hMac.init(extract);
this.info = hKDFParameters.getInfo();
this.generatedBytes = 0;
this.currentT = new byte[this.hashLen];
}
public Digest getDigest() {
return this.hMacHash.getUnderlyingDigest();
}
@Override // org.bouncycastle.crypto.DerivationFunction
public int generateBytes(byte[] bArr, int i, int i2) throws DataLengthException, IllegalArgumentException {
int i3 = this.generatedBytes;
int i4 = this.hashLen;
if (i3 + i2 > i4 * 255) {
throw new DataLengthException("HKDF may only be used for 255 * HashLen bytes of output");
}
if (i3 % i4 == 0) {
expandNext();
}
int i5 = this.generatedBytes;
int i6 = this.hashLen;
int i7 = i5 % i6;
int min = Math.min(i6 - i7, i2);
System.arraycopy(this.currentT, i7, bArr, i, min);
this.generatedBytes += min;
int i8 = i2 - min;
while (true) {
i += min;
if (i8 <= 0) {
return i2;
}
expandNext();
min = Math.min(this.hashLen, i8);
System.arraycopy(this.currentT, 0, bArr, i, min);
this.generatedBytes += min;
i8 -= min;
}
}
private KeyParameter extract(byte[] bArr, byte[] bArr2) {
this.hMacHash.init(new KeyParameter(bArr2));
if (bArr == null) {
this.hMacHash.init(new KeyParameter(new byte[this.hashLen]));
} else {
this.hMacHash.init(new KeyParameter(bArr));
}
this.hMacHash.update(bArr2, 0, bArr2.length);
byte[] bArr3 = new byte[this.hashLen];
this.hMacHash.doFinal(bArr3, 0);
return new KeyParameter(bArr3);
}
private void expandNext() throws DataLengthException {
int i = this.generatedBytes;
int i2 = this.hashLen;
int i3 = (i / i2) + 1;
if (i3 >= 256) {
throw new DataLengthException("HKDF cannot generate more than 255 blocks of HashLen size");
}
if (i != 0) {
this.hMacHash.update(this.currentT, 0, i2);
}
HMac hMac = this.hMacHash;
byte[] bArr = this.info;
hMac.update(bArr, 0, bArr.length);
this.hMacHash.update((byte) i3);
this.hMacHash.doFinal(this.currentT, 0);
}
public HKDFBytesGenerator(Digest digest) {
this.hMacHash = new HMac(digest);
this.hashLen = digest.getDigestSize();
}
}