what-the-bank/sources/org/bouncycastle/crypto/modes/OFBBlockCipher.java

118 lines
4.0 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.modes;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.StreamBlockCipher;
import org.bouncycastle.crypto.params.ParametersWithIV;
/* loaded from: classes6.dex */
public class OFBBlockCipher extends StreamBlockCipher {
private byte[] IV;
private final int blockSize;
private int byteCount;
private final BlockCipher cipher;
private byte[] ofbOutV;
private byte[] ofbV;
@Override // org.bouncycastle.crypto.BlockCipher
public void reset() {
byte[] bArr = this.IV;
System.arraycopy(bArr, 0, this.ofbV, 0, bArr.length);
this.byteCount = 0;
this.cipher.reset();
}
@Override // org.bouncycastle.crypto.BlockCipher
public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) throws DataLengthException, IllegalStateException {
processBytes(bArr, i, this.blockSize, bArr2, i2);
return this.blockSize;
}
@Override // org.bouncycastle.crypto.BlockCipher
public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
BlockCipher blockCipher;
if (cipherParameters instanceof ParametersWithIV) {
ParametersWithIV parametersWithIV = (ParametersWithIV) cipherParameters;
byte[] iv = parametersWithIV.getIV();
int length = iv.length;
byte[] bArr = this.IV;
if (length < bArr.length) {
System.arraycopy(iv, 0, bArr, bArr.length - iv.length, iv.length);
int i = 0;
while (true) {
byte[] bArr2 = this.IV;
if (i >= bArr2.length - iv.length) {
break;
}
bArr2[i] = 0;
i++;
}
} else {
System.arraycopy(iv, 0, bArr, 0, bArr.length);
}
reset();
if (parametersWithIV.getParameters() == null) {
return;
}
blockCipher = this.cipher;
cipherParameters = parametersWithIV.getParameters();
} else {
reset();
if (cipherParameters == null) {
return;
} else {
blockCipher = this.cipher;
}
}
blockCipher.init(true, cipherParameters);
}
@Override // org.bouncycastle.crypto.BlockCipher
public int getBlockSize() {
return this.blockSize;
}
@Override // org.bouncycastle.crypto.BlockCipher
public String getAlgorithmName() {
StringBuilder sb = new StringBuilder();
sb.append(this.cipher.getAlgorithmName());
sb.append("/OFB");
sb.append(this.blockSize << 3);
return sb.toString();
}
@Override // org.bouncycastle.crypto.StreamBlockCipher
public byte calculateByte(byte b) throws DataLengthException, IllegalStateException {
if (this.byteCount == 0) {
this.cipher.processBlock(this.ofbV, 0, this.ofbOutV, 0);
}
byte[] bArr = this.ofbOutV;
int i = this.byteCount;
int i2 = i + 1;
this.byteCount = i2;
byte b2 = (byte) (b ^ bArr[i]);
int i3 = this.blockSize;
if (i2 == i3) {
this.byteCount = 0;
byte[] bArr2 = this.ofbV;
System.arraycopy(bArr2, i3, bArr2, 0, bArr2.length - i3);
byte[] bArr3 = this.ofbOutV;
byte[] bArr4 = this.ofbV;
int length = bArr4.length;
int i4 = this.blockSize;
System.arraycopy(bArr3, 0, bArr4, length - i4, i4);
}
return b2;
}
public OFBBlockCipher(BlockCipher blockCipher, int i) {
super(blockCipher);
this.cipher = blockCipher;
this.blockSize = i / 8;
this.IV = new byte[blockCipher.getBlockSize()];
this.ofbV = new byte[blockCipher.getBlockSize()];
this.ofbOutV = new byte[blockCipher.getBlockSize()];
}
}