package org.bouncycastle.crypto.kems; import java.math.BigInteger; import java.security.SecureRandom; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.DerivationFunction; import org.bouncycastle.crypto.KeyEncapsulation; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECKeyParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.KDFParameters; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.math.ec.ECCurve; import org.bouncycastle.math.ec.ECMultiplier; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.FixedPointCombMultiplier; import org.bouncycastle.util.Arrays; import org.bouncycastle.util.BigIntegers; /* loaded from: classes6.dex */ public class ECIESKeyEncapsulation implements KeyEncapsulation { private static final BigInteger ONE = BigInteger.valueOf(1); private boolean CofactorMode; private boolean OldCofactorMode; private boolean SingleHashMode; private DerivationFunction kdf; private ECKeyParameters key; private SecureRandom rnd; @Override // org.bouncycastle.crypto.KeyEncapsulation public void init(CipherParameters cipherParameters) throws IllegalArgumentException { if (!(cipherParameters instanceof ECKeyParameters)) { throw new IllegalArgumentException("EC key required"); } this.key = (ECKeyParameters) cipherParameters; } @Override // org.bouncycastle.crypto.KeyEncapsulation public CipherParameters encrypt(byte[] bArr, int i, int i2) throws IllegalArgumentException { ECKeyParameters eCKeyParameters = this.key; if (!(eCKeyParameters instanceof ECPublicKeyParameters)) { throw new IllegalArgumentException("Public key required for encryption"); } ECPublicKeyParameters eCPublicKeyParameters = (ECPublicKeyParameters) eCKeyParameters; ECDomainParameters parameters = eCPublicKeyParameters.getParameters(); ECCurve curve = parameters.getCurve(); BigInteger n = parameters.getN(); BigInteger h = parameters.getH(); BigInteger createRandomInRange = BigIntegers.createRandomInRange(ONE, n, this.rnd); ECPoint[] eCPointArr = {createBasePointMultiplier().multiply(parameters.getG(), createRandomInRange), eCPublicKeyParameters.getQ().multiply(this.CofactorMode ? createRandomInRange.multiply(h).mod(n) : createRandomInRange)}; curve.normalizeAll(eCPointArr); ECPoint eCPoint = eCPointArr[0]; ECPoint eCPoint2 = eCPointArr[1]; byte[] encoded = eCPoint.getEncoded(false); System.arraycopy(encoded, 0, bArr, i, encoded.length); return deriveKey(i2, encoded, eCPoint2.getAffineXCoord().getEncoded()); } public CipherParameters encrypt(byte[] bArr, int i) { return encrypt(bArr, 0, i); } protected KeyParameter deriveKey(int i, byte[] bArr, byte[] bArr2) { if (this.SingleHashMode) { byte[] concatenate = Arrays.concatenate(bArr, bArr2); Arrays.fill(bArr2, (byte) 0); bArr2 = concatenate; } try { this.kdf.init(new KDFParameters(bArr2, null)); byte[] bArr3 = new byte[i]; this.kdf.generateBytes(bArr3, 0, i); return new KeyParameter(bArr3); } finally { Arrays.fill(bArr2, (byte) 0); } } @Override // org.bouncycastle.crypto.KeyEncapsulation public CipherParameters decrypt(byte[] bArr, int i, int i2, int i3) throws IllegalArgumentException { ECKeyParameters eCKeyParameters = this.key; if (!(eCKeyParameters instanceof ECPrivateKeyParameters)) { throw new IllegalArgumentException("Private key required for encryption"); } ECPrivateKeyParameters eCPrivateKeyParameters = (ECPrivateKeyParameters) eCKeyParameters; ECDomainParameters parameters = eCPrivateKeyParameters.getParameters(); ECCurve curve = parameters.getCurve(); BigInteger n = parameters.getN(); BigInteger h = parameters.getH(); byte[] bArr2 = new byte[i2]; System.arraycopy(bArr, i, bArr2, 0, i2); ECPoint decodePoint = curve.decodePoint(bArr2); if (this.CofactorMode || this.OldCofactorMode) { decodePoint = decodePoint.multiply(h); } BigInteger d = eCPrivateKeyParameters.getD(); if (this.CofactorMode) { d = d.multiply(h.modInverse(n)).mod(n); } return deriveKey(i3, bArr2, decodePoint.multiply(d).normalize().getAffineXCoord().getEncoded()); } public CipherParameters decrypt(byte[] bArr, int i) { return decrypt(bArr, 0, bArr.length, i); } protected ECMultiplier createBasePointMultiplier() { return new FixedPointCombMultiplier(); } public ECIESKeyEncapsulation(DerivationFunction derivationFunction, SecureRandom secureRandom, boolean z, boolean z2, boolean z3) { this.kdf = derivationFunction; this.rnd = secureRandom; this.CofactorMode = z; this.OldCofactorMode = z2; this.SingleHashMode = z3; } public ECIESKeyEncapsulation(DerivationFunction derivationFunction, SecureRandom secureRandom) { this.kdf = derivationFunction; this.rnd = secureRandom; this.CofactorMode = false; this.OldCofactorMode = false; this.SingleHashMode = false; } }