liamcottle / meshtastic-map

A map of all Meshtastic nodes heard via MQTT.
https://meshtastic.liamcottle.net
MIT License
95 stars 24 forks source link

Unable to decrypt AES256 key #36

Closed rohanki closed 5 months ago

rohanki commented 5 months ago

If using a different randomally generated PSK for primary channel, meshtastic will generate an AES256/32 byte key. mqtt.js is currently unable to decrypt this packet even when passed the key as an argument. I've added some debugging in - receiving a packet with an AES256 key throws the following:

RangeError: Invalid key length at Decipheriv.createCipherBase (node:internal/crypto/cipher:121:19) at Decipheriv.createCipherWithIV (node:internal/crypto/cipher:140:3) at new Decipheriv (node:internal/crypto/cipher:289:3) at Object.createDecipheriv (node:crypto:155:10) at decrypt (/home/ro/meshtastic-map/src/mqtt.js:241:37) at MqttClient. (/home/ro/meshtastic-map/src/mqtt.js:331:29) { code: 'ERR_CRYPTO_INVALID_KEYLEN' }

I'm not overly experienced with javascript, but I was able to solve the problem by adding the following: if (decryptionKey.length == 24) { // create aes-128-ctr decipher decipher = crypto.createDecipheriv('aes-128-ctr', key, nonceBuffer); } else { // create aes-256-ctr decipher decipher = crypto.createDecipheriv('aes-256-ctr', key, nonceBuffer); }

I'd fork/pull, but I'm not sure that's the most efficient way to do it?

liamcottle commented 5 months ago

Thanks for logging this. I had hardcoded the key type. Will get a patch pushed out for this a bit later tonight.

liamcottle commented 5 months ago

Fixed in 43d6f5be75b7b414653517dedc64402dfc80fc55. Feel free to reopen if you still have this issue :)