Search in sources :

Example 1 with LMSPrivateKeyParameters

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

the class PrivateKeyInfoFactory method createPrivateKeyInfo.

/**
 * Create a PrivateKeyInfo representation of a private key with attributes.
 *
 * @param privateKey the key to be encoded into the info object.
 * @param attributes the set of attributes to be included.
 * @return the appropriate PrivateKeyInfo
 * @throws IOException on an error encoding the key
 */
public static PrivateKeyInfo createPrivateKeyInfo(AsymmetricKeyParameter privateKey, ASN1Set attributes) throws IOException {
    if (privateKey instanceof QTESLAPrivateKeyParameters) {
        QTESLAPrivateKeyParameters keyParams = (QTESLAPrivateKeyParameters) privateKey;
        AlgorithmIdentifier algorithmIdentifier = Utils.qTeslaLookupAlgID(keyParams.getSecurityCategory());
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(keyParams.getSecret()), attributes);
    } else if (privateKey instanceof SPHINCSPrivateKeyParameters) {
        SPHINCSPrivateKeyParameters params = (SPHINCSPrivateKeyParameters) privateKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.sphincs256, new SPHINCS256KeyParams(Utils.sphincs256LookupTreeAlgID(params.getTreeDigest())));
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(params.getKeyData()));
    } else if (privateKey instanceof NHPrivateKeyParameters) {
        NHPrivateKeyParameters params = (NHPrivateKeyParameters) privateKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.newHope);
        short[] privateKeyData = params.getSecData();
        byte[] octets = new byte[privateKeyData.length * 2];
        for (int i = 0; i != privateKeyData.length; i++) {
            Pack.shortToLittleEndian(privateKeyData[i], octets, i * 2);
        }
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(octets));
    } else if (privateKey instanceof LMSPrivateKeyParameters) {
        LMSPrivateKeyParameters params = (LMSPrivateKeyParameters) privateKey;
        byte[] encoding = Composer.compose().u32str(1).bytes(params).build();
        byte[] pubEncoding = Composer.compose().u32str(1).bytes(params.getPublicKey()).build();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
    } else if (privateKey instanceof HSSPrivateKeyParameters) {
        HSSPrivateKeyParameters params = (HSSPrivateKeyParameters) privateKey;
        byte[] encoding = Composer.compose().u32str(params.getL()).bytes(params).build();
        byte[] pubEncoding = Composer.compose().u32str(params.getL()).bytes(params.getPublicKey().getLMSPublicKey()).build();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
    } else if (privateKey instanceof XMSSPrivateKeyParameters) {
        XMSSPrivateKeyParameters keyParams = (XMSSPrivateKeyParameters) privateKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss, new XMSSKeyParams(keyParams.getParameters().getHeight(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
        return new PrivateKeyInfo(algorithmIdentifier, xmssCreateKeyStructure(keyParams), attributes);
    } else if (privateKey instanceof XMSSMTPrivateKeyParameters) {
        XMSSMTPrivateKeyParameters keyParams = (XMSSMTPrivateKeyParameters) privateKey;
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.xmss_mt, new XMSSMTKeyParams(keyParams.getParameters().getHeight(), keyParams.getParameters().getLayers(), Utils.xmssLookupTreeAlgID(keyParams.getTreeDigest())));
        return new PrivateKeyInfo(algorithmIdentifier, xmssmtCreateKeyStructure(keyParams), attributes);
    } else if (privateKey instanceof McElieceCCA2PrivateKeyParameters) {
        McElieceCCA2PrivateKeyParameters priv = (McElieceCCA2PrivateKeyParameters) privateKey;
        McElieceCCA2PrivateKey mcEliecePriv = new McElieceCCA2PrivateKey(priv.getN(), priv.getK(), priv.getField(), priv.getGoppaPoly(), priv.getP(), Utils.getAlgorithmIdentifier(priv.getDigest()));
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
        return new PrivateKeyInfo(algorithmIdentifier, mcEliecePriv);
    } else {
        throw new IOException("key parameters not recognized");
    }
}
Also used : XMSSKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSKeyParams) McElieceCCA2PrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) QTESLAPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPrivateKeyParameters) NHPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.newhope.NHPrivateKeyParameters) McElieceCCA2PrivateKey(com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey) SPHINCSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) HSSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.HSSPrivateKeyParameters) IOException(java.io.IOException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SPHINCS256KeyParams(com.github.zhenwei.core.pqc.asn1.SPHINCS256KeyParams) LMSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.lms.LMSPrivateKeyParameters) XMSSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSPrivateKeyParameters) XMSSMTKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) XMSSMTPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTPrivateKeyParameters)

Example 2 with LMSPrivateKeyParameters

use of com.github.zhenwei.core.pqc.crypto.lms.LMSPrivateKeyParameters 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

HSSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.HSSPrivateKeyParameters)2 LMSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSPrivateKeyParameters)2 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 McElieceCCA2PrivateKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey)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 HSSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters)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 LMSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSPublicKeyParameters)1 McElieceCCA2PrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters)1 NHPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.newhope.NHPrivateKeyParameters)1 QTESLAPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPrivateKeyParameters)1 SPHINCSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters)1 XMSSMTPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTPrivateKeyParameters)1