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

124 lines
4.5 KiB
Java

package org.bouncycastle.crypto.modes;
import com.google.common.primitives.UnsignedBytes;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
/* loaded from: classes6.dex */
public class PaddedBlockCipher extends BufferedBlockCipher {
@Override // org.bouncycastle.crypto.BufferedBlockCipher
public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws DataLengthException, IllegalStateException {
if (i2 < 0) {
throw new IllegalArgumentException("Can't have a negative input length!");
}
int blockSize = getBlockSize();
int updateOutputSize = getUpdateOutputSize(i2);
if (updateOutputSize > 0 && updateOutputSize + i3 > bArr2.length) {
throw new DataLengthException("output buffer too short");
}
int length = this.buf.length - this.bufOff;
int i4 = 0;
if (i2 > length) {
System.arraycopy(bArr, i, this.buf, this.bufOff, length);
int processBlock = this.cipher.processBlock(this.buf, 0, bArr2, i3);
this.bufOff = 0;
i2 -= length;
i += length;
i4 = processBlock;
while (i2 > this.buf.length) {
i4 += this.cipher.processBlock(bArr, i, bArr2, i3 + i4);
i2 -= blockSize;
i += blockSize;
}
}
System.arraycopy(bArr, i, this.buf, this.bufOff, i2);
this.bufOff += i2;
return i4;
}
@Override // org.bouncycastle.crypto.BufferedBlockCipher
public int processByte(byte b, byte[] bArr, int i) throws DataLengthException, IllegalStateException {
int i2 = 0;
if (this.bufOff == this.buf.length) {
int processBlock = this.cipher.processBlock(this.buf, 0, bArr, i);
this.bufOff = 0;
i2 = processBlock;
}
byte[] bArr2 = this.buf;
int i3 = this.bufOff;
this.bufOff = i3 + 1;
bArr2[i3] = b;
return i2;
}
@Override // org.bouncycastle.crypto.BufferedBlockCipher
public int getUpdateOutputSize(int i) {
int i2 = i + this.bufOff;
int length = i2 % this.buf.length;
return length == 0 ? i2 - this.buf.length : i2 - length;
}
@Override // org.bouncycastle.crypto.BufferedBlockCipher
public int getOutputSize(int i) {
int length;
int i2 = i + this.bufOff;
int length2 = i2 % this.buf.length;
if (length2 != 0) {
i2 -= length2;
length = this.buf.length;
} else {
if (!this.forEncryption) {
return i2;
}
length = this.buf.length;
}
return i2 + length;
}
@Override // org.bouncycastle.crypto.BufferedBlockCipher
public int doFinal(byte[] bArr, int i) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
int i2;
int i3;
int blockSize = this.cipher.getBlockSize();
if (this.forEncryption) {
if (this.bufOff != blockSize) {
i3 = 0;
} else {
if ((blockSize << 1) + i > bArr.length) {
throw new DataLengthException("output buffer too short");
}
i3 = this.cipher.processBlock(this.buf, 0, bArr, i);
this.bufOff = 0;
}
byte b = (byte) (blockSize - this.bufOff);
while (this.bufOff < blockSize) {
this.buf[this.bufOff] = b;
this.bufOff++;
}
i2 = i3 + this.cipher.processBlock(this.buf, 0, bArr, i + i3);
} else {
if (this.bufOff != blockSize) {
throw new DataLengthException("last block incomplete in decryption");
}
int processBlock = this.cipher.processBlock(this.buf, 0, this.buf, 0);
this.bufOff = 0;
int i4 = this.buf[blockSize - 1] & UnsignedBytes.MAX_VALUE;
if (i4 < 0 || i4 > blockSize) {
throw new InvalidCipherTextException("pad block corrupted");
}
i2 = processBlock - i4;
System.arraycopy(this.buf, 0, bArr, i, i2);
}
reset();
return i2;
}
public PaddedBlockCipher(BlockCipher blockCipher) {
this.cipher = blockCipher;
this.buf = new byte[blockCipher.getBlockSize()];
this.bufOff = 0;
}
}