feat(core): reject chacha20poly1305 data after a partial block
What changed, and why it matters
This commit tightens the rules for a cryptographic helper (ChaCha20-Poly1305) inside Trezor hardware wallets. Previously, after you fed it a partial block of data, you could keep calling encrypt/decrypt with more data. Now the code enters a 'LAST' state after any partial block and refuses further encrypt/decrypt calls, only allowing the final finish call. This is a defensive correctness fix that prevents misuse of the cipher's streaming API, which could otherwise lead to nonce reuse or authentication failures. There is no claim in the commit that an actual exploit exists, and no CVE or vendor security advisory is supplied.
Treat as a defensive hardening patch. Review callers of the ChaCha20Poly1305 binding to confirm they already pass full blocks except for the final call, and ensure the change does not break legitimate streaming use cases. No urgent incident response is indicated by the commit materials alone.
Security signals we found
State-machine hardening of AEAD streaming API
Prevention of additional data after a partial ChaCha20-Poly1305 block
Potential misuse class: nonce reuse or tag computation over non-contiguous plaintext if caller splits data incorrectly
No changelog entry, suggesting routine hardening rather than advertised security fix
Evidence from the diff
The patch adds a LAST state to the MicroPython ChaCha20Poly1305 wrapper. encrypt()/decrypt() now set state to PROCESSING only when the input length is a multiple of CHACHA20POLY1305_BLOCK_SIZE; otherwise they set LAST. In LAST state, subsequent encrypt()/decrypt() calls raise RuntimeError, while encrypt_finish()/decrypt_finish() still accept INIT, PROCESSING, or LAST. This enforces the invariant that only a final, possibly partial block may follow full-block processing, preventing callers from supplying additional data after a partial block and potentially misusing the AEAD stream. The change is small and partial in the sense that it does not alter the underlying trezor-crypto implementation; it only hardens the Python binding.
Changed components
core/embed/upymod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.hTrezor Core firmware ChaCha20-Poly1305 MicroPython moduleInspect captured patch +5 / −4
### core/embed/upymod/modtrezorcrypto/modtrezorcrypto-chacha20poly1305.h
@@ -40,6 +40,7 @@ typedef struct _mp_obj_ChaCha20Poly1305_t {
enum {
INIT,
PROCESSING,
+ LAST,
FINISHED,
} state;
} mp_obj_ChaCha20Poly1305_t;
@@ -104,9 +105,9 @@ static mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_encrypt(mp_obj_t self,
if (o->state != INIT && o->state != PROCESSING) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Invalid state."));
}
- o->state = PROCESSING;
mp_buffer_info_t in = {0};
mp_get_buffer_raise(data, &in, MP_BUFFER_READ);
+ o->state = (in.len % CHACHA20POLY1305_BLOCK_SIZE == 0) ? PROCESSING : LAST;
vstr_t vstr = {0};
vstr_init_len(&vstr, in.len);
chacha20poly1305_encrypt(&(o->ctx), in.buf, (uint8_t *)vstr.buf, in.len);
@@ -123,7 +124,7 @@ static MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ChaCha20Poly1305_encrypt_obj,
static mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_encrypt_finish(
mp_obj_t self) {
mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self);
- if (o->state != INIT && o->state != PROCESSING) {
+ if (o->state != INIT && o->state != PROCESSING && o->state != LAST) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Invalid state."));
}
@@ -170,9 +171,9 @@ static mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_decrypt(mp_obj_t self,
if (o->state != INIT && o->state != PROCESSING) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Invalid state."));
}
- o->state = PROCESSING;
mp_buffer_info_t in = {0};
mp_get_buffer_raise(data, &in, MP_BUFFER_READ);
+ o->state = (in.len % CHACHA20POLY1305_BLOCK_SIZE == 0) ? PROCESSING : LAST;
vstr_t vstr = {0};
vstr_init_len(&vstr, in.len);
chacha20poly1305_decrypt(&(o->ctx), in.buf, (uint8_t *)vstr.buf, in.len);
@@ -189,7 +190,7 @@ static MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ChaCha20Poly1305_decrypt_obj,
static mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_decrypt_finish(
mp_obj_t self, mp_obj_t expected_mac) {
mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self);
- if (o->state != INIT && o->state != PROCESSING) {
+ if (o->state != INIT && o->state != PROCESSING && o->state != LAST) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Invalid state."));
}
Why this scored 40/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.