Part of one story: hardening cryptographic key handling in open-source wallet libraries during my time at Coinbase. The companion post covers the money-tree fix, which removed unsafe OpenSSL patches from a Ruby BIP32 wallet library.
In March 2021 I was a Security Architect at Coinbase, working on wallet and key security: how the keys that protect customer funds are held and handled in code. That meant spending a lot of time with libsecp256k1, the elliptic-curve library that Bitcoin Core uses for secp256k1 signatures and keys. Out of that work I opened two pull requests and four issues proposing changes to the library’s API. None of the pull requests were merged, and all of the issues are closed. They were proposals, and this is a record of what I proposed and why.
The problem: a secret key is just 32 bytes
In the libsecp256k1 API of the time, a public key had an opaque type, but a secret key was a bare unsigned char[32]. Secret keys come in more than one form, though. The same key can be a raw 32-byte big-endian integer, or it can be wrapped in DER/ASN.1. When both look like a pointer to bytes, nothing stops a caller from passing the wrong one, and in wallet code that is exactly the kind of mistake that is easy to make and hard to notice.
PR #911: an opaque secret key and stronger byte types
Pull request #911, “Introduce opaque secret key and stronger types for fixed size byte arrays” (commit 49b4ef4, March 26, 2021), followed issue #912, where I proposed an opaque type for secret keys like the one public keys already had. The reasons were:
- Callers can’t mix up formats. With an opaque type, a raw scalar and an encoded key are different things to the compiler, not two byte buffers.
- Representation stays behind the type. Byte order and hardware-native key formats could be handled inside the library instead of by every caller.
- Room to carry more. A future key type could hold its public key too, so it doesn’t have to be computed again each time it’s needed.
- API stability for a v1 release. API stability was one of the things blocking a v1 release. An opaque type means the internals of a key can change later without breaking the API.
The maintainers pointed to the extrakeys module’s keypair type, which holds a private and public key together, as the direction for newer code. That type already answers several of the same concerns for the APIs built on it.
PR #914: a symbol for the library version
Pull request #914, “Introduce symbol to get version of library” (commit 1446af4, March 26, 2021), implemented issue #909. A program linked against a shared libsecp256k1 had no way to ask which version it had loaded. For key-handling code that has to know which behavior it is getting, that question matters.
The other issues
- Issue #908 asked for a 0.X release, so that libsecp256k1 could have a Homebrew formula and be installed like any other library.
- Issue #913 proposed a single context type with a two-phase setup: allocate, then initialize.
Where this fits
The same month, I worked on the Ruby side of the same problem. In money-tree, a Ruby BIP32 HD wallet library, I removed monkey patches to OpenSSL’s elliptic-curve Point class and a second, FFI-based OpenSSL binding. That pull request was merged in December 2021. Together the two are one piece of work: finding the places where a wallet library can mishandle a key without anyone noticing, and closing them.
This was my first serious look at secp256k1 itself, and it was about the edges: how keys cross an API boundary and what types can prevent at compile time. My later working paper on the SEC 2 prime Koblitz curves comes at the same curve from the other end, looking at how its parameters and generator were chosen. The ECC curve research guide collects that work.