From 391fd6e194033c966b0d2c04c4260caffb4957d2 Mon Sep 17 00:00:00 2001 From: Louis Granboulan Date: Wed, 15 Mar 2017 17:36:52 +0100 Subject: [PATCH 1/3] python2 & python3 compatibility without needing 'builtins' module for python2 --- crysp/bits.py | 10 ++++++---- crysp/crc.py | 5 +++-- crysp/keccak.py | 2 +- crysp/mode.py | 13 ++++++------- crysp/padding.py | 9 ++++----- crysp/poly.py | 2 +- crysp/skein.py | 2 +- tests/test_chacha.py | 3 ++- tests/test_padding.py | 2 +- tests/test_salsa20.py | 18 ++++++++++-------- 10 files changed, 35 insertions(+), 31 deletions(-) diff --git a/crysp/bits.py b/crysp/bits.py index 218bd1f..648426f 100644 --- a/crysp/bits.py +++ b/crysp/bits.py @@ -4,7 +4,6 @@ import struct import codecs -from builtins import bytes as newbytes try: IntType = (int,long) @@ -21,7 +20,7 @@ def pack(obj,fmt='L'] s = [x.ival&0xff for x in obj.split(8)] if fmt=='>L': s.reverse() - return newbytes(s) + return struct.pack('%dB'%len(s), *s) # generalize struct.unpack to return one int value of # arbitrary bit length. @@ -95,12 +94,15 @@ def __init__(self,v,size=None,bitorder=-1): self.ival = (self.ival<<1)|(x&1) elif isinstance(v,bytes): self.load(v,bitorder) + elif isinstance(v,str): + # We are using python3, because 'str' and 'bytes' are different + self.load(v.encode(),bitorder) else: raise TypeError(v) if size!=None: self.size = size def load(self,v,bitorder=-1): - bytestr = newbytes(v) + bytestr = struct.unpack('%dB'%len(v), v) self.size = len(bytestr)*8 if bitorder==-1: l = [reverse_byte(c) for c in bytestr] @@ -165,7 +167,7 @@ def __bytes__(self): s.append(reverse_byte(v&0xff)) v = v>>8 i += 8 - return newbytes(s) + return struct.pack('%dB'%len(s), *s) def bytes(self): return self.__bytes__() diff --git a/crysp/crc.py b/crysp/crc.py index ac2e7d7..c3b3d55 100755 --- a/crysp/crc.py +++ b/crysp/crc.py @@ -45,7 +45,8 @@ def crc(data,table,Xinit=0,Xfinal=None): print("crc: bytes input required") return None r = Bits(Xinit,table[0].size) - for b in newbytes(data): + data = struct.unpack("%dB"%len(data), data) + for b in data: p = table[(r.ival^b)&0xff] r = (r>>8)^p if Xfinal: @@ -56,7 +57,7 @@ def crc_back_pos(data,pos,table,Xfinal,c): if not isinstance(data,bytes): print("crc: bytes input required") return None - data = newbytes(data) + data = struct.unpack("%dB"%len(data), data) if not (0<=posself.blocklen or (c[-q:]!=newbytes([q])*q): + q = ord(c[-1:]) + if q>self.blocklen or (c[-q:]!=struct.pack("B",q)*q): raise PaddingError(c) else: return c[:-q] @@ -147,7 +146,7 @@ def lastblock(self,m,**kargs): p = len(m) q = (self.blocklen-p) or self.blocklen r = m+(b'\0'*(q-1)) - r += newbytes([q]) + r += struct.pack("B",q) self.padflag = True self.bitcnt += p*8 self.padcnt = q*8 diff --git a/crysp/poly.py b/crysp/poly.py index 119f545..96f0950 100644 --- a/crysp/poly.py +++ b/crysp/poly.py @@ -20,7 +20,7 @@ def __init__(self,v,size=0,dim=0): self.ival = [int(x)&mask for x in v] elif isinstance(v,bytes): mask = 0xff - self.ival = list(newbytes(v)) + self.ival = list(struct.unpack("%dB"%len(v), v)) else: raise TypeError self.mask = mask diff --git a/crysp/skein.py b/crysp/skein.py index 9e93a11..7d6598d 100644 --- a/crysp/skein.py +++ b/crysp/skein.py @@ -28,7 +28,7 @@ def __init__(self,Nb,No, self.Yl = Yl self.Yf = Yf self.Ym = Ym - self.C += newbytes([Yl,Yf,Ym])+b'\0'*13 + self.C += struct.pack("BBB", Yl,Yf,Ym)+b'\0'*13 self.key = key self.prs = prs self.PK = PK diff --git a/tests/test_chacha.py b/tests/test_chacha.py index ccecc48..8bda5c2 100644 --- a/tests/test_chacha.py +++ b/tests/test_chacha.py @@ -87,7 +87,8 @@ def test_chacha_002(): # ------------------------------------------------------------------- def tobits(s): - x = newbytes([int(x,16) for x in s.split()]) + s = s.split() + x = struct.pack("%dB"%len(s), *[int(x,16) for x in s]) return Bits(x,bitorder=1) def test_chacha_003(): diff --git a/tests/test_padding.py b/tests/test_padding.py index a7d08b4..f8d0ec5 100644 --- a/tests/test_padding.py +++ b/tests/test_padding.py @@ -124,7 +124,7 @@ def test_bitpadding_003(): assert pad.padflag assert pad.bitcnt==63 assert pad.padcnt == 1 - assert pad.remove(b)[-1]&0xfe == ord(b'A')&0xfe + assert ord(pad.remove(b)[-1:])&0xfe == ord(b'A')&0xfe def test_pkcs7_001(): pad = pkcs7(64) diff --git a/tests/test_salsa20.py b/tests/test_salsa20.py index 59f7cb9..05381fa 100644 --- a/tests/test_salsa20.py +++ b/tests/test_salsa20.py @@ -3,31 +3,33 @@ from crysp.salsa20 import * L = [211, 159, 13, 115, 76, 55, 82, 183, 3, 117, 222, 37, 191, 187, 234, 136, 49, 237, 179, 48, 1, 106, 178, 219, 175, 199, 166, 48, 86, 16, 179, 207, 31, 240, 32, 63, 15, 83, 93, 161, 116, 147, 48, 113, 238, 55, 204, 36, 79, 201, 235, 79, 3, 81, 156, 47, 203, 26, 244, 243, 88, 118, 104, 54] -m = newbytes(L) +m = struct.pack("%dB"%len(L), *L) k0 = list(range(1,17)) -k1 = list(range(201,217)) +k1 = k0 + list(range(201,217)) n = list(range(101,117)) -v = Bits(newbytes(n),bitorder=1) +v = Bits(struct.pack("%dB"%len(n), *n),bitorder=1) def test_salsa20_000_hash(): r = Salsa20().hash(m) - rl = list(newbytes(r)) + rl = list(struct.unpack("%dB"%len(r), r)) assert rl[0:3] == [109,42,178] assert rl[-3:] == [19,48,202] def test_salsa20_001_cipher(): - K = Bits(newbytes(k0+k1),bitorder=1) + K = Bits(struct.pack("%dB"%len(k1),*k1),bitorder=1) S = Salsa20(K) S.p[6:10] = v.split(32) - res = list(newbytes(pack(S.core(S.p)))) + res = pack(S.core(S.p)) + res = list(struct.unpack("%dB"%len(res), res)) assert res[0:5] == [69,37,68,39,41] assert res[-5:] == [236,234,103,246,74] def test_salsa20_002_cipher(): - K = Bits(newbytes(k0),bitorder=1) + K = Bits(struct.pack("%dB"%len(k0),*k0),bitorder=1) S = Salsa20(K) S.p[6:10] = v.split(32) - res = list(newbytes(pack(S.core(S.p)))) + res = pack(S.core(S.p)) + res = list(struct.unpack("%dB"%len(res), res)) assert res[0:5] == [39,173,46,248,30] assert res[-5:] == [181,104,182,177,193] From 044dbc5b425daeb0f7ccc9e255244ccb1846a243 Mon Sep 17 00:00:00 2001 From: Louis Granboulan Date: Fri, 17 Mar 2017 10:24:13 +0100 Subject: [PATCH 2/3] cleanup of previous patch --- crysp/bits.py | 3 --- crysp/crc.py | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/crysp/bits.py b/crysp/bits.py index 648426f..f20f613 100644 --- a/crysp/bits.py +++ b/crysp/bits.py @@ -94,9 +94,6 @@ def __init__(self,v,size=None,bitorder=-1): self.ival = (self.ival<<1)|(x&1) elif isinstance(v,bytes): self.load(v,bitorder) - elif isinstance(v,str): - # We are using python3, because 'str' and 'bytes' are different - self.load(v.encode(),bitorder) else: raise TypeError(v) if size!=None: self.size = size diff --git a/crysp/crc.py b/crysp/crc.py index c3b3d55..d1a7fc3 100755 --- a/crysp/crc.py +++ b/crysp/crc.py @@ -45,8 +45,7 @@ def crc(data,table,Xinit=0,Xfinal=None): print("crc: bytes input required") return None r = Bits(Xinit,table[0].size) - data = struct.unpack("%dB"%len(data), data) - for b in data: + for b in struct.unpack("%dB"%len(data), data): p = table[(r.ival^b)&0xff] r = (r>>8)^p if Xfinal: From f03875ddfbfc734ff36e99d9ae6fe445bc8372d7 Mon Sep 17 00:00:00 2001 From: Louis Granboulan Date: Mon, 20 Mar 2017 17:03:09 +0100 Subject: [PATCH 3/3] README made compatible with python3 --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index ee2cce7..a7adacf 100644 --- a/README.rst +++ b/README.rst @@ -67,8 +67,8 @@ integer by using parameter bitorder=1: >>> b1 = Bits(10) >>> b2 = Bits([0,1,0,1]) - >>> b3 = Bits('\x50',size=4) - >>> b4 = Bits('\x0a',size=4,bitorder=1) + >>> b3 = Bits(b'\x50',size=4) + >>> b4 = Bits(b'\x0a',size=4,bitorder=1) >>> b1==b2==b3==b4 True