Search in sources :

Example 1 with HSSPublicKeyParameters

use of com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class SubjectPublicKeyInfoFactory method createSubjectPublicKeyInfo.

/**
 * Create a SubjectPublicKeyInfo public key.
 *
 * @param publicKey the key to be encoded into the info object.
 * @return a SubjectPublicKeyInfo representing the key.
 * @throws IOException on an error encoding the key
 */
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey) throws IOException {
    if (publicKey instanceof QTESLAPublicKeyParameters) {
        QTESLAPublicKeyParameters keyParams = (QTESLAPublicKeyParameters) publicKey;
        AlgorithmIdentifier algorithmIdentifier = Utils.qTeslaLookupAlgID(keyParams.getSecurityCategory());
        return new SubjectPublicKeyInfo(algorithmIdentifier, keyParams.getPublicData());
    } else if (publicKey instanceof SPHINCSPublicKeyParameters) {
        SPHINCSPublicKeyParameters params = (SPHINCSPublicKeyParameters) publicKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.sphincs256, new SPHINCS256KeyParams(Utils.sphincs256LookupTreeAlgID(params.getTreeDigest())));
        return new SubjectPublicKeyInfo(algorithmIdentifier, params.getKeyData());
    } else if (publicKey instanceof NHPublicKeyParameters) {
        NHPublicKeyParameters params = (NHPublicKeyParameters) publicKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.newHope);
        return new SubjectPublicKeyInfo(algorithmIdentifier, params.getPubData());
    } else if (publicKey instanceof LMSPublicKeyParameters) {
        LMSPublicKeyParameters params = (LMSPublicKeyParameters) publicKey;
        byte[] encoding = Composer.compose().u32str(1).bytes(params).build();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
    } else if (publicKey instanceof HSSPublicKeyParameters) {
        HSSPublicKeyParameters params = (HSSPublicKeyParameters) publicKey;
        byte[] encoding = Composer.compose().u32str(params.getL()).bytes(params.getLMSPublicKey()).build();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
    } else if (publicKey instanceof XMSSPublicKeyParameters) {
        XMSSPublicKeyParameters keyParams = (XMSSPublicKeyParameters) publicKey;
        byte[] publicSeed = keyParams.getPublicSeed();
        byte[] root = keyParams.getRoot();
        byte[] keyEnc = keyParams.getEncoded();
        if (keyEnc.length > publicSeed.length + root.length) {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(IsaraObjectIdentifiers.id_alg_xmss);
            return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(keyEnc));
        } else {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss, new XMSSKeyParams(keyParams.getParameters().getHeight(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
            return new SubjectPublicKeyInfo(algorithmIdentifier, new XMSSPublicKey(publicSeed, root));
        }
    } else if (publicKey instanceof XMSSMTPublicKeyParameters) {
        XMSSMTPublicKeyParameters keyParams = (XMSSMTPublicKeyParameters) publicKey;
        byte[] publicSeed = keyParams.getPublicSeed();
        byte[] root = keyParams.getRoot();
        byte[] keyEnc = keyParams.getEncoded();
        if (keyEnc.length > publicSeed.length + root.length) {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(IsaraObjectIdentifiers.id_alg_xmssmt);
            return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(keyEnc));
        } else {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss_mt, new XMSSMTKeyParams(keyParams.getParameters().getHeight(), keyParams.getParameters().getLayers(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
            return new SubjectPublicKeyInfo(algorithmIdentifier, new XMSSMTPublicKey(keyParams.getPublicSeed(), keyParams.getRoot()));
        }
    } else if (publicKey instanceof McElieceCCA2PublicKeyParameters) {
        McElieceCCA2PublicKeyParameters pub = (McElieceCCA2PublicKeyParameters) publicKey;
        McElieceCCA2PublicKey mcEliecePub = new McElieceCCA2PublicKey(pub.getN(), pub.getT(), pub.getG(), Utils.getAlgorithmIdentifier(pub.getDigest()));
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
        return new SubjectPublicKeyInfo(algorithmIdentifier, mcEliecePub);
    } else {
        throw new IOException("key parameters not recognized");
    }
}
Also used : XMSSKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSKeyParams) QTESLAPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPublicKeyParameters) LMSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSPublicKeyParameters) McElieceCCA2PublicKeyParameters(com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters) IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) McElieceCCA2PublicKey(com.github.zhenwei.core.pqc.asn1.McElieceCCA2PublicKey) SPHINCS256KeyParams(com.github.zhenwei.core.pqc.asn1.SPHINCS256KeyParams) XMSSMTPublicKey(com.github.zhenwei.core.pqc.asn1.XMSSMTPublicKey) XMSSMTKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams) XMSSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSPublicKeyParameters) SPHINCSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPublicKeyParameters) XMSSMTPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTPublicKeyParameters) HSSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters) NHPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.newhope.NHPublicKeyParameters) XMSSPublicKey(com.github.zhenwei.core.pqc.asn1.XMSSPublicKey)

Example 2 with HSSPublicKeyParameters

use of com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters in project LinLong-Java by zhenwei1108.

the class LMSKeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        param = new LMSKeyGenerationParameters(new LMSParameters(LMSigParameters.lms_sha256_n32_h10, LMOtsParameters.sha256_n32_w2), random);
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    if (engine instanceof LMSKeyPairGenerator) {
        LMSPublicKeyParameters pub = (LMSPublicKeyParameters) pair.getPublic();
        LMSPrivateKeyParameters priv = (LMSPrivateKeyParameters) pair.getPrivate();
        return new KeyPair(new BCLMSPublicKey(pub), new BCLMSPrivateKey(priv));
    } else {
        HSSPublicKeyParameters pub = (HSSPublicKeyParameters) pair.getPublic();
        HSSPrivateKeyParameters priv = (HSSPrivateKeyParameters) pair.getPrivate();
        return new KeyPair(new BCLMSPublicKey(pub), new BCLMSPrivateKey(priv));
    }
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) LMSKeyGenerationParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSKeyGenerationParameters) LMSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSPrivateKeyParameters) LMSKeyPairGenerator(com.github.zhenwei.core.pqc.crypto.lms.LMSKeyPairGenerator) LMSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSPublicKeyParameters) LMSParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSParameters) HSSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.HSSPrivateKeyParameters) HSSPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

HSSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters)2 LMSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSPublicKeyParameters)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)1 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 McElieceCCA2PublicKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PublicKey)1 SPHINCS256KeyParams (com.github.zhenwei.core.pqc.asn1.SPHINCS256KeyParams)1 XMSSKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSKeyParams)1 XMSSMTKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams)1 XMSSMTPublicKey (com.github.zhenwei.core.pqc.asn1.XMSSMTPublicKey)1 XMSSPublicKey (com.github.zhenwei.core.pqc.asn1.XMSSPublicKey)1 HSSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.HSSPrivateKeyParameters)1 LMSKeyGenerationParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSKeyGenerationParameters)1 LMSKeyPairGenerator (com.github.zhenwei.core.pqc.crypto.lms.LMSKeyPairGenerator)1 LMSParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSParameters)1 LMSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSPrivateKeyParameters)1 McElieceCCA2PublicKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters)1 NHPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.newhope.NHPublicKeyParameters)1 QTESLAPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPublicKeyParameters)1