All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.hana.datalake.files.shaded.org.bouncycastle.its.bc.BcITSPublicEncryptionKey Maven / Gradle / Ivy

package com.sap.hana.datalake.files.shaded.org.bouncycastle.its.bc;

import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.ASN1Encodable;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.ASN1ObjectIdentifier;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.nist.NISTNamedCurves;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.sec.SECObjectIdentifiers;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.teletrust.TeleTrusTNamedCurves;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.asn1.x9.X9ECParameters;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.crypto.params.ECNamedDomainParameters;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.crypto.params.ECPublicKeyParameters;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.its.ITSPublicEncryptionKey;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.math.ec.ECCurve;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.math.ec.ECPoint;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.BasePublicEncryptionKey;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.EccCurvePoint;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.EccP256CurvePoint;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.EccP384CurvePoint;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.PublicEncryptionKey;
import com.sap.hana.datalake.files.shaded.org.bouncycastle.oer.its.SymmAlgorithm;

public class BcITSPublicEncryptionKey
    extends ITSPublicEncryptionKey
{
    public BcITSPublicEncryptionKey(PublicEncryptionKey encryptionKey)
    {
        super(encryptionKey);
    }

    static PublicEncryptionKey fromKeyParameters(ECPublicKeyParameters pubKey)
    {
        ASN1ObjectIdentifier curveID = ((ECNamedDomainParameters)pubKey.getParameters()).getName();
        ECPoint q  = pubKey.getQ();
        
        if (curveID.equals(SECObjectIdentifiers.secp256r1))
        {
            return new PublicEncryptionKey(
                SymmAlgorithm.aes128Ccm,
                new BasePublicEncryptionKey.Builder()
                    .setChoice(BasePublicEncryptionKey.eciesNistP256)
                    .setValue(EccP256CurvePoint.builder()
                        .createUncompressedP256(
                            q.getAffineXCoord().toBigInteger(),
                            q.getAffineYCoord().toBigInteger()))
                    .createBasePublicEncryptionKey());
        }
        else if (curveID.equals(TeleTrusTObjectIdentifiers.brainpoolP256r1))
        {
            return new PublicEncryptionKey(
                SymmAlgorithm.aes128Ccm,
                new BasePublicEncryptionKey.Builder()
                    .setChoice(BasePublicEncryptionKey.eciesBrainpoolP256r1)
                    .setValue(EccP256CurvePoint.builder()
                        .createUncompressedP256(
                            q.getAffineXCoord().toBigInteger(),
                            q.getAffineYCoord().toBigInteger()))
                    .createBasePublicEncryptionKey());
        }
        else
        {
            throw new IllegalArgumentException("unknown curve in public encryption key");
        }
    }

    public BcITSPublicEncryptionKey(AsymmetricKeyParameter encryptionKey)
    {
        super(fromKeyParameters((ECPublicKeyParameters)encryptionKey));
    }

    public AsymmetricKeyParameter getKey()
    {
        X9ECParameters params;

        BasePublicEncryptionKey baseKey = encryptionKey.getBasePublicEncryptionKey();
        ASN1ObjectIdentifier curveID;

        switch (baseKey.getChoice())
        {
        case BasePublicEncryptionKey.eciesNistP256:
            curveID = SECObjectIdentifiers.secp256r1;
            params = NISTNamedCurves.getByOID(SECObjectIdentifiers.secp256r1);
            break;
        case BasePublicEncryptionKey.eciesBrainpoolP256r1:
            curveID = TeleTrusTObjectIdentifiers.brainpoolP256r1;
            params = TeleTrusTNamedCurves.getByOID(TeleTrusTObjectIdentifiers.brainpoolP256r1);
            break;
        default:
            throw new IllegalStateException("unknown key type");
        }
        ECCurve curve = params.getCurve();

        ASN1Encodable pviCurvePoint = encryptionKey.getBasePublicEncryptionKey().getValue();
        final EccCurvePoint itsPoint;
        if (pviCurvePoint instanceof EccCurvePoint)
        {
            itsPoint = (EccCurvePoint)baseKey.getValue();
        }
        else
        {
            throw new IllegalStateException("extension to public verification key not supported");
        }

        byte[] key;

        if (itsPoint instanceof EccP256CurvePoint)
        {
            key = itsPoint.getEncodedPoint();
        }
        else if (itsPoint instanceof EccP384CurvePoint)
        {
            key = itsPoint.getEncodedPoint();
        }
        else
        {
            throw new IllegalStateException("unknown key type");
        }

        ECPoint point = curve.decodePoint(key).normalize();
        return new ECPublicKeyParameters(point,
            new ECNamedDomainParameters(curveID, params));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy