pyca / pynacl

Python binding to the Networking and Cryptography (NaCl) library
https://pynacl.readthedocs.io/
Apache License 2.0
1.05k stars 228 forks source link

TypeError: object of type 'ABCMeta' has no len() #814

Closed DanielAtCosmicDNA closed 3 months ago

DanielAtCosmicDNA commented 3 months ago

Utilising a frontend with TweetNacl and a backend with Pynacl, the message decryption is failing. I have logged the information flow, but it seems to me there is something amiss. The triggered error was not helpful in finding a solution to this problem, so I am writing here.

Frontend

At apiSlice.js line 7, the private key of the message sender is:

[184, 97, 95, 148, 158, 213, 235, 119, 53, 239, 105, 5, 100, 65, 200, 234, 0, 215, 221, 76, 70, 71, 219, 29, 142, 190, 196, 224, 46, 144, 231, 144]

And the public key is:

[74, 70, 236, 172, 99, 241, 224, 13, 184, 6, 241, 161, 78, 230, 59, 232, 8, 152, 101, 197, 172, 162, 62, 77, 206, 133, 5, 30, 202, 199, 62, 23]

Then a message with the content { message: 'Hello to you!' } is encrypted at App.js line 97, which in turns calls the query at apiSlice.js line 23.

Network

A GET query message with the following payload is then sent:

parameter data
publicKey 169e9f8728dad5f95c9ee33bc3b9460c5b9ca9b279fdd563bf56f1cd2ebdd738
encryptedMessage M5bpbZdl1+yf2QiwrT2s1Viei9EZS2LHnIWusYbELqGQfQoOda/ff0gX5O5UuH19v5SYgBuu3R5GsR11eBY7Wa8eWg==

And which is responded by server with:

Traceback (most recent call last):
  File "C:\Users\aUsename\AppData\Local\plover\plover\plugins\win\Python39\site-packages\plover_engine_server\websocket\nacl_middleware.py", line 99, in returned_middleware
    decrypted_message = my_mail_box.decrypt(incoming_base64_encrypted_message, Base64Encoder)
  File "C:\Users\aUsername\AppData\Local\plover\plover\plugins\win\Python39\site-packages\nacl\public.py", line 294, in decrypt
    if len(nonce) != self.NONCE_SIZE:
TypeError: object of type 'ABCMeta' has no len()

Server

On the server side, there is Plover with the following plugin.

This plugin writes and reads to a file that has the public and private keys. In this round the server has:

{
  "private_key": "72ce753d550cdb81cd2148ffffd09c7790c76c404ba071ca4ccbd77f7dbf4651",
  "public_key": "169e9f8728dad5f95c9ee33bc3b9460c5b9ca9b279fdd563bf56f1cd2ebdd738",
}

The read and write to this file are governed by config.py

The message is then received by the nacl_middleware where the following log is written to plover.log:

2024-03-15 22:44:03,007 [Thread-3] INFO: Request received.
2024-03-15 22:44:03,007 [Thread-3] INFO: Retrieving publicKey from message...
2024-03-15 22:44:03,007 [Thread-3] INFO: PublicKey 169e9f8728dad5f95c9ee33bc3b9460c5b9ca9b279fdd563bf56f1cd2ebdd738 retrieved!
2024-03-15 22:44:03,007 [Thread-3] INFO: Decoding messager's public key hex...
2024-03-15 22:44:03,007 [Thread-3] INFO: Messager's public key b'\x16\x9e\x9f\x87(\xda\xd5\xf9\\\x9e\xe3;\xc3\xb9F\x0c[\x9c\xa9\xb2y\xfd\xd5c\xbfV\xf1\xcd.\xbd\xd78' decoded!
2024-03-15 22:44:03,007 [Thread-3] INFO: Retrieving encryptedMessage from message...
2024-03-15 22:44:03,007 [Thread-3] INFO: EncryptedMessage M5bpbZdl1+yf2QiwrT2s1Viei9EZS2LHnIWusYbELqGQfQoOda/ff0gX5O5UuH19v5SYgBuu3R5GsR11eBY7Wa8eWg== retrieved!
2024-03-15 22:44:03,007 [Thread-3] INFO: Creating Box...
2024-03-15 22:44:03,007 [Thread-3] INFO: Box b'\xdd\x11\x12Dr\xf6\xe2\xcd\x1b`\xbf\xe0\xbfR\x99P\xae5\x93\xdf\xfe\xe5\xc3\x99P\xf8vB.\xba\xfb\xa1' created!
2024-03-15 22:44:03,007 [Thread-3] INFO: Decrypting message...
2024-03-15 22:44:03,011 [Thread-3] INFO: Response hook.
2024-03-15 22:44:03,011 [Thread-3] INFO: headers: <CIMultiDict('Content-Type': 'text/plain; charset=utf-8')>
2024-03-15 22:44:03,011 [Thread-3] INFO: status: 401
2024-03-15 22:44:03,011 [Thread-3] INFO: reason: Failed to retrieve a valid message!
2024-03-15 22:44:03,011 [Thread-3] INFO: body: <aiohttp.payload.StringPayload object at 0x0000027FFEF25460>
2024-03-15 22:44:03,529 [Thread-3] INFO: Request received.
2024-03-15 22:44:03,529 [Thread-3] INFO: Retrieving publicKey from message...
2024-03-15 22:44:03,530 [Thread-3] INFO: PublicKey None retrieved!
2024-03-15 22:44:03,530 [Thread-3] INFO: Decoding messager's public key hex...
alex commented 3 months ago

The second argument to decrypt is a nonce (or None), not an encoder: https://pynacl.readthedocs.io/en/latest/public/#nacl.public.Box.decrypt