Search in sources :

Example 1 with ECParameterSpec

use of org.openecard.bouncycastle.jce.spec.ECParameterSpec in project open-ecard by ecsec.

the class PACEKey method generateKeyPair.

/**
 * Generate a key pair.
 */
public void generateKeyPair() {
    reseed();
    if (pdp.isDH()) {
        ElGamalParameterSpec p = (ElGamalParameterSpec) pdp.getParameter();
        int numBits = p.getG().bitLength();
        BigInteger d = new BigInteger(numBits, rand);
        ElGamalParameters egp = new ElGamalParameters(p.getP(), p.getG());
        sk = new ElGamalPrivateKeyParameters(d, egp);
        pk = new ElGamalPublicKeyParameters(egp.getG().multiply(d), egp);
    } else if (pdp.isECDH()) {
        ECParameterSpec p = (ECParameterSpec) pdp.getParameter();
        int numBits = p.getN().bitLength();
        BigInteger d = new BigInteger(numBits, rand);
        ECDomainParameters ecp = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
        sk = new ECPrivateKeyParameters(d, ecp);
        pk = new ECPublicKeyParameters(ecp.getG().multiply(d), ecp);
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ECPrivateKeyParameters(org.openecard.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.openecard.bouncycastle.crypto.params.ECDomainParameters) ElGamalParameters(org.openecard.bouncycastle.crypto.params.ElGamalParameters) ElGamalPrivateKeyParameters(org.openecard.bouncycastle.crypto.params.ElGamalPrivateKeyParameters) ECParameterSpec(org.openecard.bouncycastle.jce.spec.ECParameterSpec) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ElGamalPublicKeyParameters) ECPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters) ElGamalParameterSpec(org.openecard.bouncycastle.jce.spec.ElGamalParameterSpec) ECPoint(org.openecard.bouncycastle.math.ec.ECPoint)

Example 2 with ECParameterSpec

use of org.openecard.bouncycastle.jce.spec.ECParameterSpec in project open-ecard by ecsec.

the class PACEKey method decodePublicKey.

/**
 * Decodes a public key from a byte array.
 *
 * @param data Encoded key
 * @return Decoded key
 * @throws Exception
 */
