daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """Affinity test."""
  2. from hashlib import md5
  3. from binascii import crc32
  4. from hypothesis import given
  5. from hypothesis.strategies import integers, lists, tuples
  6.  
  7. # In practice, CRC operations are often started with a nonzero state. Because
  8. # of this, the actual equation is usually of the form:
  9.  
  10. # crc(a)⊕crc(b)=crc(a⊕b)⊕c
  11.  
  12. # for some constant c (which depends on the length of a, b).
  13.  
  14. # An alternative way of expressing this is, for three any equal-length
  15. # bitstrings a,b,ca,b,c, we have:
  16.  
  17. # crc(a)⊕crc(b)⊕crc(c)=crc(a⊕b⊕c)
  18.  
  19. # The technical term for this relationship is affine; in cryptography, we treat
  20. # it as linear because, for attacks that assume linearity, affine works just as
  21. # well.
  22.  
  23. _bin32 = integers(min_value=0, max_value=2**32 - 1)
  24.  
  25.  
  26. def crc32i(a):
  27.     """crc32 an integer."""
  28.     return crc32(a.to_bytes(4, "big"))
  29.  
  30.  
  31. def md532i(a):
  32.     """md5 corresponding to the crc32i, to prove it is not affine."""
  33.     d = md5(a.to_bytes(4, "big")).digest()
  34.     return int.from_bytes(d[:4], "big", signed=True)
  35.  
  36.  
  37. @given(lists(tuples(_bin32, _bin32), min_size=10))
  38. def test_crc_not_affine(data):
  39.     """If crc32 is affine as described above this should fail."""
  40.     cl = map(lambda v: crc32i(v[0]) ^ crc32i(v[1]) ^ crc32i(v[0] ^ v[1]), data)
  41.     # Check if c is constant
  42.     x = next(cl)
  43.     assert all(c == x for c in cl)
  44.  
  45.  
  46. @given(lists(tuples(_bin32, _bin32), min_size=10))
  47. def test_md5_not_affine(data):
  48.     """If crc32 is affine as described above this should fail."""
  49.     cl = map(lambda v: md532i(v[0]) ^ md532i(v[1]) ^ md532i(v[0] ^ v[1]), data)
  50.     # Check if c is constant
  51.     x = next(cl)
  52.     assert all(c == x for c in cl)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top