Skip to content

Allow loading (compressed) SEC.1-encoded ECC keys via FFI and Python - #5083

Merged
reneme merged 4 commits into
randombit:masterfrom
Rohde-Schwarz:feature/load_compressed_ecc_key
Sep 1, 2025
Merged

Allow loading (compressed) SEC.1-encoded ECC keys via FFI and Python#5083
reneme merged 4 commits into
randombit:masterfrom
Rohde-Schwarz:feature/load_compressed_ecc_key

Conversation

@reneme

@reneme reneme commented Aug 29, 2025

Copy link
Copy Markdown
Collaborator

Desired Use Case Description

Currently, loading a SEC.1 encoded ECC key doesn't seem to be possible conveniently via the FFI. It provides two suitable functions: botan_pubkey_load() which expects a PEM container and the low-level botan_pubkey_load_ecdh() which requires both coordinates of the public point. Unfortunately in my use case I only have a compressed SEC.1 encoding of an ECDH public key, so *_load_ecdh() is not really feasible. As a work-around I am wrapping this encoding into a PEM container by hard-coding the ASN.1 prologue for my curve. Meh!

Changes in this PR

This adds three new FFI functions: botan_pubkey_load_ecdsa_sec1(), botan_pubkey_load_ecdh_sec1(), and botan_pubkey_load_sm2_sec1() to facilitate the above-described use case. Also, I exposed them via the Python wrapper like botan3.PublicKey.load_ecdh_sec1(), ...

Also, this PR bumps the FFI API requirement. Which I think is necessary because of the new APIs, right? I refrained from documenting those new functions, given that the existing low-level functions aren't explicitly documented either.

I'm certainly open for better ideas for naming or approach. Or perhaps I missed some way to do that on master?
Edit: Specifically for ECDH, one could drop the serialized compressed public point straight into PKAgreement.agree() and avoid this problem. For ECDSA that wouldn't work, though.

@reneme
reneme requested a review from randombit August 29, 2025 13:55
@reneme reneme self-assigned this Aug 29, 2025
@reneme reneme added the enhancement Enhancement or new feature label Aug 29, 2025
@reneme
reneme force-pushed the feature/load_compressed_ecc_key branch from b0321ed to d173533 Compare August 29, 2025 14:11
@coveralls

coveralls commented Aug 29, 2025

Copy link
Copy Markdown

Coverage Status

coverage: 90.671% (+0.002%) from 90.669%
when pulling 6babfa3 on Rohde-Schwarz:feature/load_compressed_ecc_key
into 73ef154 on randombit:master.

Comment thread src/tests/test_ffi.cpp Outdated
Comment thread src/scripts/test_python.py Outdated
Comment thread src/lib/ffi/ffi.cpp
This allows loading ECDH, ECDSA, and SM2 public keys that are encoded
in SEC1 format (both compressed and uncompressed). Without that, there's
no convenient way to load compressed SEC1 encoded ECC public keys. For
uncompressed points one could have manually parse the coordinates and
pass them into botan_pubkey_load_ec*_load().
@reneme
reneme force-pushed the feature/load_compressed_ecc_key branch from d173533 to 6babfa3 Compare September 1, 2025 07:13
@reneme
reneme requested a review from randombit September 1, 2025 07:15
@reneme
reneme merged commit a72cc07 into randombit:master Sep 1, 2025
45 checks passed
@reneme
reneme deleted the feature/load_compressed_ecc_key branch September 1, 2025 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement or new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants