Search in sources :

Example 1 with QTESLAPrivateKeyParameters

use of org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters in project jmulticard by ctt-gob-es.

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(final PrivateKeyInfo keyInfo) throws IOException {
    final AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    final ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.on(BCObjectIdentifiers.qTESLA)) {
        final ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
        return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
    }
    if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
        return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
    }
    if (algOID.equals(BCObjectIdentifiers.newHope)) {
        return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
    }
    if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final ASN1BitString pubKey = keyInfo.getPublicKeyData();
        if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
            if (pubKey != null) {
                final 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));
        }
        if (pubKey != null) {
            final byte[] pubEnc = pubKey.getOctets();
            return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
        }
        return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.sphincsPlus)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SPHINCSPlusParameters spParams = SPHINCSPlusParameters.getParams(Integers.valueOf(Pack.bigEndianToInt(keyEnc, 0)));
        return new SPHINCSPlusPrivateKeyParameters(spParams, Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.pqc_kem_mceliece)) {
        final CMCEPrivateKey cmceKey = CMCEPrivateKey.getInstance(keyInfo.parsePrivateKey());
        final CMCEParameters spParams = Utils.mcElieceParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new CMCEPrivateKeyParameters(spParams, cmceKey.getDelta(), cmceKey.getC(), cmceKey.getG(), cmceKey.getAlpha(), cmceKey.getS());
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_frodo)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final FrodoParameters spParams = Utils.frodoParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new FrodoPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_saber)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SABERParameters spParams = Utils.saberParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new SABERPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.equals(BCObjectIdentifiers.xmss) || algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
        // $NON-NLS-1$
        throw new IOException("Modificacion para JMultiCard");
    } else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
        final 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 {
        // $NON-NLS-1$
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) 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) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) SPHINCSPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) SPHINCSPlusParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters) IOException(java.io.IOException) SABERParameters(org.bouncycastle.pqc.crypto.saber.SABERParameters) FrodoPrivateKeyParameters(org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters) ASN1BitString(org.bouncycastle.asn1.ASN1BitString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) FrodoParameters(org.bouncycastle.pqc.crypto.frodo.FrodoParameters) CMCEParameters(org.bouncycastle.pqc.crypto.cmce.CMCEParameters) CMCEPrivateKeyParameters(org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) SPHINCSPlusPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters) CMCEPrivateKey(org.bouncycastle.pqc.asn1.CMCEPrivateKey)

Example 2 with QTESLAPrivateKeyParameters

use of org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters 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 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 CMCEPrivateKey (org.bouncycastle.pqc.asn1.CMCEPrivateKey)2 McElieceCCA2PrivateKey (org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey)2 CMCEPrivateKeyParameters (org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters)2 FrodoPrivateKeyParameters (org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters)2 McElieceCCA2PrivateKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters)2 NHPrivateKeyParameters (org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters)2 QTESLAPrivateKeyParameters (org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters)2 SABERPrivateKeyParameters (org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters)2 SPHINCSPrivateKeyParameters (org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters)2 SPHINCSPlusPrivateKeyParameters (org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters)2 ASN1BitString (org.bouncycastle.asn1.ASN1BitString)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 DEROctetString (org.bouncycastle.asn1.DEROctetString)1 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)1 CMCEPublicKey (org.bouncycastle.pqc.asn1.CMCEPublicKey)1 SPHINCS256KeyParams (org.bouncycastle.pqc.asn1.SPHINCS256KeyParams)1 CMCEParameters (org.bouncycastle.pqc.crypto.cmce.CMCEParameters)1