Changeset - 735b9c2a61e9
[Not reviewed]
default
0 4 0
Laman - 7 years ago 2017-10-01 17:51:06

cli catching thrown exceptions
4 files changed with 21 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/cli.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
from argparse import ArgumentParser
 

	
 
from shamira import generate, reconstruct
 

	
 

	
 
def run():
 
	parser=ArgumentParser()
 
	subparsers=parser.add_subparsers()
 

	
 
	buildSplitParser(subparsers.add_parser("split"))
 
	buildJoinParser(subparsers.add_parser("join"))
 

	
 
	parser.set_defaults(func=lambda: parser.error("missing command"))
 
	parser.set_defaults(func=lambda _: parser.error("missing command"))
 

	
 
	args=parser.parse_args()
 
	args.func(args)
 

	
 

	
 
def buildSplitParser(parser):
 
	parser.add_argument("-k",type=int,required=True,help="number of shares necessary for recovering the secret")
 
	parser.add_argument("-n",type=int,required=True,help="number of generated shares")
 

	
 
	encoding=parser.add_mutually_exclusive_group()
 
	encoding.add_argument("--hex",action="store_true",help="encode shares' bytes as a hexadecimal string")
 
	encoding.add_argument("--b32",action="store_true",help="encode shares' bytes as a base32 string")
 
	encoding.add_argument("--b64",action="store_true",help="encode shares' bytes as a base64 string")
 

	
 
	parser.add_argument("secret",help="secret to be parsed")
 
	parser.set_defaults(func=_generate)
 
	
 

	
 
def buildJoinParser(parser):
 
	encoding=parser.add_mutually_exclusive_group()
 
	encoding.add_argument("--hex",action="store_true",help="decode shares' bytes from a hexadecimal string")
 
	encoding.add_argument("--b32",action="store_true",help="decode shares' bytes from a base32 string")
 
	encoding.add_argument("--b64",action="store_true",help="decode shares' bytes from a base64 string")
 

	
 
	parser.add_argument("-r","--raw",action="store_true",help="return secret as raw bytes")
 
	parser.add_argument("share",nargs="+",help="shares to be joined")
 
	parser.set_defaults(func=_reconstruct)
 

	
 

	
 
def _generate(args):
 
	encoding=getEncoding(args) or "b32"
 

	
 
	shares=generate(args.secret,args.k,args.n,encoding)
 
	for s in shares:
 
		print(s)
 
	try:
 
		shares=generate(args.secret,args.k,args.n,encoding)
 
		for s in shares:
 
			print(s)
 
	except ValueError as e:
 
		print("operation failed: ",e)
 

	
 

	
 
def _reconstruct(args):
 
	encoding=getEncoding(args)
 
	print(reconstruct(*args.share,encoding=encoding,raw=args.raw))
 
	try:
 
		print(reconstruct(*args.share,encoding=encoding,raw=args.raw))
 
	except ValueError as e:
 
		print("operation failed: ",e)
 

	
 

	
 
def getEncoding(args):
 
	if args.hex: return "hex"
 
	elif args.b32: return "b32"
 
	elif args.b64: return "b64"
 
	else: return ""
src/condensed.py
Show inline comments
 
@@ -78,34 +78,39 @@ def reconstruct(*shares):
 

	
 
def decode(share):
 
	try:
 
		(i,_,shareStr)=share.partition(".")
 
		i=int(i)
 
		if not 1<=i<=255:
 
			raise ValueError()
 

	
 
		shareBytes=base64.b32decode(shareStr)
 
		return (i,shareBytes)
 
	except (ValueError,binascii.Error):
 
		raise ValueError('bad share format: share="{0}"'.format(share))
 

	
 
###
 

	
 
from argparse import ArgumentParser
 

	
 

	
 
def run():
 
	parser=ArgumentParser()
 
	subparsers=parser.add_subparsers()
 

	
 
	joiner=subparsers.add_parser("join")
 
	joiner.add_argument("share",nargs="+",help="shares to be joined")
 
	joiner.set_defaults(func=lambda args: print(reconstruct(*args.share)))
 
	joiner.set_defaults(func=_reconstruct)
 

	
 
	parser.set_defaults(func=lambda: parser.error("missing command"))
 

	
 
	args=parser.parse_args()
 
	args.func(args)
 

	
 

	
 
