Skip to content

Commit

Permalink
pad shamir recovered string with 0s if too small
Browse files Browse the repository at this point in the history
  • Loading branch information
sudssm committed Jan 5, 2016
1 parent 8798608 commit fad67a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 3 additions & 0 deletions crypto/shamir_secret_sharing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import binascii
from secretsharing import SecretSharer
from custom_exceptions.exceptions import SecretReconstructionError
import nacl.secret


def share(secret, threshold, total_shares):
Expand Down Expand Up @@ -35,4 +36,6 @@ def reconstruct(shares):
secret = SecretSharer.recover_secret(shares)
except ValueError:
raise SecretReconstructionError
while len(secret) < nacl.secret.SecretBox.KEY_SIZE * 2:
secret = "0" + secret
return binascii.unhexlify(secret)
6 changes: 2 additions & 4 deletions crypto/tests/test_secret_sharing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import crypto.shamir_secret_sharing
from custom_exceptions import exceptions
from crypto.encryption import generate_key
import pytest


secret = generate_key()
def test_min_shares():
# First share
secret = "FOO BAR WOOHOO!"
shares = crypto.shamir_secret_sharing.share(secret, 2, 5)

# Then attempt to recover
Expand All @@ -15,7 +15,6 @@ def test_min_shares():

def test_max_shares():
# First share
secret = "FOO BAR WOOHOO!"
shares = crypto.shamir_secret_sharing.share(secret, 2, 5)

# Then attempt to recover
Expand All @@ -25,7 +24,6 @@ def test_max_shares():

def test_invalid_share_formatting():
# First share
secret = "FOO BAR WOOHOO!"
shares = crypto.shamir_secret_sharing.share(secret, 2, 5)

# Then corrupt shares
Expand Down

1 comment on commit fad67a8

@DoronShapiro
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better solution is to prepend all secrets with a standard character before sharing (this solution leaks information in the shares whenever it's used).
Alternatively, we can fix the underlying bug in the SS lib: shea256/secret-sharing#16, stacks-archive/python-utilitybelt#6

Please sign in to comment.