Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Duckdb Duckdb Mbedtls RSA

From Leeroopedia


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 N and public exponent E
  • Private key: private exponent D, prime factors P and Q, CRT optimization values DP, DQ, QP
  • Cached values: Montgomery parameters RN, RP, RQ and blinding values Vi, 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 httpfs extension's HTTPS connections
  • Key parsing: RSA public and private keys are parsed from DER/PEM formats during TLS certificate processing

Code Reference

Source Location

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);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment