micropython / micropython-esp32

Old port of MicroPython to the ESP32 -- new port is at https://github.com/micropython/micropython
MIT License
673 stars 216 forks source link

Using DTLS, with UDP, and generalized packet objects #188

Open MrSurly opened 6 years ago

MrSurly commented 6 years ago
End Goal

Using DTLS for LoRa data. Why? Because I'm using the RFM95 (sx1276) module that doesn't appear to have encryption, and the RFM69 (sx1231)modules (that do have encryption) are using AES ECB, which is bad for the highly repetitive nature of my packets.

Intermediate Goal

Enable DTLS for ordinary UDP sockets, using mbedtls, which is already used in ussl.

Doing this for UDP sockets seems relatively straight-forward, as all µPy sockets implement an underlying "stream" protocol, thus the ussl module will work, with some modification.

It would be nice if there were a generic µPy "stream" protocol class that could be subclassed (to talk to LoRa, which is very UDP-like), and handed directly to (modified for DTLS) ussl for wrapping -- or is there already?

dpgeorge commented 6 years ago

It would be nice if there were a generic µPy "stream" protocol class that could be subclassed

Do you mean on the C or Python side? On the C side you just make an mp_stream_p_t struct for your type. But I guess you are asking about how to do that on the Python side. In that case you may want to look at https://github.com/micropython/micropython/pull/3034 which adds an io.IOBase class for this purpose.

MrSurly commented 6 years ago

But I guess you are asking about how to do that on the Python side. In that case you may want to look at micropython/micropython#3034 which adds an io.IOBase class for this purpose.

Yes, this is exactly what I had in mind. Since ussl expects something stream-like, and calls mp_get_stream_raise(), it requires the underlying class to implement mp_stream_p_t. I just want something that implements mp_stream_p_t, but allows me to overload read, write, and ioctl (in Python), allowing me to pass an object to ussl that actually implements datagrams over LoRa.

dpgeorge commented 6 years ago

Ok. That IOBase class allows you to implement read/write on the Python side, but not ioctl (but not hard to add the latter).

MrSurly commented 6 years ago

Also, I should mention, I have DTLS working for plain-vanilla UDP sockets, with the exception that hello verify cookies aren't working for me.

Implementation is somewhat involved -- there are a lot of things that have to be just right. But that's the nature of socket programming.

C code changes:

Python code requirements:

MrSurly commented 6 years ago

Ok. That IOBase class allows you to implement read/write on the Python side, but not ioctl (but not hard to add the latter).

Yeah, that's something I was considering implementing (though I'd have to ask you how to do it!), for this use-case.

And, yes, I'd need ioctl.

Is that going to be merged?

It's worth noting that this is only to support my end goal of robust encryption over LoRa. I'm not even sure if it will work properly given the max LoRa payload size, and other factors (such as filtering on client MAC, etc). But I figure if it can be made to work for UDP (as intended), then there's a good shot at using that as a stepping stone to other transports.

nickzoic commented 6 years ago

Hmmm, this is also interesting to me as I'm looking at ESP-Now which passes small messages around in an ad-hoc manner (see #176)

It has a send method and a recv callback and I'd like to expose that as a generic datagram transport.

MrSurly commented 6 years ago

I haven't abandoned this. I'm juggling several work projects right now. This is required for one of them, and I'll revisit when I'm working again on that project.

malachib commented 6 years ago

Curious if you made any progress. I'm doing some similar work on the ESP8266 mbedtls DTLS and hitting handshake problems