Search in sources :

Example 1 with McEliecePrivateKey

use of com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey in project jmulticard by ctt-gob-es.

the class BCMcEliecePrivateKey method getEncoded.

/**
 * Return the key data to encode in the SubjectPublicKeyInfo structure.
 * <p>
 * The ASN.1 definition of the key structure is
 * </p>
 * <pre>
 *   McEliecePrivateKey ::= SEQUENCE {
 *     n          INTEGER                   -- length of the code
 *     k          INTEGER                   -- dimension of the code
 *     fieldPoly  OCTET STRING              -- field polynomial defining GF(2&circ;m)
 *     getGoppaPoly()  OCTET STRING              -- irreducible Goppa polynomial
 *     sInv       OCTET STRING              -- matrix S&circ;-1
 *     p1         OCTET STRING              -- permutation P1
 *     p2         OCTET STRING              -- permutation P2
 *     h          OCTET STRING              -- canonical check matrix
 *     qInv       SEQUENCE OF OCTET STRING  -- matrix used to compute square roots
 *   }
 * </pre>
 *
 * @return the key data to encode in the SubjectPublicKeyInfo structure
 */
public byte[] getEncoded() {
    McEliecePrivateKey privateKey = new McEliecePrivateKey(params.getN(), params.getK(), params.getField(), params.getGoppaPoly(), params.getP1(), params.getP2(), params.getSInv());
    PrivateKeyInfo pki;
    try {
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcEliece);
        pki = new PrivateKeyInfo(algorithmIdentifier, privateKey);
    } catch (IOException e) {
        return null;
    }
    try {
        byte[] encoded = pki.getEncoded();
        return encoded;
    } catch (IOException e) {
        return null;
    }
}
Also used : IOException(java.io.IOException) McEliecePrivateKey(org.bouncycastle.pqc.asn1.McEliecePrivateKey) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 2 with McEliecePrivateKey

use of com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey in project jmulticard by ctt-gob-es.

the class McElieceKeyFactorySpi method engineGeneratePrivate.

/**
 * Converts, if possible, a key specification into a
 * {@link BCMcEliecePrivateKey}.
 *
 * @param keySpec the key specification
 * @return the McEliece 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.mcEliece.equals(pki.getPrivateKeyAlgorithm().getAlgorithm())) {
                McEliecePrivateKey key = McEliecePrivateKey.getInstance(pki.parsePrivateKey());
                return new BCMcEliecePrivateKey(new McEliecePrivateKeyParameters(key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP1(), key.getP2(), key.getSInv()));
            } else {
                throw new InvalidKeySpecException("Unable to recognise OID in McEliece private key");
            }
        } catch (IOException cce) {
            throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec.");
        }
    }
    throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Also used : McEliecePrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McEliecePrivateKeyParameters) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) McEliecePrivateKey(org.bouncycastle.pqc.asn1.McEliecePrivateKey)

Example 3 with McEliecePrivateKey

use of com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey in project LinLong-Java by zhenwei1108.

the class McElieceKeyFactorySpi method engineGeneratePrivate.

/**
 * Converts, if possible, a key specification into a {@link BCMcEliecePrivateKey}.
 *
 * @param keySpec the key specification
 * @return the McEliece 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.mcEliece.equals(pki.getPrivateKeyAlgorithm().getAlgorithm())) {
                McEliecePrivateKey key = McEliecePrivateKey.getInstance(pki.parsePrivateKey());
                return new BCMcEliecePrivateKey(new McEliecePrivateKeyParameters(key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP1(), key.getP2(), key.getSInv()));
            } else {
                throw new InvalidKeySpecException("Unable to recognise OID in McEliece private key");
            }
        } catch (IOException cce) {
            throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec.");
        }
    }
    throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Also used : McEliecePrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.mceliece.McEliecePrivateKeyParameters) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) McEliecePrivateKey(com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey)

Example 4 with McEliecePrivateKey

use of com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey in project jmulticard by ctt-gob-es.

the class McElieceKeyFactorySpi method generatePrivate.

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

Example 5 with McEliecePrivateKey

use of com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey in project LinLong-Java by zhenwei1108.

the class McElieceKeyFactorySpi method generatePrivate.

public PrivateKey generatePrivate(PrivateKeyInfo pki) throws IOException {
    // get the inner type inside the BIT STRING
    ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive();
    McEliecePrivateKey key = McEliecePrivateKey.getInstance(innerType);
    return new BCMcEliecePrivateKey(new McEliecePrivateKeyParameters(key.getN(), key.getK(), key.getField(), key.getGoppaPoly(), key.getP1(), key.getP2(), key.getSInv()));
}
Also used : McEliecePrivateKeyParameters(com.github.zhenwei.core.pqc.crypto.mceliece.McEliecePrivateKeyParameters) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) McEliecePrivateKey(com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey)

Aggregations

IOException (java.io.IOException)5 PrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo)3 McEliecePrivateKey (com.github.zhenwei.core.pqc.asn1.McEliecePrivateKey)3 McEliecePrivateKey (org.bouncycastle.pqc.asn1.McEliecePrivateKey)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 McEliecePrivateKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McEliecePrivateKeyParameters)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)2 McEliecePrivateKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McEliecePrivateKeyParameters)2 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 McElieceCCA2PrivateKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PrivateKey)1 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1