Replace uses of foo.(de|en)code('hex') with binascii.(un)?hexlify(foo)

Python 3 no longer doesn't support encoding/decoding hexadecimal numbers using
the `str.format` method. The backwards compatible new method (using the
binascii module/methods) is a comparable means of converting to/from
hexadecimal format.

In short, the functional change is the following:
* `foo.decode('hex')` -> `binascii.unhexlify(foo)`
* `foo.encode('hex')` -> `binascii.hexlify(foo)`

While here, move the dpkt import in `cryptodev.py` down per PEP8, so it comes
after the standard library provided imports.

PR:		237403
MFC after:	1 week
This commit is contained in:
Enji Cooper 2019-05-20 16:38:12 +00:00
parent 8fcd12f526
commit d99c2cecc8
2 changed files with 73 additions and 69 deletions

View File

@ -32,7 +32,7 @@
from __future__ import print_function
import array
import dpkt
import binascii
from fcntl import ioctl
import os
import random
@ -40,6 +40,8 @@
from struct import pack as _pack
import time
import dpkt
from cryptodevh import *
__all__ = [ 'Crypto', 'MismatchError', ]
@ -493,7 +495,7 @@ def __iter__(self):
def _spdechex(s):
return ''.join(s.split()).decode('hex')
return binascii.hexlify(''.join(s.split()))
if __name__ == '__main__':
if True:
@ -525,15 +527,15 @@ def _spdechex(s):
c = Crypto(CRYPTO_AES_ICM, key)
enc = c.encrypt(pt, iv)
print('enc:', enc.encode('hex'))
print(' ct:', ct.encode('hex'))
print('enc:', binascii.hexlify(enc))
print(' ct:', binascii.hexlify(ct))
assert ct == enc
dec = c.decrypt(ct, iv)
print('dec:', dec.encode('hex'))
print(' pt:', pt.encode('hex'))
print('dec:', binascii.hexlify(dec))
print(' pt:', binascii.hexlify(pt))
assert pt == dec
elif False:
@ -546,15 +548,15 @@ def _spdechex(s):
c = Crypto(CRYPTO_AES_ICM, key)
enc = c.encrypt(pt, iv)
print('enc:', enc.encode('hex'))
print(' ct:', ct.encode('hex'))
print('enc:', binascii.hexlify(enc))
print(' ct:', binascii.hexlify(ct))
assert ct == enc
dec = c.decrypt(ct, iv)
print('dec:', dec.encode('hex'))
print(' pt:', pt.encode('hex'))
print('dec:', binascii.hexlify(dec))
print(' pt:', binascii.hexlify(pt))
assert pt == dec
elif False:
@ -566,15 +568,15 @@ def _spdechex(s):
enc = c.encrypt(pt, iv)
print('enc:', enc.encode('hex'))
print(' ct:', ct.encode('hex'))
print('enc:', binascii.hexlify(enc))
print(' ct:', binascii.hexlify(ct))
assert ct == enc
dec = c.decrypt(ct, iv)
print('dec:', dec.encode('hex'))
print(' pt:', pt.encode('hex'))
print('dec:', binascii.hexlify(dec))
print(' pt:', binascii.hexlify(pt))
assert pt == dec
elif False:
@ -592,26 +594,26 @@ def _spdechex(s):
enc, enctag = c.encrypt(pt, iv, aad=aad)
print('enc:', enc.encode('hex'))
print(' ct:', ct.encode('hex'))
print('enc:', binascii.hexlify(enc))
print(' ct:', binascii.hexlify(ct))
assert enc == ct
print('etg:', enctag.encode('hex'))
print('tag:', tag.encode('hex'))
print('etg:', binascii.hexlify(enctag))
print('tag:', binascii.hexlify(tag))
assert enctag == tag
# Make sure we get EBADMSG
#enctag = enctag[:-1] + 'a'
dec, dectag = c.decrypt(ct, iv, aad=aad, tag=enctag)
print('dec:', dec.encode('hex'))
print(' pt:', pt.encode('hex'))
print('dec:', binascii.hexlify(dec))
print(' pt:', binascii.hexlify(pt))
assert dec == pt
print('dtg:', dectag.encode('hex'))
print('tag:', tag.encode('hex'))
print('dtg:', binascii.hexlify(dectag))
print('tag:', binascii.hexlify(tag))
assert dectag == tag
elif False:
@ -628,27 +630,27 @@ def _spdechex(s):
enc, enctag = c.encrypt(pt, iv, aad=aad)
print('enc:', enc.encode('hex'))
print(' ct:', ct.encode('hex'))
print('enc:', binascii.hexlify(enc))
print(' ct:', binascii.hexlify(ct))
assert enc == ct
print('etg:', enctag.encode('hex'))
print('tag:', tag.encode('hex'))
print('etg:', binascii.hexlify(enctag))
print('tag:', binascii.hexlify(tag))
assert enctag == tag
elif False:
for i in range(100000):
c = Crypto(CRYPTO_AES_XTS, '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'.decode('hex'))
data = '52a42bca4e9425a25bbc8c8bf6129dec'.decode('hex')
ct = '517e602becd066b65fa4f4f56ddfe240'.decode('hex')
c = Crypto(CRYPTO_AES_XTS, binascii.unhexlify('1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'))
data = binascii.unhexlify('52a42bca4e9425a25bbc8c8bf6129dec')
ct = binascii.unhexlify('517e602becd066b65fa4f4f56ddfe240')
iv = _pack('QQ', 71, 0)
enc = c.encrypt(data, iv)
assert enc == ct
elif True:
c = Crypto(CRYPTO_AES_XTS, '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'.decode('hex'))
data = '52a42bca4e9425a25bbc8c8bf6129dec'.decode('hex')
ct = '517e602becd066b65fa4f4f56ddfe240'.decode('hex')
c = Crypto(CRYPTO_AES_XTS, binascii.unhexlify('1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'))
data = binascii.unhexlify('52a42bca4e9425a25bbc8c8bf6129dec')
ct = binascii.unhexlify('517e602becd066b65fa4f4f56ddfe240')
iv = _pack('QQ', 71, 0)
enc = c.encrypt(data, iv)
@ -660,7 +662,7 @@ def _spdechex(s):
#c.perftest(COP_ENCRYPT, 192*1024, reps=30000)
else:
key = '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'.decode('hex')
key = binascii.unhexlify('1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382')
print('XTS %d testing:' % (len(key) * 8))
c = Crypto(CRYPTO_AES_XTS, key)
for i in [ 8192, 192*1024]:

