From 634592a1e6fb10f48eb857a903c1543bf518428c Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 1 Aug 2024 03:32:56 +0300 Subject: [PATCH 1/2] Special yield() variant for thunked functions cont_suspend() wants stack pointer to be within cont.stack and panic()s otherwise https://github.com/esp8266/Arduino/issues/9170#issue-2425737426 stack_thunk_yield() func then gets added from the Core side, which should do 'SP store -> yield() -> SP restore' with known stack thunk values https://github.com/esp8266/Arduino/compare/master...mcspr:esp8266-Arduino:bssl/9170-thunk --- src/ec/ec_prime_i15.c | 2 +- src/ec/ec_prime_i31.c | 2 +- src/inner.h | 4 ++-- src/rsa/rsa_i15_priv.c | 6 +++--- src/rsa/rsa_i31_priv.c | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ec/ec_prime_i15.c b/src/ec/ec_prime_i15.c index 7e079f6..db2c241 100644 --- a/src/ec/ec_prime_i15.c +++ b/src/ec/ec_prime_i15.c @@ -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. diff --git a/src/ec/ec_prime_i31.c b/src/ec/ec_prime_i31.c index 1aba875..3080b05 100644 --- a/src/ec/ec_prime_i31.c +++ b/src/ec/ec_prime_i31.c @@ -464,7 +464,7 @@ run_code(jacobian *P1, const jacobian *P2, memcpy(t[P1x], P1->c, 3 * I31_LEN * sizeof(uint32_t)); memcpy(t[P2x], P2->c, 3 * I31_LEN * sizeof(uint32_t)); - optimistic_yield(10000); + stack_thunk_yield(); /* * Run formulas. diff --git a/src/inner.h b/src/inner.h index 0a62d9c..78a3101 100644 --- a/src/inner.h +++ b/src/inner.h @@ -2595,13 +2595,13 @@ br_cpuid(uint32_t mask_eax, uint32_t mask_ebx, #endif #define _debugBearSSL (0) - extern void optimistic_yield(uint32_t); + extern void stack_thunk_yield(); #ifdef __cplusplus } #endif #else - #define optimistic_yield(ignored) + #define stack_thunk_yield() #endif diff --git a/src/rsa/rsa_i15_priv.c b/src/rsa/rsa_i15_priv.c index 50318ce..f86db30 100644 --- a/src/rsa/rsa_i15_priv.c +++ b/src/rsa/rsa_i15_priv.c @@ -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. @@ -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. @@ -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: diff --git a/src/rsa/rsa_i31_priv.c b/src/rsa/rsa_i31_priv.c index f5a9c3c..5069602 100644 --- a/src/rsa/rsa_i31_priv.c +++ b/src/rsa/rsa_i31_priv.c @@ -135,7 +135,7 @@ br_rsa_i31_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. @@ -146,7 +146,7 @@ br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk) r &= br_i31_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i, mq + 3 * fwlen, TLEN - 3 * fwlen); - optimistic_yield(10000); + stack_thunk_yield(); /* * Compute s1 = x^dp mod p. @@ -178,7 +178,7 @@ br_rsa_i31_private(unsigned char *x, const br_rsa_private_key *sk) br_i31_decode_reduce(t1, sk->iq, sk->iqlen, mp); br_i31_montymul(t2, s1, t1, mp, p0i); - optimistic_yield(10000); + stack_thunk_yield(); /* * h is now in t2. We compute the final result: From 6cd56b54708ed9c05e4355e5c59458fda69c2afd Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 1 Aug 2024 03:45:00 +0300 Subject: [PATCH 2/2] no args --- src/inner.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inner.h b/src/inner.h index 78a3101..04d63cf 100644 --- a/src/inner.h +++ b/src/inner.h @@ -2595,7 +2595,7 @@ br_cpuid(uint32_t mask_eax, uint32_t mask_ebx, #endif #define _debugBearSSL (0) - extern void stack_thunk_yield(); + extern void stack_thunk_yield(void); #ifdef __cplusplus } #endif