package org.bouncycastle.crypto.macs; import java.util.Hashtable; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.ExtendedDigest; import org.bouncycastle.crypto.Mac; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec; import org.bouncycastle.util.Integers; import org.bouncycastle.util.Memoable; /* loaded from: classes6.dex */ public class HMac implements Mac { private static final byte IPAD = 54; private static final byte OPAD = 92; private static Hashtable blockLengths; private int blockLength; private Digest digest; private int digestSize; private byte[] inputPad; private Memoable ipadState; private Memoable opadState; private byte[] outputBuf; @Override // org.bouncycastle.crypto.Mac public void update(byte[] bArr, int i, int i2) { this.digest.update(bArr, i, i2); } @Override // org.bouncycastle.crypto.Mac public void update(byte b) { this.digest.update(b); } @Override // org.bouncycastle.crypto.Mac public void reset() { this.digest.reset(); Digest digest = this.digest; byte[] bArr = this.inputPad; digest.update(bArr, 0, bArr.length); } @Override // org.bouncycastle.crypto.Mac public void init(CipherParameters cipherParameters) { byte[] bArr; this.digest.reset(); byte[] key = ((KeyParameter) cipherParameters).getKey(); int length = key.length; if (length > this.blockLength) { this.digest.update(key, 0, length); this.digest.doFinal(this.inputPad, 0); length = this.digestSize; } else { System.arraycopy(key, 0, this.inputPad, 0, length); } while (true) { bArr = this.inputPad; if (length >= bArr.length) { break; } bArr[length] = 0; length++; } System.arraycopy(bArr, 0, this.outputBuf, 0, this.blockLength); xorPad(this.inputPad, this.blockLength, IPAD); xorPad(this.outputBuf, this.blockLength, OPAD); Digest digest = this.digest; if (digest instanceof Memoable) { Memoable copy = ((Memoable) digest).copy(); this.opadState = copy; ((Digest) copy).update(this.outputBuf, 0, this.blockLength); } Digest digest2 = this.digest; byte[] bArr2 = this.inputPad; digest2.update(bArr2, 0, bArr2.length); Digest digest3 = this.digest; if (digest3 instanceof Memoable) { this.ipadState = ((Memoable) digest3).copy(); } } public Digest getUnderlyingDigest() { return this.digest; } @Override // org.bouncycastle.crypto.Mac public int getMacSize() { return this.digestSize; } @Override // org.bouncycastle.crypto.Mac public String getAlgorithmName() { StringBuilder sb = new StringBuilder(); sb.append(this.digest.getAlgorithmName()); sb.append("/HMAC"); return sb.toString(); } @Override // org.bouncycastle.crypto.Mac public int doFinal(byte[] bArr, int i) { this.digest.doFinal(this.outputBuf, this.blockLength); Memoable memoable = this.opadState; if (memoable != null) { ((Memoable) this.digest).reset(memoable); Digest digest = this.digest; digest.update(this.outputBuf, this.blockLength, digest.getDigestSize()); } else { Digest digest2 = this.digest; byte[] bArr2 = this.outputBuf; digest2.update(bArr2, 0, bArr2.length); } int doFinal = this.digest.doFinal(bArr, i); int i2 = this.blockLength; while (true) { byte[] bArr3 = this.outputBuf; if (i2 >= bArr3.length) { break; } bArr3[i2] = 0; i2++; } Memoable memoable2 = this.ipadState; if (memoable2 != null) { ((Memoable) this.digest).reset(memoable2); } else { Digest digest3 = this.digest; byte[] bArr4 = this.inputPad; digest3.update(bArr4, 0, bArr4.length); } return doFinal; } private static void xorPad(byte[] bArr, int i, byte b) { for (int i2 = 0; i2 < i; i2++) { bArr[i2] = (byte) (bArr[i2] ^ b); } } private static int getByteLength(Digest digest) { if (digest instanceof ExtendedDigest) { return ((ExtendedDigest) digest).getByteLength(); } Integer num = (Integer) blockLengths.get(digest.getAlgorithmName()); if (num != null) { return num.intValue(); } StringBuilder sb = new StringBuilder("unknown digest passed: "); sb.append(digest.getAlgorithmName()); throw new IllegalArgumentException(sb.toString()); } private HMac(Digest digest, int i) { this.digest = digest; int digestSize = digest.getDigestSize(); this.digestSize = digestSize; this.blockLength = i; this.inputPad = new byte[i]; this.outputBuf = new byte[i + digestSize]; } public HMac(Digest digest) { this(digest, getByteLength(digest)); } static { Hashtable hashtable = new Hashtable(); blockLengths = hashtable; hashtable.put("GOST3411", Integers.valueOf(32)); blockLengths.put("MD2", Integers.valueOf(16)); blockLengths.put("MD4", Integers.valueOf(64)); blockLengths.put("MD5", Integers.valueOf(64)); blockLengths.put("RIPEMD128", Integers.valueOf(64)); blockLengths.put("RIPEMD160", Integers.valueOf(64)); blockLengths.put(McElieceCCA2KeyGenParameterSpec.SHA1, Integers.valueOf(64)); blockLengths.put(McElieceCCA2KeyGenParameterSpec.SHA224, Integers.valueOf(64)); blockLengths.put(McElieceCCA2KeyGenParameterSpec.SHA256, Integers.valueOf(64)); blockLengths.put(McElieceCCA2KeyGenParameterSpec.SHA384, Integers.valueOf(128)); blockLengths.put(McElieceCCA2KeyGenParameterSpec.SHA512, Integers.valueOf(128)); blockLengths.put("Tiger", Integers.valueOf(64)); blockLengths.put("Whirlpool", Integers.valueOf(64)); } }