what-the-bank/sources/org/bouncycastle/crypto/macs/ISO9797Alg3Mac.java

174 lines
6.0 KiB
Java

package org.bouncycastle.crypto.macs;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.BlockCipherPadding;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
/* loaded from: classes6.dex */
public class ISO9797Alg3Mac implements Mac {
private byte[] buf;
private int bufOff;
private BlockCipher cipher;
private KeyParameter lastKey2;
private KeyParameter lastKey3;
private byte[] mac;
private int macSize;
private BlockCipherPadding padding;
@Override // org.bouncycastle.crypto.Mac
public void update(byte[] bArr, int i, int i2) {
if (i2 < 0) {
throw new IllegalArgumentException("Can't have a negative input length!");
}
int blockSize = this.cipher.getBlockSize();
int i3 = this.bufOff;
int i4 = blockSize - i3;
if (i2 > i4) {
System.arraycopy(bArr, i, this.buf, i3, i4);
this.cipher.processBlock(this.buf, 0, this.mac, 0);
this.bufOff = 0;
i2 -= i4;
i += i4;
while (i2 > blockSize) {
this.cipher.processBlock(bArr, i, this.mac, 0);
i2 -= blockSize;
i += blockSize;
}
}
System.arraycopy(bArr, i, this.buf, this.bufOff, i2);
this.bufOff += i2;
}
@Override // org.bouncycastle.crypto.Mac
public void update(byte b) {
int i = this.bufOff;
byte[] bArr = this.buf;
if (i == bArr.length) {
this.cipher.processBlock(bArr, 0, this.mac, 0);
this.bufOff = 0;
}
byte[] bArr2 = this.buf;
int i2 = this.bufOff;
this.bufOff = i2 + 1;
bArr2[i2] = b;
}
@Override // org.bouncycastle.crypto.Mac
public void reset() {
int i = 0;
while (true) {
byte[] bArr = this.buf;
if (i >= bArr.length) {
this.bufOff = 0;
this.cipher.reset();
return;
} else {
bArr[i] = 0;
i++;
}
}
}
@Override // org.bouncycastle.crypto.Mac
public void init(CipherParameters cipherParameters) {
KeyParameter keyParameter;
reset();
boolean z = cipherParameters instanceof KeyParameter;
if (!z && !(cipherParameters instanceof ParametersWithIV)) {
throw new IllegalArgumentException("params must be an instance of KeyParameter or ParametersWithIV");
}
byte[] key = (z ? (KeyParameter) cipherParameters : (KeyParameter) ((ParametersWithIV) cipherParameters).getParameters()).getKey();
if (key.length == 16) {
keyParameter = new KeyParameter(key, 0, 8);
this.lastKey2 = new KeyParameter(key, 8, 8);
this.lastKey3 = keyParameter;
} else {
if (key.length != 24) {
throw new IllegalArgumentException("Key must be either 112 or 168 bit long");
}
keyParameter = new KeyParameter(key, 0, 8);
this.lastKey2 = new KeyParameter(key, 8, 8);
this.lastKey3 = new KeyParameter(key, 16, 8);
}
if (cipherParameters instanceof ParametersWithIV) {
this.cipher.init(true, new ParametersWithIV(keyParameter, ((ParametersWithIV) cipherParameters).getIV()));
} else {
this.cipher.init(true, keyParameter);
}
}
@Override // org.bouncycastle.crypto.Mac
public int getMacSize() {
return this.macSize;
}
@Override // org.bouncycastle.crypto.Mac
public String getAlgorithmName() {
return "ISO9797Alg3";
}
@Override // org.bouncycastle.crypto.Mac
public int doFinal(byte[] bArr, int i) {
int blockSize = this.cipher.getBlockSize();
if (this.padding == null) {
while (true) {
int i2 = this.bufOff;
if (i2 >= blockSize) {
break;
}
this.buf[i2] = 0;
this.bufOff = i2 + 1;
}
} else {
if (this.bufOff == blockSize) {
this.cipher.processBlock(this.buf, 0, this.mac, 0);
this.bufOff = 0;
}
this.padding.addPadding(this.buf, this.bufOff);
}
this.cipher.processBlock(this.buf, 0, this.mac, 0);
DESEngine dESEngine = new DESEngine();
dESEngine.init(false, this.lastKey2);
byte[] bArr2 = this.mac;
dESEngine.processBlock(bArr2, 0, bArr2, 0);
dESEngine.init(true, this.lastKey3);
byte[] bArr3 = this.mac;
dESEngine.processBlock(bArr3, 0, bArr3, 0);
System.arraycopy(this.mac, 0, bArr, i, this.macSize);
reset();
return this.macSize;
}
public ISO9797Alg3Mac(BlockCipher blockCipher, BlockCipherPadding blockCipherPadding) {
this(blockCipher, blockCipher.getBlockSize() << 3, blockCipherPadding);
}
public ISO9797Alg3Mac(BlockCipher blockCipher, int i, BlockCipherPadding blockCipherPadding) {
if (i % 8 != 0) {
throw new IllegalArgumentException("MAC size must be multiple of 8");
}
if (!(blockCipher instanceof DESEngine)) {
throw new IllegalArgumentException("cipher must be instance of DESEngine");
}
this.cipher = new CBCBlockCipher(blockCipher);
this.padding = blockCipherPadding;
this.macSize = i / 8;
this.mac = new byte[blockCipher.getBlockSize()];
this.buf = new byte[blockCipher.getBlockSize()];
this.bufOff = 0;
}
public ISO9797Alg3Mac(BlockCipher blockCipher, int i) {
this(blockCipher, i, null);
}
public ISO9797Alg3Mac(BlockCipher blockCipher) {
this(blockCipher, blockCipher.getBlockSize() << 3, null);
}
}