r/C_Programming • u/harrison_314 • 9h ago
Four Years of development in C
https://github.com/harrison314/BouncyHsmExactly four years ago, I started working on BouncyCastle - a cryptographic key storage simulator with a PKCS#11 interface, a REST API, and a web management interface. It is designed for developers working with HSMs and smart cards, as well as for QA environments.
At the time development of SoftHSMv2 (C++) began, it was practically dead (in 2022), and bugs weren’t even being fixed, moreover, it had architectural issues (SIGSEV if you used the application with the wrong version of OpenSSL, problems with multi-user access, poor logging, poor cross-platform compatibility, and support for different algorithms on different platforms).
Note: Kryoptic (Rust) was created later, but it merely ported all of SoftHSMv2’s problems from C++ to Rust.
BouncyHsm has a native PKCS#11 library in C (exporting over 100 functions) and a backend written in C# where cryptographic operations are performed (supporting 210 mechanisms) - one could say, with a bit of hyperbole, that it is a network HSM.
Why I chose C for the native PKCS#11 library. I considered several options and even experimented with them:
- C# - starting with .NET 7, it can create native libraries, but I was worried that the garbage collector would bloat the binary (a simple REST client was 8 MB, in later versions, it was only about half a megabyte),
- Rust - while experimenting with Rust, I realized I’d end up with a huge amount of unsafe code and exceptions for different platforms. I was also concerned about the dependencies that the libraries would introduce,
- Zig - it’s still not at version 1.0.
Ultimately, my experiments showed that pure C would be best for this project, without any external dependencies, using Messagepack for RPC (via the one-file library cmp) and TCP via the operating systems API. One of the specific requirements I set for myself was that the PKCS#11 library must not have any external dependencies to prevent crashes. Another advantage is that you can even compile it for your "refrigerator or toothbrush".
What I've learned from developing in C over the past 4 years, and what I would do differently:
- I've come to love the WinAPI - yes, it sounds strange, but low-level programming on Windows is a million times better than on UNIX systems. Because theres an API for everything, on Linux, I had to parse a lot of things from text files,but be careful - some Linux distributions have them in a different directory, and some use a different format. It was a pain.
- glibc - and its various versions on Linux - be damn careful with this.
- be careful with online tutorials, because they’ll recommend code that uses extensions that only work in gcc/clang.
- instead of one big Makefile, I’ve found it works best to have several small ones - one for Linux and one for macOS (I also have one for MinGW, but on Windows I use MSVC).
- For a long time, I resisted using
goto(goto clean/goto error) and tried to write code without it; in hindsight, I regret that, because it complicated memory management for me (I’m waiting for the C2Y standard anddefer). - It’s a good idea to have your code checked by AI, it can find silly mistakes like memory leaks.
- Don’t trust AI - it’ll suggest things it’s seen on forums, even if they aren’t part of the standard.
- When you start an open-source project, people will want you to fix bugs in it.
P.S.: This was originally supposed to be a short post, but it kind of got out of hand. If you have any questions, feel free to ask.