Chaffing.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #
  2. # Chaffing.py : chaffing & winnowing support
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # Written by Andrew M. Kuchling, Barry A. Warsaw, and others
  7. #
  8. # ===================================================================
  9. # The contents of this file are dedicated to the public domain. To
  10. # the extent that dedication to the public domain is not available,
  11. # everyone is granted a worldwide, perpetual, royalty-free,
  12. # non-exclusive license to exercise all rights associated with the
  13. # contents of this file for any purpose whatsoever.
  14. # No rights are reserved.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # ===================================================================
  25. #
  26. """This file implements the chaffing algorithm.
  27. Winnowing and chaffing is a technique for enhancing privacy without requiring
  28. strong encryption. In short, the technique takes a set of authenticated
  29. message blocks (the wheat) and adds a number of chaff blocks which have
  30. randomly chosen data and MAC fields. This means that to an adversary, the
  31. chaff blocks look as valid as the wheat blocks, and so the authentication
  32. would have to be performed on every block. By tailoring the number of chaff
  33. blocks added to the message, the sender can make breaking the message
  34. computationally infeasible. There are many other interesting properties of
  35. the winnow/chaff technique.
  36. For example, say Alice is sending a message to Bob. She packetizes the
  37. message and performs an all-or-nothing transformation on the packets. Then
  38. she authenticates each packet with a message authentication code (MAC). The
  39. MAC is a hash of the data packet, and there is a secret key which she must
  40. share with Bob (key distribution is an exercise left to the reader). She then
  41. adds a serial number to each packet, and sends the packets to Bob.
  42. Bob receives the packets, and using the shared secret authentication key,
  43. authenticates the MACs for each packet. Those packets that have bad MACs are
  44. simply discarded. The remainder are sorted by serial number, and passed
  45. through the reverse all-or-nothing transform. The transform means that an
  46. eavesdropper (say Eve) must acquire all the packets before any of the data can
  47. be read. If even one packet is missing, the data is useless.
  48. There's one twist: by adding chaff packets, Alice and Bob can make Eve's job
  49. much harder, since Eve now has to break the shared secret key, or try every
  50. combination of wheat and chaff packet to read any of the message. The cool
  51. thing is that Bob doesn't need to add any additional code; the chaff packets
  52. are already filtered out because their MACs don't match (in all likelihood --
  53. since the data and MACs for the chaff packets are randomly chosen it is
  54. possible, but very unlikely that a chaff MAC will match the chaff data). And
  55. Alice need not even be the party adding the chaff! She could be completely
  56. unaware that a third party, say Charles, is adding chaff packets to her
  57. messages as they are transmitted.
  58. For more information on winnowing and chaffing see this paper:
  59. Ronald L. Rivest, "Chaffing and Winnowing: Confidentiality without Encryption"
  60. http://theory.lcs.mit.edu/~rivest/chaffing.txt
  61. """
  62. __revision__ = "$Id$"
  63. from Crypto.Util.number import bytes_to_long
  64. class Chaff:
  65. """Class implementing the chaff adding algorithm.
  66. Methods for subclasses:
  67. _randnum(size):
  68. Returns a randomly generated number with a byte-length equal
  69. to size. Subclasses can use this to implement better random
  70. data and MAC generating algorithms. The default algorithm is
  71. probably not very cryptographically secure. It is most
  72. important that the chaff data does not contain any patterns
  73. that can be used to discern it from wheat data without running
  74. the MAC.
  75. """
  76. def __init__(self, factor=1.0, blocksper=1):
  77. """Chaff(factor:float, blocksper:int)
  78. factor is the number of message blocks to add chaff to,
  79. expressed as a percentage between 0.0 and 1.0. blocksper is
  80. the number of chaff blocks to include for each block being
  81. chaffed. Thus the defaults add one chaff block to every
  82. message block. By changing the defaults, you can adjust how
  83. computationally difficult it could be for an adversary to
  84. brute-force crack the message. The difficulty is expressed
  85. as:
  86. pow(blocksper, int(factor * number-of-blocks))
  87. For ease of implementation, when factor < 1.0, only the first
  88. int(factor*number-of-blocks) message blocks are chaffed.
  89. """
  90. if not (0.0<=factor<=1.0):
  91. raise ValueError, "'factor' must be between 0.0 and 1.0"
  92. if blocksper < 0:
  93. raise ValueError, "'blocksper' must be zero or more"
  94. self.__factor = factor
  95. self.__blocksper = blocksper
  96. def chaff(self, blocks):
  97. """chaff( [(serial-number:int, data:string, MAC:string)] )
  98. : [(int, string, string)]
  99. Add chaff to message blocks. blocks is a list of 3-tuples of the
  100. form (serial-number, data, MAC).
  101. Chaff is created by choosing a random number of the same
  102. byte-length as data, and another random number of the same
  103. byte-length as MAC. The message block's serial number is
  104. placed on the chaff block and all the packet's chaff blocks
  105. are randomly interspersed with the single wheat block. This
  106. method then returns a list of 3-tuples of the same form.
  107. Chaffed blocks will contain multiple instances of 3-tuples
  108. with the same serial number, but the only way to figure out
  109. which blocks are wheat and which are chaff is to perform the
  110. MAC hash and compare values.
  111. """
  112. chaffedblocks = []
  113. # count is the number of blocks to add chaff to. blocksper is the
  114. # number of chaff blocks to add per message block that is being
  115. # chaffed.
  116. count = len(blocks) * self.__factor
  117. blocksper = range(self.__blocksper)
  118. for i, wheat in zip(range(len(blocks)), blocks):
  119. # it shouldn't matter which of the n blocks we add chaff to, so for
  120. # ease of implementation, we'll just add them to the first count
  121. # blocks
  122. if i < count:
  123. serial, data, mac = wheat
  124. datasize = len(data)
  125. macsize = len(mac)
  126. addwheat = 1
  127. # add chaff to this block
  128. for j in blocksper:
  129. import sys
  130. chaffdata = self._randnum(datasize)
  131. chaffmac = self._randnum(macsize)
  132. chaff = (serial, chaffdata, chaffmac)
  133. # mix up the order, if the 5th bit is on then put the
  134. # wheat on the list
  135. if addwheat and bytes_to_long(self._randnum(16)) & 0x40:
  136. chaffedblocks.append(wheat)
  137. addwheat = 0
  138. chaffedblocks.append(chaff)
  139. if addwheat:
  140. chaffedblocks.append(wheat)
  141. else:
  142. # just add the wheat
  143. chaffedblocks.append(wheat)
  144. return chaffedblocks
  145. def _randnum(self, size):
  146. from Crypto import Random
  147. return Random.new().read(size)
  148. if __name__ == '__main__':
  149. text = """\
  150. We hold these truths to be self-evident, that all men are created equal, that
  151. they are endowed by their Creator with certain unalienable Rights, that among
  152. these are Life, Liberty, and the pursuit of Happiness. That to secure these
  153. rights, Governments are instituted among Men, deriving their just powers from
  154. the consent of the governed. That whenever any Form of Government becomes
  155. destructive of these ends, it is the Right of the People to alter or to
  156. abolish it, and to institute new Government, laying its foundation on such
  157. principles and organizing its powers in such form, as to them shall seem most
  158. likely to effect their Safety and Happiness.
  159. """
  160. print 'Original text:\n=========='
  161. print text
  162. print '=========='
  163. # first transform the text into packets
  164. blocks = [] ; size = 40
  165. for i in range(0, len(text), size):
  166. blocks.append( text[i:i+size] )
  167. # now get MACs for all the text blocks. The key is obvious...
  168. print 'Calculating MACs...'
  169. from Crypto.Hash import HMAC, SHA
  170. key = 'Jefferson'
  171. macs = [HMAC.new(key, block, digestmod=SHA).digest()
  172. for block in blocks]
  173. assert len(blocks) == len(macs)
  174. # put these into a form acceptable as input to the chaffing procedure
  175. source = []
  176. m = zip(range(len(blocks)), blocks, macs)
  177. print m
  178. for i, data, mac in m:
  179. source.append((i, data, mac))
  180. # now chaff these
  181. print 'Adding chaff...'
  182. c = Chaff(factor=0.5, blocksper=2)
  183. chaffed = c.chaff(source)
  184. from base64 import encodestring
  185. # print the chaffed message blocks. meanwhile, separate the wheat from
  186. # the chaff
  187. wheat = []
  188. print 'chaffed message blocks:'
  189. for i, data, mac in chaffed:
  190. # do the authentication
  191. h = HMAC.new(key, data, digestmod=SHA)
  192. pmac = h.digest()
  193. if pmac == mac:
  194. tag = '-->'
  195. wheat.append(data)
  196. else:
  197. tag = ' '
  198. # base64 adds a trailing newline
  199. print tag, '%3d' % i, \
  200. repr(data), encodestring(mac)[:-1]
  201. # now decode the message packets and check it against the original text
  202. print 'Undigesting wheat...'
  203. # PY3K: This is meant to be text, do not change to bytes (data)
  204. newtext = "".join(wheat)
  205. if newtext == text:
  206. print 'They match!'
  207. else:
  208. print 'They differ!'