test_SecretSharing.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Cryptodome.Util.py3compat import *
  36. from Cryptodome.SelfTest.st_common import list_test_cases
  37. from Cryptodome.Protocol.SecretSharing import Shamir, _Element, \
  38. _mult_gf2, _div_gf2
  39. class GF2_Tests(TestCase):
  40. def test_mult_gf2(self):
  41. # Prove mult by zero
  42. x = _mult_gf2(0,0)
  43. self.assertEqual(x, 0)
  44. # Prove mult by unity
  45. x = _mult_gf2(34, 1)
  46. self.assertEqual(x, 34)
  47. z = 3 # (x+1)
  48. y = _mult_gf2(z, z)
  49. self.assertEqual(y, 5) # (x+1)^2 = x^2 + 1
  50. y = _mult_gf2(y, z)
  51. self.assertEqual(y, 15) # (x+1)^3 = x^3 + x^2 + x + 1
  52. y = _mult_gf2(y, z)
  53. self.assertEqual(y, 17) # (x+1)^4 = x^4 + 1
  54. # Prove linearity works
  55. comps = [1, 4, 128, 2**34]
  56. sum_comps = 1+4+128+2**34
  57. y = 908
  58. z = _mult_gf2(sum_comps, y)
  59. w = 0
  60. for x in comps:
  61. w ^= _mult_gf2(x, y)
  62. self.assertEqual(w, z)
  63. def test_div_gf2(self):
  64. from Cryptodome.Util.number import size as deg
  65. x, y = _div_gf2(567, 7)
  66. self.failUnless(deg(y) < deg(7))
  67. w = _mult_gf2(x, 7) ^ y
  68. self.assertEqual(567, w)
  69. x, y = _div_gf2(7, 567)
  70. self.assertEqual(x, 0)
  71. self.assertEqual(y, 7)
  72. class Element_Tests(TestCase):
  73. def test1(self):
  74. # Test encondings
  75. e = _Element(256)
  76. self.assertEqual(int(e), 256)
  77. self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x00"))
  78. e = _Element(bchr(0)*14 + b("\x01\x10"))
  79. self.assertEqual(int(e), 0x110)
  80. self.assertEqual(e.encode(), bchr(0)*14 + b("\x01\x10"))
  81. # Only 16 byte string are a valid encoding
  82. self.assertRaises(ValueError, _Element, bchr(0))
  83. def test2(self):
  84. # Test addition
  85. e = _Element(0x10)
  86. f = _Element(0x0A)
  87. self.assertEqual(int(e+f), 0x1A)
  88. def test3(self):
  89. # Test multiplication
  90. zero = _Element(0)
  91. one = _Element(1)
  92. two = _Element(2)
  93. x = _Element(6) * zero
  94. self.assertEqual(int(x), 0)
  95. x = _Element(6) * one
  96. self.assertEqual(int(x), 6)
  97. x = _Element(2**127) * two
  98. self.assertEqual(int(x), 1 + 2 + 4 + 128)
  99. def test4(self):
  100. # Test inversion
  101. one = _Element(1)
  102. x = one.inverse()
  103. self.assertEqual(int(x), 1)
  104. x = _Element(82323923)
  105. y = x.inverse()
  106. self.assertEqual(int(x * y), 1)
  107. class Shamir_Tests(TestCase):
  108. def test1(self):
  109. # Test splitting
  110. shares = Shamir.split(2, 3, bchr(90)*16)
  111. self.assertEqual(len(shares), 3)
  112. for index in range(3):
  113. self.assertEqual(shares[index][0], index+1)
  114. self.assertEqual(len(shares[index][1]), 16)
  115. def test2(self):
  116. # Test recombine
  117. from itertools import permutations
  118. test_vectors = (
  119. (2, "d9fe73909bae28b3757854c0af7ad405",
  120. "1-594ae8964294174d95c33756d2504170",
  121. "2-d897459d29da574eb40e93ec552ffe6e",
  122. "3-5823de9bf0e068b054b5f07a28056b1b",
  123. "4-db2c1f8bff46d748f795da995bd080cb"),
  124. (2, "bf4f902d9a7efafd1f3ffd9291fd5de9",
  125. "1-557bd3b0748064b533469722d1cc7935",
  126. "2-6b2717164783c66d47cd28f2119f14d0",
  127. "3-8113548ba97d58256bb4424251ae300c",
  128. "4-179e9e5a218483ddaeda57539139cf04"),
  129. (3, "ec96aa5c14c9faa699354cf1da74e904",
  130. "1-64579fbf1908d66f7239bf6e2b4e41e1",
  131. "2-6cd9428df8017b52322561e8c672ae3e",
  132. "3-e418776ef5c0579bd9299277374806dd",
  133. "4-ab3f77a0107398d23b323e581bb43f5d",
  134. "5-23fe42431db2b41bd03ecdc7ea8e97ac"),
  135. (3, "44cf249b68b80fcdc27b47be60c2c145",
  136. "1-d6515a3905cd755119b86e311c801e31",
  137. "2-16693d9ac9f10c254036ced5f8917fa3",
  138. "3-84f74338a48476b99bf5e75a84d3a0d1",
  139. "4-3fe8878dc4a5d35811cf3cbcd33dbe52",
  140. "5-ad76f92fa9d0a9c4ca0c1533af7f6132"),
  141. (5, "5398717c982db935d968eebe53a47f5a",
  142. "1-be7be2dd4c068e7ef576aaa1b1c11b01",
  143. "2-f821f5848441cb98b3eb467e2733ee21",
  144. "3-25ee52f53e203f6e29a0297b5ab486b5",
  145. "4-fc9fb58ef74dab947fbf9acd9d5d83cd",
  146. "5-b1949cce46d81552e65f248d3f74cc5c",
  147. "6-d64797f59977c4d4a7956ad916da7699",
  148. "7-ab608a6546a8b9af8820ff832b1135c7"),
  149. (5, "4a78db90fbf35da5545d2fb728e87596",
  150. "1-08daf9a25d8aa184cfbf02b30a0ed6a0",
  151. "2-dda28261e36f0b14168c2cf153fb734e",
  152. "3-e9fdec5505d674a57f9836c417c1ecaa",
  153. "4-4dce5636ae06dee42d2c82e65f06c735",
  154. "5-3963dc118afc2ba798fa1d452b28ef00",
  155. "6-6dfe6ff5b09e94d2f84c382b12f42424",
  156. "7-6faea9d4d4a4e201bf6c90b9000630c3"),
  157. (10, "eccbf6d66d680b49b073c4f1ddf804aa",
  158. "01-7d8ac32fe4ae209ead1f3220fda34466",
  159. "02-f9144e76988aad647d2e61353a6e96d5",
  160. "03-b14c3b80179203363922d60760271c98",
  161. "04-770bb2a8c28f6cee89e00f4d5cc7f861",
  162. "05-6e3d7073ea368334ef67467871c66799",
  163. "06-248792bc74a98ce024477c13c8fb5f8d",
  164. "07-fcea4640d2db820c0604851e293d2487",
  165. "08-2776c36fb714bb1f8525a0be36fc7dba",
  166. "09-6ee7ac8be773e473a4bf75ee5f065762",
  167. "10-33657fc073354cf91d4a68c735aacfc8",
  168. "11-7645c65094a5868bf225c516fdee2d0c",
  169. "12-840485aacb8226631ecd9c70e3018086"),
  170. (10, "377e63bdbb5f7d4dc58a483d035212bb",
  171. "01-32c53260103be431c843b1a633afe3bd",
  172. "02-0107eb16cb8695084d452d2cc50bc7d6",
  173. "03-df1e5c66cd755287fb0446faccd72a06",
  174. "04-361bbcd5d40797f49dfa1898652da197",
  175. "05-160d3ad1512f7dec7fd9344aed318591",
  176. "06-659af6d95df4f25beca4fb9bfee3b7e8",
  177. "07-37f3b208977bad50b3724566b72bfa9d",
  178. "08-6c1de2dfc69c2986142c26a8248eb316",
  179. "09-5e19220837a396bd4bc8cd685ff314c3",
  180. "10-86e7b864fb0f3d628e46d50c1ba92f1c",
  181. "11-065d0082c80b1aea18f4abe0c49df72e",
  182. "12-84a09430c1d20ea9f388f3123c3733a3"),
  183. )
  184. def get_share(p):
  185. pos = p.find('-')
  186. return int(p[:pos]), unhexlify(p[pos + 1:])
  187. for tv in test_vectors:
  188. k = tv[0]
  189. secret = unhexlify(tv[1])
  190. max_perms = 10
  191. for perm, shares_idx in enumerate(permutations(range(2, len(tv)), k)):
  192. if perm > max_perms:
  193. break
  194. shares = [ get_share(tv[x]) for x in shares_idx ]
  195. result = Shamir.combine(shares, True)
  196. self.assertEqual(secret, result)
  197. def test3(self):
  198. # Loopback split/recombine
  199. secret = unhexlify(b("000102030405060708090a0b0c0d0e0f"))
  200. shares = Shamir.split(2, 3, secret)
  201. secret2 = Shamir.combine(shares[:2])
  202. self.assertEqual(secret, secret2)
  203. secret3 = Shamir.combine([ shares[0], shares[2] ])
  204. self.assertEqual(secret, secret3)
  205. def test4(self):
  206. # Loopback split/recombine (SSSS)
  207. secret = unhexlify(b("000102030405060708090a0b0c0d0e0f"))
  208. shares = Shamir.split(2, 3, secret, ssss=True)
  209. secret2 = Shamir.combine(shares[:2], ssss=True)
  210. self.assertEqual(secret, secret2)
  211. def test5(self):
  212. # Detect duplicate shares
  213. secret = unhexlify(b("000102030405060708090a0b0c0d0e0f"))
  214. shares = Shamir.split(2, 3, secret)
  215. self.assertRaises(ValueError, Shamir.combine, (shares[0], shares[0]))
  216. def get_tests(config={}):
  217. tests = []
  218. tests += list_test_cases(GF2_Tests)
  219. tests += list_test_cases(Element_Tests)
  220. tests += list_test_cases(Shamir_Tests)
  221. return tests
  222. if __name__ == '__main__':
  223. suite = lambda: TestSuite(get_tests())
  224. main(defaultTest='suite')