nfcpy icon indicating copy to clipboard operation
nfcpy copied to clipboard

Tagtool: TypeError: key: expected bytes or bytearray, but got 'str'

Open joetomasone opened this issue 4 years ago • 1 comments

Trying to run tagtool protect and getting the following:

root@ubuntu:~#` python3 tagtool.py protect -p "12345678"
[nfc.clf] searching for reader on path usb
[nfc.clf] using ACS ACR122U PN532v1.6 at usb:001:010
** waiting for a tag **
Type2Tag 'NXP NTAG215' ID=04CF37625D6480
generating diversified key from password
Traceback (most recent call last):
  File "tagtool.py", line 620, in <module>
    TagTool().run()
  File "/root/cli.py", line 375, in run
    while self.run_once() and self.options.loop:
  File "/root/cli.py", line 370, in run_once
    return clf.connect(**kwargs)
  File "/usr/local/lib/python3.6/dist-packages/nfc/clf/__init__.py", line 581, in connect
    result = self._rdwr_connect(rdwr_options, terminate)
  File "/usr/local/lib/python3.6/dist-packages/nfc/clf/__init__.py", line 612, in _rdwr_connect
    if options['on-connect'](tag):
  File "tagtool.py", line 319, in on_rdwr_connect
    self.rdwr_commands[self.options.command](tag)
  File "tagtool.py", line 495, in protect_tag
    password = hmac.new(key, msg, hashlib.sha256).digest()
  File "/usr/lib/python3.6/hmac.py", line 144, in new
    return HMAC(key, msg, digestmod)
  File "/usr/lib/python3.6/hmac.py", line 42, in __init__
    raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'
root@ubuntu:~#

Python is Python 3.6.9. OS is Ubuntu 18.04.5 LTS.

joetomasone avatar Apr 11 '21 05:04 joetomasone

I was having the same problem so I look into it and found a quickfix and can confirm protect works correctly.

I convert the input unicode password 'str' to 'bytes' as per parameter requirement of the hmac.new().

Following patch for your conveniences

diff --git a/examples/tagtool.py b/examples/tagtool.py
index 6dc7aee..c287ee5 100755
--- a/examples/tagtool.py
+++ b/examples/tagtool.py
@@ -304,7 +304,7 @@ class TagTool(cli.CommandLineInterface):
         if self.options.authenticate is not None:
             if len(self.options.authenticate) > 0:
                 key, msg = self.options.authenticate, tag.identifier
-                password = hmac.new(key, msg, hashlib.sha256).digest()
+                password = hmac.new(bytes(key,'utf-8'), msg, hashlib.sha256).digest()
             else:
                 password = ""  # use factory default password
             result = tag.authenticate(password)
@@ -492,7 +492,7 @@ class TagTool(cli.CommandLineInterface):
             if len(self.options.password) >= 8:
                 print("generating diversified key from password")
                 key, msg = self.options.password, tag.identifier
-                password = hmac.new(key, msg, hashlib.sha256).digest()
+                password = hmac.new(bytes(key,'utf-8'), msg, hashlib.sha256).digest()
             elif len(self.options.password) == 0:
                 print("using factory default key for password")
                 password = ""

tinng81 avatar Apr 12 '21 07:04 tinng81