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;
}
}
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()));
}
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() + ".");
}
Aggregations