Search in sources :

Example 1 with XMSSMTParameters

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

the class XMSSMTKeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        param = new XMSSMTKeyGenerationParameters(new XMSSMTParameters(10, 20, new SHA512Digest()), random);
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    XMSSMTPublicKeyParameters pub = (XMSSMTPublicKeyParameters) pair.getPublic();
    XMSSMTPrivateKeyParameters priv = (XMSSMTPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCXMSSMTPublicKey(treeDigest, pub), new BCXMSSMTPrivateKey(treeDigest, priv));
}
Also used : SHA512Digest(com.github.zhenwei.core.crypto.digests.SHA512Digest) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) XMSSMTKeyGenerationParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTKeyGenerationParameters) XMSSMTPublicKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTPublicKeyParameters) XMSSMTParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) XMSSMTPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTPrivateKeyParameters)

Example 2 with XMSSMTParameters

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

the class XMSSMTKeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof XMSSMTParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a XMSSMTParameterSpec");
    }
    XMSSMTParameterSpec xmssParams = (XMSSMTParameterSpec) params;
    if (xmssParams.getTreeDigest().equals(XMSSParameterSpec.SHA256)) {
        treeDigest = NISTObjectIdentifiers.id_sha256;
        param = new XMSSMTKeyGenerationParameters(new XMSSMTParameters(xmssParams.getHeight(), xmssParams.getLayers(), new SHA256Digest()), random);
    } else if (xmssParams.getTreeDigest().equals(XMSSParameterSpec.SHA512)) {
        treeDigest = NISTObjectIdentifiers.id_sha512;
        param = new XMSSMTKeyGenerationParameters(new XMSSMTParameters(xmssParams.getHeight(), xmssParams.getLayers(), new SHA512Digest()), random);
    } else if (xmssParams.getTreeDigest().equals(XMSSParameterSpec.SHAKE128)) {
        treeDigest = NISTObjectIdentifiers.id_shake128;
        param = new XMSSMTKeyGenerationParameters(new XMSSMTParameters(xmssParams.getHeight(), xmssParams.getLayers(), new SHAKEDigest(128)), random);
    } else if (xmssParams.getTreeDigest().equals(XMSSParameterSpec.SHAKE256)) {
        treeDigest = NISTObjectIdentifiers.id_shake256;
        param = new XMSSMTKeyGenerationParameters(new XMSSMTParameters(xmssParams.getHeight(), xmssParams.getLayers(), new SHAKEDigest(256)), random);
    }
    engine.init(param);
    initialised = true;
}
Also used : SHA512Digest(com.github.zhenwei.core.crypto.digests.SHA512Digest) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) XMSSMTKeyGenerationParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTKeyGenerationParameters) XMSSMTParameterSpec(com.github.zhenwei.provider.jcajce.spec.XMSSMTParameterSpec) XMSSMTParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTParameters) SHAKEDigest(com.github.zhenwei.core.crypto.digests.SHAKEDigest)

Example 3 with XMSSMTParameters

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

the class PrivateKeyFactory method createKey.

