test_SecretSharing.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.SelfTest.st_common import list_test_cases
  37. from Crypto.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, 2L**34]
  56. sum_comps = 1+4+128+2L**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 Crypto.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(2L**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 xrange(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. # These shares were obtained with ssss v0.5:
  118. # ssss-split -t 2 -n 3 -s 128 -D -x
  119. secret = b("000102030405060708090a0b0c0d0e0f")
  120. shares = (
  121. (1,"0b8cbb92e2a750defa563537d72942a2"),
  122. (2,"171a7120c941abb4ecb77472ba459753"),
  123. (3,"1c97c8b12fe3fd6d1ee84b4e6161dbfe")
  124. )
  125. bin_shares = []
  126. for share in shares:
  127. bin_shares.append((share[0], unhexlify(b(share[1]))))
  128. result = Shamir.combine(bin_shares)
  129. self.assertEqual(hexlify(result), secret)
  130. def test3(self):
  131. # Loopback split/recombine
  132. secret = unhexlify(b("000102030405060708090a0b0c0d0e0f"))
  133. shares = Shamir.split(2, 3, secret)
  134. secret2 = Shamir.combine(shares[:2])
  135. self.assertEqual(secret, secret2)
  136. secret3 = Shamir.combine([ shares[0], shares[2] ])
  137. self.assertEqual(secret, secret3)
  138. secret4 = Shamir.combine(shares)
  139. self.assertEqual(secret, secret4) # One share too many
  140. def get_tests(config={}):
  141. tests = []
  142. tests += list_test_cases(GF2_Tests)
  143. tests += list_test_cases(Element_Tests)
  144. tests += list_test_cases(Shamir_Tests)
  145. return tests
  146. if __name__ == '__main__':
  147. suite = lambda: TestSuite(get_tests())
  148. main(defaultTest='suite')