public static K2Output calculateK2(final byte[] data, final byte[] p) {
        if (data == null || p == null)
            return null;

        final byte[] salt = calculateSalt(SMK2);
        final byte[] t = calculateCMAC(data, salt);

        final byte[] t0 = {};
        final ByteBuffer inputBufferT0 = ByteBuffer.allocate(t0.length + p.length + 1);
        inputBufferT0.put(t0);
        inputBufferT0.put(p);
        inputBufferT0.put((byte) 0x01);
        final byte[] t1 = calculateCMAC(inputBufferT0.array(), t);
        final byte nid = (byte) (t1[15] & 0x7F);

        final ByteBuffer inputBufferT1 = ByteBuffer.allocate(t1.length + p.length + 1);
        inputBufferT1.put(t1);
        inputBufferT1.put(p);
        inputBufferT1.put((byte) 0x02);
        final byte[] encryptionKey = calculateCMAC(inputBufferT1.array(), t);

        final ByteBuffer inputBufferT2 = ByteBuffer.allocate(encryptionKey.length + p.length + 1);
        inputBufferT2.put(encryptionKey);
        inputBufferT2.put(p);
        inputBufferT2.put((byte) 0x03);
        final byte[] privacyKey = calculateCMAC(inputBufferT2.array(), t);

        return new K2Output(nid, encryptionKey, privacyKey);
    }
Calculate k2

@param data network key
@param p    master input
mesh/src/main/java/no/nordicsemi/android/mesh/utils/SecureUtils.java