public byte[] decodePublicKey(byte[] data) throws Exception {
    byte[] keyBytes;
    if (data[0] == (byte) 0x7C) {
        keyBytes = TLV.fromBER(data).getChild().getValue();
    } else if (data[0] != 04) {
        keyBytes = ByteUtils.concatenate((byte) 0x04, data);
    } else {
        keyBytes = data;
    }
    if (pdp.isECDH()) {
        ECParameterSpec p = (ECParameterSpec) pdp.getParameter();
        ECDomainParameters ecp = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
        ECPoint q = p.getCurve().decodePoint(keyBytes);
        pk = new ECPublicKeyParameters(q, ecp);
        return getEncodedPublicKey();
    } else if (pdp.isDH()) {
        logger.error("Not implemented yet.");
        throw new UnsupportedOperationException("Not implemented yet.");
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ECDomainParameters(org.openecard.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.openecard.bouncycastle.jce.spec.ECParameterSpec) ECPoint(org.openecard.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 3 with ECParameterSpec

use of org.openecard.bouncycastle.jce.spec.ECParameterSpec in project open-ecard by ecsec.

the class ExplicitDomainParameters method loadECDHParameter.

private void loadECDHParameter(ASN1Sequence seq) {
    // ASN1Integer version = (ASN1Integer) seq.getObjectAt(0);
    // ASN1Sequence modulus = (ASN1Sequence) seq.getObjectAt(1);
    // ASN1Sequence coefficient = (ASN1Sequence) seq.getObjectAt(2);
    // ASN1OctetString basepoint = (ASN1OctetString) seq.getObjectAt(3);
    ASN1Integer order = (ASN1Integer) seq.getObjectAt(4);
    ASN1Integer cofactor = (ASN1Integer) seq.getObjectAt(5);
    try {
        // BigInteger p = new BigInteger(modulus.getObjectAt(1).toASN1Primitive().getEncoded());
        // BigInteger a = new BigInteger(coefficient.getObjectAt(0).toASN1Primitive().getEncoded());
        // BigInteger b = new BigInteger(coefficient.getObjectAt(1).toASN1Primitive().getEncoded());
        BigInteger r = order.getValue();
        BigInteger f = cofactor.getValue();
        X9ECParameters ECParameters = X9ECParameters.getInstance(seq);
        ECCurve curve = ECParameters.getCurve();
        domainParameter = new ECParameterSpec(curve, ECParameters.getG(), r, f);
    } catch (Exception e) {
        _logger.error("Failed to load proprietary domain parameters", e);
    }
}
Also used : X9ECParameters(org.openecard.bouncycastle.asn1.x9.X9ECParameters) ECParameterSpec(org.openecard.bouncycastle.jce.spec.ECParameterSpec) ECCurve(org.openecard.bouncycastle.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1Integer(org.openecard.bouncycastle.asn1.ASN1Integer)

Example 4 with ECParameterSpec

use of org.openecard.bouncycastle.jce.spec.ECParameterSpec in project open-ecard by ecsec.

the class CAKey method decodePublicKey.

/**
 * Decodes a public key from a byte array.
 *
 * @param data Encoded key
 * @return Decoded key
 * @throws TLVException
 * @throws IllegalArgumentException
 */
public byte[] decodePublicKey(byte[] data) throws TLVException {
    byte[] keyBytes;
    if (data[0] == (byte) 0x7C) {
        keyBytes = TLV.fromBER(data).getChild().getValue();
    } else if (data[0] != 04) {
        keyBytes = ByteUtils.concatenate((byte) 0x04, data);
    } else {
        keyBytes = data;
    }
    if (cdp.isECDH()) {
        ECParameterSpec p = (ECParameterSpec) cdp.getParameter();
        ECDomainParameters ecp = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
        ECPoint q = p.getCurve().decodePoint(keyBytes);
        pk = new ECPublicKeyParameters(q, ecp);
        return getEncodedPublicKey();
    } else if (cdp.isDH()) {
        // TODO
        logger.error("Not implemented yet.");
        throw new UnsupportedOperationException("Not implemented yet.");
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ECDomainParameters(org.openecard.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.openecard.bouncycastle.jce.spec.ECParameterSpec) ECPoint(org.openecard.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 5 with ECParameterSpec

use of org.openecard.bouncycastle.jce.spec.ECParameterSpec in project open-ecard by ecsec.

the class CAKey method generateKeyPair.

/**
 * Generate a key pair.
 */
public void generateKeyPair() {
    reseed();
    if (cdp.isDH()) {
        ElGamalParameterSpec p = (ElGamalParameterSpec) cdp.getParameter();
        int numBits = p.getG().bitLength();
        BigInteger d = new BigInteger(numBits, rand);
        ElGamalParameters egp = new ElGamalParameters(p.getP(), p.getG());
        sk = new ElGamalPrivateKeyParameters(d, egp);
        pk = new ElGamalPublicKeyParameters(egp.getG().multiply(d), egp);
    } else if (cdp.isECDH()) {
        ECParameterSpec p = (ECParameterSpec) cdp.getParameter();
        int numBits = p.getN().bitLength();
        BigInteger d = new BigInteger(numBits, rand);
        ECDomainParameters ecp = new ECDomainParameters(p.getCurve(), p.getG(), p.getN(), p.getH());
        sk = new ECPrivateKeyParameters(d, ecp);
        pk = new ECPublicKeyParameters(ecp.getG().multiply(d), ecp);
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ECPrivateKeyParameters(org.openecard.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.openecard.bouncycastle.crypto.params.ECDomainParameters) ElGamalParameters(org.openecard.bouncycastle.crypto.params.ElGamalParameters) ElGamalPrivateKeyParameters(org.openecard.bouncycastle.crypto.params.ElGamalPrivateKeyParameters) ECParameterSpec(org.openecard.bouncycastle.jce.spec.ECParameterSpec) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ElGamalPublicKeyParameters) ECPublicKeyParameters(org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters) ElGamalParameterSpec(org.openecard.bouncycastle.jce.spec.ElGamalParameterSpec) ECPoint(org.openecard.bouncycastle.math.ec.ECPoint)

Aggregations

ECParameterSpec (org.openecard.bouncycastle.jce.spec.ECParameterSpec)5 ECDomainParameters (org.openecard.bouncycastle.crypto.params.ECDomainParameters)4 ECPublicKeyParameters (org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters)4 ECPoint (org.openecard.bouncycastle.math.ec.ECPoint)4 BigInteger (java.math.BigInteger)3 ECPrivateKeyParameters (org.openecard.bouncycastle.crypto.params.ECPrivateKeyParameters)2 ElGamalParameters (org.openecard.bouncycastle.crypto.params.ElGamalParameters)2 ElGamalPrivateKeyParameters (org.openecard.bouncycastle.crypto.params.ElGamalPrivateKeyParameters)2 ElGamalPublicKeyParameters (org.openecard.bouncycastle.crypto.params.ElGamalPublicKeyParameters)2 ElGamalParameterSpec (org.openecard.bouncycastle.jce.spec.ElGamalParameterSpec)2 ASN1Integer (org.openecard.bouncycastle.asn1.ASN1Integer)1 X9ECParameters (org.openecard.bouncycastle.asn1.x9.X9ECParameters)1 ECCurve (org.openecard.bouncycastle.math.ec.ECCurve)1