Implementation:Duckdb Duckdb Mbedtls RSA
| Knowledge Sources | |
|---|---|
| Domains | Cryptography, Third_Party |
| Last Updated | 2026-02-07 12:00 GMT |
Overview
The mbedTLS RSA module implements the RSA public-key cryptosystem as defined in PKCS#1 v1.5 and PKCS#1 v2.1, providing key generation, encryption, decryption, signature creation, and signature verification operations.
Description
This module provides a complete RSA implementation based on the original Rivest-Shamir-Adleman algorithm. The core data structure mbedtls_rsa_context stores all RSA key components:
- Public key: modulus
Nand public exponentE - Private key: private exponent
D, prime factorsPandQ, CRT optimization valuesDP,DQ,QP - Cached values: Montgomery parameters
RN,RP,RQand blinding valuesVi,Vf
The module supports two padding schemes:
- PKCS#1 v1.5 (
MBEDTLS_RSA_PKCS_V15): Traditional padding for encryption (RSAES-PKCS1-v1_5) and signatures (RSASSA-PKCS1-v1_5) - PKCS#1 v2.1 (
MBEDTLS_RSA_PKCS_V21): Modern padding for encryption (RSAES-OAEP) and signatures (RSASSA-PSS)
The rsa_alt_helpers.cpp file provides helper functions for deriving RSA parameters from partial key material, including mbedtls_rsa_deduce_primes() which recovers prime factors P and Q from the public and private exponents using a probabilistic algorithm based on the structure of the RSA group.
The minimum supported key size is configured via MBEDTLS_RSA_GEN_KEY_MIN_BITS (default 1024 bits). Thread safety is supported through an optional mutex when MBEDTLS_THREADING_C is enabled.
Usage
DuckDB uses the RSA module for:
- Extension signature verification: RSA signatures (PKCS#1 v1.5 with SHA-256) are used to verify that DuckDB extensions are authentically signed before loading
- TLS handshake: RSA key exchange and server certificate verification in the
httpfsextension's HTTPS connections - Key parsing: RSA public and private keys are parsed from DER/PEM formats during TLS certificate processing
Code Reference
Source Location
- Repository: Duckdb_Duckdb
- Files:
- third_party/mbedtls/include/mbedtls/rsa.h -- RSA API header (1164 lines)
- third_party/mbedtls/library/rsa.cpp -- RSA implementation (3066 lines)
- third_party/mbedtls/library/rsa_alt_helpers.cpp -- RSA helper functions (447 lines)
Signature
// Context lifecycle
void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
// Configuration
int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
mbedtls_md_type_t hash_id);
int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
// Key parsing (from DER-encoded data)
int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa,
const unsigned char *key, size_t keylen);
// RSA helper: deduce prime factors from public/private exponents
int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
mbedtls_mpi const *E,
mbedtls_mpi const *D,
mbedtls_mpi *P, mbedtls_mpi *Q);
Import
#include "mbedtls/rsa.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ctx | mbedtls_rsa_context * |
Yes | Initialized RSA context |
| padding | int |
Yes | Padding mode: MBEDTLS_RSA_PKCS_V15 (0) or MBEDTLS_RSA_PKCS_V21 (1)
|
| hash_id | mbedtls_md_type_t |
For V21 | Hash identifier for PSS/OAEP mask generation function |
| key | const unsigned char * |
For parsing | DER-encoded RSA key buffer |
| keylen | size_t |
For parsing | Length of the key buffer in bytes |
| N, E, D | const mbedtls_mpi * |
For deduce | RSA modulus, public exponent, private exponent (for prime factor recovery) |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | int |
0 on success; negative error codes include MBEDTLS_ERR_RSA_BAD_INPUT_DATA (-0x4080), MBEDTLS_ERR_RSA_INVALID_PADDING (-0x4100), MBEDTLS_ERR_RSA_KEY_CHECK_FAILED (-0x4200), MBEDTLS_ERR_RSA_VERIFY_FAILED (-0x4380)
|
| P, Q | mbedtls_mpi * |
Recovered prime factors (from mbedtls_rsa_deduce_primes)
|
| ctx | mbedtls_rsa_context * |
Populated RSA context after key parsing |
Usage Examples
// Initialize RSA context with PKCS#1 v1.5 padding
mbedtls_rsa_context rsa;
mbedtls_rsa_init(&rsa);
mbedtls_rsa_set_padding(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
// Parse a DER-encoded RSA key
const unsigned char *der_key = /* ... */;
size_t der_key_len = /* ... */;
int ret = mbedtls_rsa_parse_key(&rsa, der_key, der_key_len);
if (ret != 0) { /* handle error */ }
// Use rsa context for verification, encryption, etc.
// ...
mbedtls_rsa_free(&rsa);