|
CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
|
#include "rsa.h"#include "cdc_log.h"#include <mbedtls/rsa.h>#include <mbedtls/bignum.h>#include <mbedtls/platform_util.h>#include <esp_random.h>#include <esp_attr.h>#include <string.h>Go to the source code of this file.
Functions | |
| static int | rsa_rng (void *ctx, unsigned char *out, size_t len) |
| mbedTLS RNG callback backed by the ESP hardware RNG. | |
| static bool | rd_u16 (const uint8_t *b, size_t len, size_t *pos, uint16_t *out) |
| Reads a big-endian u16 from a buffer and advances the cursor. | |
| static bool | rsa_load_ctx (const uint8_t *blob, size_t blob_len, mbedtls_rsa_context *rsa) |
Parses a private-key blob and imports it into an RSA context. On success the caller owns rsa and must free it. | |
| static bool | rsa_serialize (mbedtls_rsa_context *rsa, uint8_t *out, size_t cap, size_t *out_len) |
| Canonically serialises an RSA context (primes + exponent) into a blob. | |
| 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_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). | |
| 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. | |
Variables | |
| static const char * | TAG = "GPGRsa" |
| Software RSA backend for the OpenPGP card (see rsa.h). | |
| 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.
| n_bits | Modulus length in bits (2048 / 3072 / 4096). |
| e | Public exponent bytes (big-endian). |
| e_len | Public exponent length. |
| p | First prime (big-endian). |
| p_len | First prime length. |
| q | Second prime (big-endian). |
| q_len | Second prime length. |
| blob_out | Output buffer. |
| blob_cap | Output capacity. |
| blob_len_out | Receives the serialized length. |
Definition at line 110 of file rsa.cpp.
References LOG_W, rsa_serialize(), and TAG.
Referenced by cmd_put_data_odd().
| 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.
| blob | Serialized private-key blob. |
| blob_len | Blob length. |
| n_out | Receives the modulus (big-endian, n_bits/8 bytes). |
| n_cap | Capacity of n_out. |
| n_len_out | Receives the modulus length. |
| e_out | Receives the public exponent (big-endian). |
| e_cap | Capacity of e_out. |
| e_len_out | Receives the exponent length. |
Definition at line 156 of file rsa.cpp.
References rsa_load_ctx().
Referenced by build_rsa_pubkey_from_storage().
| 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.
| blob | Serialized private-key blob. |
| blob_len | Blob length. |
| ct | Ciphertext (exactly n_bits/8 bytes). |
| ct_len | Ciphertext length. |
| pt_out | Output plaintext buffer. |
| pt_cap | Capacity of pt_out. |
| pt_len_out | Receives the recovered plaintext length. |
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().
| 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.
| n_bits | Modulus length in bits (2048 / 3072 / 4096). |
| blob_out | Output buffer. |
| blob_cap | Output capacity. |
| blob_len_out | Receives the serialized length. |
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().
| 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).
| n_bits | Modulus length to test (2048 / 3072 / 4096). |
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().
| 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.
| blob | Serialized private-key blob. |
| blob_len | Blob length. |
| digestinfo | DigestInfo bytes to sign. |
| di_len | DigestInfo length. |
| sig_out | Output signature buffer (>= n_bits/8 bytes). |
| sig_cap | Capacity of sig_out. |
| sig_len_out | Receives the signature length (= n_bits/8). |
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().
|
static |
Reads a big-endian u16 from a buffer and advances the cursor.
Definition at line 29 of file rsa.cpp.
Referenced by rsa_load_ctx().
|
static |
Parses a private-key blob and imports it into an RSA context. On success the caller owns rsa and must free it.
Definition at line 40 of file rsa.cpp.
References rd_u16().
Referenced by gpg_rsa_blob_public(), gpg_rsa_decrypt(), gpg_rsa_selftest(), and gpg_rsa_sign().
|
static |
mbedTLS RNG callback backed by the ESP hardware RNG.
Definition at line 20 of file rsa.cpp.
Referenced by gpg_rsa_decrypt(), gpg_rsa_generate(), gpg_rsa_selftest(), and gpg_rsa_sign().
|
static |
Canonically serialises an RSA context (primes + exponent) into a blob.
Definition at line 70 of file rsa.cpp.
Referenced by gpg_rsa_blob_build(), and gpg_rsa_generate().