use of org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters in project open-ecard by ecsec.
the class PACEKey method getEncodedCompressedPublicKey.
/**
* Returns the byte encoded compressed public key.
*
* @return Public key
*/
public byte[] getEncodedCompressedPublicKey() {
if (pdp.isDH()) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] input = ((ElGamalPublicKeyParameters) pk).getY().toByteArray();
byte[] compKey = md.digest(input);
return compKey;
} catch (NoSuchAlgorithmException ex) {
logger.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
}
} else if (pdp.isECDH()) {
byte[] compKey = ((ECPublicKeyParameters) pk).getQ().getAffineXCoord().toBigInteger().toByteArray();
return ByteUtils.cutLeadingNullByte(compKey);
} else {
throw new IllegalArgumentException();
}
}
use of org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters 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();
}
}
use of org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters 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();
}
}
use of org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters 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();
}
}
use of org.openecard.bouncycastle.crypto.params.ECPublicKeyParameters in project open-ecard by ecsec.
the class CAKey method getEncodedCompressedPublicKey.
/**
* Returns the byte encoded compressed public key.
*
* @return Public key
*/
public byte[] getEncodedCompressedPublicKey() {
if (cdp.isDH()) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] input = ((ElGamalPublicKeyParameters) pk).getY().toByteArray();
byte[] compKey = md.digest(input);
return compKey;
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
} else if (cdp.isECDH()) {
byte[] compKey = ((ECPublicKeyParameters) pk).getQ().getAffineXCoord().toBigInteger().toByteArray();
return ByteUtils.cutLeadingNullByte(compKey);
} else {
throw new IllegalArgumentException();
}
}
Aggregations