CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
rsa.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define GPG_RSA_MAX_MODULUS_BYTES   512
 Software RSA backend for the OpenPGP card (mbedTLS).

Functions

bool gpg_rsa_blob_build (uint16_t n_bits, const uint8_t *e, size_t e_len, const uint8_t *p, size_t p_len, const uint8_t *q, size_t q_len, uint8_t *blob_out, size_t blob_cap, size_t *blob_len_out)
 Serialises raw RSA components into a private-key blob.
bool gpg_rsa_generate (uint16_t n_bits, uint8_t *blob_out, size_t blob_cap, size_t *blob_len_out)
 Generates a fresh RSA key pair and serialises its private blob. The public exponent is fixed to 65537. This is computationally heavy on the ESP32-S3 (seconds to minutes for 4096); the caller is expected to keep the task watchdog fed.
bool gpg_rsa_blob_public (const uint8_t *blob, size_t blob_len, uint8_t *n_out, size_t n_cap, size_t *n_len_out, uint8_t *e_out, size_t e_cap, size_t *e_len_out)
 Extracts the public modulus and exponent from a private-key blob.
bool gpg_rsa_sign (const uint8_t *blob, size_t blob_len, const uint8_t *digestinfo, size_t di_len, uint8_t *sig_out, size_t sig_cap, size_t *sig_len_out)
 RSASSA-PKCS1-v1.5 signature over a host-supplied DigestInfo.
bool gpg_rsa_decrypt (const uint8_t *blob, size_t blob_len, const uint8_t *ct, size_t ct_len, uint8_t *pt_out, size_t pt_cap, size_t *pt_len_out)
 RSAES-PKCS1-v1.5 decryption of a cryptogram.
bool gpg_rsa_selftest (uint16_t n_bits)
 End-to-end self-test: generate a key, serialise/reload its blob, then sign+verify and encrypt+decrypt a known sample. Exercises the full software RSA path on the running firmware (no SE / NVS).

Macro Definition Documentation

◆ GPG_RSA_MAX_MODULUS_BYTES

#define GPG_RSA_MAX_MODULUS_BYTES   512

Software RSA backend for the OpenPGP card (mbedTLS).

The TROPIC01 secure element handles only ECC, so RSA roles are software keys. The serialized private-key blob layout (stored encrypted in R-Memory by GpgStorage) is, all length fields big-endian:

[n_bits:u16][e_len:u16][e...][p_len:u16][p...][q_len:u16][q...]

Only the two primes plus the public exponent are kept; mbedTLS reconstructs the remaining CRT parameters on load.

Supported RSA modulus sizes share this maximum byte count (4096).

Definition at line 24 of file rsa.h.

Referenced by build_rsa_pubkey_from_storage(), cmd_internal_authenticate(), cmd_pso_cds(), cmd_pso_decipher(), and gpg_rsa_selftest().

Function Documentation

◆ gpg_rsa_blob_build()

bool gpg_rsa_blob_build ( uint16_t n_bits,
const uint8_t * e,
size_t e_len,
const uint8_t * p,
size_t p_len,
const uint8_t * q,
size_t q_len,
uint8_t * blob_out,
size_t blob_cap,
size_t * blob_len_out )

Serialises raw RSA components into a private-key blob.

Parameters
n_bitsModulus length in bits (2048 / 3072 / 4096).
ePublic exponent bytes (big-endian).
e_lenPublic exponent length.
pFirst prime (big-endian).
p_lenFirst prime length.
qSecond prime (big-endian).
q_lenSecond prime length.
blob_outOutput buffer.
blob_capOutput capacity.
blob_len_outReceives the serialized length.
Returns
true when the components form a valid RSA private key.

Definition at line 110 of file rsa.cpp.

References LOG_W, rsa_serialize(), and TAG.

Referenced by cmd_put_data_odd().

◆ gpg_rsa_blob_public()

bool gpg_rsa_blob_public ( const uint8_t * blob,
size_t blob_len,
uint8_t * n_out,
size_t n_cap,
size_t * n_len_out,
uint8_t * e_out,
size_t e_cap,
size_t * e_len_out )