def _reconstruct(args):
 
	try: print(reconstruct(*args.share))
 
	except ValueError as e: print("operation failed: ",e)
 

	
 

	
 
if __name__=="__main__":
 
	run()
src/shamira.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
import os
 
import re
 
import base64
 
import binascii
 

	
 
import gf256
 

	
 

	
 
def _shareByte(secretB,k,n):
 
	assert k<=n<255
 
	if not k<=n<255:
 
		raise ValueError("failing k<=n<255, k={0}, n={1}".format(k,n))
 
	coefs=[int(secretB)]+[int(b) for b in os.urandom(k-1)]
 
	points=[gf256.evaluate(coefs,i) for i in range(1,n+1)]
 
	return points
 

	
 

	
 
def generateRaw(secret,k,n):
 
	"""Splits secret into shares.
 

	
 
	:param secret: (bytes)
 
	:param k: number of shares necessary for secret recovery. 1 <= k <= n
 
	:param n: (int) number of shares generated. 1 <= n < 255
 
	:return: [(i, (bytes) share), ...]"""
 
	shares=[_shareByte(b,k,n) for b in secret]
 
	return [(i+1, bytes([s[i] for s in shares])) for i in range(n)]
 

	
 

	
 
def reconstructRaw(*shares):
 
	"""Tries to recover the secret from its shares.
 

	
 
	:param shares: ((i, (bytes) share), ...)
 
	:return: (bytes) reconstructed secret. Too few shares returns garbage."""
 
	secretLen=len(shares[0][1])
 
	res=[None]*secretLen
 
	for i in range(secretLen):
src/tests/test_shamira.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
import random
 
from unittest import TestCase
 

	
 
from shamira import _shareByte
 
from shamira import *
 

	
 

	
 
class TestShamira(TestCase):
 
	_urandom=os.urandom
 

	
 
	@classmethod
 
	def setUpClass(cls):
 
		random.seed(17)
 
		os.urandom=lambda n: bytes(random.randint(0,255) for i in range(n))
 

	
 
	@classmethod
 
	def tearDownClass(cls):
 
		os.urandom=cls._urandom
 

	
 
	def test_shareByte(self):
 
		with self.assertRaises(AssertionError): # too few shares
 
		with self.assertRaises(ValueError): # too few shares
 
			_shareByte(b"a",5,4)
 
		with self.assertRaises(AssertionError): # too many shares
 
		with self.assertRaises(ValueError): # too many shares
 
			_shareByte(b"a",5,255)
 
		with self.assertRaises(ValueError): # not castable to int
 
			_shareByte("x",2,3)
 

	
 
		vals=_shareByte(ord(b"a"),2,3)
 
		points=list(zip(range(1,256), vals))
 
		self.assertEqual(gf256.getConstantCoef(*points), ord(b"a"))
 
		self.assertEqual(gf256.getConstantCoef(*points[:2]), ord(b"a"))
 
		self.assertNotEqual(gf256.getConstantCoef(*points[:1]), ord(b"a")) # underdetermined => random
 

	
 
	def testGenerateReconstructRaw(self):
 
		for (k,n) in [(2,3), (254,254)]:
 
			shares=generateRaw(b"abcd",k,n)
 
			random.shuffle(shares)
 
			self.assertEqual(reconstructRaw(*shares[:k]), b"abcd")
 
			self.assertNotEqual(reconstructRaw(*shares[:k-1]), b"abcd")
 

	
 
	def testGenerateReconstruct(self):
 
		for encoding in ["hex","b32","b64"]:
 
			for secret in [b"abcd","abcde","ěščřžý"]:
 
				for (k,n) in [(2,3), (254,254)]:
 
					raw=isinstance(secret,bytes)
 
					with self.subTest(enc=encoding,r=raw,sec=secret,k=k,n=n):
 
						shares=generate(secret,k,n,encoding)
0 comments (0 inline, 0 general)