def whitener(buf, coef):
    """Whiten and dewhiten data according to the given coefficient."""
    data = bytearray(buf)
    for i, byte in enumerate(data):
        res, mask = (0, 1)
        for _ in range(8):
            if coef & 1:
                coef ^= 0x88
                byte ^= mask
            mask <<= 1
            coef >>= 1
        data[i] = byte ^ res
    return data