Arduino Secure Bootloader
This is a modified arduino caterina bootloader that allows itself to be locked and unlocked from the user space application (or any arduino sketch). This was developed to allow automatic upgrades to our AnyKey device running on kickstarter now AnyKey kickstarter. But it's generic enough to be used on any arduino leonardo or atmega32u4 project as well.
The github repository containing all source code and a hex file to flash to your arduino is here https://github.com/w-A-L-L-e/anykey-bootloader.
How it works
The bootloader was modified to read a certain byte at pos 1023 in the eeprom. Depending on the value stored it either jumps back to the original sketch or continues to operate like a normal bootloader. Theres also a latch value (which is most interesting for firmware upgrades). This basically allows writing once. By adding some more security in the protocol like we have for AnyKey you can basically do a SHA256 challenge response and if it's correct we allow flashing new code once. The bootloader kicks in, sets the bits back into locked position and then proceeds to flash the new firmware. The cool thing is even if the user tries to disrupt the process the bootloader would already be locked again, thereby making this pretty secure.