public static AsymmetricMasterSecret generateAsymmetricMasterSecret( Context context, MasterSecret masterSecret) { MasterCipher masterCipher = new MasterCipher(masterSecret); ECKeyPair keyPair = Curve.generateKeyPair(); save(context, ASYMMETRIC_LOCAL_PUBLIC_DJB, keyPair.getPublicKey().serialize()); save(context, ASYMMETRIC_LOCAL_PRIVATE_DJB, masterCipher.encryptKey(keyPair.getPrivateKey())); return new AsymmetricMasterSecret(keyPair.getPublicKey(), keyPair.getPrivateKey()); }
private void initializeSessionsV3(SessionState aliceSessionState, SessionState bobSessionState) throws InvalidKeyException { ECKeyPair aliceIdentityKeyPair = Curve.generateKeyPair(); IdentityKeyPair aliceIdentityKey = new IdentityKeyPair( new IdentityKey(aliceIdentityKeyPair.getPublicKey()), aliceIdentityKeyPair.getPrivateKey()); ECKeyPair aliceBaseKey = Curve.generateKeyPair(); ECKeyPair aliceEphemeralKey = Curve.generateKeyPair(); ECKeyPair alicePreKey = aliceBaseKey; ECKeyPair bobIdentityKeyPair = Curve.generateKeyPair(); IdentityKeyPair bobIdentityKey = new IdentityKeyPair( new IdentityKey(bobIdentityKeyPair.getPublicKey()), bobIdentityKeyPair.getPrivateKey()); ECKeyPair bobBaseKey = Curve.generateKeyPair(); ECKeyPair bobEphemeralKey = bobBaseKey; ECKeyPair bobPreKey = Curve.generateKeyPair(); AliceAxolotlParameters aliceParameters = AliceAxolotlParameters.newBuilder() .setOurBaseKey(aliceBaseKey) .setOurIdentityKey(aliceIdentityKey) .setTheirOneTimePreKey(Optional.<ECPublicKey>absent()) .setTheirRatchetKey(bobEphemeralKey.getPublicKey()) .setTheirSignedPreKey(bobBaseKey.getPublicKey()) .setTheirIdentityKey(bobIdentityKey.getPublicKey()) .create(); BobAxolotlParameters bobParameters = BobAxolotlParameters.newBuilder() .setOurRatchetKey(bobEphemeralKey) .setOurSignedPreKey(bobBaseKey) .setOurOneTimePreKey(Optional.<ECKeyPair>absent()) .setOurIdentityKey(bobIdentityKey) .setTheirIdentityKey(aliceIdentityKey.getPublicKey()) .setTheirBaseKey(aliceBaseKey.getPublicKey()) .create(); RatchetingSession.initializeSession(aliceSessionState, 3, aliceParameters); RatchetingSession.initializeSession(bobSessionState, 3, bobParameters); }
public byte[] encryptBytes(byte[] body) { try { ECPublicKey theirPublic = asymmetricMasterSecret.getDjbPublicKey(); ECKeyPair ourKeyPair = Curve.generateKeyPair(); byte[] secret = Curve.calculateAgreement(theirPublic, ourKeyPair.getPrivateKey()); MasterCipher masterCipher = getMasterCipherForSecret(secret); byte[] encryptedBodyBytes = masterCipher.encryptBytes(body); PublicKey ourPublicKey = new PublicKey(31337, ourKeyPair.getPublicKey()); byte[] publicKeyBytes = ourPublicKey.serialize(); return Util.combine(publicKeyBytes, encryptedBodyBytes); } catch (InvalidKeyException e) { throw new AssertionError(e); } }