chore(crypto): ensure `secp256k1_context` alignment
What changed, and why it matters
This commit fixes how a memory buffer used by the secp256k1 cryptographic library is aligned in memory. The library requires the buffer to be aligned for any data type, but the previous code used a plain byte array, which could be misaligned on some platforms or compilers. Misalignment can cause crashes or, in rare cases, subtle cryptographic failures. The fix explicitly aligns the buffer to the strictest alignment the C compiler supports.
Treat as a low-risk hardening fix. Include in routine firmware release notes as a robustness improvement. No urgent security response is indicated by the commit alone, but verify that other preallocated buffers in the codebase meet the same alignment contract.
Security signals we found
Memory alignment hardening for cryptographic context buffer
Undefined behavior mitigation in secp256k1-zkp preallocated context creation
Potential platform-dependent misalignment risk removed
Evidence from the diff
The change in crypto/zkp_context.c replaces a static uint8_t array used as preallocated storage for secp256k1_context with the same array annotated attribute((aligned(_Alignof(max_align_t)))). The secp256k1_context_preallocated_create() API requires caller-provided memory to be suitably aligned to hold an object of any type. Without this alignment guarantee, passing an unaligned buffer is undefined behavior under the C standard and the library’s contract. The patch is defensive and corrects a contract violation, but the diff alone does not demonstrate an exploitable bug or a known incident.
Changed components
crypto/zkp_context.csecp256k1_context lifecycleTrezor cryptographic operations using secp256k1-zkpInspect captured patch +7 / −1
### crypto/zkp_context.c
@@ -23,14 +23,20 @@
#include <assert.h>
#include <stdatomic.h>
#include <stdbool.h>
+#include <stddef.h>
#include "memzero.h"
#include "rand.h"
#include "zkp_context.h"
#include "vendor/secp256k1-zkp/include/secp256k1.h"
-static uint8_t context_buffer[SECP256K1_CONTEXT_SIZE];
+// Make sure `context_buffer` is properly aligned for `secp256k1_context`,
+// since it should be "aligned to hold an object of any type".
+// See `secp256k1_context_preallocated_create()` comment and
+// https://github.com/rust-bitcoin/rust-secp256k1/pull/233.
+static uint8_t context_buffer[SECP256K1_CONTEXT_SIZE]
+ __attribute__((aligned(_Alignof(max_align_t))));
static secp256k1_context *context;
static volatile atomic_flag locked;
Why this scored 46/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.