/**
 * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
 *
 * @param keyInfo the PrivateKeyInfo object containing the key material
 * @return a suitable private key parameter
 * @throws IOException on an error decoding the key
 */
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
    AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.on(BCObjectIdentifiers.qTESLA)) {
        ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
        return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
    } else if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
        return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
    } else if (algOID.equals(BCObjectIdentifiers.newHope)) {
        return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
    } else if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
        byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        ASN1BitString pubKey = keyInfo.getPublicKeyData();
        if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
            if (pubKey != null) {
                byte[] pubEnc = pubKey.getOctets();
                return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), Arrays.copyOfRange(pubEnc, 4, pubEnc.length));
            }
            return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
        } else {
            if (pubKey != null) {
                byte[] pubEnc = pubKey.getOctets();
                return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
            }
            return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
        }
    } else if (algOID.equals(BCObjectIdentifiers.xmss)) {
        XMSSKeyParams keyParams = XMSSKeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters());
        ASN1ObjectIdentifier treeDigest = keyParams.getTreeDigest().getAlgorithm();
        XMSSPrivateKey xmssPrivateKey = XMSSPrivateKey.getInstance(keyInfo.parsePrivateKey());
        try {
            XMSSPrivateKeyParameters.Builder keyBuilder = new XMSSPrivateKeyParameters.Builder(new XMSSParameters(keyParams.getHeight(), Utils.getDigest(treeDigest))).withIndex(xmssPrivateKey.getIndex()).withSecretKeySeed(xmssPrivateKey.getSecretKeySeed()).withSecretKeyPRF(xmssPrivateKey.getSecretKeyPRF()).withPublicSeed(xmssPrivateKey.getPublicSeed()).withRoot(xmssPrivateKey.getRoot());
            if (xmssPrivateKey.getVersion() != 0) {
                keyBuilder.withMaxIndex(xmssPrivateKey.getMaxIndex());
            }
            if (xmssPrivateKey.getBdsState() != null) {
                BDS bds = (BDS) XMSSUtil.deserialize(xmssPrivateKey.getBdsState(), BDS.class);
                keyBuilder.withBDSState(bds.withWOTSDigest(treeDigest));
            }
            return keyBuilder.build();
        } catch (ClassNotFoundException e) {
            throw new IOException("ClassNotFoundException processing BDS state: " + e.getMessage());
        }
    } else if (algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
        XMSSMTKeyParams keyParams = XMSSMTKeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters());
        ASN1ObjectIdentifier treeDigest = keyParams.getTreeDigest().getAlgorithm();
        try {
            XMSSMTPrivateKey xmssMtPrivateKey = XMSSMTPrivateKey.getInstance(keyInfo.parsePrivateKey());
            XMSSMTPrivateKeyParameters.Builder keyBuilder = new XMSSMTPrivateKeyParameters.Builder(new XMSSMTParameters(keyParams.getHeight(), keyParams.getLayers(), Utils.getDigest(treeDigest))).withIndex(xmssMtPrivateKey.getIndex()).withSecretKeySeed(xmssMtPrivateKey.getSecretKeySeed()).withSecretKeyPRF(xmssMtPrivateKey.getSecretKeyPRF()).withPublicSeed(xmssMtPrivateKey.getPublicSeed()).withRoot(xmssMtPrivateKey.getRoot());
            if (xmssMtPrivateKey.getVersion() != 0) {
                keyBuilder.withMaxIndex(xmssMtPrivateKey.getMaxIndex());
            }
            if (xmssMtPrivateKey.getBdsState() != null) {
                BDSStateMap bdsState = (BDSStateMap) XMSSUtil.deserialize(xmssMtPrivateKey.getBdsState(), BDSStateMap.class);
                keyBuilder.withBDSState(bdsState.withWOTSDigest(treeDigest));
            }
            return keyBuilder.build();
        } catch (ClassNotFoundException e) {
            throw new IOException("ClassNotFoundException processing BDS state: " + e.getMessage());
        }
    } else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
        McElieceCCA2PrivateKey mKey = McElieceCCA2PrivateKey.getInstance(keyInfo.parsePrivateKey());
        return new McElieceCCA2PrivateKeyParameters(mKey.getN(), mKey.getK(), mKey.getField(), mKey.getGoppaPoly(), mKey.getP(), Utils.getDigestName(mKey.getDigest().getAlgorithm()));
    } else {
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) 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) XMSSMTPrivateKey(com.github.zhenwei.core.pqc.asn1.XMSSMTPrivateKey) McElieceCCA2PrivateKey(com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey) BDSStateMap(com.github.zhenwei.core.pqc.crypto.xmss.BDSStateMap) SPHINCSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) IOException(java.io.IOException) ASN1BitString(com.github.zhenwei.core.asn1.ASN1BitString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) XMSSMTParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTParameters) BDS(com.github.zhenwei.core.pqc.crypto.xmss.BDS) XMSSParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSParameters) XMSSPrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.xmss.XMSSPrivateKeyParameters) XMSSMTKeyParams(com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams) XMSSPrivateKey(com.github.zhenwei.core.pqc.asn1.XMSSPrivateKey) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

XMSSMTParameters (com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTParameters)3 SHA512Digest (com.github.zhenwei.core.crypto.digests.SHA512Digest)2 XMSSMTKeyGenerationParameters (com.github.zhenwei.core.pqc.crypto.xmss.XMSSMTKeyGenerationParameters)2 ASN1BitString (com.github.zhenwei.core.asn1.ASN1BitString)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)1 SHAKEDigest (com.github.zhenwei.core.crypto.digests.SHAKEDigest)1 McElieceCCA2PrivateKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey)1 XMSSKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSKeyParams)1 XMSSMTKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams)1 XMSSMTPrivateKey (com.github.zhenwei.core.pqc.asn1.XMSSMTPrivateKey)1 XMSSPrivateKey (com.github.zhenwei.core.pqc.asn1.XMSSPrivateKey)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 BDS (com.github.zhenwei.core.pqc.crypto.xmss.BDS)1