Skip to content

Instantly share code, notes, and snippets.

@ralt
Last active September 7, 2016 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralt/ee1f05fd969ae98bea0f to your computer and use it in GitHub Desktop.
Save ralt/ee1f05fd969ae98bea0f to your computer and use it in GitHub Desktop.
Lossless token change

Some information:

  • fingerprint is 5 bytes, and is at position 0 on device. There's no fingerprint on user file.
  • a token is 128 bytes

  • on device, fseek to position 133 (i.e. after the fingerprint + token). Write the fingerprint + the old token. If power is cut during the writing, or after, the next login will be fine. (successful login if there's a failure = old token at pos 5 on device + old token at pos 0 on user file)

  • once the old token is written, fseek back to position 5 (right after the fingerprint), and write the new token. If power is cut during the writing or after, the next login will fail, look for a fingerprint, if there is, compare with the token after. Since it's the old one, the login will be fine. (successful login if there's a failure = old token at pos 138 on device + old token at position 0 on user file)

  • on user file, fseek to position 128 (right after the token), write fingerprint + old token. If it fails, same scenario as the previous line. (successful login if there's a failure = old token at pos 138 on device + old token at pos 0 on user file)

  • once that's done, fseek to position 0 and write the new token. If power is cut during this time, the login will fail, look for fingerprint on device at position 133, find the old token, login will fail again, look for the fingerprint on user file at position 128, find the old token, and login will work. (successful login if there's a failure = old token at pos 138 on device + old token at pos 128 on user file)

  • at this point, fill with zeroes the redundant fingerprints and old tokens from both the device and the user file. If this fails, the next login will work right away, and the process will be repeated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment