Search in sources :

Example 1 with HMAC

use of im.actor.runtime.crypto.primitives.hmac.HMAC in project actor-platform by actorapp.

the class PRF method calculate.

public byte[] calculate(byte[] secret, String label, byte[] seed, int length) {
    HMAC hmac = new HMAC(secret, digest);
    // PRF(secret: bytes, label: string, seed: bytes) = P_HASH(secret, bytes(label) + seed);
    // P_HASH(secret, seed) = HASH(secret, A(1) + seed) + HASH(secret, A(2) + seed) + HASH(secret, A(3) + seed) + ...
    // where A():
    // A(0) = seed
    // A(i) = HMAC_HASH(secret, A(i-1))
    byte[] rSeed = ByteStrings.merge(label.getBytes(), seed);
    byte[] res = new byte[length];
    byte[] A = rSeed;
    byte[] tHash = new byte[digest.getDigestSize()];
    int offset = 0;
    while (offset * 32 < length) {
        // Update A
        hmac.reset();
        hmac.update(A, 0, A.length);
        hmac.doFinal(tHash, 0);
        A = new byte[digest.getDigestSize()];
        ByteStrings.write(A, 0, tHash, 0, A.length);
        // Writing digest
        digest.reset();
        digest.update(secret, 0, secret.length);
        digest.update(A, 0, A.length);
        digest.update(rSeed, 0, rSeed.length);
        digest.doFinal(tHash, 0);
        ByteStrings.write(res, offset * digest.getDigestSize(), tHash, 0, Math.min(tHash.length, res.length - offset * digest.getDigestSize()));
        offset++;
    }
    return res;
}
Also used : HMAC(im.actor.runtime.crypto.primitives.hmac.HMAC)

Example 2 with HMAC

use of im.actor.runtime.crypto.primitives.hmac.HMAC in project actor-platform by actorapp.

the class HKDF method hkdfExpand.

byte[] hkdfExpand(byte[] prk, byte[] info, int outputSize) {
    byte[] res = new byte[outputSize];
    HMAC hmac = new HMAC(prk, baseDigest);
    hmac.reset();
    byte[] prevHash = new byte[0];
    int offset = 0;
    int index = 0;
    byte[] indexB = new byte[1];
    while (offset < res.length) {
        hmac.reset();
        hmac.update(prevHash, 0, prevHash.length);
        hmac.update(info, 0, info.length);
        indexB[0] = (byte) index;
        hmac.update(indexB, 0, 1);
        byte[] result = new byte[baseDigest.getDigestSize()];
        hmac.doFinal(result, 0);
        int digestSize = baseDigest.getDigestSize();
        int blockLength = Math.min(outputSize - offset, digestSize);
        ByteStrings.write(res, offset, result, 0, blockLength);
        prevHash = result;
        offset += digestSize;
        index++;
    }
    return res;
}
Also used : HMAC(im.actor.runtime.crypto.primitives.hmac.HMAC)

Example 3 with HMAC

use of im.actor.runtime.crypto.primitives.hmac.HMAC in project actor-platform by actorapp.

the class HKDF method hkdfExtract.

byte[] hkdfExtract(byte[] keyMaterial, byte[] salt) {
    HMAC hmac = new HMAC(salt, baseDigest);
    hmac.reset();
    hmac.update(keyMaterial, 0, keyMaterial.length);
    byte[] res = new byte[baseDigest.getDigestSize()];
    hmac.doFinal(res, 0);
    return res;
}
Also used : HMAC(im.actor.runtime.crypto.primitives.hmac.HMAC)

Example 4 with HMAC

use of im.actor.runtime.crypto.primitives.hmac.HMAC in project actor-platform by actorapp.

the class RatchetMessageKey method buildKey.

public static ActorBoxKey buildKey(byte[] rootChainKey, int index) {
    HMAC hmac = new HMAC(rootChainKey, Crypto.createSHA256());
    byte[] indx = ByteStrings.intToBytes(index);
    hmac.update(indx, 0, indx.length);
    byte[] messageKey = new byte[32];
    hmac.doFinal(messageKey, 0);
    byte[] messageKeyExt = new HKDF(Crypto.createSHA256()).deriveSecrets(messageKey, 128);
    byte[] aesCipherKey = ByteStrings.substring(messageKeyExt, 0, 32);
    byte[] aesMacKey = ByteStrings.substring(messageKeyExt, 32, 32);
    byte[] kuzCipherKey = ByteStrings.substring(messageKeyExt, 64, 32);
    byte[] kuzMacKey = ByteStrings.substring(messageKeyExt, 96, 32);
    return new ActorBoxKey(aesCipherKey, aesMacKey, kuzCipherKey, kuzMacKey);
}
Also used : HKDF(im.actor.runtime.crypto.primitives.kdf.HKDF) HMAC(im.actor.runtime.crypto.primitives.hmac.HMAC) ActorBoxKey(im.actor.runtime.crypto.box.ActorBoxKey)

Aggregations

HMAC (im.actor.runtime.crypto.primitives.hmac.HMAC)4 ActorBoxKey (im.actor.runtime.crypto.box.ActorBoxKey)1 HKDF (im.actor.runtime.crypto.primitives.kdf.HKDF)1