Extracts the public modulus and exponent from a private-key blob.

Parameters
blobSerialized private-key blob.
blob_lenBlob length.
n_outReceives the modulus (big-endian, n_bits/8 bytes).
n_capCapacity of n_out.
n_len_outReceives the modulus length.
e_outReceives the public exponent (big-endian).
e_capCapacity of e_out.
e_len_outReceives the exponent length.
Returns
true on success.

Definition at line 156 of file rsa.cpp.

References rsa_load_ctx().

Referenced by build_rsa_pubkey_from_storage().

◆ gpg_rsa_decrypt()

bool gpg_rsa_decrypt ( const uint8_t * blob,
size_t blob_len,
const uint8_t * ct,
size_t ct_len,
uint8_t * pt_out,
size_t pt_cap,
size_t * pt_len_out )

RSAES-PKCS1-v1.5 decryption of a cryptogram.

Parameters
blobSerialized private-key blob.
blob_lenBlob length.
ctCiphertext (exactly n_bits/8 bytes).
ct_lenCiphertext length.
pt_outOutput plaintext buffer.
pt_capCapacity of pt_out.
pt_len_outReceives the recovered plaintext length.
Returns
true on success.

Definition at line 267 of file rsa.cpp.

References LOG_W, rsa_load_ctx(), rsa_rng(), and TAG.

Referenced by cmd_pso_decipher(), and gpg_rsa_selftest().

◆ gpg_rsa_generate()

bool gpg_rsa_generate ( uint16_t n_bits,
uint8_t * blob_out,
size_t blob_cap,
size_t * blob_len_out )

Generates a fresh RSA key pair and serialises its private blob. The public exponent is fixed to 65537. This is computationally heavy on the ESP32-S3 (seconds to minutes for 4096); the caller is expected to keep the task watchdog fed.

Parameters
n_bitsModulus length in bits (2048 / 3072 / 4096).
blob_outOutput buffer.
blob_capOutput capacity.
blob_len_outReceives the serialized length.
Returns
true on success.

Definition at line 138 of file rsa.cpp.

References LOG_E, rsa_rng(), rsa_serialize(), and TAG.

Referenced by cmd_generate_keypair(), and gpg_rsa_selftest().

◆ gpg_rsa_selftest()

bool gpg_rsa_selftest ( uint16_t n_bits)

End-to-end self-test: generate a key, serialise/reload its blob, then sign+verify and encrypt+decrypt a known sample. Exercises the full software RSA path on the running firmware (no SE / NVS).

Parameters
n_bitsModulus length to test (2048 / 3072 / 4096).
Returns
true if every step round-trips correctly.

Definition at line 208 of file rsa.cpp.

References gpg_rsa_decrypt(), gpg_rsa_generate(), GPG_RSA_MAX_MODULUS_BYTES, gpg_rsa_sign(), LOG_E, LOG_I, rsa_load_ctx(), rsa_rng(), and TAG.

Referenced by cdc::mod_gpg::cmd_gpg_rsa_selftest().

◆ gpg_rsa_sign()

bool gpg_rsa_sign ( const uint8_t * blob,
size_t blob_len,
const uint8_t * digestinfo,
size_t di_len,
uint8_t * sig_out,
size_t sig_cap,
size_t * sig_len_out )

RSASSA-PKCS1-v1.5 signature over a host-supplied DigestInfo.

GnuPG sends the full DER DigestInfo as the PSO:CDS / INTERNAL AUTHENTICATE payload; the card applies the EMSA-PKCS1-v1.5 padding and the raw RSA private operation.

Parameters
blobSerialized private-key blob.
blob_lenBlob length.
digestinfoDigestInfo bytes to sign.
di_lenDigestInfo length.
sig_outOutput signature buffer (>= n_bits/8 bytes).
sig_capCapacity of sig_out.
sig_len_outReceives the signature length (= n_bits/8).
Returns
true on success.

Definition at line 185 of file rsa.cpp.

References LOG_W, rsa_load_ctx(), rsa_rng(), and TAG.

Referenced by cmd_internal_authenticate(), cmd_pso_cds(), and gpg_rsa_selftest().