summaryrefslogtreecommitdiff
path: root/lib/Crypto/Protocol
diff options
context:
space:
mode:
authorLegrandin <gooksankoo@hoiptorrow.mailexpire.com>2011-10-18 23:20:26 +0200
committerLegrandin <gooksankoo@hoiptorrow.mailexpire.com>2011-10-18 23:20:26 +0200
commitc22fa18c0dedb43a8b19dcb9b29512ba59e1764b (patch)
treee7864a848ed2c37d4a2c0d65bcae0f0cbdc6ea27 /lib/Crypto/Protocol
parent897b75983c31a9e2630af92161e6206c2480685e (diff)
parentb9658a26003ebfcfce1804a2363a29354799b47e (diff)
downloadpycrypto-c22fa18c0dedb43a8b19dcb9b29512ba59e1764b.tar.gz
Merged from upstream (py3k support) and modified so that all unit tests pass.
Diffstat (limited to 'lib/Crypto/Protocol')
-rw-r--r--lib/Crypto/Protocol/AllOrNothing.py31
-rw-r--r--lib/Crypto/Protocol/Chaffing.py5
-rw-r--r--lib/Crypto/Protocol/KDF.py7
3 files changed, 24 insertions, 19 deletions
diff --git a/lib/Crypto/Protocol/AllOrNothing.py b/lib/Crypto/Protocol/AllOrNothing.py
index 4b00c0b..4ece960 100644
--- a/lib/Crypto/Protocol/AllOrNothing.py
+++ b/lib/Crypto/Protocol/AllOrNothing.py
@@ -45,8 +45,9 @@ http://theory.lcs.mit.edu/~rivest/fusion.pdf
__revision__ = "$Id$"
import operator
-import string
+import sys
from Crypto.Util.number import bytes_to_long, long_to_bytes
+from Crypto.Util.py3compat import *
@@ -83,7 +84,7 @@ class AllOrNothing:
if self.__key_size == 0:
self.__key_size = 16
- __K0digit = chr(0x69)
+ __K0digit = bchr(0x69)
def digest(self, text):
"""digest(text:string) : [string]
@@ -113,7 +114,7 @@ class AllOrNothing:
# the undigest() step.
block_size = self.__ciphermodule.block_size
padbytes = block_size - (len(text) % block_size)
- text = text + ' ' * padbytes
+ text = text + b(' ') * padbytes
# Run through the algorithm:
# s: number of message blocks (size of text / block_size)
@@ -127,7 +128,7 @@ class AllOrNothing:
# The one complication I add is that the last message block is hard
# coded to the number of padbytes added, so that these can be stripped
# during the undigest() step
- s = len(text) / block_size
+ s = divmod(len(text), block_size)[0]
blocks = []
hashes = []
for i in range(1, s+1):
@@ -189,13 +190,14 @@ class AllOrNothing:
# encrypted, and create the hash cipher.
K0 = self.__K0digit * self.__key_size
hcipher = self.__newcipher(K0)
+ block_size = self.__ciphermodule.block_size
# 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 = []
for i in range(1, len(blocks)):
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))
# now we can calculate K' (key). remember the last block contains
@@ -203,8 +205,7 @@ class AllOrNothing:
key = blocks[-1] ^ reduce(operator.xor, hashes)
# and now we can create the cipher object
- mcipher = self.__newcipher(long_to_bytes(key))
- block_size = self.__ciphermodule.block_size
+ mcipher = self.__newcipher(long_to_bytes(key, self.__key_size))
# And we can now decode the original message blocks
parts = []
@@ -218,7 +219,7 @@ class AllOrNothing:
# of the cipher's block_size. This number should be small enough that
# the conversion from long integer to integer should never overflow
padbytes = int(parts[-1])
- text = string.join(map(long_to_bytes, parts[:-1]), '')
+ text = b('').join(map(long_to_bytes, parts[:-1]))
return text[:-padbytes]
def _inventkey(self, key_size):
@@ -289,13 +290,13 @@ Where:
# ugly hack to force __import__ to give us the end-path module
module = __import__('Crypto.Cipher.'+ciphermodule, None, None, ['new'])
- a = AllOrNothing(module)
+ x = AllOrNothing(module)
print 'Original text:\n=========='
print __doc__
print '=========='
- msgblocks = a.digest(__doc__)
+ msgblocks = x.digest(b(__doc__))
print 'message blocks:'
- for i, blk in map(None, range(len(msgblocks)), msgblocks):
+ for i, blk in zip(range(len(msgblocks)), msgblocks):
# base64 adds a trailing newline
print ' %3d' % i,
if aslong:
@@ -304,9 +305,9 @@ Where:
print base64.encodestring(blk)[:-1]
#
# get a new undigest-only object so there's no leakage
- b = AllOrNothing(module)
- text = b.undigest(msgblocks)
- if text == __doc__:
+ y = AllOrNothing(module)
+ text = y.undigest(msgblocks)
+ if text == b(__doc__):
print 'They match!'
else:
print 'They differ!'
diff --git a/lib/Crypto/Protocol/Chaffing.py b/lib/Crypto/Protocol/Chaffing.py
index ba272ab..c19e037 100644
--- a/lib/Crypto/Protocol/Chaffing.py
+++ b/lib/Crypto/Protocol/Chaffing.py
@@ -140,7 +140,7 @@ class Chaff:
# chaffed.
count = len(blocks) * self.__factor
blocksper = range(self.__blocksper)
- for i, wheat in map(None, range(len(blocks)), blocks):
+ for i, wheat in zip(range(len(blocks)), blocks):
# it shouldn't matter which of the n blocks we add chaff to, so for
# ease of implementation, we'll just add them to the first count
# blocks
@@ -205,7 +205,7 @@ likely to effect their Safety and Happiness.
# put these into a form acceptable as input to the chaffing procedure
source = []
- m = map(None, range(len(blocks)), blocks, macs)
+ m = zip(range(len(blocks)), blocks, macs)
print m
for i, data, mac in m:
source.append((i, data, mac))
@@ -237,6 +237,7 @@ likely to effect their Safety and Happiness.
# now decode the message packets and check it against the original text
print 'Undigesting wheat...'
+ # PY3K: This is meant to be text, do not change to bytes (data)
newtext = "".join(wheat)
if newtext == text:
print 'They match!'
diff --git a/lib/Crypto/Protocol/KDF.py b/lib/Crypto/Protocol/KDF.py
index 301ae4f..c6979c8 100644
--- a/lib/Crypto/Protocol/KDF.py
+++ b/lib/Crypto/Protocol/KDF.py
@@ -38,6 +38,7 @@ __revision__ = "$Id$"
import math
import struct
+from Crypto.Util.py3compat import *
from Crypto.Hash import SHA as SHA1, HMAC
from Crypto.Util.strxor import strxor
@@ -54,7 +55,7 @@ def PBKDF1(password, salt, dkLen, count=1000, hashAlgo=SHA1):
:Parameters:
password : string
The secret password or pass phrase to generate the key from.
- salt : string
+ salt : byte string
An 8 byte string to use for better protection from dictionary attacks.
This value does not need to be kept secret, but it should be randomly
chosen for each derivation.
@@ -68,6 +69,7 @@ def PBKDF1(password, salt, dkLen, count=1000, hashAlgo=SHA1):
:Return: A byte string of length `dkLen` that can be used as key.
"""
+ password = tobytes(password)
pHash = hashAlgo.new(password+salt)
digest = pHash.digest_size
if dkLen>digest:
@@ -102,9 +104,10 @@ def PBKDF2(password, salt, dkLen=16, count=1000, prf=None):
:Return: A byte string of length `dkLen` that can be used as key material.
If you wanted multiple keys, just break up this string into segments of the desired length.
"""
+ password = tobytes(password)
if prf is None:
prf = lambda p,s: HMAC.new(p,s,SHA1).digest()
- key = ''
+ key = b('')
i = 1
while len(key)<dkLen:
U = previousU = prf(password,salt+struct.pack(">I", i))