def iso7816_4_unpad(padded):
    """Removes ISO 7816-4 padding"""

    msg_end = padded.rfind(b'\x80')
    if msg_end == -1 or any(x for x in padded[msg_end+1:]):
        raise ValueError(f'Invalid padding')
    return padded[:msg_end]