ESPythoNOW icon indicating copy to clipboard operation
ESPythoNOW copied to clipboard

Sending encrypted ESP-NOW

Open ChuckMash opened this issue 1 year ago • 4 comments

~ESPythoNOW does not currently support the encryption method optionally used with ESP-NOW.~

According to the documentation

  • Uses the CCMP method, which is described in IEEE Std. 802.11-2012, to protect the vendor-specific action frame.
  • The lengths of both PMK and LMK are 16 bytes.
  • PMK is used to encrypt LMK with the AES-128 algorithm.

ChuckMash avatar Sep 04 '24 08:09 ChuckMash

Receiving encrypted ESP-NOW messages is now supported, but sending is not yet.

Possibly related to needing AAD/MIC calculation.

AAD/MIC may also benefit validating received encrypted messages.

ChuckMash avatar Sep 13 '24 04:09 ChuckMash

If the issue is the 8 byte MIC validation, it is made worse by the failing the validation check of received messages.

def callback(from_mac, to_mac, msg):
  packet = espnow.packet

  nonce = b'\x00'+bytes.fromhex(from_mac.replace(':',''))+struct.pack("BBBBBB",packet.PN5,packet.PN4,packet.PN3,packet.PN2,packet.PN1,packet.PN0)
  cipher = AES.new(espnow.key, AES.MODE_CCM, nonce, mac_len=8)

  try:
    data = cipher.decrypt_and_verify(packet.data[:-8], packet.data[-8:]) # does not validate
    print("success")
  except Exception as e:
    print("Error decrypting:",e)

If a solution is found to calculate MIC and validate correctly for receiving encrypted messages, it will be a big help for generating the MIC for sending encrypted messages.


CCMP documentation suggests the MIC is calculated/validated with AES CBC apart from the message itself encrypted with CCM

ChuckMash avatar Sep 16 '24 18:09 ChuckMash

Compiled ESP-IDF from source with modified debug and ccmp.c to track through AAD and MIC. Unable to see output of DEBUG statements in ccmp.c.

ChuckMash avatar Sep 17 '24 07:09 ChuckMash

List of potentially helpful links found so far. This comment will be updated

  • https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/network/esp_now.html#security
  • https://github.com/espressif/esp-idf/blob/master/components/wpa_supplicant/src/crypto/ccmp.c
  • https://en.wikipedia.org/wiki/CCMP_(cryptography)
  • https://praneethwifi.in/2020/05/02/ctr-with-cbc-mac-protocol-ccmp-aes-ccmp/
  • https://stackoverflow.com/questions/70814579/need-assistance-aes-ccm-decryption-of-an-802-11-payload-using-python-pycryptodom
  • https://github.com/espressif/esp-idf/blob/master/components/wpa_supplicant/src/crypto/aes-ccm.c#L179
  • https://mrncciew.com/2014/08/19/cwsp-ccmp-encryption-method/
  • https://github.com/stryngs/pyDot11/blob/main/SRC/pyDot11/pyDot11/lib/ccmp.py
  • https://www.techtarget.com/searchsecurity/definition/CCMP-Counter-Mode-with-Cipher-Block-Chaining-Message-Authentication-Code-Protocol

ChuckMash avatar Sep 17 '24 19:09 ChuckMash