I am not a cybersecurity expert, nor am I a cryptography expert. I make absolutely no guarentees about the correctness or security of the implementation provided here. As such, I take no responsibility for losses incurred as a result of using this library/app. Don't roll your own crypto, as they say... You have been warned with abundant clarity and in no uncertain terms!
I thought it would be fun to play around with implementing Shamir's secret sharing scheme in python, having learned about the scheme in UVic's course on cryptography. It is a simple and elegant (yet secure) scheme, which I invite you to appreciate by checking out the source code, and/or by reading about the scheme on Wikipedia.
The practical utility of this scheme is that it provides a means to "break down" a key into shares without comprimising the security of the key that the shares protect. More concretely, you could take a key you wish to protect (say the backup key for your password manager) and split it into 3 shares such that:
- 2 of the 3 shares are required to recover the key.
- an attacker is in no better a position with 1 share than he is with 0 shares. (If an attacker comprimizes fewer than the threshold number of shares, he has not learned any information about the key.)
Setup for the application should be quick and painless:
- It is recommended that you set up a Python virtual environment in the working directory where
sss.pylives so that the needed dependencies can be installed at specific versions. To do so, runpython3 -m venv venvin the same directory thatsss.pylives in. - Run
pipto install the needed dependencies at the specified versions:pip install -r requirements.txt. - If you are a mac user, you may need also need to install
zbarfor full functionality. To do so, it is recommended that zbar be installed usingbrewas follows:brew install zbar.
You should be ready to run sss.py now! It can be invoked as a command line application, or imported as a library. Descriptions of each follow:
- To view the CLI, run
python3 sss.py (-h | --help) - Two example invocations of
sss.pyvia the command line are as follows:- Example 1:
python3 sss.py -s -m "Hello!" --enc_type "qr" 2 3. Shares the message "Hello!" to 3 QR code shares with a recovery threshold of 2. - Example 2:
python3 sss.py --share -f message.txt --enc_type "base64" 3 5. Shares the message whose contents lie in the filemessage.txtto 5 shares with a recovery threshold of 3. - Example 3:
python3 sss.py -r --enc_type "qr" shares_qr. Recovers a message whose shares lie in shares_qr, provided a threshold number of shares or more. - Example 4:
python3 sss.py -r --enc_type "base64" shares_base64/share_1.txt shares_base64/share_2.txt shares_base64/share_3.txt. Recovers the message encoded in example 2 by using 3 of the 5 shares (try using 2 or fewer!).
- Example 1:
Pending! (Don't hold your breath.)