Search in sources :

Example 1 with CMCEPublicKey

use of org.bouncycastle.pqc.asn1.CMCEPublicKey in project jmulticard by ctt-gob-es.

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 java.io.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 SPHINCSPlusPublicKeyParameters) {
        SPHINCSPlusPublicKeyParameters params = (SPHINCSPlusPublicKeyParameters) publicKey;
        byte[] encoding = params.getEncoded();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.sphincsPlusOidLookup(params.getParameters()));
        return new SubjectPublicKeyInfo(algorithmIdentifier, new DEROctetString(encoding));
    } else if (publicKey instanceof CMCEPublicKeyParameters) {
        CMCEPublicKeyParameters params = (CMCEPublicKeyParameters) publicKey;
        byte[] encoding = params.getEncoded();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mcElieceOidLookup(params.getParameters()));
        // https://datatracker.ietf.org/doc/draft-uni-qsckeys/
        return new SubjectPublicKeyInfo(algorithmIdentifier, new CMCEPublicKey(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 if (publicKey instanceof FrodoPublicKeyParameters) {
        FrodoPublicKeyParameters params = (FrodoPublicKeyParameters) publicKey;
        byte[] encoding = params.getEncoded();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.frodoOidLookup(params.getParameters()));
        return new SubjectPublicKeyInfo(algorithmIdentifier, (new DEROctetString(encoding)));
    } else if (publicKey instanceof SABERPublicKeyParameters) {
        SABERPublicKeyParameters params = (SABERPublicKeyParameters) publicKey;
        byte[] encoding = params.getEncoded();
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.saberOidLookup(params.getParameters()));
        // https://datatracker.ietf.org/doc/draft-uni-qsckeys/
        return new SubjectPublicKeyInfo(algorithmIdentifier, new DERSequence(new DEROctetString(encoding)));
    } else {
        throw new IOException("key parameters not recognized");
    }
}
Also used : CMCEPublicKeyParameters(org.bouncycastle.pqc.crypto.cmce.CMCEPublicKeyParameters) SABERPublicKeyParameters(org.bouncycastle.pqc.crypto.saber.SABERPublicKeyParameters) XMSSKeyParams(org.bouncycastle.pqc.asn1.XMSSKeyParams) CMCEPublicKey(org.bouncycastle.pqc.asn1.CMCEPublicKey) LMSPublicKeyParameters(org.bouncycastle.pqc.crypto.lms.LMSPublicKeyParameters) McElieceCCA2PublicKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) XMSSMTPublicKey(org.bouncycastle.pqc.asn1.XMSSMTPublicKey) DERSequence(org.bouncycastle.asn1.DERSequence) SPHINCSPublicKeyParameters(org.bouncycastle.pqc.crypto.sphincs.SPHINCSPublicKeyParameters) HSSPublicKeyParameters(org.bouncycastle.pqc.crypto.lms.HSSPublicKeyParameters) QTESLAPublicKeyParameters(org.bouncycastle.pqc.crypto.qtesla.QTESLAPublicKeyParameters) IOException(java.io.IOException) McElieceCCA2PublicKey(org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey) SPHINCS256KeyParams(org.bouncycastle.pqc.asn1.SPHINCS256KeyParams) XMSSMTKeyParams(org.bouncycastle.pqc.asn1.XMSSMTKeyParams) FrodoPublicKeyParameters(org.bouncycastle.pqc.crypto.frodo.FrodoPublicKeyParameters) XMSSPublicKeyParameters(org.bouncycastle.pqc.crypto.xmss.XMSSPublicKeyParameters) XMSSMTPublicKeyParameters(org.bouncycastle.pqc.crypto.xmss.XMSSMTPublicKeyParameters) SPHINCSPlusPublicKeyParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPublicKeyParameters) NHPublicKeyParameters(org.bouncycastle.pqc.crypto.newhope.NHPublicKeyParameters) XMSSPublicKey(org.bouncycastle.pqc.asn1.XMSSPublicKey)

Example 2 with CMCEPublicKey

use of org.bouncycastle.pqc.asn1.CMCEPublicKey in project jmulticard by ctt-gob-es.

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 java.io.IOException on an error encoding the key
 */
