<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _flash_encryption_tweak_key(key, offset, tweak_range):
 """Apply XOR "tweak" values to the key, derived from flash offset 'offset'. This matches the ESP32 hardware flash encryption. tweak_range is a list of bit indexes to apply the tweak to, as generated by _flash_encryption_tweak_range() from the FLASH_CRYPT_CONFIG efuse value. Return tweaked key """

    if esptool.PYTHON2:
        key = [ord(k) for k in key]
    else:
        key = list(key)
    assert len(key) == 32

    offset_bits = [(offset & (1 << x)) != 0 for x in range(24)]

    for bit in tweak_range:
        if offset_bits[_FLASH_ENCRYPTION_TWEAK_PATTERN[bit]]:
            # note that each byte has a backwards bit order, compared
            # to how it is looked up in the tweak pattern table
            key[bit // 8] ^= 1 << (7 - (bit % 8))

    if esptool.PYTHON2:
        return b"".join(chr(k) for k in key)
    else:
        return bytes(key)