Search in sources :

Example 1 with McEliecePublicKeyParameters

use of org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters 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 McEliecePublicKeyParameters

use of org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters 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)

Aggregations

McEliecePublicKey (org.bouncycastle.pqc.asn1.McEliecePublicKey)2 McEliecePublicKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McEliecePublicKeyParameters)2 IOException (java.io.IOException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)1 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)1