Skip to content

Support AES-256 and post-quantum ML-KEM certificate encryption - #1815

Merged
andreasrosdalw merged 3 commits into
LibrePDF:masterfrom
andreasrosdal:post-quantum-public-key-encryption
Dec 3, 2026
Merged

andreasrosdalw merged 3 commits into
LibrePDF:masterfrom
andreasrosdal:post-quantum-public-key-encryption

Conversation

@andreasrosdal

@andreasrosdal andreasrosdal commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stronger encryption for documents that are encrypted for certificates (the public-key security handler):

  • AES-256 for certificate encryption. setEncryption(Certificate[], int[], PdfWriter.ENCRYPTION_AES_256_V3) on PdfWriter and PdfStamper now writes the AES-256 public-key security handler of ISO 32000-2 (/V 5, /SubFilter /adbe.pkcs7.s5, /CFM /AESV3, SHA-256 key derivation). PdfReader decrypts these documents. Before, this mode wrote a broken /V 4 dictionary with the RC4 crypt filter.
  • Post-quantum key encapsulation (ML-KEM, FIPS 203). A recipient certificate with an ML-KEM-512/768/1024 public key gets a CMS KEMRecipientInfo (RFC 9629, RFC 9936) that wraps the content key with AES-256-WRAP. This replaces RSA key transport, which a quantum computer could break. RSA recipients keep using key transport, and RSA and ML-KEM recipients can share one document. Decryption chooses the KEM or key transport recipient automatically.
  • AES-256 for the content is already considered quantum-resistant, so a document that is AES-256 + ML-KEM encrypted is protected end to end against "harvest now, decrypt later" attacks.
  • Hardening: a missing /Recipients array or a too-short decrypted seed now raises InvalidPdfException/UnsupportedPdfException instead of an NPE or an ArrayIndexOutOfBoundsException.

The Bouncy Castle provider used for ML-KEM is created privately and is not registered with java.security.Security.

API changes

No new public methods except BouncyCastleHelper.getProvider(). ENCRYPTION_AES_256_V3 with certificates now produces a valid AES-256 document.

Interoperability

AES-256 certificate encryption follows ISO 32000-2. ML-KEM recipients are read by OpenPDF and other CMS implementations with ML-KEM support. Adobe Acrobat does not support ML-KEM recipients yet.

Tests

PdfPublicKeyEncryptionTest: AES-256 round trips with RSA, ML-KEM-768 with AES-256 and AES-128, mixed RSA + ML-KEM recipients, PdfStamper, wrong-key rejection for RSA and ML-KEM, CMS structure checks. The full openpdf-core suite passes (2593 tests) and checkstyle:check is clean.

andreasrosdal and others added 2 commits December 3, 2026 14:56
Documents encrypted for certificates can use ENCRYPTION_AES_256_V3, the
AES-256 public-key security handler of ISO 32000-2 (V 5, AESV3, SHA-256
key derivation), for writing and reading. Before, this mode wrote a
broken V 4 dictionary with the RC4 crypt filter.

A recipient certificate with an ML-KEM (FIPS 203) public key gets a CMS
KEMRecipientInfo (RFC 9629, RFC 9936) that wraps the content key with
AES-256, so the document key is protected against quantum attacks. RSA
recipients still use key transport, and both can share one document.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codacy-production

codacy-production Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 27 complexity · 1 duplication

Metric Results
Complexity 27
Duplication 1

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@andreasrosdalw
andreasrosdalw merged commit f7a30a3 into LibrePDF:master Dec 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants