Fix AllOrNothing and random.sample()

o AllOrNothing no longer fails occasionally. Patch by Lorenz Quack
o random.sample() works on Python 2.1. Patch by Paul Koning and Lorenz
  Quack
This commit is contained in:
Thorsten Behrens 2011-01-06 07:18:12 -05:00
parent b27696462b
commit 60896cc61a
3 changed files with 7 additions and 9 deletions

View file

@ -190,13 +190,14 @@ class AllOrNothing:
# encrypted, and create the hash cipher. # encrypted, and create the hash cipher.
K0 = self.__K0digit * self.__key_size K0 = self.__K0digit * self.__key_size
hcipher = self.__newcipher(K0) hcipher = self.__newcipher(K0)
block_size = self.__ciphermodule.block_size
# Since we have all the blocks (or this method would have been called # Since we have all the blocks (or this method would have been called
# prematurely), we can calcualte all the hash blocks. # prematurely), we can calculate all the hash blocks.
hashes = [] hashes = []
for i in range(1, len(blocks)): for i in range(1, len(blocks)):
mticki = blocks[i-1] ^ i mticki = blocks[i-1] ^ i
hi = hcipher.encrypt(long_to_bytes(mticki)) hi = hcipher.encrypt(long_to_bytes(mticki, block_size))
hashes.append(bytes_to_long(hi)) hashes.append(bytes_to_long(hi))
# now we can calculate K' (key). remember the last block contains # now we can calculate K' (key). remember the last block contains
@ -204,8 +205,7 @@ class AllOrNothing:
key = blocks[-1] ^ reduce(operator.xor, hashes) key = blocks[-1] ^ reduce(operator.xor, hashes)
# and now we can create the cipher object # and now we can create the cipher object
mcipher = self.__newcipher(long_to_bytes(key)) mcipher = self.__newcipher(long_to_bytes(key, self.__key_size))
block_size = self.__ciphermodule.block_size
# And we can now decode the original message blocks # And we can now decode the original message blocks
parts = [] parts = []

View file

@ -122,7 +122,7 @@ class StrongRandom(object):
selected = {} # we emulate a set using a dict here selected = {} # we emulate a set using a dict here
for i in xrange(k): for i in xrange(k):
r = None r = None
while r is None or r in selected: while r is None or selected.has_key(r):
r = self.randrange(num_choices) r = self.randrange(num_choices)
retval.append(population[r]) retval.append(population[r])
selected[r] = 1 selected[r] = 1

View file

@ -58,12 +58,10 @@ Use instead assertEqual(expr,True) for assert_ and assertEqual(expr,False) for
failIf failIf
Added unit tests for Crypto.Random.random. Fixed random.shuffle(). Added unit tests for Crypto.Random.random. Fixed random.shuffle().
Not changed: random.sample() fails on Python 2.1. This is now exposed through random.sample() changed to no longer fail on Python 2.1.
the unit test.
Added unit test for Crypto.Protocol.AllOrNothing. Added unit test for Crypto.Protocol.AllOrNothing.
Not changed: AllOrNothing fails when called a few times (<10, usually). This AllOrNothing changed to no longer fail occasionally.
is now exposed through the unit test.
C code: C code: