| 1 |
Secret Sharing Schemes |
| 2 |
====================== |
| 3 |
|
| 4 |
This module implements the Shamir's secret sharing protocol |
| 5 |
described in the paper `"How to share a secret"`__. |
| 6 |
|
| 7 |
The secret can be split into an arbitrary number of shares (``n``), |
| 8 |
such that it is sufficient to collect just ``k`` of them to reconstruct it (``k < n``). |
| 9 |
For instance, one may want to grant 16 people the ability to access a system |
| 10 |
with a pass code, at the condition that at least 3 of them are present at |
| 11 |
the same time. As they join their shares, the pass code is revealed. |
| 12 |
In that case, ``n=16`` and ``k=3``. |
| 13 |
|
| 14 |
In the Shamir's secret sharing scheme, the ``n`` shares are created by first |
| 15 |
defining a polynomial of degree ``k-1``: |
| 16 |
|
| 17 |
:math:`q(x) = a_0 + a_1 x + a_2 x^2 + \ldots + a_{k-1} x^{k-1}` |
| 18 |
|
| 19 |
The coefficient :math:`a_0` is fixed with the secret value. |
| 20 |
The coefficients :math:`a_1 \ldots a_{k-1}` are random and they are discarded as soon as the shares are created. |
| 21 |
|
| 22 |
Each share is a pair :math:`(x_i, y_i)`, where :math:`x_i` is an arbitrary |
| 23 |
but unique number assigned to the share's recipient and :math:`y_i=q(x_i)`. |
| 24 |
|
| 25 |
This implementation has the following properties: |
| 26 |
|
| 27 |
* The secret is a byte string of 16 bytes (e.g. an AES 128 key). |
| 28 |
* Each share is a byte string of 16 bytes. |
| 29 |
* The recipients of the shares are assigned an integer starting from 1 (share number :math:`x_i`). |
| 30 |
* The polynomial :math:`q(x)` is defined over the field GF(:math:`2^{128}`) with |
| 31 |
the same irriducible polynomial as used in AES-GCM: :math:`1 + x + x^2 + x^7 + x^{128}`. |
| 32 |
* It can be compatible with the popular `ssss`_ tool when used with the 128 bit security level |
| 33 |
and no dispersion: the command line arguments must include ``-s 128 -D``. |
| 34 |
Note that ``ssss`` uses a slightly different polynomial: |
| 35 |
|
| 36 |
:math:`r(x) = a_0 + a_1 x + a_2 x^2 + \ldots + a_{k-1} x^{k-1} + x^k` |
| 37 |
|
| 38 |
which requires you to specify ``ssss=True`` when calling ``split()`` and ``combine()``. |
| 39 |
|
| 40 |
Each recipient needs to hold both the share number (:math:`x_i`, which is not confidential) and |
| 41 |
the secret (which needs to be protected securely). |
| 42 |
|
| 43 |
As an example, the following code shows how to protect a file meant |
| 44 |
for 5 people, in such a way that any 2 of them are sufficient to |
| 45 |
reassemble it:: |
| 46 |
|
| 47 |
>>> from binascii import hexlify |
| 48 |
>>> from Crypto.Cipher import AES |
| 49 |
>>> from Crypto.Random import get_random_bytes |
| 50 |
>>> from Crypto.Protocol.SecretSharing import Shamir |
| 51 |
>>> |
| 52 |
>>> key = get_random_bytes(16) |
| 53 |
>>> shares = Shamir.split(2, 5, key) |
| 54 |
>>> for idx, share in shares: |
| 55 |
>>> print "Index #%d: %s" % (idx, hexlify(share)) |
| 56 |
>>> |
| 57 |
>>> with open("clear.txt", "rb") as fi, open("enc.txt", "wb") as fo: |
| 58 |
>>> cipher = AES.new(key, AES.MODE_EAX) |
| 59 |
>>> ct, tag = cipher.encrypt(fi.read()), cipher.digest() |
| 60 |
>>> fo.write(cipher.nonce + tag + ct) |
| 61 |
|
| 62 |
Each person can be given one share and the encrypted file. |
| 63 |
|
| 64 |
When 2 people gather together with their shares, they can |
| 65 |
decrypt the file:: |
| 66 |
|
| 67 |
>>> from binascii import unhexlify |
| 68 |
>>> from Crypto.Cipher import AES |
| 69 |
>>> from Crypto.Protocol.SecretSharing import Shamir |
| 70 |
>>> |
| 71 |
>>> shares = [] |
| 72 |
>>> for x in range(2): |
| 73 |
>>> in_str = raw_input("Enter index and share separated by comma: ") |
| 74 |
>>> idx, share = [ strip(s) for s in in_str.split(",") ] |
| 75 |
>>> shares.append((idx, unhexlify(share))) |
| 76 |
>>> key = Shamir.combine(shares) |
| 77 |
>>> |
| 78 |
>>> with open("enc.txt", "rb") as fi: |
| 79 |
>>> nonce, tag = [ fi.read(16) for x in range(2) ] |
| 80 |
>>> cipher = AES.new(key, AES.MODE_EAX, nonce) |
| 81 |
>>> try: |
| 82 |
>>> result = cipher.decrypt(fi.read()) |
| 83 |
>>> cipher.verify(tag) |
| 84 |
>>> with open("clear2.txt", "wb") as fo: |
| 85 |
>>> fo.write(result) |
| 86 |
>>> except ValueError: |
| 87 |
>>> print "The shares were incorrect" |
| 88 |
|
| 89 |
.. attention:: |
| 90 |
Reconstruction may succeed but still produce the incorrect secret |
| 91 |
if any of the presented shares is incorrect (due to data corruption |
| 92 |
or to a malicious participant). |
| 93 |
|
| 94 |
It is extremely important to also use an authentication mechanism |
| 95 |
(such as the EAX cipher mode in the example). |
| 96 |
|
| 97 |
.. __: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.8910&rep=rep1&type=pdf |
| 98 |
.. _ssss: http://point-at-infinity.org/ssss/ |
| 99 |
|
| 100 |
.. automodule:: Crypto.Protocol.SecretSharing |
| 101 |
:members: |