GordianCoreSignatureSpec.java

/*
 * GordianKnot: Security Suite
 * Copyright 2026. Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.github.tonywasher.joceanus.gordianknot.impl.core.spec.sign;

import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
import io.github.tonywasher.joceanus.gordianknot.api.keypair.spec.GordianKeyPairType;
import io.github.tonywasher.joceanus.gordianknot.api.sign.spec.GordianSignatureSpec;
import io.github.tonywasher.joceanus.gordianknot.api.sign.spec.GordianSignatureType;
import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.base.GordianSpecConstants;
import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.digest.GordianCoreDigestSpec;
import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.keypair.GordianCoreKeyPairType;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * Signature Specification.
 */
public class GordianCoreSignatureSpec
        implements GordianSignatureSpec {
    /**
     * KeyPairType.
     */
    private final GordianCoreKeyPairType theKeyPairType;

    /**
     * SignatureType.
     */
    private final GordianCoreSignatureType theSignatureType;

    /**
     * SignatureSpec.
     */
    private final Object theSignatureSpec;

    /**
     * The Validity.
     */
    private final boolean isValid;

    /**
     * The String name.
     */
    private String theName;

    /**
     * Constructor.
     *
     * @param pKeyPairType   the keyPairType
     * @param pSignatureType the signatureType
     * @param pSignatureSpec the signatureSpec
     */
    GordianCoreSignatureSpec(final GordianKeyPairType pKeyPairType,
                             final GordianSignatureType pSignatureType,
                             final Object pSignatureSpec) {
        /* Store parameters */
        theKeyPairType = GordianCoreKeyPairType.mapCoreType(pKeyPairType);
        theSignatureType = GordianCoreSignatureType.mapCoreType(pSignatureType);
        theSignatureSpec = pSignatureSpec;
        isValid = checkValidity();
    }

    @Override
    public GordianKeyPairType getKeyPairType() {
        return theKeyPairType.getType();
    }

    /**
     * Obtain core keyPairType.
     *
     * @return the core type
     */
    public GordianCoreKeyPairType getCoreKeyPairType() {
        return theKeyPairType;
    }

    @Override
    public GordianSignatureType getSignatureType() {
        return theSignatureType.getType();
    }

    /**
     * Obtain the core signatureType.
     *
     * @return the type
     */
    public GordianCoreSignatureType getCoreType() {
        return theSignatureType;
    }

    @Override
    public Object getSignatureSpec() {
        return theSignatureSpec;
    }

    /**
     * Obtain the DigestSpec.
     *
     * @return the digestSpec.
     */
    public GordianCoreDigestSpec getDigestSpec() {
        if (theSignatureSpec instanceof GordianCoreDigestSpec mySpec) {
            return mySpec;
        }
        throw new IllegalArgumentException();
    }

    /**
     * Obtain the composite signatureSpec iterator.
     *
     * @return the signatureSpec iterator.
     */
    @SuppressWarnings("unchecked")
    public Iterator<GordianSignatureSpec> signatureSpecIterator() {
        if (theSignatureSpec instanceof List) {
            return ((List<GordianSignatureSpec>) theSignatureSpec).iterator();
        }
        throw new IllegalArgumentException();
    }

    @Override
    public boolean isValid() {
        return isValid;
    }

    /**
     * Does this signatureSpec support context?
     *
     * @return true/false
     */
    public boolean supportsContext() {
        return switch (theKeyPairType.getType()) {
            case MLDSA, SLHDSA -> true;
            default -> false;
        };
    }

    /**
     * Check spec validity.
     *
     * @return valid true/false
     */
    private boolean checkValidity() {
        if (theKeyPairType == null || theSignatureType == null) {
            return false;
        }
        return switch (theKeyPairType.getType()) {
            case RSA, DSA, EC, DSTU, GOST -> theSignatureSpec instanceof GordianCoreDigestSpec mySpec
                    && mySpec.isValid()
                    && mySpec.getCoreDigestType().supportsLargeData();
            case EDDSA, SLHDSA, MLDSA, FALCON, MAYO, SNOVA, XMSS, LMS -> theSignatureSpec == null;
            case PICNIC -> theSignatureSpec == null || checkPICNICDigest();
            case SM2 -> checkSM2Digest();
            case COMPOSITE -> theSignatureSpec instanceof List && checkComposite();
            default -> false;
        };
    }

    /**
     * Check composite spec validity.
     *
     * @return valid true/false
     */
    private boolean checkComposite() {
        final Iterator<GordianSignatureSpec> myIterator = signatureSpecIterator();
        while (myIterator.hasNext()) {
            /* Check that each spec is valid */
            final GordianCoreSignatureSpec mySpec = (GordianCoreSignatureSpec) myIterator.next();
            if (mySpec == null || !mySpec.isValid()) {
                return false;
            }
        }
        return true;
    }

    /**
     * Check picnic spec validity.
     *
     * @return valid true/false
     */
    private boolean checkPICNICDigest() {
        /* Check that signature length is 512 */
        if (!(theSignatureSpec instanceof GordianCoreDigestSpec mySpec)
                || (!GordianLength.LEN_512.equals(mySpec.getDigestLength()))) {
            return false;
        }

        /* Switch on DigestType */
        return switch (mySpec.getDigestType()) {
            case SHA2, SHA3, SHAKE -> true;
            default -> false;
        };
    }

    /**
     * Check sm2 spec validity.
     *
     * @return valid true/false
     */
    private boolean checkSM2Digest() {
        /* Switch on DigestType */
        if (!(theSignatureSpec instanceof GordianCoreDigestSpec mySpec)) {
            return false;
        }
        return switch (mySpec.getDigestType()) {
            case SM3 -> true;
            case SHA2 -> GordianLength.LEN_256.equals(mySpec.getDigestLength())
                    && !mySpec.isSha2Hybrid();
            default -> false;
        };
    }

    @Override
    public String toString() {
        /* If we have not yet loaded the name */
        if (theName == null) {
            /* Load the name */
            theName = theKeyPairType.toString();
            if (theSignatureType.getType() != GordianSignatureType.NATIVE) {
                theName += GordianSpecConstants.SEP + theSignatureType;
            }
            if (theSignatureSpec != null) {
                if (theKeyPairType.getType() == GordianKeyPairType.COMPOSITE) {
                    final Iterator<GordianSignatureSpec> myIterator = signatureSpecIterator();
                    final StringBuilder myBuilder = new StringBuilder(theName);
                    while (myIterator.hasNext()) {
                        myBuilder.append(GordianSpecConstants.SEP).append(myIterator.next().toString());
                    }
                    theName = myBuilder.toString();
                } else {
                    theName += GordianSpecConstants.SEP + theSignatureSpec.toString();
                }
            }
        }

        /* return the name */
        return theName;
    }

    @Override
    public boolean equals(final Object pThat) {
        /* Handle the trivial cases */
        if (this == pThat) {
            return true;
        }
        if (pThat == null) {
            return false;
        }

        /* Check KeyPairType, signatureType and signatureSpec */
        return pThat instanceof GordianCoreSignatureSpec myThat
                && Objects.equals(theKeyPairType, myThat.getCoreKeyPairType())
                && Objects.equals(theSignatureType, myThat.getCoreType())
                && Objects.equals(theSignatureSpec, myThat.getSignatureSpec());
    }

    @Override
    public int hashCode() {
        return Objects.hash(theKeyPairType, theSignatureType, theSignatureSpec);
    }
}