Search in sources :

Example 6 with McElieceCCA2PublicKey

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

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(), MessageDigestUtils.getDigestAlgID(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(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) McElieceCCA2PublicKey(org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 7 with McElieceCCA2PublicKey

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

the class McElieceCCA2KeyFactorySpi method generatePublic.

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

Example 8 with McElieceCCA2PublicKey

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

the class McElieceCCA2KeyFactorySpi method engineGeneratePublic.

/**
 * Converts, if possible, a key specification into a
 * {@link BCMcElieceCCA2PublicKey}. Currently, the following key
 * specifications are supported:
 * {@link X509EncodedKeySpec}.
 *
 * @param keySpec the key specification
 * @return the McEliece CCA2 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.mcElieceCca2.equals(pki.getAlgorithm().getAlgorithm())) {
                McElieceCCA2PublicKey key = McElieceCCA2PublicKey.getInstance(pki.parsePublicKey());
                return new BCMcElieceCCA2PublicKey(new McElieceCCA2PublicKeyParameters(key.getN(), key.getT(), key.getG(), Utils.getDigest(key.getDigest()).getAlgorithmName()));
            } else {
                throw new InvalidKeySpecException("Unable to recognise OID in McEliece private key");
            }
        } catch (IOException cce) {
            throw new InvalidKeySpecException("Unable to decode X509EncodedKeySpec: " + cce.getMessage());
        }
    }
    throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
}
Also used : McElieceCCA2PublicKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) McElieceCCA2PublicKey(org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey)

Aggregations

IOException (java.io.IOException)6 McElieceCCA2PublicKey (com.github.zhenwei.core.pqc.asn1.McElieceCCA2PublicKey)4 McElieceCCA2PublicKey (org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey)4 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)3 McElieceCCA2PublicKeyParameters (com.github.zhenwei.core.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters)3 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)3 McElieceCCA2PublicKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)2 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 SPHINCS256KeyParams (com.github.zhenwei.core.pqc.asn1.SPHINCS256KeyParams)1 XMSSKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSKeyParams)1 XMSSMTKeyParams (com.github.zhenwei.core.pqc.asn1.XMSSMTKeyParams)1 XMSSMTPublicKey (com.github.zhenwei.core.pqc.asn1.XMSSMTPublicKey)1 XMSSPublicKey (com.github.zhenwei.core.pqc.asn1.XMSSPublicKey)1 HSSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.HSSPublicKeyParameters)1 LMSPublicKeyParameters (com.github.zhenwei.core.pqc.crypto.lms.LMSPublicKeyParameters)1