Search in sources :

Example 1 with McEliecePublicKey

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

the class McElieceKeyFactorySpi method engineGeneratePublic.

/**
 * Converts, if possible, a key specification into a
 * {@link BCMcEliecePublicKey}.  {@link X509EncodedKeySpec}.
 *
 * @param keySpec the key specification
 * @return the McEliece public key
 * @throws InvalidKeySpecException if the key specification is not supported.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof X509EncodedKeySpec) {
        // get the DER-encoded Key according to X.509 from the spec
        byte[] encKey = ((X509EncodedKeySpec) keySpec).getEncoded();
        // decode the SubjectPublicKeyInfo data structure to the pki object
        SubjectPublicKeyInfo pki;
        try {
            pki = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey));
        } catch (IOException e) {
            throw new InvalidKeySpecException(e.toString());
        }
        try {
            if (PQCObjectIdentifiers.mcEliece.equals(pki.getAlgorithm().getAlgorithm())) {
                McEliecePublicKey key = McEliecePublicKey.getInstance(pki.parsePublicKey());
                return new BCMcEliecePublicKey(new McEliecePublicKeyParameters(key.getN(), key.getT(), key.getG()));
            } else {
                throw new InvalidKeySpecException("Unable to recognise OID in McEliece public key");
            }
        } catch (IOException cce) {
            throw new InvalidKeySpecException("Unable to decode X509EncodedKeySpec: " + cce.getMessage());
        }
    }
    throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) McEliecePublicKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) McEliecePublicKey(org.bouncycastle.pqc.asn1.McEliecePublicKey)

Example 2 with McEliecePublicKey

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

the class McElieceKeyFactorySpi method generatePublic.

public PublicKey generatePublic(SubjectPublicKeyInfo pki) throws IOException {
    // get the inner type inside the BIT STRING
    ASN1Primitive innerType = pki.parsePublicKey();
    McEliecePublicKey key = McEliecePublicKey.getInstance(innerType);
    return new BCMcEliecePublicKey(new McEliecePublicKeyParameters(key.getN(), key.getT(), key.getG()));
}
Also used : McEliecePublicKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) McEliecePublicKey(org.bouncycastle.pqc.asn1.McEliecePublicKey)

Example 3 with McEliecePublicKey

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

the class BCMcEliecePublicKey method getEncoded.

/**
 * Return the keyData to encode in the SubjectPublicKeyInfo structure.
 * <p>
 * The ASN.1 definition of the key structure is
 * </p>
 * <pre>
 *       McEliecePublicKey ::= SEQUENCE {
 *         n           Integer      -- length of the code
 *         t           Integer      -- error correcting capability
 *         matrixG     OctetString  -- generator matrix as octet string
 *       }
 * </pre>
 *
 * @return the keyData to encode in the SubjectPublicKeyInfo structure
 */
public byte[] getEncoded() {
    McEliecePublicKey key = new McEliecePublicKey(params.getN(), params.getT(), params.getG());
    AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcEliece);
    try {
        SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algorithmIdentifier, key);
        return subjectPublicKeyInfo.getEncoded();
    } catch (IOException e) {
        return null;
    }
}
Also used : IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) McEliecePublicKey(com.github.zhenwei.core.pqc.asn1.McEliecePublicKey) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 4 with McEliecePublicKey

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

the class BCMcElieceCCA2PublicKey method getEncoded.

/**
 * Return the keyData to encode in the SubjectPublicKeyInfo structure.
 * <p>
 * The ASN.1 definition of the key structure is
 * <pre>
 *       McEliecePublicKey ::= SEQUENCE {
 *         n           Integer      -- length of the code
 *         t           Integer      -- error correcting capability
 *         matrixG     OctetString  -- generator matrix as octet string
 *       }
 * </pre>
 *
 * @return the keyData to encode in the SubjectPublicKeyInfo structure
 */
public byte[] getEncoded() {
    McElieceCCA2PublicKey key = new McElieceCCA2PublicKey(params.getN(), params.getT(), params.getG(), Utils.getDigAlgId(params.getDigest()));
    AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcElieceCca2);
    try {
        SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algorithmIdentifier, key);
        return subjectPublicKeyInfo.getEncoded();
    } catch (IOException e) {
        return null;
    }
}
Also used : IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) McElieceCCA2PublicKey(com.github.zhenwei.core.pqc.asn1.McElieceCCA2PublicKey) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 5 with McEliecePublicKey

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

the class BCMcEliecePublicKey method getEncoded.

/**
 * Return the keyData to encode in the SubjectPublicKeyInfo structure.
 * <p>
 * The ASN.1 definition of the key structure is
 * </p>
 * <pre>
 *       McEliecePublicKey ::= SEQUENCE {
 *         n           Integer      -- length of the code
 *         t           Integer      -- error correcting capability
 *         matrixG     OctetString  -- generator matrix as octet string
 *       }
 * </pre>
 * @return the keyData to encode in the SubjectPublicKeyInfo structure
 */
public byte[] getEncoded() {
    McEliecePublicKey key = new McEliecePublicKey(params.getN(), params.getT(), params.getG());
    AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.mcEliece);
    try {
        SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algorithmIdentifier, key);
        return subjectPublicKeyInfo.getEncoded();
    } catch (IOException e) {
        return null;
    }
}
Also used : IOException(java.io.IOException) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) McEliecePublicKey(org.bouncycastle.pqc.asn1.McEliecePublicKey) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Aggregations

IOException (java.io.IOException)5 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)3 McEliecePublicKey (com.github.zhenwei.core.pqc.asn1.McEliecePublicKey)3 McEliecePublicKey (org.bouncycastle.pqc.asn1.McEliecePublicKey)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 McEliecePublicKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McEliecePublicKeyParameters)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)2 McEliecePublicKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters)2 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 McElieceCCA2PublicKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PublicKey)1 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1