Changeset - 4fa21dbcdb9d
[Not reviewed]
default
9 0 13
Laman - 5 years ago 2020-04-13 14:46:29

created installation script, reorganized directory structure
13 files changed with 39 insertions and 26 deletions:
0 comments (0 inline, 0 general)
.hgignore
Show inline comments
 
new file 100644
 
^\..*
 
\.pyc$
 
^build/
 
^dist/
 
.egg-info/
setup.py
Show inline comments
 
new file 100644
 
from setuptools import setup
 

	
 

	
 
setup(
 
	name="Shamira",
 
	version="0.1",
 
	packages=["shamira"],
 
	package_dir={"": "src"}
 
)
src/shamira/__init__.py
Show inline comments
 
new file 100644
 
from .core import generate, generate_raw, reconstruct, reconstruct_raw
 
from .core import SException, InvalidParams, DetectionException, DecodingException, MalformedShare
src/shamira/__main__.py
Show inline comments
 
new file 100644
 
from . import cli
 

	
 
cli.run()
src/shamira/benchmark.py
Show inline comments
 
file renamed from src/benchmark.py to src/shamira/benchmark.py
 
from argparse import ArgumentParser
 
import cProfile
 
import timeit
 

	
 
from shamira import generate, reconstruct
 
from tests.test_shamira import TestShamira
 
from . import generate, reconstruct
 
from .tests.test_shamira import TestShamira
 

	
 

	
 
def measure(args):
 
	secret = "1234567890123456"
 
	shares = generate(secret, args.k, args.n)
 
	symbols = globals()
 
	symbols.update(locals())
 

	
 
	time = timeit.timeit("""generate(secret, args.k, args.n)""", number=1, globals=symbols)
 
	print("The generation took {0:.3}s, {1:.3} per byte.".format(time, time/16))
 
	print("The generation took {0:.3}s, {1:.3}s per byte.".format(time, time/16))
 

	
 
	time = timeit.timeit("""reconstruct(*shares)""", number=1, globals=symbols)
 
	print("The reconstruction took {0:.3}s, {1:.3} per byte.".format(time, time/16))
 
	print("The reconstruction took {0:.3}s, {1:.3}s per byte.".format(time, time/16))
 

	
 

	
 
def profile(args):
 
	t = TestShamira()
 

	
 
	cProfile.runctx(r"""t.test_generate_reconstruct()""", globals=globals(), locals=locals())
 

	
 

	
 
parser = ArgumentParser()
 
parser.set_defaults(func=lambda _: parser.error("missing command"))
 
subparsers = parser.add_subparsers()
 
def build_subparsers(parent):
 
	parent.set_defaults(func=lambda _: parent.error("missing command"))
 
	subparsers = parent.add_subparsers()
 

	
 
profile_parser = subparsers.add_parser("profile")
 
profile_parser.set_defaults(func=profile)
 

	
 
measure_parser = subparsers.add_parser("measure")
 
measure_parser.add_argument("-k", type=int, required=True)
 
measure_parser.add_argument("-n", type=int, required=True)
 
measure_parser.set_defaults(func=measure)
 

	
 
args = parser.parse_args()
 
args.func(args)
src/shamira/cli.py
Show inline comments
 
file renamed from src/cli.py to src/shamira/cli.py
 
# GNU GPLv3, see LICENSE
 

	
 
import sys
 
from argparse import ArgumentParser
 

	
 
from shamira import generate, reconstruct, SException
 
from .core import generate, reconstruct, SException
 
from .benchmark import build_subparsers as build_benchmark
 

	
 

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

	
 
	build_split_parser(subparsers.add_parser("split"))
 
	build_join_parser(subparsers.add_parser("join"))
 
	build_benchmark(subparsers.add_parser("benchmark"))
 

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

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

	
 

	
 
