package org.bouncycastle.jcajce.provider.asymmetric.compositesignatures;

import java.io.ByteArrayOutputStream;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.InvalidParameterException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PSSParameterSpec;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.internal.asn1.iana.IANAObjectIdentifiers;
import org.bouncycastle.jcajce.CompositePrivateKey;
import org.bouncycastle.jcajce.CompositePublicKey;
import org.bouncycastle.jcajce.interfaces.BCKey;
import org.bouncycastle.jcajce.spec.CompositeSignatureSpec;
import org.bouncycastle.jcajce.spec.ContextParameterSpec;
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.jcajce.util.SpecUtil;
import org.bouncycastle.pqc.crypto.xmss.XMSSKeyParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Exceptions;
import org.bouncycastle.util.encoders.Hex;

/* JADX INFO: loaded from: classes2.dex */
public class SignatureSpi extends java.security.SignatureSpi {
    private static final String ML_DSA_44 = "ML-DSA-44";
    private static final String ML_DSA_65 = "ML-DSA-65";
    private static final String ML_DSA_87 = "ML-DSA-87";
    private ASN1ObjectIdentifier algorithm;
    private String[] algs;
    private Digest baseDigest;
    private Signature[] componentSignatures;
    private Key compositeKey;
    private ContextParameterSpec contextSpec;
    private byte[] domain;
    private AlgorithmParameters engineParams;
    private JcaJceHelper helper;
    private final boolean isPrehash;
    private Digest preHashDigest;
    private final SecureRandom random;
    private boolean unprimed;
    private static final byte[] prefix = Hex.decode("436f6d706f73697465416c676f726974686d5369676e61747572657332303235");
    private static final Map<String, String> canonicalNames = new HashMap();
    private static final HashMap<ASN1ObjectIdentifier, byte[]> domainSeparators = new LinkedHashMap();
    private static final HashMap<ASN1ObjectIdentifier, AlgorithmParameterSpec> algorithmsParameterSpecs = new HashMap<>();

    /* JADX INFO: loaded from: classes3.dex */
    public static final class COMPOSITE extends SignatureSpi {
        public COMPOSITE() {
            super(null, null, false);
        }
    }