View File

@ -30,6 +30,8 @@
#
from __future__ import print_function
import binascii
import errno
import cryptodev
import itertools
@ -106,13 +108,13 @@ def runGCM(self, fname, mode):
[ 'Count', 'Key', 'IV', 'CT', 'AAD', 'Tag', 'PT', ]):
for data in lines:
curcnt = int(data['Count'])
cipherkey = data['Key'].decode('hex')
iv = data['IV'].decode('hex')
aad = data['AAD'].decode('hex')
tag = data['Tag'].decode('hex')
cipherkey = binascii.unhexlify(data['Key'])
iv = binascii.unhexlify(data['IV'])
aad = binascii.unhexlify(data['AAD'])
tag = binascii.unhexlify(data['Tag'])
if 'FAIL' not in data:
pt = data['PT'].decode('hex')
ct = data['CT'].decode('hex')
pt = binascii.unhexlify(data['PT'])
ct = binascii.unhexlify(data['CT'])
if len(iv) != 12:
# XXX - isn't supported
@ -139,8 +141,8 @@ def runGCM(self, fname, mode):
raise
continue
rtag = rtag[:len(tag)]
data['rct'] = rct.encode('hex')
data['rtag'] = rtag.encode('hex')
data['rct'] = binascii.hexlify(rct)
data['rtag'] = binascii.hexlify(rtag)
self.assertEqual(rct, ct, repr(data))
self.assertEqual(rtag, tag, repr(data))
else:
@ -158,8 +160,8 @@ def runGCM(self, fname, mode):
if e.errno != errno.EINVAL:
raise
continue
data['rpt'] = rpt.encode('hex')
data['rtag'] = rtag.encode('hex')
data['rpt'] = binascii.hexlify(rpt)
data['rtag'] = binascii.hexlify(rtag)
self.assertEqual(rpt, pt,
repr(data))
@ -178,10 +180,10 @@ def runCBC(self, fname):
for data in lines:
curcnt = int(data['COUNT'])
cipherkey = data['KEY'].decode('hex')
iv = data['IV'].decode('hex')
pt = data['PLAINTEXT'].decode('hex')
ct = data['CIPHERTEXT'].decode('hex')
cipherkey = binascii.unhexlify(data['KEY'])
iv = binascii.unhexlify(data['IV'])
pt = binascii.unhexlify(data['PLAINTEXT'])
ct = binascii.unhexlify(data['CIPHERTEXT'])
if swapptct:
pt, ct = ct, pt
@ -207,10 +209,10 @@ def runXTS(self, fname, meth):
for data in lines:
curcnt = int(data['COUNT'])
nbits = int(data['DataUnitLen'])
cipherkey = data['Key'].decode('hex')
cipherkey = binascii.unhexlify(data['Key'])
iv = struct.pack('QQ', int(data['DataUnitSeqNumber']), 0)
pt = data['PT'].decode('hex')
ct = data['CT'].decode('hex')
pt = binascii.unhexlify(data['PT'])
ct = binascii.unhexlify(data['CT'])
if nbits % 128 != 0:
# XXX - mark as skipped
@ -234,15 +236,15 @@ def runCCMEncrypt(self, fname):
if Nlen != 12:
# OCF only supports 12 byte IVs
continue
key = data['Key'].decode('hex')
nonce = data['Nonce'].decode('hex')
key = binascii.unhexlify(data['Key'])
nonce = binascii.unhexlify(data['Nonce'])
Alen = int(data['Alen'])
if Alen != 0:
aad = data['Adata'].decode('hex')
aad = binascii.unhexlify(data['Adata'])
else:
aad = None
payload = data['Payload'].decode('hex')
ct = data['CT'].decode('hex')
payload = binascii.unhexlify(data['Payload'])
ct = binascii.unhexlify(data['CT'])
try:
c = Crypto(crid=crid,
@ -277,14 +279,14 @@ def runCCMDecrypt(self, fname):
if Tlen != 16:
# OCF only supports 16 byte tags
continue
key = data['Key'].decode('hex')
nonce = data['Nonce'].decode('hex')
key = binascii.unhexlify(data['Key'])
nonce = binascii.unhexlify(data['Nonce'])
Alen = int(data['Alen'])
if Alen != 0:
aad = data['Adata'].decode('hex')
aad = binascii.unhexlify(data['Adata'])
else:
aad = None
ct = data['CT'].decode('hex')
ct = binascii.unhexlify(data['CT'])
tag = ct[-16:]
ct = ct[:-16]
@ -306,7 +308,7 @@ def runCCMDecrypt(self, fname):
r = Crypto.decrypt(c, payload, nonce,
aad, tag)
payload = data['Payload'].decode('hex')
payload = binascii.unhexlify(data['Payload'])
plen = int(data('Plen'))
payload = payload[:plen]
self.assertEqual(r, payload,
@ -339,10 +341,10 @@ def runTDES(self, fname):
for data in lines:
curcnt = int(data['COUNT'])
key = data['KEYs'] * 3
cipherkey = key.decode('hex')
iv = data['IV'].decode('hex')
pt = data['PLAINTEXT'].decode('hex')
ct = data['CIPHERTEXT'].decode('hex')
cipherkey = binascii.unhexlify(key)
iv = binascii.unhexlify(data['IV'])
pt = binascii.unhexlify(data['PLAINTEXT'])
ct = binascii.unhexlify(data['CIPHERTEXT'])
if swapptct:
pt, ct = ct, pt
@ -387,9 +389,9 @@ def runSHA(self, fname):
continue
for data in lines:
msg = data['Msg'].decode('hex')
msg = binascii.unhexlify(data['Msg'])
msg = msg[:int(data['Len'])]
md = data['MD'].decode('hex')
md = binascii.unhexlify(data['MD'])
try:
c = Crypto(mac=alg, crid=crid,
@ -440,9 +442,9 @@ def runSHA1HMAC(self, fname):
continue
for data in lines:
key = data['Key'].decode('hex')
msg = data['Msg'].decode('hex')
mac = data['Mac'].decode('hex')
key = binascii.unhexlify(data['Key'])
msg = binascii.unhexlify(data['Msg'])
mac = binascii.unhexlify(data['Mac'])
tlen = int(data['Tlen'])
if len(key) > blocksize: