logo elektroda
logo elektroda
X
logo elektroda

The simplest (type in) password manager with USB configuration like a memory stick

p.kaczmarek2  0 96 Cool? (+2)
📢 Listen (AI):

TL;DR

  • Built a PIC16LF1459-based USB password manager that acts as a HID keyboard on demand and as a fake flash drive for editing the stored password.
  • The design splits USB support into separate keyboard and mass-storage branches, using usb_service, keyboard_service, and a button on RA5 to switch modes.
  • It stores the password in EERAM and exposes it through a single FAT12 file, while the keyboard sends bytes non-blocking from memory after the button press.
  • The LED indicates the current mode by flash speed, and in practice the device appears as removable storage before typing the saved text automatically.
  • The current version still needs more buttons and safer editing control, and it cannot protect against keylogger-style programs at the operating-system level.
Generated by the language model.

I have previously shown how a simple keyboard[/url] a simple keyboard can be realised on the PIC16LF1659 a simple keyboard and I have shown the simplest emulation of a flash drive with a single file and writing to non-volatile EERAM memory. I will now go one step further and do a practical project based on the experience of the two previous experiments. This will be a device that pretends to be a keyboard and automatically enters a password when a button is pressed. What's more, in passive mode, the device will in turn pretend to be a pen drive and allow you to edit the stored password in a plain text file. The current status of the device will be indicated by the speed of the LED flashing.

Let me remind you of the two previous topics:
- PIC16LF1459 tutorial - USB HID support in the free SDCC compiler - LED, mouse and keyboard
- House-made flash drive from scratch - PIC microcontroller and EERAM memory - no external libraries

Now you can start with the overall roadmap. We already have the two component modules running, now all we need to do is:
- separate the keyboard and flash drive into separate files respectively
- create a common loop function in the main and there decide which USB stack is running
- select a digital pin - for example RA5 - and configure it as a digital input
- in the loop, based on the reading from this pin, switch the state to the keyboard, and when the keyboard finishes sending characters, return to mass memory mode
For simplicity's sake I thought I'd give a hardware substitute to limit the effect of pin oscillation and simply use a capacitor....

In practice it got a bit more complicated. The Mass Storage Device support is based on interrupts, it is reactive, the MSD only responds when asked. For this reason it is implemented in usb_handle_transaction.
The HID Keyboard, on the other hand, is proactive - the keyboard speaks itself and sends pressed keys, it does not need to be asked.
For this reason, we have a generic usb_service handler and additionally a keyboard_service function.
Code: C / C++
Log in, to see the code

Similarly, I have separated other common functions, e.g. descriptor retrieval:
Code: C / C++
Log in, to see the code

or initialisations:
Code: C / C++
Log in, to see the code


This now tells us how separation works and how entering keyboard mode works. What's left is to exit keyboard mode and send keys. The main keyboard loop respects the delays in a non-blocking way and sends the next keys, reading them from EERAM beforehand:
Code: C / C++
Log in, to see the code

Well, and now we return to the main loop shown - this is where the end-of-writing flag is checked:
Code: C / C++
Log in, to see the code

Basically that's it. A few ifs, a button handler, iterating bytes from memory and off we go.

Time for the final presentation.
This is what the data carrier looks like in "This Computer":

Yes in the device manager:
Windows Device Manager screenshot with “USB Mass Storage Device” properties showing value “RAM Drive”.
Yes when opened in Windows Explorer (interesting fact: this file from Dropbox and the system folder do not physically exist in my file table):
Windows File Explorer showing PIC_DRIVE and DATA.TXT opened in Notepad with “Hello world!”
This is how the device works in practice - the video shows the button being pressed, the LED flashing and the text being entered:



... and that intrusive window from Dropbox after typing in text is a good illustration of the device reverting to storage media mode.

In summary , all the hard work boiled down to getting the HID keyboard up and running (that was relatively easy) and a pretend memory stick with FAT12 (that's a bit more difficult), both of which I've covered in previous topics. Here, basically the whole thing came down to cleverly splitting the USB support into two branches and programming the keyboard to send bytes from memory and then return to media mode.
The only question left is - does such a developed device make sense?
In the current version rather not, it would be useful to add a few more buttons and control over the content editing mode (pen drive) may be necessary. Text editing should be on-demand only, perhaps also password protected, just how do you do that without external drivers? Probably better to secure with a button. I can imagine a situation where this kind of device is such a portable password manager, allowing you to conveniently log into an account at school, work or the library. Of course there are off-the-shelf ones, but this is all about DIY. Similarly, a display would be useful.... maybe something with an I2C interface, there are enough pins for that.
Of course, this won't protect against keylogger-type programs on the computer, because they intercept at the operating system level, but that's another matter already.
Maybe this is not the end of it, and I will present another version soon.
The full SDCC project is attached.
Attachments:
  • pic16lf1459_usb_drive_hybrid.zip (182.66 KB) You must be logged in to download this attachment.

About Author
p.kaczmarek2
p.kaczmarek2 wrote 14446 posts with rating 12420 , helped 650 times. Been with us since 2014 year.

Comments

%}