Search in sources :

Example 1 with Digest

use of im.actor.runtime.crypto.Digest in project actor-platform by actorapp.

the class TrustedKey method load.

private synchronized void load() {
    if (!isLoaded) {
        isLoaded = true;
        this.key = Hex.fromHex(hexKey);
        byte[] hash = new byte[32];
        Digest sha256 = Crypto.createSHA256();
        sha256.update(key, 0, key.length);
        sha256.doFinal(hash, 0);
        this.keyId = ByteStrings.bytesToLong(hash);
    }
}
Also used : Digest(im.actor.runtime.crypto.Digest)

Example 2 with Digest

use of im.actor.runtime.crypto.Digest 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());
            }
        }
    });
}
Also used : ProtoStruct(im.actor.core.network.mtp.entity.ProtoStruct) PRF(im.actor.runtime.crypto.primitives.prf.PRF) Digest(im.actor.runtime.crypto.Digest) Curve25519KeyPair(im.actor.runtime.crypto.Curve25519KeyPair) IOException(java.io.IOException) ResponseDoDH(im.actor.core.network.mtp.entity.ResponseDoDH) RequestDH(im.actor.core.network.mtp.entity.RequestDH)

Example 3 with Digest

use of im.actor.runtime.crypto.Digest in project actor-platform by actorapp.

the class RatchetKeySignature method hashForSignature.

public static byte[] hashForSignature(long keyId, String keyAlg, byte[] publicKey) {
    byte[] toSign;
    try {
        DataOutput dataOutput = new DataOutput();
        BserWriter writer = new BserWriter(dataOutput);
        writer.writeLong(1, keyId);
        writer.writeString(2, keyAlg);
        Digest sha256 = Crypto.createSHA256();
        sha256.update(publicKey, 0, publicKey.length);
        byte[] hash = new byte[32];
        sha256.doFinal(hash, 0);
        writer.writeBytes(3, hash);
        toSign = dataOutput.toByteArray();
    } catch (Exception e) {
        // Never happens
        return new byte[0];
    }
    return toSign;
}
Also used : DataOutput(im.actor.runtime.bser.DataOutput) Digest(im.actor.runtime.crypto.Digest) BserWriter(im.actor.runtime.bser.BserWriter)

Example 4 with Digest

use of im.actor.runtime.crypto.Digest in project actor-platform by actorapp.

the class Crypto method SHA256.

/**
 * Calculating SHA256
 *
 * @param data source data
 * @return SHA256 of data
 */
public static byte[] SHA256(byte[] data) {
    Digest sha256 = createSHA256();
    sha256.update(data, 0, data.length);
    byte[] res = new byte[32];
    sha256.doFinal(res, 0);
    return res;
}
Also used : KeyDigest(im.actor.runtime.crypto.primitives.digest.KeyDigest) Digest(im.actor.runtime.crypto.Digest)

Example 5 with Digest

use of im.actor.runtime.crypto.Digest in project actor-platform by actorapp.

the class RatchetMasterSecret method calculateMasterSecret.

public static byte[] calculateMasterSecret(RatchetPrivateKey ownIdentity, RatchetPrivateKey ownEphermal, RatchetPublicKey foreignIdentity, RatchetPublicKey foreignEphermal) {
    byte[] ecResult;
    if (ownIdentity.isBigger(foreignIdentity.getKey())) {
        ecResult = ByteStrings.merge(Curve25519.calculateAgreement(ownIdentity.getPrivateKey(), foreignEphermal.getKey()), Curve25519.calculateAgreement(ownEphermal.getPrivateKey(), foreignIdentity.getKey()), Curve25519.calculateAgreement(ownEphermal.getPrivateKey(), foreignEphermal.getKey()));
    } else {
        ecResult = ByteStrings.merge(Curve25519.calculateAgreement(ownEphermal.getPrivateKey(), foreignIdentity.getKey()), Curve25519.calculateAgreement(ownIdentity.getPrivateKey(), foreignEphermal.getKey()), Curve25519.calculateAgreement(ownEphermal.getPrivateKey(), foreignEphermal.getKey()));
    }
    Digest sha256 = Crypto.createSHA256();
    sha256.update(ecResult, 0, ecResult.length);
    byte[] res = new byte[32];
    sha256.doFinal(res, 0);
    return res;
}
Also used : Digest(im.actor.runtime.crypto.Digest)

Aggregations

Digest (im.actor.runtime.crypto.Digest)5 ProtoStruct (im.actor.core.network.mtp.entity.ProtoStruct)1 RequestDH (im.actor.core.network.mtp.entity.RequestDH)1 ResponseDoDH (im.actor.core.network.mtp.entity.ResponseDoDH)1 BserWriter (im.actor.runtime.bser.BserWriter)1 DataOutput (im.actor.runtime.bser.DataOutput)1 Curve25519KeyPair (im.actor.runtime.crypto.Curve25519KeyPair)1 KeyDigest (im.actor.runtime.crypto.primitives.digest.KeyDigest)1 PRF (im.actor.runtime.crypto.primitives.prf.PRF)1 IOException (java.io.IOException)1