Search in sources :

Example 1 with SPHINCSPlusParameters

use of org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters in project jmulticard by ctt-gob-es.

the class PrivateKeyFactory method createKey.

/**
 * Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
 *
 * @param keyInfo the PrivateKeyInfo object containing the key material
 * @return a suitable private key parameter
 * @throws IOException on an error decoding the key
 */
public static AsymmetricKeyParameter createKey(final PrivateKeyInfo keyInfo) throws IOException {
    final AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
    final ASN1ObjectIdentifier algOID = algId.getAlgorithm();
    if (algOID.on(BCObjectIdentifiers.qTESLA)) {
        final ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
        return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
    }
    if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
        return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
    }
    if (algOID.equals(BCObjectIdentifiers.newHope)) {
        return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
    }
    if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final ASN1BitString pubKey = keyInfo.getPublicKeyData();
        if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
            if (pubKey != null) {
                final byte[] pubEnc = pubKey.getOctets();
                return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), Arrays.copyOfRange(pubEnc, 4, pubEnc.length));
            }
            return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
        }
        if (pubKey != null) {
            final byte[] pubEnc = pubKey.getOctets();
            return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
        }
        return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.sphincsPlus)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SPHINCSPlusParameters spParams = SPHINCSPlusParameters.getParams(Integers.valueOf(Pack.bigEndianToInt(keyEnc, 0)));
        return new SPHINCSPlusPrivateKeyParameters(spParams, Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
    }
    if (algOID.on(BCObjectIdentifiers.pqc_kem_mceliece)) {
        final CMCEPrivateKey cmceKey = CMCEPrivateKey.getInstance(keyInfo.parsePrivateKey());
        final CMCEParameters spParams = Utils.mcElieceParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new CMCEPrivateKeyParameters(spParams, cmceKey.getDelta(), cmceKey.getC(), cmceKey.getG(), cmceKey.getAlpha(), cmceKey.getS());
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_frodo)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final FrodoParameters spParams = Utils.frodoParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new FrodoPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.on(BCObjectIdentifiers.pqc_kem_saber)) {
        final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
        final SABERParameters spParams = Utils.saberParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
        return new SABERPrivateKeyParameters(spParams, keyEnc);
    } else if (algOID.equals(BCObjectIdentifiers.xmss) || algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
        // $NON-NLS-1$
        throw new IOException("Modificacion para JMultiCard");
    } else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
        final McElieceCCA2PrivateKey mKey = McElieceCCA2PrivateKey.getInstance(keyInfo.parsePrivateKey());
        return new McElieceCCA2PrivateKeyParameters(mKey.getN(), mKey.getK(), mKey.getField(), mKey.getGoppaPoly(), mKey.getP(), Utils.getDigestName(mKey.getDigest().getAlgorithm()));
    } else {
        // $NON-NLS-1$
        throw new RuntimeException("algorithm identifier in private key not recognised");
    }
}
Also used : ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) McElieceCCA2PrivateKeyParameters(org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters) QTESLAPrivateKeyParameters(org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters) SABERPrivateKeyParameters(org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters) NHPrivateKeyParameters(org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters) McElieceCCA2PrivateKey(org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey) SPHINCSPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters) SPHINCSPlusParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters) IOException(java.io.IOException) SABERParameters(org.bouncycastle.pqc.crypto.saber.SABERParameters) FrodoPrivateKeyParameters(org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters) ASN1BitString(org.bouncycastle.asn1.ASN1BitString) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) FrodoParameters(org.bouncycastle.pqc.crypto.frodo.FrodoParameters) CMCEParameters(org.bouncycastle.pqc.crypto.cmce.CMCEParameters) CMCEPrivateKeyParameters(org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) SPHINCSPlusPrivateKeyParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusPrivateKeyParameters) CMCEPrivateKey(org.bouncycastle.pqc.asn1.CMCEPrivateKey)

Example 2 with SPHINCSPlusParameters

use of org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters in project jmulticard by ctt-gob-es.

the class SPHINCSPlusKeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof SPHINCSPlusParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a CMCEParameterSpec");
    }
    param = new SPHINCSPlusKeyGenerationParameters(random, (SPHINCSPlusParameters) parameters.get(getNameFromParams(params)));
    engine.init(param);
    initialised = true;
}
Also used : SPHINCSPlusParameterSpec(org.bouncycastle.pqc.jcajce.spec.SPHINCSPlusParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SPHINCSPlusKeyGenerationParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusKeyGenerationParameters) SPHINCSPlusParameters(org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters)

Aggregations

SPHINCSPlusParameters (org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters)2 IOException (java.io.IOException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 ASN1BitString (org.bouncycastle.asn1.ASN1BitString)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 CMCEPrivateKey (org.bouncycastle.pqc.asn1.CMCEPrivateKey)1 McElieceCCA2PrivateKey (org.bouncycastle.pqc.asn1.McElieceCCA2PrivateKey)1 CMCEParameters (org.bouncycastle.pqc.crypto.cmce.CMCEParameters)1 CMCEPrivateKeyParameters (org.bouncycastle.pqc.crypto.cmce.CMCEPrivateKeyParameters)1 FrodoParameters (org.bouncycastle.pqc.crypto.frodo.FrodoParameters)1 FrodoPrivateKeyParameters (org.bouncycastle.pqc.crypto.frodo.FrodoPrivateKeyParameters)1 McElieceCCA2PrivateKeyParameters (org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PrivateKeyParameters)1 NHPrivateKeyParameters (org.bouncycastle.pqc.crypto.newhope.NHPrivateKeyParameters)1 QTESLAPrivateKeyParameters (org.bouncycastle.pqc.crypto.qtesla.QTESLAPrivateKeyParameters)1 SABERParameters (org.bouncycastle.pqc.crypto.saber.SABERParameters)1 SABERPrivateKeyParameters (org.bouncycastle.pqc.crypto.saber.SABERPrivateKeyParameters)1 SPHINCSPrivateKeyParameters (org.bouncycastle.pqc.crypto.sphincs.SPHINCSPrivateKeyParameters)1 SPHINCSPlusKeyGenerationParameters (org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusKeyGenerationParameters)1