feat(core/rust/crypto): add ECDSA public key uncompression
What changed, and why it matters
This commit adds a new internal helper function that converts compressed ECDSA public keys into their uncompressed form. It is a feature addition with no visible security bug: the code validates the key prefix, uses the correct buffer sizes, and returns an error if the underlying C function fails. There is no indication this fixes or introduces a vulnerability.
No security action required. Review as normal feature code; ensure callers validate the Result and do not bypass the prefix check.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change exposes trezor-crypto’s ecdsa_uncompress_pubkey through the Rust crypto crate. The new uncompress_pubkey wrapper checks that the first byte is 0x02 or 0x03 (compressed SEC1 encoding), allocates a 65-byte uncompressed buffer, calls the C function, and maps a non-success return into Error::InvalidEncoding. Tests cover round-trip compression/decompression, rejection of an uncompressed prefix (0x04), and rejection of an invalid x-coordinate for both secp256k1 and nist256p1.
Changed components
core/embed/crypto/src/ecdsa.rscore/embed/crypto/build.rsInspect captured patch +57 / −0
### core/embed/crypto/build.rs
@@ -173,6 +173,7 @@ fn add_crypto_base(lib: &mut CLibrary, common_attrs: &CompileAttrs) -> Result<()
.allowlist_var("nist256p1")
.allowlist_function("ecdsa_verify_digest")
.allowlist_function("ecdsa_recover_pub_from_sig")
+ .allowlist_function("ecdsa_uncompress_pubkey")
.allowlist_function("zkp_context_init")
// ed25519
.allowlist_type("ed25519_signature")
### core/embed/crypto/src/ecdsa.rs
@@ -159,6 +159,34 @@ pub fn verify_recover(
}
}
+/// Uncompress the compressed SEC1 `public_key` on `curve`.
+pub fn uncompress_pubkey(
+ curve: Curve,
+ public_key: &[u8; ECDSA_PUBLIC_KEY_COMPRESSED_SIZE],
+) -> Result<EcdsaPublicKey, Error> {
+ // the C code reads 65 bytes for a 0x04 prefix
+ if !matches!(public_key[0], 0x02 | 0x03) {
+ return Err(Error::InvalidEncoding);
+ }
+ let mut uncompressed = [0u8; ECDSA_PUBLIC_KEY_SIZE];
+ // SAFETY:
+ // * the curve is one of the supported builtin curves
+ // * public_key is a compressed public key of correct size
+ // * uncompressed has room for an uncompressed public key
+ let result = unsafe {
+ ffi::ecdsa_uncompress_pubkey(
+ curve.to_ffi_curve(),
+ public_key.as_ptr(),
+ uncompressed.as_mut_ptr(),
+ )
+ };
+ if result == 1 {
+ Ok(uncompressed)
+ } else {
+ Err(Error::InvalidEncoding)
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;
@@ -447,6 +475,34 @@ mod test {
}
}
+ #[test]
+ fn test_uncompress_pubkey() {
+ for v in VERIFY_VECTORS.iter().filter(|v| v.valid) {
+ let uncompressed: EcdsaPublicKey = v.pubkey().as_slice().try_into().unwrap();
+ let compressed = compress(&uncompressed);
+ assert_eq!(
+ uncompress_pubkey(v.curve, &compressed).unwrap(),
+ uncompressed
+ );
+
+ let mut bad_prefix = compressed;
+ bad_prefix[0] = 0x04;
+ assert!(matches!(
+ uncompress_pubkey(v.curve, &bad_prefix),
+ Err(Error::InvalidEncoding)
+ ));
+ }
+ // x beyond the field, not a point on either curve
+ let mut not_on_curve = [0xff; ECDSA_PUBLIC_KEY_COMPRESSED_SIZE];
+ not_on_curve[0] = 0x02;
+ for curve in [Curve::Secp256k1, Curve::Nist256p1] {
+ assert!(matches!(
+ uncompress_pubkey(curve, ¬_on_curve),
+ Err(Error::InvalidEncoding)
+ ));
+ }
+ }
+
#[test]
fn test_verify_digest_rejects_bad_pubkey_encoding() {
let v = &VERIFY_VECTORS[0];Why this scored 12/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.