-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move BearSSL from STACK_PROXY to a real, thunked 2nd stack #5168
Conversation
Pull in latest BearSSL head (0.6 + minor additions) release and add AES_CCM modes to the encryption options.
406d9a6
to
ff9cbef
Compare
@Adam5Wu Here's the changes I was suggesting. Let me know your thoughts on where the thunks should live... |
Remove "#ifdef ESP8266;...;#else;...;#endif" brackets in BearSSL to ensure the host-tested code is the same as the ESP8266-run code.
Add reference counting br_thunk_add/del_ref() to replace stack handling code in the class. Add in stack painting and max usage calculation.
93cf9cf
to
2f83ca9
Compare
When a crash occurs while in the second stack, dump the BSSL stack and then also the stack that it was called from (either cont or sys).
BearSSL stack now decodes properly followed by the main stack
And decodes with the standard exception decoder to the right thing:
|
The thunk code needs to be visible to the core routines, so move it to the cores/esp8266 directory. Probably need to refactor the stack setup and the bearssl portion to avoid dependency on bearssl libs in cores/esp8266
This has been refactored per the discussion on gitter and is ready for review. |
Make stack_thunks generic, remove bearssl include inside of cores/esp8266. Allocate the stack on a WiFiServerSecure object creation to avoid fragmentation since we will need to allocate the stack to do any connected work, anyway. A stress test is now included which checks the total BearSSL second stack usage for a variety of TLS handshake and certificate options from badssl.org.
6facf7a
to
a25f023
Compare
👍 Thank you for making BearSSL rock-stable ! 👍 |
Run a series of SSL connection and transmission tests that stress BearSSL and its stack usage to the device tests. Modify device tests to include a possible SPIFFS generation and upload when a make_spiffs.py file is present in a test directory.
@pornin made BearSSL stable. I just make sure we don't make it unstable in the core. The latest push includes the stress test as part of the device test suite. It was a simpler hack running the device tests than I though it would be. |
Update to use the merged master branch of bearssl. Should have no code changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Merging this to get some exposure. |
This is still WIP, but it introduces a new bit to the BearSSL client so I wanted to get feedback early.-edit 10/27-
No longer WIP
BearSSL needs 5.6KB of stack for its own uses due to allocating large local variables in the encryption routines. To make this happen originally, I hacked almost every function in the BearSSL library to use a
#define
macro to allocate local variables on a SW managed stack.This meant that almost every function was edited, sometimes significantly (replacing a structure with a pointer-to-structure). This is painful and error prone, especially as new functions are added or existing ones modified.
To work around this I've written a simple "thunking" wrapper in assembly that changes the ESP8266's stack pointer itself to point to a local (heap-allocated) 2nd stack for function calls and then back to the main one on return. It's pretty simple conceptually, and effectively moves all BearSSL locals to a private stack without requiring any code changes in the library itself.
The assembly wrapper is maybe 20 lines of code and reasonably readable, and only a small subset of bearssl calls need to be wrapped due to stack depth (basically those calls related to actual SSL engine processing). Because the stack pointer itself is changed, not only the called function, but every function in the library that the main SSL function calls will use this secondary space.
There may be impact on the
postmortem
, and potentially interrupts (but I don't think it's really an issue, depends on where the IRQ call stack is located). (Interestingly enough, the postmortem still works mostly, but doesn't understand to skip stuff from the top of the BSSL stack until the bottom of the CONT stack...