top | item 38550026

Unveiling secrets of the ESP32: creating an open-source MAC layer

293 points| redfast00 | 2 years ago |zeus.ugent.be

149 comments

order

finnjohnsen2|2 years ago

I wasnt aware of this wifi blob. This feeds a tiny paranoia I have at the back of my head when dealing with esp32/espressif. I have dozens of esp32s around and I love them, but Espressif is 100% Chinese.

Im uncomfortable with what I read that every company of significant size in China automatically requires CCP party members to be involved in the company at a high level.

So Im very happy to hear people such as these guys are looking deep at this.

Ofcourse since Espressif controls the hardware, so they can do anything eventually. My itch will always be there and Im going to switch once I find something made in preferably the EU when I find something comparable to esp32. Maybe Nordic Semiconductors will make some nice risk-v chips and dev-boards soon.

RF_Savage|2 years ago

It is because of FCC certification requirements. Usually, if the end user can modify the lowlevel radio firmware on the device, the device looses it's FCC certification and cannot be sold in USA.

It also seems that Espressif has bought their wifi IP, so their contracts and licensing terms with the IP vendor likely prevent any sharing.

But FCC is the reason for closed binary blob firmware for all wifi radios out there these days.

ajsnigrutin|2 years ago

Wifi is easy... there's no way to send anything undetected, since you control the routers, etc.

GSM->5G modems are a lot harder to debug... maybe now in recent years with cheaper SDRs, but a lot harder then wifi.

And not sure why you'd be afraid of CCP, we saw the wikileaks, USA does a lot of similiarly bad stuff too and even got caught doing it... and if you live in a "western" country, USA has much easier access to you than China.

deegone|2 years ago

https://www.businesstimes.com.sg/events-awards/singapore-bus...

Learn more about Espressif's founder. And I think the CCP party cannot impact Espressif.

------- Singapore’s bilingual education gave the engineer an adequate command of Chinese; he played translator for his Chinese and non-Chinese speaking staff in meetings during Espressif’s early days.

And the time he spent in national service with the Singapore Armed Forces taught him the importance of being in the front line, of knowing the ground well.

The CEO believes that entrepreneurship cannot be taught. One needs to have a head for risk-taking, creativity, a big-picture perspective, and to be prepared to fail.

And passion, of course.

He has a nugget of wisdom for those who have yet to find theirs: “There are two things that drive people... One is passion, the other is fear... If you lose that fear, you might find your passion.”

boffinAudio|2 years ago

Your paranoia about the Chinese is equally applicable to Americans, whose NSA has given itself carte-blanche to infiltrate any computing system it desires, for whatever reason, in total secrecy - without recourse for the public to address any wrongs.

So I'm not sure that framing your paranoia in terms of "the Chinese" is productive - you might just want to update that thought with "any state actor who operates covert torture sites and violates human rights at immense scale", in which case your set of actually hostile actors becomes a little more realistic.

The biggest threat to your freedom and human rights, as an American, is your own government.

127361|2 years ago

Some old Realtek switch chips featured a protocol called RRCP[1] where you could write to the hardware registers using a specific type of Ethernet frame. So I guess a CCP-designed backdoor would probably detect a specially encrypted WiFi packet and allow then internal memory of the device to be written/read over the air. The key would be hardwired into the chip, part of the random logic - so there will be no visible block to identify on visual inspection of the die.

Or more subtly they could insert (or just not fix) a bug which allows packet descriptors to be overwritten on reception of a certain malformed WiFi packet, e.g. too short or long, which makes it possible to overwrite regions of the device's memory and thus compromise it. A SDR might be required to transmit the malformed packet(s).

By the way, I wonder if modern Realtek switch chips might still support RRCP, and an undocumented EEPROM bit or strapping resistor might re-enable it?

1. https://en.wikipedia.org/wiki/Realtek_Remote_Control_Protoco...

asylteltine|2 years ago

I restrict my esps to local network only absolutely no internet access for them. No trust for Chinese products from Chinese companies

fennecbutt|2 years ago

Tbf I think China is more interested in the money espressif makes than anything like spying. Because they'd be so easily caught out by anyone with a decent router. And if they ever happened then the whole company would be gibbed.

DeathArrow|2 years ago

I would rather have CCP listen than other parties. I am not a Chinese citizen, so I don't care.

DeathArrow|2 years ago

I use a Chinese phone with a Chinese ROM. I installed Google service as apps, with limited permissions. I'd rather have Uncle Xi listening than Uncle Sam.

Max-q|2 years ago

The article claims that the ESP32 costs $5. The reality is around half of that for the MCU, and around $3 for pre certified modules including crystal, PCB antenna or UF-L connector. So it's really affordable.

