Debian Codegraph @

lib/Crypto/SelfTest/Protocol/test_SecretSharing.py

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