package org.bouncycastle.openpgp.operator.bc;

import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import org.bouncycastle.crypto.params.HKDFParameters;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Hex;

/* JADX INFO: loaded from: classes2.dex */
class RFC6637KDFCalculator {
    private static final byte[] ANONYMOUS_SENDER = Hex.decode("416E6F6E796D6F75732053656E64657220202020");
    private final PGPDigestCalculator digCalc;
    private final int keyAlgorithm;

    public RFC6637KDFCalculator(PGPDigestCalculator pGPDigestCalculator, int i) {
        this.digCalc = pGPDigestCalculator;
        this.keyAlgorithm = i;
    }

    private static byte[] KDF(PGPDigestCalculator pGPDigestCalculator, byte[] bArr, int i, byte[] bArr2) throws IOException {
        OutputStream outputStream = pGPDigestCalculator.getOutputStream();
        outputStream.write(0);
        outputStream.write(0);
        outputStream.write(0);
        outputStream.write(1);
        outputStream.write(bArr);
        outputStream.write(bArr2);
        byte[] bArr3 = new byte[i];
        System.arraycopy(pGPDigestCalculator.getDigest(), 0, bArr3, 0, i);
        return bArr3;
    }

    public static byte[] createKey(int i, int i2, byte[] bArr, String str) throws PGPException {
        HKDFBytesGenerator hKDFBytesGenerator = new HKDFBytesGenerator(BcImplProvider.createDigest(i));
        hKDFBytesGenerator.init(new HKDFParameters(bArr, null, Strings.toByteArray(str)));
        int keyLen = getKeyLen(i2);
        byte[] bArr2 = new byte[keyLen];
        hKDFBytesGenerator.generateBytes(bArr2, 0, keyLen);
        return bArr2;
    }

    private static int getKeyLen(int i) throws PGPException {
        switch (i) {
            case 7:
            case 11:
                return 16;
            case 8:
            case 12:
                return 24;
            case 9:
            case 13:
                return 32;
            case 10:
            default:
                throw new PGPException("unknown symmetric algorithm ID: " + i);
        }
    }

    public byte[] createKey(ECPoint eCPoint, byte[] bArr) throws PGPException {
        return createKey(eCPoint.getAffineXCoord().getEncoded(), bArr);
    }

    public byte[] createKey(byte[] bArr, byte[] bArr2) throws PGPException {
        try {
            return KDF(this.digCalc, bArr, getKeyLen(this.keyAlgorithm), bArr2);
        } catch (IOException e) {
            throw new PGPException("Exception performing KDF: " + e.getMessage(), e);
        }
    }
}
