svn commit: r346449 - user/ngie/bug-237403/tests/sys/opencrypto

Enji Cooper ngie at FreeBSD.org
Tue Sep 3 14:07:13 UTC 2019


Author: ngie
Date: Sat Apr 20 16:07:47 2019
New Revision: 346449
URL: https://svnweb.freebsd.org/changeset/base/346449

Log:
  Replace `str.{decode,encode}("hex")` with binascii.(un)?hexlify
  
  Encoding/decoding base_string objects as "hex" was removed in py3 and replaced
  with other methods, one being `binascii.(un)?hexlify`.

Modified:
  user/ngie/bug-237403/tests/sys/opencrypto/cryptodev.py
  user/ngie/bug-237403/tests/sys/opencrypto/cryptotest.py

Modified: user/ngie/bug-237403/tests/sys/opencrypto/cryptodev.py
==============================================================================
--- user/ngie/bug-237403/tests/sys/opencrypto/cryptodev.py	Sat Apr 20 16:07:29 2019	(r346448)
+++ user/ngie/bug-237403/tests/sys/opencrypto/cryptodev.py	Sat Apr 20 16:07:47 2019	(r346449)
@@ -1,8 +1,9 @@
-#!/usr/local/bin/python2
+#!/usr/bin/env python
 #
 # Copyright (c) 2014 The FreeBSD Foundation
 # Copyright 2014 John-Mark Gurney
 # All rights reserved.
+# Copyright 2019 Enji Cooper
 #
 # This software was developed by John-Mark Gurney under
 # the sponsorship from the FreeBSD Foundation.
@@ -32,12 +33,15 @@
 
 from __future__ import print_function
 import array
-import dpkt
 from fcntl import ioctl
 import os
+import random
 import signal
 from struct import pack as _pack
+import time
 
+import dpkt
+
 from cryptodevh import *
 
 __all__ = [ 'Crypto', 'MismatchError', ]
@@ -116,10 +120,12 @@ CIOCFINDDEV = 3223610220
 CIOCCRYPTAEAD = 3225445229
 
 def _getdev():
-    fd = os.open('/dev/crypto', os.O_RDWR)
     buf = array.array('I', [0])
-    ioctl(fd, CRIOGET, buf, 1)
-    os.close(fd)
+    fd = os.open('/dev/crypto', os.O_RDWR)
+    try:
+        ioctl(fd, CRIOGET, buf, 1)
+    finally:
+        os.close(fd)
 
     return buf[0]
 
@@ -128,13 +134,13 @@ _cryptodev = _getdev()
 def _findop(crid, name):
     fop = FindOp()
     fop.crid = crid
-    fop.name = name
+    fop.name = name.encode("ascii")
     s = array.array('B', fop.pack_hdr())
     ioctl(_cryptodev, CIOCFINDDEV, s, 1)
     fop.unpack(s)
 
     try:
-        idx = fop.name.index('\x00')
+        idx = fop.name.index(b'\x00')
         name = fop.name[:idx]
     except ValueError:
         name = fop.name
@@ -223,10 +229,10 @@ class Crypto:
         caead.flags = CRD_F_IV_EXPLICIT
         caead.flags = 0
         caead.len = len(src)
-        s = array.array('B', src)
+        s = array.array('B', src.encode("ascii"))
         caead.src = caead.dst = s.buffer_info()[0]
         caead.aadlen = len(aad)
-        saad = array.array('B', aad)
+        saad = array.array('B', aad.encode("ascii"))
         caead.aad = saad.buffer_info()[0]
 
         if self._maclen is None:
@@ -237,7 +243,7 @@ class Crypto:
         else:
             assert len(tag) == self._maclen, \
                 '%d != %d' % (len(tag), self._maclen)
-            tag = array.array('B', tag)
+            tag = array.array('B', tag.encode("ascii"))
 
         caead.tag = tag.buffer_info()[0]
 
@@ -252,11 +258,9 @@ class Crypto:
         return s, tag.tostring()
 
     def perftest(self, op, size, timeo=3):
-        import random
-        import time
 
         inp = array.array('B', (random.randint(0, 255) for x in xrange(size)))
-        out = array.array('B', inp)
+        out = array.array('B', inp.encode("ascii"))
 
         # prep ioctl
         cop = CryptOp()
@@ -264,7 +268,7 @@ class Crypto:
         cop.op = op
         cop.flags = 0
         cop.len = len(inp)
-        s = array.array('B', inp)
+        s = array.array('B', inp.encode("ascii"))
         cop.src = s.buffer_info()[0]
         cop.dst = out.buffer_info()[0]
         if self._maclen is not None:

Modified: user/ngie/bug-237403/tests/sys/opencrypto/cryptotest.py
==============================================================================
--- user/ngie/bug-237403/tests/sys/opencrypto/cryptotest.py	Sat Apr 20 16:07:29 2019	(r346448)
+++ user/ngie/bug-237403/tests/sys/opencrypto/cryptotest.py	Sat Apr 20 16:07:47 2019	(r346449)
@@ -30,6 +30,8 @@
 #
 
 from __future__ import print_function
+
+import binascii
 import errno
 import cryptodev
 import itertools
@@ -96,13 +98,13 @@ def GenTestCase(cname):
                 [ '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
@@ -128,8 +130,8 @@ def GenTestCase(cname):
                                 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:
@@ -147,8 +149,8 @@ def GenTestCase(cname):
                                 if e.errno != errno.EINVAL:
                                     raise
                                 continue
-                            data['rpt'] = rpt.encode('hex')
-                            data['rtag'] = rtag.encode('hex')
+                            data['rpt'] = binascii.unhexlify(rpt)
+                            data['rtag'] = binascii.unhexlify(rtag)
                             self.assertEqual(rpt, pt,
                                 repr(data))
 
@@ -167,10 +169,10 @@ def GenTestCase(cname):
 
                 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
@@ -196,10 +198,10 @@ def GenTestCase(cname):
                 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
@@ -241,10 +243,10 @@ def GenTestCase(cname):
                 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
@@ -298,9 +300,9 @@ def GenTestCase(cname):
                     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:




More information about the svn-src-user mailing list