JcaValidator.java

/*
 * GordianKnot: Security Suite
 * Copyright 2012-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.jca;

import io.github.tonywasher.joceanus.gordianknot.api.cipher.spec.GordianStreamKeyType;
import io.github.tonywasher.joceanus.gordianknot.api.cipher.spec.GordianSymKeyType;
import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestType;
import io.github.tonywasher.joceanus.gordianknot.impl.core.base.GordianValidator;

/**
 * Jca Validator.
 */
public class JcaValidator
        extends GordianValidator {
    @Override
    public boolean validSymKeyType(final GordianSymKeyType pKeyType) {
        return supportedSymKeyType(pKeyType)
                && super.validSymKeyType(pKeyType);
    }

    @Override
    protected boolean validHMacDigestType(final GordianDigestType pDigestType) {
        return JcaDigest.isHMacSupported(pDigestType)
                && validDigestType(pDigestType);
    }

    @Override
    public boolean validDigestType(final GordianDigestType pDigestType) {
        /* Perform standard checks */
        if (!super.validDigestType(pDigestType)) {
            return false;
        }

        /* Disable non-JCE digests */
        return switch (pDigestType) {
            case JH, GROESTL, CUBEHASH, KANGAROO, ASCON, ISAP, PHOTONBEETLE, ROMULUS, SPARKLE, XOODYAK -> false;
            default -> true;
        };
    }

    @Override
    public boolean validStreamKeyType(final GordianStreamKeyType pKeyType) {
        if (pKeyType == null) {
            return false;
        }
        return switch (pKeyType) {
            case ISAAC, SOSEMANUK, RABBIT, SNOW3G, SKEINXOF, BLAKE2XOF, BLAKE3XOF, ASCON, ELEPHANT, ISAP, PHOTONBEETLE,
                 ROMULUS, SPARKLE, XOODYAK -> false;
            default -> super.validStreamKeyType(pKeyType);
        };
    }

    /**
     * Is this symKeyType supported by Jca?
     *
     * @param pKeyType the keyType
     * @return true/false
     */
    private static boolean supportedSymKeyType(final GordianSymKeyType pKeyType) {
        if (pKeyType == null) {
            return false;
        }
        return switch (pKeyType) {
            case SPECK, ANUBIS, SIMON, MARS, LEA -> false;
            default -> true;
        };
    }

    @Override
    public boolean isXofSupported() {
        return false;
    }
}