test_SecretSharing.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #
  2. # SelfTest/Protocol/test_secret_sharing.py: Self-test for secret sharing protocols
  3. #
  4. # ===================================================================
  5. #
  6. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. # ===================================================================
  33. from unittest import main, TestCase, TestSuite
  34. from binascii import unhexlify, hexlify
  35. from Crypto.Util.py3compat import *
  36. from Crypto.Hash import SHAKE128
  37. from Crypto.SelfTest.st_common import list_test_cases
  38. from Crypto.Protocol.SecretSharing import Shamir, _Element, \
  39. _mult_gf2, _div_gf2
  40. class GF2_Tests(TestCase):
  41. def test_mult_gf2(self):
  42. # Prove mult by zero
  43. x = _mult_gf2(0,0)
  44. self.assertEqual(x, 0)
  45. # Prove mult by unity
  46. x = _mult_gf2(34, 1)
  47. self.assertEqual(x, 34)
  48. z = 3 # (x+1)
  49. y = _mult_gf2(z, z)
  50. self.assertEqual(y, 5) # (x+1)^2 = x^2 + 1
  51. y = _mult_gf2(y, z)
  52. self.assertEqual(y, 15) # (x+1)^3 = x^3 + x^2 + x + 1
  53. y = _mult_gf2(y, z)
  54. self.assertEqual(y, 17) # (x+1)^4 = x^4 + 1
  55. # Prove linearity works
  56. comps = [1, 4, 128, 2**34]
  57. sum_comps = 1+4+128+2**34
  58. y = 908
  59. z = _mult_gf2(sum_comps, y)
  60. w = 0
  61. for x in comps:
  62. w ^= _mult_gf2(x, y)
  63. self.assertEqual(w, z)
  64. def test_div_gf2(self):
  65. from Crypto.Util.number import size as deg
  66. x, y = _div_gf2(567, 7)
  67. self.assertTrue(deg(y) < deg(7))
  68. w = _mult_gf2(x, 7) ^ y
  69. self.assertEqual(567, w)
  70. x, y = _div_gf2(7, 567)
  71. self.assertEqual(x, 0)
  72. self.assertEqual(y, 7)
  73. class Element_Tests(TestCase):
  74. def test1(self):
  75. # Test encondings
  76. e = _Element(256)
  77. self.assertEqual(int(e), 256)
  78. self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x00"))
  79. e = _Element(bchr(0)*14 + b("\x01\x10"))
  80. self.assertEqual(int(e), 0x110)
  81. self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x10"))
  82. # Only 16 byte string are a valid encoding
  83. self.assertRaises(ValueError, _Element, bchr(0))
  84. def test2(self):
  85. # Test addition
  86. e = _Element(0x10)
  87. f = _Element(0x0A)
  88. self.assertEqual(int(e+f), 0x1A)
  89. def test3(self):
  90. # Test multiplication
  91. zero = _Element(0)
  92. one = _Element(1)
  93. two = _Element(2)
  94. x = _Element(6) * zero
  95. self.assertEqual(int(x), 0)
  96. x = _Element(6) * one
  97. self.assertEqual(int(x), 6)
  98. x = _Element(2**127) * two
  99. self.assertEqual(int(x), 1 + 2 + 4 + 128)
  100. def test4(self):
  101. # Test inversion
  102. one = _Element(1)
  103. x = one.inverse()
  104. self.assertEqual(int(x), 1)
  105. x = _Element(82323923)
  106. y = x.inverse()
  107. self.assertEqual(int(x * y), 1)
  108. class Shamir_Tests(TestCase):
  109. def test1(self):
  110. # Test splitting
  111. shares = Shamir.split(2, 3, bchr(90)*16)
  112. self.assertEqual(len(shares), 3)
  113. for index in range(3):
  114. self.assertEqual(shares[index][0], index+1)
  115. self.assertEqual(len(shares[index][1]), 16)
  116. def test2(self):
  117. # Test recombine
  118. from itertools import permutations
  119. # Generated by ssss (index, secret, shares)
  120. # in hex mode, without "diffusion" mode
  121. test_vectors = (
  122. (2, "d9fe73909bae28b3757854c0af7ad405",
  123. "1-594ae8964294174d95c33756d2504170",
  124. "2-d897459d29da574eb40e93ec552ffe6e",
  125. "3-5823de9bf0e068b054b5f07a28056b1b",
  126. "4-db2c1f8bff46d748f795da995bd080cb"),
  127. (2, "bf4f902d9a7efafd1f3ffd9291fd5de9",
  128. "1-557bd3b0748064b533469722d1cc7935",
  129. "2-6b2717164783c66d47cd28f2119f14d0",
  130. "3-8113548ba97d58256bb4424251ae300c",
  131. "4-179e9e5a218483ddaeda57539139cf04"),
  132. (3, "ec96aa5c14c9faa699354cf1da74e904",
  133. "1-64579fbf1908d66f7239bf6e2b4e41e1",
  134. "2-6cd9428df8017b52322561e8c672ae3e",
  135. "3-e418776ef5c0579bd9299277374806dd",
  136. "4-ab3f77a0107398d23b323e581bb43f5d",
  137. "5-23fe42431db2b41bd03ecdc7ea8e97ac"),
  138. (3, "44cf249b68b80fcdc27b47be60c2c145",
  139. "1-d6515a3905cd755119b86e311c801e31",
  140. "2-16693d9ac9f10c254036ced5f8917fa3",
  141. "3-84f74338a48476b99bf5e75a84d3a0d1",
  142. "4-3fe8878dc4a5d35811cf3cbcd33dbe52",
  143. "5-ad76f92fa9d0a9c4ca0c1533af7f6132"),
  144. (5, "5398717c982db935d968eebe53a47f5a",
  145. "1-be7be2dd4c068e7ef576aaa1b1c11b01",
  146. "2-f821f5848441cb98b3eb467e2733ee21",
  147. "3-25ee52f53e203f6e29a0297b5ab486b5",
  148. "4-fc9fb58ef74dab947fbf9acd9d5d83cd",
  149. "5-b1949cce46d81552e65f248d3f74cc5c",
  150. "6-d64797f59977c4d4a7956ad916da7699",
  151. "7-ab608a6546a8b9af8820ff832b1135c7"),
  152. (5, "4a78db90fbf35da5545d2fb728e87596",
  153. "1-08daf9a25d8aa184cfbf02b30a0ed6a0",
  154. "2-dda28261e36f0b14168c2cf153fb734e",
  155. "3-e9fdec5505d674a57f9836c417c1ecaa",
  156. "4-4dce5636ae06dee42d2c82e65f06c735",
  157. "5-3963dc118afc2ba798fa1d452b28ef00",
  158. "6-6dfe6ff5b09e94d2f84c382b12f42424",
  159. "7-6faea9d4d4a4e201bf6c90b9000630c3"),
  160. (10, "eccbf6d66d680b49b073c4f1ddf804aa",
  161. "01-7d8ac32fe4ae209ead1f3220fda34466",
  162. "02-f9144e76988aad647d2e61353a6e96d5",
  163. "03-b14c3b80179203363922d60760271c98",
  164. "04-770bb2a8c28f6cee89e00f4d5cc7f861",
  165. "05-6e3d7073ea368334ef67467871c66799",
  166. "06-248792bc74a98ce024477c13c8fb5f8d",
  167. "07-fcea4640d2db820c0604851e293d2487",
  168. "08-2776c36fb714bb1f8525a0be36fc7dba",
  169. "09-6ee7ac8be773e473a4bf75ee5f065762",
  170. "10-33657fc073354cf91d4a68c735aacfc8",
  171. "11-7645c65094a5868bf225c516fdee2d0c",
  172. "12-840485aacb8226631ecd9c70e3018086"),
  173. (10, "377e63bdbb5f7d4dc58a483d035212bb",
  174. "01-32c53260103be431c843b1a633afe3bd",
  175. "02-0107eb16cb8695084d452d2cc50bc7d6",
  176. "03-df1e5c66cd755287fb0446faccd72a06",
  177. "04-361bbcd5d40797f49dfa1898652da197",
  178. "05-160d3ad1512f7dec7fd9344aed318591",
  179. "06-659af6d95df4f25beca4fb9bfee3b7e8",
  180. "07-37f3b208977bad50b3724566b72bfa9d",
  181. "08-6c1de2dfc69c2986142c26a8248eb316",
  182. "09-5e19220837a396bd4bc8cd685ff314c3",
  183. "10-86e7b864fb0f3d628e46d50c1ba92f1c",
  184. "11-065d0082c80b1aea18f4abe0c49df72e",
  185. "12-84a09430c1d20ea9f388f3123c3733a3"),
  186. )
  187. def get_share(p):
  188. pos = p.find('-')
  189. return int(p[:pos]), unhexlify(p[pos + 1:])
  190. for tv in test_vectors:
  191. k = tv[0]
  192. secret = unhexlify(tv[1])
  193. max_perms = 10
  194. for perm, shares_idx in enumerate(permutations(range(2, len(tv)), k)):
  195. if perm > max_perms:
  196. break
  197. shares = [ get_share(tv[x]) for x in shares_idx ]
  198. result = Shamir.combine(shares, True)
  199. self.assertEqual(secret, result)
  200. def test3(self):
  201. # Loopback split/recombine
  202. rng = SHAKE128.new(b"test3")
  203. for _ in range(100):
  204. secret = rng.read(16)
  205. shares = Shamir.split(2, 3, secret)
  206. secret2 = Shamir.combine(shares[:2])
  207. self.assertEqual(secret, secret2)
  208. secret3 = Shamir.combine([ shares[0], shares[2] ])
  209. self.assertEqual(secret, secret3)
  210. def test4(self):
  211. # Loopback split/recombine (SSSS)
  212. rng = SHAKE128.new(b"test4")
  213. for _ in range(10):
  214. secret = rng.read(16)
  215. shares = Shamir.split(2, 3, secret, ssss=True)
  216. secret2 = Shamir.combine(shares[:2], ssss=True)
  217. self.assertEqual(secret, secret2)
  218. for _ in range(10):
  219. secret = rng.read(16)
  220. shares = Shamir.split(3, 7, secret, ssss=True)
  221. secret2 = Shamir.combine([shares[3], shares[4], shares[6]], ssss=True)
  222. self.assertEqual(secret, secret2)
  223. def test5(self):
  224. # Detect duplicate shares
  225. secret = unhexlify(b("000102030405060708090a0b0c0d0e0f"))
  226. shares = Shamir.split(2, 3, secret)
  227. self.assertRaises(ValueError, Shamir.combine, (shares[0], shares[0]))
  228. def get_tests(config={}):
  229. tests = []
  230. tests += list_test_cases(GF2_Tests)
  231. tests += list_test_cases(Element_Tests)
  232. tests += list_test_cases(Shamir_Tests)
  233. return tests
  234. if __name__ == '__main__':
  235. suite = lambda: TestSuite(get_tests())
  236. main(defaultTest='suite')