Search in sources :

Example 1 with McElieceCCA2PrivateKey

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

the class BCMcElieceCCA2PrivateKey method getEncoded.

/**
 * Return the keyData to encode in the SubjectPublicKeyInfo structure.
 * <p>
 * The ASN.1 definition of the key structure is
 * <pre>
 *   McEliecePrivateKey ::= SEQUENCE {
 *     m             INTEGER                  -- extension degree of the field
 *     k             INTEGER                  -- dimension of the code
 *     field         OCTET STRING             -- field polynomial
 *     goppaPoly     OCTET STRING             -- irreducible Goppa polynomial
 *     p             OCTET STRING             -- permutation vector
 *     matrixH       OCTET STRING             -- canonical check matrix
 *     sqRootMatrix  SEQUENCE OF OCTET STRING -- square root matrix
 *   }
 * </pre>
 * @return the keyData to encode in the SubjectPublicKeyInfo structure
 */
public byte[] getEncoded() {
    PrivateKeyInfo pki;
    try {
        McElieceCCA2PrivateKey privateKey = new McElieceCCA2PrivateKey(getN(), getK(), getField(), getGoppaPoly(), getP(), MessageDigestUtils.getDigestAlgID(params.getDigest()));
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
        pki = new PrivateKeyInfo(algorithmIdentifier, privateKey);
        return pki.getEncoded();
    } catch (IOException e) {
        return null;
    }
}
Also used : McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) IOException(java.io.IOException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 2 with McElieceCCA2PrivateKey

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

the class McElieceCCA2KeyFactorySpi method engineGeneratePrivate.

/**
 * Converts, if possible, a key specification into a
 * {@link BCMcElieceCCA2PrivateKey}. Currently, the following key
 * specifications are supported:
 * {@link PKCS8EncodedKeySpec}.
 *
 * @param keySpec the key specification
 * @return the McEliece CCA2 private key
 * @throws InvalidKeySpecException if the KeySpec is not supported.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof PKCS8EncodedKeySpec) {
        // get the DER-encoded Key according to PKCS#8 from the spec
        byte[] encKey = ((PKCS8EncodedKeySpec) keySpec).getEncoded();
        // decode the PKCS#8 data structure to the pki object
        PrivateKeyInfo pki;
        try {
            pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey));
        } catch (IOException e) {
            throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e);
        }
        try {
            if (PQCObjectIdentifiers.mcElieceCca2.equals(pki.getPrivateKeyAlgorithm().getAlgorithm())) {
                McElieceCCA2PrivateKey key = McElieceCCA2PrivateKey.getInstance(pki.parsePrivateKey());
                return new BCMcElieceCCA2PrivateKey(new McElieceCCA2PrivateKeyParameters(key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP(), Utils.getDigest(key.getDigest()).getAlgorithmName()));
            } else {
                throw new InvalidKeySpecException("Unable to recognise OID in McEliece public key");
            }
        } catch (IOException cce) {
            throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec.");
        }
    }
    throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Also used : McElieceCCA2PrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 3 with McElieceCCA2PrivateKey

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

the class McElieceCCA2KeyFactorySpi method generatePrivate.

public PrivateKey generatePrivate(PrivateKeyInfo pki) throws IOException {
    // get the inner type inside the BIT STRING
    ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive();
    McElieceCCA2PrivateKey key = McElieceCCA2PrivateKey.getInstance(innerType);
    return new BCMcElieceCCA2PrivateKey(new McElieceCCA2PrivateKeyParameters(key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP(), null));
}
Also used : McElieceCCA2PrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive)

Example 4 with McElieceCCA2PrivateKey

use of org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey 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 5 with McElieceCCA2PrivateKey

use of org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey 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)

Aggregations

IOException (java.io.IOException)8 McElieceCCA2PrivateKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey)5 McElieceCCA2PrivateKey (org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey)5 McElieceCCA2PrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters)4 McElieceCCA2PrivateKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters)4 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)3 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)3 XMSSKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSKeyParams)2 XMSSMTKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams)2 NHPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.newhope.NHPrivateKeyParameters)2 QTESLAPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.qtesla.QTESLAPrivateKeyParameters)2 SPHINCSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters)2 XMSSPrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.xmss.XMSSPrivateKeyParameters)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 CMCEPrivateKey (org.bouncycastle.pqc.asn1.CMCEPrivateKey)2 CMCEPrivateKeyParameters (org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters)2 FrodoPrivateKeyParameters (org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters)2