Espressif has also launched a new ESP32C3 based on RISC-V, with modules priced at around $2.

KRAKRISMOTT|2 years ago

That's the development unit price. ESP's MCUs have amazing value when you buy in bulk.

rwaksmunski|2 years ago

ESP32C3 has great Rust support too.

jareklupinski|2 years ago

that second core is worth its weight in gold

cant wait until they can make a dual-core risc-v but p.happy on the -s3

WatchDog|2 years ago

The section on trying to attenuate outside wifi signals interested me.

There is a bunch of hand wavy information on building faraday cages online, some people suggesting to utilize a microwave oven, since they operate at the same frequency.

There are even wifi faraday cages for sale on amazon.

However I can't really find much actual benchmark data online about how well these various approaches actually attenuate signals.

RF_Savage|2 years ago

Because the stuff on amazon is mostly targeted at consumers, not professionals.

Less about protocol development and more fear of 5G and wifi.

The prices of this things can be quite high. Which is why I bought my small one as used surplus.

One can find a great amount of models from various manufacturers here: https://www.everythingrf.com/search/shielded-test-enclosures

And none of them look cheap. But more "please call for quote"-type of things.

zamadatix|2 years ago

If the product doesn't list some form of dB loss (at at least 1 frequency) I assume it's more or less just a standard box. Sometimes that'll be right, sometimes it won't. Some of it comes down to how you use the box as well. If you're feeding un-isolated power in via a big hole in the back then it doesn't really matter how perfect the front is at blocking signals.

I'm surprised their paint can only gave them a 10 dB difference. I've found simply wrapping things in aluminum foil is good for about 40 dB when it comes to Wi-Fi.

AlotOfReading|2 years ago

A microwave oven is an excellent (30-40dB) attenuator specifically around 2.4GHz. The legal requirements essentially mandate 30dB + manufacturing safety margin. Your mileage will vary for other frequencies, as they're not actually faraday cages, so it's usually easier to simply use aluminum foil or a copper mesh/PVC box depending on your needs.

cchance|2 years ago

Not sure on specific numbers but based on the picture he posted of it in a can he would have probably done better with metal netting from a window screen or metal chicken wire it destroys wifi signal pretty damn effectively

raajg|2 years ago

For someone unexperienced with ESP32 but wanting to dip your toes, I'd highly recommend M5Stack - https://m5stack.com/ . No affiliation whatsoever, but I started playing with some basic boards last year for the first time and the the tiny devices they build have so many different sensors, transmitters, etc that you can start with a lot of early experiments just using a single device and a USB-C cable.

icpmacdo|2 years ago

I can attest to the challenges of the section on Dynamic analysis on real hardware and the struggles of attenuating signal interference on the ESP.

Anyone have a recommendation on conducting fabric for RF isolation as briefly mentioned in the article or resources on the subject of rf isolation/Faraday cages for microcontrollers?

redfast00|2 years ago

As part of the NLNet grant, I will build an affordable Faraday cage; I'll post the BOM, assembly process and a test report in a separate blog post.

PeterisP|2 years ago

We needed a large highly isolating cage for a project and the cost-effective (but still costly) way to do that was to build our own, having local welders make a custom steel box and lining it up with rf anechoic pyramid foam, otherwise the reflections make weird RF noise. A tricky part is the seams where your doors meet the box. It's easier to get good isolation from outside if you don't have to connect any wires, i.e. if any "control" you need is done by a battery-powered computer (laptop or smartphone) inside the isolation.

jcalvinowens|2 years ago

Based only on my own anecdotal experience... I think the hole the cables go through is the biggest problem in OPs setup. I'd solder the shields to the cage circumferentially around the hole. Shielded USB cables aren't too hard to find, it wouldn't be as good as something optical but it's a lot easier.

atVelocet|2 years ago

I used the isolation „fabric“ wrapped around the extension cable off a super cheap PCIe riser card. I put an iPhone inside which blocked WiFi, BT and 4G.

jjtech|2 years ago

…I wonder if this could be used to implement AWDL (Apple Wireless Direct Link) for use with AirDrop… if I recall correctly, the blocker on normal WiFi chipsets is being unable to send the ACK frames, which this should enable?

vbezhenar|2 years ago

What kind of programmer does one need to work with ESP32? I bought jlink for stm32 thinking that's the ultimate programmer for all my needs, however it does not claim compatibility with esp32.

connicpu|2 years ago

Depends a lot on the exact model of ESP32 you get. Even for non-devboards, a lot of the newer modules have a built in USB serial peripheral. If it's got a USB port, you're already ready to go. The ESP32-C3 QtPy modules from adafruit are awesome, they've got a USB-C port on them and you can just plug them in and they're ready to be flashed.

