home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __revision__ = '$Id: AllOrNothing.py,v 1.8 2003/02/28 15:23:20 akuchling Exp $'
- import operator
- import string
- from Crypto.Util.number import bytes_to_long, long_to_bytes
-
- class AllOrNothing:
-
- def __init__(self, ciphermodule, mode = None, IV = None):
- self._AllOrNothing__ciphermodule = ciphermodule
- self._AllOrNothing__mode = mode
- self._AllOrNothing__IV = IV
- self._AllOrNothing__key_size = ciphermodule.key_size
- if self._AllOrNothing__key_size == 0:
- self._AllOrNothing__key_size = 16
-
-
- __K0digit = chr(105)
-
- def digest(self, text):
- key = self._inventkey(self._AllOrNothing__key_size)
- K0 = self._AllOrNothing__K0digit * self._AllOrNothing__key_size
- mcipher = self._AllOrNothing__newcipher(key)
- hcipher = self._AllOrNothing__newcipher(K0)
- block_size = self._AllOrNothing__ciphermodule.block_size
- padbytes = block_size - len(text) % block_size
- text = text + ' ' * padbytes
- s = len(text) / block_size
- blocks = []
- hashes = []
- for i in range(1, s + 1):
- start = (i - 1) * block_size
- end = start + block_size
- mi = text[start:end]
- cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
- mticki = bytes_to_long(mi) ^ bytes_to_long(cipherblock)
- blocks.append(mticki)
- hi = hcipher.encrypt(long_to_bytes(mticki ^ i, block_size))
- hashes.append(bytes_to_long(hi))
-
- i = i + 1
- cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
- mticki = padbytes ^ bytes_to_long(cipherblock)
- blocks.append(mticki)
- hi = hcipher.encrypt(long_to_bytes(mticki ^ i, block_size))
- hashes.append(bytes_to_long(hi))
- mtick_stick = bytes_to_long(key) ^ reduce(operator.xor, hashes)
- blocks.append(mtick_stick)
- return map(long_to_bytes, blocks)
-
-
- def undigest(self, blocks):
- if len(blocks) < 2:
- raise ValueError, 'List must be at least length 2.'
- len(blocks) < 2
- blocks = map(bytes_to_long, blocks)
- K0 = self._AllOrNothing__K0digit * self._AllOrNothing__key_size
- hcipher = self._AllOrNothing__newcipher(K0)
- hashes = []
- for i in range(1, len(blocks)):
- mticki = blocks[i - 1] ^ i
- hi = hcipher.encrypt(long_to_bytes(mticki))
- hashes.append(bytes_to_long(hi))
-
- key = blocks[-1] ^ reduce(operator.xor, hashes)
- mcipher = self._AllOrNothing__newcipher(long_to_bytes(key))
- block_size = self._AllOrNothing__ciphermodule.block_size
- parts = []
- for i in range(1, len(blocks)):
- cipherblock = mcipher.encrypt(long_to_bytes(i, block_size))
- mi = blocks[i - 1] ^ bytes_to_long(cipherblock)
- parts.append(mi)
-
- padbytes = int(parts[-1])
- text = string.join(map(long_to_bytes, parts[:-1]), '')
- return text[:-padbytes]
-
-
- def _inventkey(self, key_size):
- import time
- randpool = randpool
- import Crypto.Util
- pool = randpool.RandomPool(key_size * 2)
- while key_size > pool.entropy:
- pool.add_event()
- return pool.get_bytes(key_size)
-
-
- def __newcipher(self, key):
- if self._AllOrNothing__mode is None and self._AllOrNothing__IV is None:
- return self._AllOrNothing__ciphermodule.new(key)
- if self._AllOrNothing__IV is None:
- return self._AllOrNothing__ciphermodule.new(key, self._AllOrNothing__mode)
- return self._AllOrNothing__ciphermodule.new(key, self._AllOrNothing__mode, self._AllOrNothing__IV)
-
-
- if __name__ == '__main__':
- import sys
- import getopt
- import base64
- usagemsg = 'Test module usage: %(program)s [-c cipher] [-l] [-h]\n\nWhere:\n --cipher module\n -c module\n Cipher module to use. Default: %(ciphermodule)s\n\n --aslong\n -l\n Print the encoded message blocks as long integers instead of base64\n encoded strings\n\n --help\n -h\n Print this help message\n'
- ciphermodule = 'AES'
- aslong = 0
-
- def usage(code, msg = None):
- if msg:
- print msg
-
- print usagemsg % {
- 'program': sys.argv[0],
- 'ciphermodule': ciphermodule }
- sys.exit(code)
-
-
- try:
- (opts, args) = getopt.getopt(sys.argv[1:], 'c:l', [
- 'cipher=',
- 'aslong'])
- except getopt.error:
- msg = None
- usage(1, msg)
-
- if args:
- usage(1, 'Too many arguments')
-
- for opt, arg in opts:
- if opt in ('-h', '--help'):
- usage(0)
- continue
- if opt in ('-c', '--cipher'):
- ciphermodule = arg
- continue
- if opt in ('-l', '--aslong'):
- aslong = 1
- continue
-
- module = __import__('Crypto.Cipher.' + ciphermodule, None, None, [
- 'new'])
- a = AllOrNothing(module)
- print 'Original text:\n=========='
- print __doc__
- print '=========='
- msgblocks = a.digest(__doc__)
- print 'message blocks:'
- for i, blk in map(None, range(len(msgblocks)), msgblocks):
- print ' %3d' % i,
- if aslong:
- print bytes_to_long(blk)
- continue
- print base64.encodestring(blk)[:-1]
-
- b = AllOrNothing(module)
- text = b.undigest(msgblocks)
- if text == __doc__:
- print 'They match!'
- else:
- print 'They differ!'
-
-