Skip to content

Commit

Permalink
BearSSL panic on ESP8266 in rare conditions (#22017)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Aug 25, 2024
1 parent 8d6a4bd commit 5c30d92
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- Matter fixed UI bug when no endpoints configured
- Zigbee extend timeout for MCU reboot from 5s to 10s
- Matter fix when Rules are disabled
- BearSSL panic on ESP8266 in rare conditions

### Removed

Expand Down
2 changes: 1 addition & 1 deletion lib/lib_ssl/bearssl-esp8266/src/ec/ec_prime_i15.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ run_code(jacobian *P1, const jacobian *P2,
memcpy(t[P1x], P1->c, 3 * I15_LEN * sizeof(uint16_t));
memcpy(t[P2x], P2->c, 3 * I15_LEN * sizeof(uint16_t));

optimistic_yield(10000);
stack_thunk_yield();

/*
* Run formulas.
Expand Down
6 changes: 3 additions & 3 deletions lib/lib_ssl/bearssl-esp8266/src/rsa/rsa_i15_priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
mp = mq + 2 * fwlen;
memmove(mp, t1, fwlen * sizeof *t1);

optimistic_yield(10000);
stack_thunk_yield();

/*
* Compute s2 = x^dq mod q.
Expand All @@ -152,7 +152,7 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
r &= br_i15_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
mq + 3 * fwlen, TLEN - 3 * fwlen);

optimistic_yield(10000);
stack_thunk_yield();

/*
* Compute s1 = x^dq mod q.
Expand Down Expand Up @@ -184,7 +184,7 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
br_i15_decode_reduce(t1, sk->iq, sk->iqlen, mp);
br_i15_montymul(t2, s1, t1, mp, p0i);

optimistic_yield(10000);
stack_thunk_yield();

/*
* h is now in t2. We compute the final result:
Expand Down
6 changes: 3 additions & 3 deletions lib/lib_ssl/bearssl-esp8266/src/t_inner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2598,16 +2598,16 @@ br_cpuid(uint32_t mask_eax, uint32_t mask_ebx,

#define _debugBearSSL (0)
#ifdef ESP8266
extern void optimistic_yield(uint32_t);
extern void stack_thunk_yield(void);
#else
#define optimistic_yield(ignored)
#define stack_thunk_yield(ignored)
#endif
#ifdef __cplusplus
}
#endif

#else
#define optimistic_yield(ignored)
#define stack_thunk_yield(ignored)
#endif

#ifdef ESP32
Expand Down
22 changes: 22 additions & 0 deletions lib/lib_ssl/tls_mini/src/StackThunk_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

extern "C" {

extern void yield();
extern bool can_yield();

uint32_t *stack_thunk_light_ptr = NULL;
uint32_t *stack_thunk_light_top = NULL;
uint32_t *stack_thunk_light_save = NULL; /* Saved A1 while in BearSSL */
Expand All @@ -48,6 +51,25 @@ uint32_t stack_thunk_light_refcnt = 0;
#endif
#define _stackPaint 0xdeadbeef

void stack_thunk_yield()
{
if (can_yield()) {
uint32_t tmp;
register uint32_t* save __asm__("a3") = stack_thunk_light_save;

__asm__ __volatile__ (
"mov.n %0, a1\n\t"
"mov.n a1, %1\n\t"
: "=r"(tmp) : "r"(save) : "memory");

yield();

__asm__ __volatile__ (
"mov.n a1, %0\n\t"
:: "r"(tmp) : "memory");
}
}

/* Add a reference, and allocate the stack if necessary */
void stack_thunk_light_add_ref()
{
Expand Down
2 changes: 2 additions & 0 deletions lib/lib_ssl/tls_mini/src/StackThunk_light.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
extern "C" {
#endif

extern void stack_thunk_yield();

extern void stack_thunk_light_add_ref();
extern void stack_thunk_light_del_ref();
extern void stack_thunk_light_repaint();
Expand Down

0 comments on commit 5c30d92

Please sign in to comment.