def build_split_parser(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("--label", help="any label to prefix the shares with")
 
	parser.add_argument("--omit_k_n", action="store_true", help="suppress the default shares prefix")
 

	
 
	parser.add_argument("secret", nargs="?", help="a secret to be split. Can be provided on the command line,"
 
		" redirected through stdin, or will be asked for interactively.")
 
	parser.set_defaults(func=_generate)
 
	
 

	
src/shamira/condensed.py
Show inline comments
 
file renamed from src/condensed.py to src/shamira/condensed.py
src/shamira/core.py
Show inline comments
 
file renamed from src/shamira.py to src/shamira/core.py
 
# GNU GPLv3, see LICENSE
 

	
 
import os
 
import re
 
import base64
 
import binascii
 

	
 
import gf256
 
from . import gf256
 

	
 

	
 
class SException(Exception): pass
 
class InvalidParams(SException): pass
 
class DetectionException(SException): pass
 
class DecodingException(SException): pass
 
class MalformedShare(SException): pass
 

	
 

	
 
def _share_byte(secret_b, k, n):
 
	if not k<=n<255:
 
		raise InvalidParams("Failed k<=n<255, k={0}, n={1}".format(k, n))
 
	# we might be concerned with zero coefficients degenerating our polynomial, but there's no reason - we still need k shares to determine it is the case
 
	coefs = [int(secret_b)]+[int(b) for b in os.urandom(k-1)]
 
	points = [gf256.evaluate(coefs, i) for i in range(1, n+1)]
 
	return points
 

	
 

	
 
def generate_raw(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
 
@@ -103,29 +103,24 @@ def decode(share, encoding="b32"):
 
	try:
 
		(*_, i, share_str) = share.split(".")
 
		i = int(i)
 
		if not 1<=i<=255:
 
			raise MalformedShare("Malformed share: Failed 1<=k<=255, k={0}".format(i))
 
		if encoding=="hex": f = base64.b16decode
 
		elif encoding=="b32": f = base64.b32decode
 
		else: f = base64.b64decode
 
		share_bytes = f(share_str)
 
		return (i, share_bytes)
 
	except (ValueError, binascii.Error):
 
		raise MalformedShare('Malformed share: share="{0}", encoding="{1}"'.format(share, encoding))
 

	
 

	
 
def detect_encoding(shares):
 
	classes = [
 
		(re.compile(r"(.*\.)?\d+\.([0-9A-F]{2})+"), "hex"),
 
		(re.compile(r"(.*\.)?\d+\.([A-Z2-7]{8})*([A-Z2-7]{8}|[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}={1})"), "b32"),
 
		(re.compile(r"(.*\.)?\d+\.([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}={2}|[A-Za-z0-9+/]{3}={1})"), "b64")
 
	]
 
	for (regexp, res) in classes:
 
		if all(regexp.fullmatch(share) for share in shares):
 
			return res
 
	raise DetectionException("No expected encoding detected")
 

	
 

	
 
if __name__=="__main__":
 
	import cli
 
	cli.run()
src/shamira/gf256.py
Show inline comments
 
file renamed from src/gf256.py to src/shamira/gf256.py
src/shamira/tests/__init__.py
Show inline comments
 
file renamed from src/tests/__init__.py to src/shamira/tests/__init__.py
src/shamira/tests/test_condensed.py
Show inline comments
 
file renamed from src/tests/test_condensed.py to src/shamira/tests/test_condensed.py
 
# GNU GPLv3, see LICENSE
 

	
 
import os
 
import random
 
from unittest import TestCase
 

	
 
from gf256 import _gfmul, evaluate
 
from shamira import generate_raw, generate
 
from condensed import *
 
from ..gf256 import _gfmul, evaluate
 
from .. import generate_raw, generate
 
from ..condensed import *
 

	
 

	
 
class TestCondensed(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_gfmul(self):
 
		for a in range(256):
 
			for b in range(256):
 
				self.assertEqual(_gfmul(a, b), gfmul(a, b))
 

	
 
	def test_get_constant_coef(self):
 
		self.assertEqual(get_constant_coef((1, 1), (2, 2), (3, 3)), 0)
 

	
 
		random.seed(17)
 
		random_matches = 0
src/shamira/tests/test_gf256.py
Show inline comments
 
file renamed from src/tests/test_gf256.py to src/shamira/tests/test_gf256.py
 
# GNU GPLv3, see LICENSE
 

	
 
import random
 
import unittest
 
from unittest import TestCase
 

	
 
from gf256 import _gfmul
 
from gf256 import *
 
from ..gf256 import _gfmul
 
from ..gf256 import *
 

	
 

	
 
class TestGF256(TestCase):
 
	def test__gfmul(self):
 
		self.assertEqual(_gfmul(0, 0), 0)
 
		self.assertEqual(_gfmul(1, 1), 1)
 
		self.assertEqual(_gfmul(2, 2), 4)
 
		self.assertEqual(_gfmul(0, 21), 0)
 
		self.assertEqual(_gfmul(0x53, 0xca), 0x01)
 
		self.assertEqual(_gfmul(0xff, 0xff), 0x13)
 

	
 
	def test_gfmul(self):
 
		for a in range(256):
 
			for b in range(256):
 
				self.assertEqual(_gfmul(a, b), gfmul(a, b))
 

	
 
	def test_evaluate(self):
 
		for x in range(256):
 
			(a0, a1, a2, a3) = (x, x>>1, x>>2, x>>3)
 
			self.assertEqual(evaluate([17], x), 17)  # constant polynomial
 
			self.assertEqual(evaluate([a0, a1, a2, a3], 0), x)  # any polynomial at 0
 
			self.assertEqual(evaluate([a0, a1, a2, a3], 1), a0^a1^a2^a3)  # polynomial at 1 == sum of coefficients
 

	
 
	def test_get_constant_coef(self):
src/shamira/tests/test_shamira.py
Show inline comments
 
file renamed from src/tests/test_shamira.py to src/shamira/tests/test_shamira.py
 
# GNU GPLv3, see LICENSE
 

	
 
import os
 
import random
 
from unittest import TestCase
 

	
 
from shamira import _share_byte
 
from shamira import *
 
from .. import *
 
from .. import gf256
 
from ..core import encode, decode,detect_encoding, _share_byte
 

	
 

	
 
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_share_byte(self):
 
		with self.assertRaises(InvalidParams):  # too few shares
 
			_share_byte(b"a", 5, 4)
 
		with self.assertRaises(InvalidParams):  # too many shares
 
			_share_byte(b"a", 5, 255)
 
		with self.assertRaises(ValueError):  # not castable to int
 
			_share_byte("x", 2, 3)
 

	
 
		vals = _share_byte(ord(b"a"), 2, 3)
 
		points = list(zip(range(1, 256), vals))
0 comments (0 inline, 0 general)