// 
// Decompiled by Procyon v0.6.0
// 

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

import java.security.NoSuchAlgorithmException;
import org.bouncycastle.crypto.CipherParameters;
import java.security.SignatureException;
import java.security.SecureRandom;
import java.security.PrivateKey;
import java.security.InvalidKeyException;
import java.security.PublicKey;
import org.bouncycastle.jcajce.spec.MLDSAParameterSpec;
import org.bouncycastle.pqc.crypto.mldsa.MLDSAParameters;
import org.bouncycastle.pqc.crypto.mldsa.HashMLDSASigner;
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseDeterministicOrRandomSignature;

public class HashSignatureSpi extends BaseDeterministicOrRandomSignature
{
    private HashMLDSASigner signer;
    private MLDSAParameters parameters;
    
    protected HashSignatureSpi(final HashMLDSASigner signer) {
        super("HashMLDSA");
        this.signer = signer;
        this.parameters = null;
    }
    
    protected HashSignatureSpi(final HashMLDSASigner signer, final MLDSAParameters parameters) {
        super(MLDSAParameterSpec.fromName(parameters.getName()).getName());
        this.signer = signer;
        this.parameters = parameters;
    }
    
    @Override
    protected void verifyInit(final PublicKey publicKey) throws InvalidKeyException {
        if (publicKey instanceof BCMLDSAPublicKey) {
            final BCMLDSAPublicKey bcmldsaPublicKey = (BCMLDSAPublicKey)publicKey;
            this.keyParams = bcmldsaPublicKey.getKeyParams();
            if (this.parameters != null) {
                final String name = MLDSAParameterSpec.fromName(this.parameters.getName()).getName();
                if (!name.equals(bcmldsaPublicKey.getAlgorithm())) {
                    throw new InvalidKeyException("signature configured for " + name);
                }
            }
            return;
        }
        throw new InvalidKeyException("unknown public key passed to ML-DSA");
    }
    
    @Override
    protected void signInit(final PrivateKey privateKey, final SecureRandom appRandom) throws InvalidKeyException {
        this.appRandom = appRandom;
        if (privateKey instanceof BCMLDSAPrivateKey) {
            final BCMLDSAPrivateKey bcmldsaPrivateKey = (BCMLDSAPrivateKey)privateKey;
            this.keyParams = bcmldsaPrivateKey.getKeyParams();
            if (this.parameters != null) {
                final String name = MLDSAParameterSpec.fromName(this.parameters.getName()).getName();
                if (!name.equals(bcmldsaPrivateKey.getAlgorithm())) {
                    throw new InvalidKeyException("signature configured for " + name);
                }
            }
            return;
        }
        throw new InvalidKeyException("unknown private key passed to ML-DSA");
    }
    
    @Override
    protected void updateEngine(final byte b) throws SignatureException {
        this.signer.update(b);
    }
    
    @Override
    protected void updateEngine(final byte[] array, final int n, final int n2) throws SignatureException {
        this.signer.update(array, n, n2);
    }
    
    @Override
    protected byte[] engineSign() throws SignatureException {
        try {
            return this.signer.generateSignature();
        }
        catch (final Exception ex) {
            throw new SignatureException(ex.toString());
        }
    }
    
    @Override
    protected boolean engineVerify(final byte[] array) throws SignatureException {
        return this.signer.verifySignature(array);
    }
    
    @Override
    protected void reInitialize(final boolean b, final CipherParameters cipherParameters) {
        this.signer.init(b, cipherParameters);
    }
    
    public static class MLDSA extends HashSignatureSpi
    {
        public MLDSA() {
            super(new HashMLDSASigner());
        }
    }
    
    public static class MLDSA44 extends HashSignatureSpi
    {
        public MLDSA44() {
            super(new HashMLDSASigner(), MLDSAParameters.ml_dsa_44_with_sha512);
        }
    }
    
    public static class MLDSA65 extends HashSignatureSpi
    {
        public MLDSA65() {
            super(new HashMLDSASigner(), MLDSAParameters.ml_dsa_65_with_sha512);
        }
    }
    
    public static class MLDSA87 extends HashSignatureSpi
    {
        public MLDSA87() throws NoSuchAlgorithmException {
            super(new HashMLDSASigner(), MLDSAParameters.ml_dsa_87_with_sha512);
        }
    }
}