public static PrivateKeyInfo createPrivateKeyInfo(final AsymmetricKeyParameter privateKey, final ASN1Set attributes) throws IOException {
    if (privateKey instanceof QTESLAPrivateKeyParameters) {
        final QTESLAPrivateKeyParameters keyParams = (QTESLAPrivateKeyParameters) privateKey;
        final AlgorithmIdentifier algorithmIdentifier = Utils.qTeslaLookupAlgID(keyParams.getSecurityCategory());
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(keyParams.getSecret()), attributes);
    }
    if (privateKey instanceof SPHINCSPrivateKeyParameters) {
        final SPHINCSPrivateKeyParameters params = (SPHINCSPrivateKeyParameters) privateKey;
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.sphincs256, new SPHINCS256KeyParams(Utils.sphincs256LookupTreeAlgID(params.getTreeDigest())));
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(params.getKeyData()));
    }
    if (privateKey instanceof NHPrivateKeyParameters) {
        final NHPrivateKeyParameters params = (NHPrivateKeyParameters) privateKey;
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.newHope);
        final short[] privateKeyData = params.getSecData();
        final 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));
    }
    if (privateKey instanceof LMSPrivateKeyParameters) {
        final LMSPrivateKeyParameters params = (LMSPrivateKeyParameters) privateKey;
        final byte[] encoding = Composer.compose().u32str(1).bytes(params).build();
        final byte[] pubEncoding = Composer.compose().u32str(1).bytes(params.getPublicKey()).build();
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
    }
    if (privateKey instanceof HSSPrivateKeyParameters) {
        final HSSPrivateKeyParameters params = (HSSPrivateKeyParameters) privateKey;
        final byte[] encoding = Composer.compose().u32str(params.getL()).bytes(params).build();
        final byte[] pubEncoding = Composer.compose().u32str(params.getL()).bytes(params.getPublicKey().getLMSPublicKey()).build();
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig);
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
    }
    if (privateKey instanceof SPHINCSPlusPrivateKeyParameters) {
        final SPHINCSPlusPrivateKeyParameters params = (SPHINCSPlusPrivateKeyParameters) privateKey;
        final byte[] encoding = params.getEncoded();
        final byte[] pubEncoding = params.getEncodedPublicKey();
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.sphincsPlusOidLookup(params.getParameters()));
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes, pubEncoding);
    }
    if (privateKey instanceof CMCEPrivateKeyParameters) {
        final CMCEPrivateKeyParameters params = (CMCEPrivateKeyParameters) privateKey;
        final byte[] encoding = params.getEncoded();
        // todo either make CMCEPrivateKey split the parameters from the private key or
        // (current) Make CMCEPrivateKey take parts of the private key splitted in the params
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mcElieceOidLookup(params.getParameters()));
        final CMCEPublicKey cmcePub = new CMCEPublicKey(params.reconstructPublicKey());
        final CMCEPrivateKey cmcePriv = new CMCEPrivateKey(0, params.getDelta(), params.getC(), params.getG(), params.getAlpha(), params.getS(), cmcePub);
        return new PrivateKeyInfo(algorithmIdentifier, cmcePriv, attributes);
    } else if (privateKey instanceof XMSSPrivateKeyParameters || privateKey instanceof XMSSMTPrivateKeyParameters) {
        // $NON-NLS-1$
        throw new IOException("Modificacion para JMultiCard");
    } else if (privateKey instanceof McElieceCCA2PrivateKeyParameters) {
        final McElieceCCA2PrivateKeyParameters priv = (McElieceCCA2PrivateKeyParameters) privateKey;
        final McElieceCCA2PrivateKey mcEliecePriv = new McElieceCCA2PrivateKey(priv.getN(), priv.getK(), priv.getField(), priv.getGoppaPoly(), priv.getP(), Utils.getAlgorithmIdentifier(priv.getDigest()));
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
        return new PrivateKeyInfo(algorithmIdentifier, mcEliecePriv);
    } else if (privateKey instanceof FrodoPrivateKeyParameters) {
        final FrodoPrivateKeyParameters params = (FrodoPrivateKeyParameters) privateKey;
        final byte[] encoding = params.getEncoded();
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.frodoOidLookup(params.getParameters()));
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes);
    } else if (privateKey instanceof SABERPrivateKeyParameters) {
        final SABERPrivateKeyParameters params = (SABERPrivateKeyParameters) privateKey;
        final byte[] encoding = params.getEncoded();
        final AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.saberOidLookup(params.getParameters()));
        return new PrivateKeyInfo(algorithmIdentifier, new DEROctetString(encoding), attributes);
    } else {
        // $NON-NLS-1$
        throw new IOException("key parameters not recognized");
    }
}
Also used : McElieceCCA2PrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) QTESLAPrivateKeyParameters(org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters) SABERPrivateKeyParameters(org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters) NHPrivateKeyParameters(org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters) CMCEPublicKey(org.bouncycastle.pqc.asn1.CMCEPublicKey) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) SPHINCSPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) HSSPrivateKeyParameters(org.bouncycastle.pqc.crypto.lms.HSSPrivateKeyParameters) IOException(java.io.IOException) FrodoPrivateKeyParameters(org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters) DEROctetString(org.bouncycastle.asn1.DEROctetString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) SPHINCS256KeyParams(org.bouncycastle.pqc.asn1.SPHINCS256KeyParams) LMSPrivateKeyParameters(org.bouncycastle.pqc.crypto.lms.LMSPrivateKeyParameters) XMSSPrivateKeyParameters(org.bouncycastle.pqc.crypto.xmss.XMSSPrivateKeyParameters) CMCEPrivateKeyParameters(org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) SPHINCSPlusPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters) CMCEPrivateKey(org.bouncycastle.pqc.asn1.CMCEPrivateKey) XMSSMTPrivateKeyParameters(org.bouncycastle.pqc.crypto.xmss.XMSSMTPrivateKeyParameters)

Aggregations

IOException (java.io.IOException)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 CMCEPublicKey (org.bouncycastle.pqc.asn1.CMCEPublicKey)2 SPHINCS256KeyParams (org.bouncycastle.pqc.asn1.SPHINCS256KeyParams)2 DERSequence (org.bouncycastle.asn1.DERSequence)1 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)1 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)1 CMCEPrivateKey (org.bouncycastle.pqc.asn1.CMCEPrivateKey)1 McElieceCCA2PrivateKey (org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey)1 McElieceCCA2PublicKey (org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey)1 XMSSKeyParams (org.bouncycastle.pqc.asn1.XMSSKeyParams)1 XMSSMTKeyParams (org.bouncycastle.pqc.asn1.XMSSMTKeyParams)1 XMSSMTPublicKey (org.bouncycastle.pqc.asn1.XMSSMTPublicKey)1 XMSSPublicKey (org.bouncycastle.pqc.asn1.XMSSPublicKey)1 CMCEPrivateKeyParameters (org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters)1 CMCEPublicKeyParameters (org.bouncycastle.pqc.crypto.cmce.CMCEPublicKeyParameters)1 FrodoPrivateKeyParameters (org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters)1 FrodoPublicKeyParameters (org.bouncycastle.pqc.crypto.frodo.FrodoPublicKeyParameters)1 HSSPrivateKeyParameters (org.bouncycastle.pqc.crypto.lms.HSSPrivateKeyParameters)1