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
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=347996
2 changed files with 73 additions and 69 deletions

View File

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

View File

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