public ECDHLightContext(ECDH.Curves curve) { this.ecdh = new ECDH(curve); this.lastUsedTime = System.currentTimeMillis(); }
public ECPublicKey getPublicKey() { return ecdh.getPublicKey(); }
@Override public byte[] getPublicKeyNetworkFormat() { return ecdh.getPublicKeyNetworkFormat(); } }
public static void main(String[] args) throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException { Security.addProvider(new BouncyCastleProvider()); ECDH alice = new ECDH(Curves.P256); ECDH bob = new ECDH(Curves.P256); PublicKey bobP = bob.getPublicKey(); PublicKey aliceP = alice.getPublicKey(); System.out.println("Alice C: "+alice.curve); System.out.println("Bob C: "+bob.curve); System.out.println("Alice P: "+toHex(aliceP.getEncoded())); System.out.println("Bob P: "+toHex(bobP.getEncoded())); System.out.println("Alice S: "+toHex(alice.getAgreedSecret(bob.getPublicKey()))); System.out.println("Bob S: "+toHex(bob.getAgreedSecret(alice.getPublicKey()))); }
public void testGetAgreedSecret() throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { byte[] aliceS = alice.getAgreedSecret(bob.getPublicKey()); byte[] bobS = bob.getAgreedSecret(alice.getPublicKey()); assertNotNull(aliceS); assertNotNull(bobS); assertEquals(toHex(aliceS), toHex(bobS)); assertEquals(aliceS.length, curveToTest.derivedSecretSize); assertEquals(bobS.length, curveToTest.derivedSecretSize); }
ECDH.blockingInit(); } else { this.random = r;
public byte[] getHMACKey(ECPublicKey peerExponential) { synchronized(this) { lastUsedTime = System.currentTimeMillis(); } byte[] sharedKey = ecdh.getAgreedSecret(peerExponential); if (logMINOR) { Logger.minor(this, "Curve in use: " + ecdh.curve.toString()); if(logDEBUG) { Logger.debug(this, "My exponential: " + HexUtil.bytesToHex(ecdh.getPublicKey().getEncoded())); Logger.debug( this, "Peer's exponential: " + HexUtil.bytesToHex(peerExponential.getEncoded())); Logger.debug(this, "SharedSecret = " + HexUtil.bytesToHex(sharedKey)); } } return sharedKey; }
/** Return the public key as a byte[] in network format */ public byte[] getPublicKeyNetworkFormat() { byte[] ret = getPublicKey().getEncoded(); if(ret.length == curve.modulusSize) { return ret; } else if(ret.length > curve.modulusSize) { throw new IllegalStateException("Encoded public key too long: should be "+curve.modulusSize+" bytes but is "+ret.length); } else { Logger.warning(this, "Padding public key from "+ret.length+" to "+curve.modulusSize+" bytes"); byte[] out = new byte[curve.modulusSize]; System.arraycopy(ret, 0, out, 0, ret.length); return ret; } } }
public void testGetPublicKey() { PublicKey aliceP = alice.getPublicKey(); PublicKey bobP = bob.getPublicKey(); assertNotNull(aliceP); assertNotSame(aliceP, bobP); assertEquals(aliceP.getEncoded().length, curveToTest.modulusSize); assertEquals(bobP.getEncoded().length, curveToTest.modulusSize); }
byte[] sig = crypto.ecdsaSign(toSign); byte[] computedExponential=((ECDHLightContext)ctx).getHMACKey(ECDH.getPublicKey(hisExponential, ecdhCurveToUse));
ECPublicKey initiatorKey = ECDH.getPublicKey(initiatorExponential, ecdhCurveToUse); ECPublicKey responderKey = ECDH.getPublicKey(responderExponential, ecdhCurveToUse); ECDHLightContext ctx = findECDHContextByPubKey(responderKey); if (ctx == null) {