use of im.actor.core.network.mtp.entity.ResponseDoDH in project actor-platform by actorapp.
the class AuthKeyActor method gotoDHState.
private void gotoDHState(final long keyId, final byte[] key, final byte[] serverNonce) {
final byte[] clientNonce = new byte[32];
Crypto.nextBytes(clientNonce);
byte[] keyMaterial = new byte[32];
Crypto.nextBytes(keyMaterial);
final Curve25519KeyPair clientKeyPair = Curve25519.keyGen(keyMaterial);
goToState(new ActorState() {
@Override
public ProtoStruct sendStartMessage() throws IOException {
Log.d(TAG, "Sending RequestDH");
return new RequestDH(randomId, keyId, clientNonce, clientKeyPair.getPublicKey());
}
@Override
public void onMessage(ProtoStruct struct) throws IOException {
if (struct instanceof ResponseDoDH) {
Log.d(TAG, "Received ResponseDoDH");
ResponseDoDH r = (ResponseDoDH) struct;
if (r.getRandomId() != randomId) {
throw new IOException("Incorrect RandomId");
}
PRF combinedPrf = Cryptos.PRF_SHA_STREEBOG_256();
byte[] nonce = ByteStrings.merge(clientNonce, serverNonce);
byte[] pre_master_secret = Curve25519.calculateAgreement(clientKeyPair.getPrivateKey(), key);
byte[] master_secret = combinedPrf.calculate(pre_master_secret, "master secret", nonce, 256);
byte[] verify = combinedPrf.calculate(master_secret, "client finished", nonce, 256);
if (!Curve25519.verifySignature(key, verify, r.getVerifySign())) {
throw new IOException("Incorrect Signature");
}
Digest sha256 = Crypto.createSHA256();
sha256.update(master_secret, 0, master_secret.length);
byte[] authIdHash = new byte[32];
sha256.doFinal(authIdHash, 0);
long authId = ByteStrings.bytesToLong(authIdHash);
Log.d(TAG, "Key successfully created #" + authId);
gotoSuccess(master_secret, authId);
} else {
throw new IOException("Expected: ResponseGetServerKey, got: " + struct.getClass().getName());
}
}
});
}
Aggregations