def load_sd_salt() -> Optional[bytearray]: salt_auth_key = storage.device.get_sd_salt_auth_key() if salt_auth_key is None: return None salt_path = _get_salt_path() new_salt_path = _get_salt_path(new=True) salt = _load_salt(salt_auth_key, salt_path) if salt is not None: return salt # Check if there is a new salt. salt = _load_salt(salt_auth_key, new_salt_path) if salt is None: # No valid salt file on this SD card. raise WrongSdCard # Normal salt file does not exist, but new salt file exists. That means that # SD salt regeneration was interrupted earlier. Bring into consistent state. # TODO Possibly overwrite salt file with random data. try: fatfs.unlink(salt_path) except fatfs.FatFSError: pass # fatfs.rename can fail with a write error, which falls through as an FatFSError. # This should be handled in calling code, by allowing the user to retry. fatfs.rename(new_salt_path, salt_path) return salt
def commit_sd_salt() -> None: salt_path = _get_salt_path(new=False) new_salt_path = _get_salt_path(new=True) try: fatfs.unlink(salt_path) except fatfs.FatFSError: pass fatfs.rename(new_salt_path, salt_path)