ultrarunner|2 years ago

They do have a boot loader that speaks uart, but are also compatible with jtag and openocd. However, I haven’t needed to burn the pins on jtag because they’ve implemented a gdb stub over uart, including the ability to trap interrupts and dump into a paused gdb session. It’s not ideal, but it’s pretty impressive from a preexisting tools perspective.

This doesn’t really answer your question, but it feels like it’s worth mentioning.

gh02t|2 years ago

They have a built in serial bootloader. Most dev boards don't need anything, they come with a USB-to-serial adapter. Some of the new ones even have the USB part integrated on chip.

If you use a bare module then you need a basic 3.3V serial adapter and maybe a jumper wire to ground pin 0 to enter programming mode.

You can also do stuff with JTAG, which will let you use a debugger, but that's not mandatory. Espressif sells a dirt cheap one. I think maybe you can use your jlink for that with some fiddling but I'm not certain.

halayli|2 years ago

You don't need anything. Plug it into your machine's usb, open arduino ide(or their web ide + agent) and upload any of the examples that arduino ide comes with and use its serial monitor to debug/read whatever output you write on the esp32's serial. This applies to windows/mac/linux.

sifttio|2 years ago

A usb cord to your computer. Flash the firmware with whatever you want. Check out WLED if you have wifi.

bobsmooth|2 years ago

If you get a dev kit they'll usually have a usb-serial interface for power and data.

vGPU|2 years ago

I’ve actually found that ESPHome is sufficient for most basic use cases

127361|2 years ago

I think Espressif have or at least used to have their own in-house developed MAC and PHY, which is not publicly documented.

For the Bouffalo Lab and Beken WiFi SoCs we already have SVD files[1] for the WiFi MAC (and likely the PHY too). Thus we have nearly complete documentation for all chip registers and their bitfields. Both SoCs are based on CEVA RivieraWaves WiFi IP.

Also you might be able to use it as a SDR for the 2.4GHz band, there appears to be registers to send ADC data to on-chip SRAM. And USB 2.0 High Speed device functionality on some of the Bouffalo chips.

I was thinking of hacking it to use as a cheap uplink to the QO-100 amateur radio satellite, which uplinks in the 2.4GHz band. I think 100mW of power might be just enough for CW or some very narrowband PSK mode.

By the way, on the Bouffalo devices, watch out for the eFuse registers, they're not fully lockable and write protectable, one wrong register write and the whole chip itself can be bricked and stuck permanently in secure boot mode. It happened to me, and I'm going to try and work around it by glitching the clock input on boot, just at the right time, to disrupt the eFuse reading, just for the fun of it.

1. https://github.com/bouffalolab/bl_iot_sdk/blob/master/compon...

londons_explore|2 years ago

> 50000 peripheral memory accesses are needed [to initialize the hardware]

Wow, that's a lot. If OP could upload somewhere the list of accesses together with a stack trace for each, I think we could crowd source a rewrite of each function - I'd be willing to bet the vast majority of those are repetitive patterns - ie. 'run this transmission test 1000 times while increasing the power levels each time until the received power = some set value'.

dgreensp|2 years ago

Headline should read "MAC" layer like it does in the article, not "Mac" layer. Two very different things :)

redfast00|2 years ago

Oops, my bad, copy-pasted it incorrectly; fixed now

droptablemain|2 years ago

I picked up an ESP32 devboard recently. I've always been intrigued by embedded but don't have a background in it at all.

I have no idea what my first project should be. Any ideas?

rgovostes|2 years ago

The barrier to entry has never been lower. Last night I prototyped some code in Python on my Mac to talk to a Bluetooth peripheral, and then had ChatGPT translate it to Arduino C++ code for a $5 ESP32, and it mostly worked on the first go.

You can even run Python on microcontrollers these days. See Adafruit's https://circuitpython.org for which they publish modules for many (almost all?) of the sensors they sell. The modern microcontroller frameworks hide much of the complexity of Wi-Fi, Bluetooth, filesystems, etc. so you can do complicated things with minimal effort. You can really cobble something together in an afternoon.

The "hello world" of microcontrollers is making an LED blink. Then figuring out how to print a message out over serial (print debugging is invaluable). Then maybe figure out how to make a Wi-Fi connection and an HTTP request. Then go on a shopping spree on Adafruit or SparkFun for $9 sensors that spark your imagination and figure out how to talk to them; Adafruit publishes zillions of tutorials you can copy from: https://learn.adafruit.com

jof|2 years ago

Make an LED blink.

Then, connect an RGB LED and experiment with PWM signal generation.

Then, experiment with network programming, accepting a UDP packet to the ESP32 that sets the color of the RGB LED.