    private static final class ErasableOutputStream extends ByteArrayOutputStream {
        public byte[] getBuf() {
            return this.buf;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA44_ECDSA_P256_SHA256 extends SignatureSpi {
        public MLDSA44_ECDSA_P256_SHA256() {
            super(IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, new SHA256Digest());
        }
    }

    public static final class MLDSA44_ECDSA_P256_SHA256_PREHASH extends SignatureSpi {
        public MLDSA44_ECDSA_P256_SHA256_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, new SHA256Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA44_Ed25519_SHA512 extends SignatureSpi {
        public MLDSA44_Ed25519_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA44_Ed25519_SHA512_PREHASH extends SignatureSpi {
        public MLDSA44_Ed25519_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, new SHA512Digest(), true);
        }
    }

    public static final class MLDSA44_RSA2048_PKCS15_SHA256 extends SignatureSpi {
        public MLDSA44_RSA2048_PKCS15_SHA256() {
            super(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, new SHA256Digest());
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA44_RSA2048_PKCS15_SHA256_PREHASH extends SignatureSpi {
        public MLDSA44_RSA2048_PKCS15_SHA256_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, new SHA256Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA44_RSA2048_PSS_SHA256 extends SignatureSpi {
        public MLDSA44_RSA2048_PSS_SHA256() {
            super(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, new SHA256Digest());
        }
    }

    public static final class MLDSA44_RSA2048_PSS_SHA256_PREHASH extends SignatureSpi {
        public MLDSA44_RSA2048_PSS_SHA256_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, new SHA256Digest(), true);
        }
    }

    public static final class MLDSA65_ECDSA_P256_SHA512 extends SignatureSpi {
        public MLDSA65_ECDSA_P256_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA65_ECDSA_P256_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_ECDSA_P256_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_ECDSA_P384_SHA512 extends SignatureSpi {
        public MLDSA65_ECDSA_P384_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA65_ECDSA_P384_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_ECDSA_P384_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA65_ECDSA_brainpoolP256r1_SHA512 extends SignatureSpi {
        public MLDSA65_ECDSA_brainpoolP256r1_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA65_ECDSA_brainpoolP256r1_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_ECDSA_brainpoolP256r1_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_Ed25519_SHA512 extends SignatureSpi {
        public MLDSA65_Ed25519_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA65_Ed25519_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_Ed25519_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, new SHA512Digest(), true);
        }
    }

    public static final class MLDSA65_RSA3072_PKCS15_SHA512 extends SignatureSpi {
        public MLDSA65_RSA3072_PKCS15_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_RSA3072_PKCS15_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_RSA3072_PKCS15_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_RSA3072_PSS_SHA512 extends SignatureSpi {
        public MLDSA65_RSA3072_PSS_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA65_RSA3072_PSS_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_RSA3072_PSS_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_RSA4096_PKCS15_SHA512 extends SignatureSpi {
        public MLDSA65_RSA4096_PKCS15_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA65_RSA4096_PKCS15_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_RSA4096_PKCS15_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA65_RSA4096_PSS_SHA512 extends SignatureSpi {
        public MLDSA65_RSA4096_PSS_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA65_RSA4096_PSS_SHA512_PREHASH extends SignatureSpi {
        public MLDSA65_RSA4096_PSS_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, new SHA512Digest(), true);
        }
    }

    public static final class MLDSA87_ECDSA_P384_SHA512 extends SignatureSpi {
        public MLDSA87_ECDSA_P384_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA87_ECDSA_P384_SHA512_PREHASH extends SignatureSpi {
        public MLDSA87_ECDSA_P384_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_ECDSA_P521_SHA512 extends SignatureSpi {
        public MLDSA87_ECDSA_P521_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA87_ECDSA_P521_SHA512_PREHASH extends SignatureSpi {
        public MLDSA87_ECDSA_P521_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static final class MLDSA87_ECDSA_brainpoolP384r1_SHA512 extends SignatureSpi {
        public MLDSA87_ECDSA_brainpoolP384r1_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_ECDSA_brainpoolP384r1_SHA512_PREHASH extends SignatureSpi {
        public MLDSA87_ECDSA_brainpoolP384r1_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, new SHA512Digest(), true);
        }
    }

    public static final class MLDSA87_Ed448_SHAKE256 extends SignatureSpi {
        public MLDSA87_Ed448_SHAKE256() {
            super(IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, new SHAKEDigest(256));
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_Ed448_SHAKE256_PREHASH extends SignatureSpi {
        public MLDSA87_Ed448_SHAKE256_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, new SHAKEDigest(256), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_RSA3072_PSS_SHA512 extends SignatureSpi {
        public MLDSA87_RSA3072_PSS_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, new SHA512Digest());
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_RSA3072_PSS_SHA512_PREHASH extends SignatureSpi {
        public MLDSA87_RSA3072_PSS_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static final class MLDSA87_RSA4096_PSS_SHA512 extends SignatureSpi {
        public MLDSA87_RSA4096_PSS_SHA512() {
            super(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, new SHA512Digest());
        }
    }

    public static final class MLDSA87_RSA4096_PSS_SHA512_PREHASH extends SignatureSpi {
        public MLDSA87_RSA4096_PSS_SHA512_PREHASH() {
            super(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, new SHA512Digest(), true);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    private static class NullDigest implements Digest {
        private final OpenByteArrayOutputStream bOut = new OpenByteArrayOutputStream();
        private final int expectedSize;

        private static class OpenByteArrayOutputStream extends ByteArrayOutputStream {
            private OpenByteArrayOutputStream() {
            }

            void copy(byte[] bArr, int i) {
                System.arraycopy(this.buf, 0, bArr, i, size());
            }

            @Override // java.io.ByteArrayOutputStream
            public void reset() {
                super.reset();
                Arrays.clear(this.buf);
            }
        }

        NullDigest(int i) {
            this.expectedSize = i;
        }

        @Override // org.bouncycastle.crypto.Digest
        public int doFinal(byte[] bArr, int i) {
            int size = this.bOut.size();
            if (size != this.expectedSize) {
                throw new IllegalStateException("provided pre-hash digest is the wrong length");
            }
            this.bOut.copy(bArr, i);
            reset();
            return size;
        }

        @Override // org.bouncycastle.crypto.Digest
        public String getAlgorithmName() {
            return "NULL";
        }

        @Override // org.bouncycastle.crypto.Digest
        public int getDigestSize() {
            return this.bOut.size();
        }

        @Override // org.bouncycastle.crypto.Digest
        public void reset() {
            this.bOut.reset();
        }

        @Override // org.bouncycastle.crypto.Digest
        public void update(byte b) {
            this.bOut.write(b);
        }

        @Override // org.bouncycastle.crypto.Digest
        public void update(byte[] bArr, int i, int i2) {
            this.bOut.write(bArr, i, i2);
        }
    }

    static {
        canonicalNames.put("MLDSA44", ML_DSA_44);
        canonicalNames.put("MLDSA65", ML_DSA_65);
        canonicalNames.put("MLDSA87", ML_DSA_87);
        canonicalNames.put(NISTObjectIdentifiers.id_ml_dsa_44.getId(), ML_DSA_44);
        canonicalNames.put(NISTObjectIdentifiers.id_ml_dsa_65.getId(), ML_DSA_65);
        canonicalNames.put(NISTObjectIdentifiers.id_ml_dsa_87.getId(), ML_DSA_87);
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, Hex.decode("434f4d505349472d4d4c44534134342d525341323034382d5053532d534841323536"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, Hex.decode("434f4d505349472d4d4c44534134342d525341323034382d504b435331352d534841323536"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, Hex.decode("434f4d505349472d4d4c44534134342d456432353531392d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, Hex.decode("434f4d505349472d4d4c44534134342d45434453412d503235362d534841323536"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d525341333037322d5053532d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d525341333037322d504b435331352d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d525341343039362d5053532d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d525341343039362d504b435331352d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d45434453412d503235362d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d45434453412d503338342d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d45434453412d42503235362d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, Hex.decode("434f4d505349472d4d4c44534136352d456432353531392d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, Hex.decode("434f4d505349472d4d4c44534138372d45434453412d42503338342d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, Hex.decode("434f4d505349472d4d4c44534138372d45643434382d5348414b45323536"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, Hex.decode("434f4d505349472d4d4c44534138372d525341333037322d5053532d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, Hex.decode("434f4d505349472d4d4c44534138372d525341343039362d5053532d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, Hex.decode("434f4d505349472d4d4c44534138372d45434453412d503338342d534841353132"));
        domainSeparators.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, Hex.decode("434f4d505349472d4d4c44534138372d45434453412d503532312d534841353132"));
        algorithmsParameterSpecs.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, new PSSParameterSpec(XMSSKeyParameters.SHA_256, "MGF1", new MGF1ParameterSpec(XMSSKeyParameters.SHA_256), 32, 1));
        algorithmsParameterSpecs.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, new PSSParameterSpec(XMSSKeyParameters.SHA_256, "MGF1", new MGF1ParameterSpec(XMSSKeyParameters.SHA_256), 32, 1));
        algorithmsParameterSpecs.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, new PSSParameterSpec("SHA-384", "MGF1", new MGF1ParameterSpec("SHA-384"), 48, 1));
        algorithmsParameterSpecs.put(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, new PSSParameterSpec("SHA-384", "MGF1", new MGF1ParameterSpec("SHA-384"), 48, 1));
        algorithmsParameterSpecs.put(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, new PSSParameterSpec(XMSSKeyParameters.SHA_256, "MGF1", new MGF1ParameterSpec(XMSSKeyParameters.SHA_256), 32, 1));
    }

    SignatureSpi(ASN1ObjectIdentifier aSN1ObjectIdentifier, Digest digest) {
        this(aSN1ObjectIdentifier, digest, false);
    }

    SignatureSpi(ASN1ObjectIdentifier aSN1ObjectIdentifier, Digest digest, boolean z) {
        this.random = CryptoServicesRegistrar.getSecureRandom();
        this.helper = new BCJcaJceHelper();
        this.engineParams = null;
        this.unprimed = true;
        this.algorithm = aSN1ObjectIdentifier;
        this.isPrehash = z;
        if (aSN1ObjectIdentifier != null) {
            this.baseDigest = digest;
            this.preHashDigest = z ? new NullDigest(digest.getDigestSize()) : digest;
            this.domain = domainSeparators.get(aSN1ObjectIdentifier);
            this.algs = CompositeIndex.getPairing(aSN1ObjectIdentifier);
            this.componentSignatures = new Signature[this.algs.length];
        }
    }

    private void baseSigInit() throws SignatureException {
        try {
            this.componentSignatures[0].setParameter(new ContextParameterSpec(this.domain));
            AlgorithmParameterSpec algorithmParameterSpec = algorithmsParameterSpecs.get(this.algorithm);
            if (algorithmParameterSpec != null) {
                this.componentSignatures[1].setParameter(algorithmParameterSpec);
            }
            this.unprimed = false;
        } catch (InvalidAlgorithmParameterException e) {
            throw new IllegalStateException("unable to set context on ML-DSA");
        }
    }

    private void createComponentSignatures(List list, List<Provider> list2) {
        int i = 0;
        try {
            if (list2 == null) {
                while (i != this.componentSignatures.length) {
                    this.componentSignatures[i] = getDefaultSignature(this.algs[i], list.get(i));
                    i++;
                }
                return;
            }
            while (i != this.componentSignatures.length) {
                Provider provider = list2.get(i);
                Signature[] signatureArr = this.componentSignatures;
                if (provider == null) {
                    signatureArr[i] = getDefaultSignature(this.algs[i], list.get(i));
                } else {
                    signatureArr[i] = Signature.getInstance(this.algs[i], list2.get(i));
                }
                i++;
            }
        } catch (GeneralSecurityException e) {
            throw Exceptions.illegalStateException(e.getMessage(), e);
        }
    }

    private String getCanonicalName(String str) {
        String str2 = canonicalNames.get(str);
        return str2 != null ? str2 : str;
    }

    private Signature getDefaultSignature(String str, Object obj) throws NoSuchAlgorithmException, NoSuchProviderException {
        return obj instanceof BCKey ? this.helper.createSignature(str) : Signature.getInstance(str);
    }

    private void processPreHashedMessage(byte[] bArr) throws SignatureException {
        int digestSize = this.baseDigest.getDigestSize();
        byte[] bArr2 = new byte[digestSize];
        try {
            this.preHashDigest.doFinal(bArr2, 0);
            for (int i = 0; i < this.componentSignatures.length; i++) {
                Signature signature = this.componentSignatures[i];
                signature.update(prefix);
                signature.update(this.domain);
                if (this.contextSpec == null) {
                    signature.update((byte) 0);
                } else {
                    byte[] context = this.contextSpec.getContext();
                    signature.update((byte) context.length);
                    signature.update(context);
                }
                if (bArr != null) {
                    signature.update(bArr, 0, bArr.length);
                }
                signature.update(bArr2, 0, digestSize);
            }
        } catch (IllegalStateException e) {
            throw new SignatureException(e.getMessage());
        }
    }

    private void sigInitSign() throws InvalidKeyException {
        CompositePrivateKey compositePrivateKey = (CompositePrivateKey) this.compositeKey;
        for (int i = 0; i < this.componentSignatures.length; i++) {
            this.componentSignatures[i].initSign(compositePrivateKey.getPrivateKeys().get(i));
        }
        this.unprimed = true;
    }

    private void sigInitVerify() throws InvalidKeyException {
        CompositePublicKey compositePublicKey = (CompositePublicKey) this.compositeKey;
        for (int i = 0; i < this.componentSignatures.length; i++) {
            this.componentSignatures[i].initVerify(compositePublicKey.getPublicKeys().get(i));
        }
        this.unprimed = true;
    }

    public static byte[][] splitCompositeSignature(byte[] bArr, int i) {
        byte[] bArr2 = new byte[i];
        int length = bArr.length - i;
        byte[] bArr3 = new byte[length];
        System.arraycopy(bArr, 0, bArr2, 0, i);
        System.arraycopy(bArr, i, bArr3, 0, length);
        return new byte[][]{bArr2, bArr3};
    }

    @Override // java.security.SignatureSpi
    protected Object engineGetParameter(String str) throws InvalidParameterException {
        throw new UnsupportedOperationException("engineGetParameter unsupported");
    }

    @Override // java.security.SignatureSpi
    protected final AlgorithmParameters engineGetParameters() {
        if (this.engineParams == null && this.contextSpec != null) {
            try {
                this.engineParams = this.helper.createAlgorithmParameters("CONTEXT");
                this.engineParams.init(this.contextSpec);
            } catch (Exception e) {
                throw Exceptions.illegalStateException(e.toString(), e);
            }
        }
        return this.engineParams;
    }

    @Override // java.security.SignatureSpi
    protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
        if (!(privateKey instanceof CompositePrivateKey)) {
            throw new InvalidKeyException("Private key is not composite.");
        }
        this.compositeKey = privateKey;
        CompositePrivateKey compositePrivateKey = (CompositePrivateKey) privateKey;
        if (this.algorithm == null) {
            ASN1ObjectIdentifier algorithm = compositePrivateKey.getAlgorithmIdentifier().getAlgorithm();
            this.algorithm = algorithm;
            this.baseDigest = CompositeIndex.getDigest(algorithm);
            this.preHashDigest = this.isPrehash ? new NullDigest(this.baseDigest.getDigestSize()) : this.baseDigest;
            this.domain = domainSeparators.get(algorithm);
            this.algs = CompositeIndex.getPairing(algorithm);
            this.componentSignatures = new Signature[this.algs.length];
        } else if (!compositePrivateKey.getAlgorithmIdentifier().getAlgorithm().equals((ASN1Primitive) this.algorithm)) {
            throw new InvalidKeyException("provided composite public key cannot be used with the composite signature algorithm");
        }
        createComponentSignatures(compositePrivateKey.getPrivateKeys(), compositePrivateKey.getProviders());
        sigInitSign();
    }

    @Override // java.security.SignatureSpi
    protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
        if (!(publicKey instanceof CompositePublicKey)) {
            throw new InvalidKeyException("public key is not composite");
        }
        this.compositeKey = publicKey;
        CompositePublicKey compositePublicKey = (CompositePublicKey) this.compositeKey;
        if (this.algorithm == null) {
            ASN1ObjectIdentifier algorithm = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getAlgorithm().getAlgorithm();
            this.algorithm = algorithm;
            this.baseDigest = CompositeIndex.getDigest(algorithm);
            this.preHashDigest = this.isPrehash ? new NullDigest(this.baseDigest.getDigestSize()) : this.baseDigest;
            this.domain = domainSeparators.get(algorithm);
            this.algs = CompositeIndex.getPairing(algorithm);
            this.componentSignatures = new Signature[this.algs.length];
        } else if (!compositePublicKey.getAlgorithmIdentifier().getAlgorithm().equals((ASN1Primitive) this.algorithm)) {
            throw new InvalidKeyException("provided composite public key cannot be used with the composite signature algorithm");
        }
        createComponentSignatures(compositePublicKey.getPublicKeys(), compositePublicKey.getProviders());
        sigInitVerify();
    }

    @Override // java.security.SignatureSpi
    protected void engineSetParameter(String str, Object obj) throws InvalidParameterException {
        throw new UnsupportedOperationException("engineSetParameter unsupported");
    }

    @Override // java.security.SignatureSpi
    protected void engineSetParameter(AlgorithmParameterSpec algorithmParameterSpec) throws InvalidAlgorithmParameterException {
        if (!this.unprimed) {
            throw new InvalidAlgorithmParameterException("attempt to set parameter after update");
        }
        if (algorithmParameterSpec instanceof ContextParameterSpec) {
            this.contextSpec = (ContextParameterSpec) algorithmParameterSpec;
            try {
                if (this.compositeKey instanceof PublicKey) {
                    sigInitVerify();
                } else {
                    sigInitSign();
                }
                return;
            } catch (InvalidKeyException e) {
                throw new InvalidAlgorithmParameterException("keys invalid on reset: " + e.getMessage(), e);
            }
        }
        if (!(algorithmParameterSpec instanceof CompositeSignatureSpec)) {
            byte[] contextFrom = SpecUtil.getContextFrom(algorithmParameterSpec);
            if (contextFrom != null) {
                this.contextSpec = new ContextParameterSpec(contextFrom);
                try {
                    if (this.compositeKey instanceof PublicKey) {
                        sigInitVerify();
                    } else {
                        sigInitSign();
                    }
                } catch (InvalidKeyException e2) {
                    throw new InvalidAlgorithmParameterException("keys invalid on reset: " + e2.getMessage(), e2);
                }
            }
            throw new InvalidAlgorithmParameterException("unknown parameterSpec passed to composite signature");
        }
        CompositeSignatureSpec compositeSignatureSpec = (CompositeSignatureSpec) algorithmParameterSpec;
        this.preHashDigest = compositeSignatureSpec.isPrehashMode() ? new NullDigest(this.baseDigest.getDigestSize()) : this.baseDigest;
        AlgorithmParameterSpec secondarySpec = compositeSignatureSpec.getSecondarySpec();
        if (secondarySpec == null || (secondarySpec instanceof ContextParameterSpec)) {
            this.contextSpec = (ContextParameterSpec) compositeSignatureSpec.getSecondarySpec();
            return;
        }
        byte[] contextFrom2 = SpecUtil.getContextFrom(secondarySpec);
        if (contextFrom2 == null) {
            throw new InvalidAlgorithmParameterException("unknown parameterSpec passed to composite signature");
        }
        this.contextSpec = new ContextParameterSpec(contextFrom2);
    }

    @Override // java.security.SignatureSpi
    protected byte[] engineSign() throws SignatureException {
        this.random.nextBytes(new byte[32]);
        if (this.preHashDigest != null) {
            processPreHashedMessage(null);
        }
        byte[] bArrSign = this.componentSignatures[0].sign();
        byte[] bArrSign2 = this.componentSignatures[1].sign();
        byte[] bArr = new byte[bArrSign.length + bArrSign2.length];
        System.arraycopy(bArrSign, 0, bArr, 0, bArrSign.length);
        System.arraycopy(bArrSign2, 0, bArr, bArrSign.length, bArrSign2.length);
        return bArr;
    }

    @Override // java.security.SignatureSpi
    protected void engineUpdate(byte b) throws SignatureException {
        if (this.unprimed) {
            baseSigInit();
        }
        if (this.preHashDigest != null) {
            this.preHashDigest.update(b);
            return;
        }
        for (int i = 0; i < this.componentSignatures.length; i++) {
            this.componentSignatures[i].update(b);
        }
    }

    @Override // java.security.SignatureSpi
    protected void engineUpdate(byte[] bArr, int i, int i2) throws SignatureException {
        if (this.unprimed) {
            baseSigInit();
        }
        if (this.preHashDigest != null) {
            this.preHashDigest.update(bArr, i, i2);
            return;
        }
        for (int i3 = 0; i3 < this.componentSignatures.length; i3++) {
            this.componentSignatures[i3].update(bArr, i, i2);
        }
    }

    @Override // java.security.SignatureSpi
    protected boolean engineVerify(byte[] bArr) throws SignatureException {
        int i;
        if (this.algs[0].indexOf("44") > 0) {
            i = 2420;
        } else if (this.algs[0].indexOf("65") > 0) {
            i = 3309;
        } else {
            i = this.algs[0].indexOf("87") > 0 ? 4627 : 0;
        }
        byte[][] bArrSplitCompositeSignature = splitCompositeSignature(bArr, i);
        if (this.preHashDigest != null) {
            processPreHashedMessage(null);
        }
        boolean z = false;
        for (int i2 = 0; i2 < this.componentSignatures.length; i2++) {
            if (!this.componentSignatures[i2].verify(bArrSplitCompositeSignature[i2])) {
                z = true;
            }
        }
        return !z;
    }
}
