Search in sources :

Example 1 with ECPoint

use of org.openecard.bouncycastle.math.ec.ECPoint 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 2 with ECPoint

use of org.openecard.bouncycastle.math.ec.ECPoint 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)

Aggregations

ECDomainParameters (org.openecard.bouncycastle.crypto.params.ECDomainParameters)2 ECPublicKeyParameters (org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters)2 ECParameterSpec (org.openecard.bouncycastle.jce.spec.ECParameterSpec)2 ECPoint (org.openecard.bouncycastle.math.ec.ECPoint)2