anigbrowl|2 years ago

Depending on the board and language support, a good first time project is syncing a real-time clock to NTP. At least that was my first one, followed by a wifi signal scanner. I was using MicroPython which is fast and easy, C is a little more work obviously.

floitsch|2 years ago

Have a look at https://docs.toit.io/tutorials (using Toit).

I wrote lots of easy to follow tutorials there.

It depends a lot on the sensors you have. That said, even without any (or few) sensor(s) you can still have fun with network related applications like a Telegram bot.

halayli|2 years ago

There are a lot of interesting sensors out there. A good start can be by picking a sensor you're interested in and use it for monitoring. It can be as basic as temperature monitoring, or gas/CO2.

mardifoufs|2 years ago

I tried making a small servo tester as a first small project on the esp32. I obviously took the servo testing code from somewhere else but yeah, the control part was useful.

londons_explore|2 years ago

> 50000 peripheral memory accesses are needed

Have you tried just replaying those 50,000 accesses and seeing if things work? Obviously some things might not be correctly calibrated, but merely knowing that a simple replay works tells you that there are no complex hardware/software handshakes (ie. Take random token from here and write it to there). It also tells you that the process is probably fairly timing independent.

calamari4065|2 years ago

Wouldn't this invalidate the FCC certification on the prebuilt modules? You'd have to get certified with this firmware to ensure you aren't violating transmission power requirements.

Admittedly, this is a non-issue for hobby scale projects, but is potentially a blocker for commercial applications.

I wouldn't say it's necessarily a bad thing, but worth discussion.

throw0101b|2 years ago

> Wouldn't this invalidate the FCC certification on the prebuilt modules?

MAC is at OSI Layer 2. FCC concerns about radio power occur at the PHY layer, OSI Layer 1:

> On the ESP32, the PHY layer is implemented in hardware; most of the MAC layer is implemented in the proprietary blob. One notable exception to this separation is sending acknowlegement frame: if a device receives a frame, it should send a packet back to acknowledge that this packet was received correctly. This ACK packet needs to be sent within ~10 microseconds; it would be hard to get this timing correct in software.

* https://pics.zeus.gent/vYXyQm2t9pJCzpDdWFvq9oWR2DACoUJoTsYf8...

rurban|2 years ago

He should just go with stm and its open source LwMesh library instead.

But the closed radio parts are indeed horrible. Qualcomm (US Intelligence) and Broadcom (Chinese intelligence) controlling the physical layer underneath is as disturbing as the various Intel, AMD, ARM backdoors in their pre-OS layers.

madushan1000|2 years ago

I think bl602 shares the wifi rf/mac layer with esp32. There is a monitor mode implementation here https://github.com/stschake/bl60x-wifimon/

Sprite_tm|2 years ago

No, it doesn't. I know with 100% certainty ESP WiFi hardware is developed in-house and not shared. I think BL602 uses CEVA IP, not sure about that, but it certainly is not shared with ESP chips.

seba_dos1|2 years ago

Just a few months ago I was thinking "surely someone must have tried to RE the ESP32 Wi-Fi stack" and tried to find some research on it, but couldn't find anything. Great work!

no_time|2 years ago

the esp32 also has a mask ROM (which includes BASIC for some weird reason). Hence fully deblobbing it is a hopeless battle.

https://docs.espressif.com/projects/esp-idf/en/v4.3/esp32/ap...

wkat4242|2 years ago

Nothing an electron microscope can't handle. It's not a hardened security chip. It'll cost a bit but it's probably possible for a company to do it for free to show off their services. This is how die shots usually happen.

dezgeg|2 years ago

I would guess most 32-bit microcontrollers and any SoCs capable of running Linux have some sort of boot ROM, at least for the flash programming mode (especially if it's over USB).

calamari4065|2 years ago

Nothing about a mask ROM makes the data unrecoverable. It's still Memory that can be Read.

I'd be very surprised if there's not an exploit that will get the CPU to barf up the full ROM contents. That's if there isn't a more direct way to read it.

Even in the extremely unlikely case that it can't be read programmatically, you can always physically decode it with a microscope and a working eyeball.

From there, it's "just" a matter of decompiling the machine code into something readable. It's not trivial, but it can be done by a single person in a reasonable timeframe.

rkfjrjrkfnrkd|2 years ago

This is very interesting. I'm keen to get involved but, while I'm very experienced with ESP32, I don't have experience with this type of reverse engineering.

How long did it take you to get the environment and tools set up, so you could start digging in?

Is time or money a more valuable investment at this stage? If it's not too forward, how much would be useful to your organisation? (I can email if preferred.)

redfast00|2 years ago

Please contact me via email (at the bottom of the blog post)