feat(libSceLibreSsl): implement RSA_verify_PKCS1_PSS and RSA_padding_add_PKCS1_PSS #11175

Merged
ahmadmuj merged 31 commits from upstream-pr/11175 into main 2026-11-06 09:05:05 +00:00
ahmadmuj commented 2026-10-25 20:37:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T20:37:30Z.


What

Implements RSA_verify_PKCS1_PSS and RSA_padding_add_PKCS1_PSS, following LibreSSL 2.5.4 rsa_pss.c and PKCS1_MGF1 in rsa_oaep.c.

Head b6882dc6619b295599e5310fc73e16a0041671e4 merges main e16cf70591044d50e01e7111eadd0a7d0720be7e without rewriting published history. #11151, #11128, #10915 and #9448 are merged. The common guarded RSA body matches main exactly; the PSS extension and its test stay unchanged.

The merge resolves only CMake placement. It keeps the PSS target and main's private-RSA/PKEY targets once. Main's native32/legacy56 PKEY split and the documented forwarding gaps stay unchanged. No unsupported-throw assertion is added.

Fresh Linux Release checks: 404-step build passes. RSA, private RSA, PSS, EVP, errors, legacy PKEY lifecycle/RSA and native PKEY lifecycle pass (8/8). The inherited actual-NID binding check and 65 public headers pass. Conventions and whitespace pass. Earlier differential and mutation results below are reused for unchanged feature code. Windows and firmware reports by other contributors remain separate evidence.

  • Both calls read only rsa->n (bit count and RSA_size), like 2.5.4. No method slot is involved. A non-normalised or negative n behaves the way BN_num_bits would.
  • Digests and MGF1 go through #9448's EVP_DigestInit_ex/Update/Final_ex on a local context, so a guest-defined EVP_MD gets the same calls in the same order. hLen over 64 returns 0 through EVP_DigestFinal_ex, as in 2.5.4.
  • Same step order as 2.5.4, including the bytes written on failure: add writes EM[0] = 0 for a modulus with bits % 8 == 1 before its length check, and a null Hash or sLen < -2 returns 0 before rsa or EM is read.
  • The salt comes from host entropy instead of arc4random_buf.
  • Throws where 2.5.4 has UB or loops forever: int overflow in hLen + sLen + 2, an sLen of -2 that leaves a negative salt (at the salt digest update), an empty masked DB in verify (read past malloc(0)), MGF1 with a 0-byte digest, and the null pointers it dereferences. Verify with a masked DB length of -1 returns 0 and queues ERR_R_MALLOC_FAILURE, because 2.5.4's malloc(SIZE_MAX) fails there.
  • Each failing call queues the one RSA error 2.5.4 queues at that check (RSAerror in rsa_pss.c), and a success queues none. The EVP errors 2.5.4 adds for a null or over-64-byte digest are missing, since #9448's EVP functions queue none (in the debt entry). One new debt entry; the merged debt entry has no pending count.

Verify accepts a key from d2i_RSA_PUBKEY and pairs with RSA_public_decrypt using RSA_NO_PADDING. Add pairs with RSA_private_encrypt from #11151. Main now implements BN_new and BN_bin2bn for private-key construction.

Tested

The following feature checks predate this alignment. Fresh checks appear above.
Ubuntu 24.04 on WSL2, GCC 13.3, Release, on main + #11151 + #9448.

  • Differential against real LibreSSL 2.5.4 static libcrypto.a: a host generator recorded 6420 cases. They cover 80 moduli (124 to 4096 bits, every bits % 8 class near the length limits), MD5/SHA-256/SHA-384/SHA-512, sLen -1, -2, 0, small, max and max+1, and single-byte mutations of 2.5.4-made EMs (first octet, trailer, masked DB, H, salt, separator, wrong mHash). A guest harness (not committed) got these results:
    • add: 667/667 byte-exact with prefilled buffers; 267/267 salted outputs match the fixed bits and round-trip.
    • verify: 5122/5122 return values match.
    • 350 deliberate throws (2.5.4 UB, not run there).
  • Real 2.5.4 RSA_verify_PKCS1_PSS accepts all 267 salted outputs from the emulator.
  • Error queue: the generator also records 2.5.4's queued codes. 0 RSA-code differences, with all 8 codes from BunnyFl's table plus ERR_R_MALLOC_FAILURE hit. The only differences are 16 null-digest rows where 2.5.4 also queues the EVP code (the gap above).
  • New guest_libressl_rsa_pss covers:
    • exact 2.5.4 add bytes with sLen 0, and 2.5.4-made EMs with mutations;
    • salt-length boundaries per key and digest, and round trips on 513/1031/2048/4096-bit keys with all 4 digests;
    • two salted adds differing;
    • check order (null Hash, sLen < -2, trailer before the empty-DB throw);
    • the exact queued code for every failing check, one code per call, none on success or on a digest failure;
    • guest EVP_MDs whose init fails or whose size is 0, 65 or INT_MAX.
  • Red: with the exports throwing again, the test aborts (RSA_padding_add_PKCS1_PSS_nid_postfix not implemented). Each of 15 mutations fails the test, among them: no DB top-bit mask, an MGF1 counter starting at 1, EM[0] zeroed only on success, a zero salt, the empty-DB throw moved above the trailer check, and the negative-salt throw moved before the digest calls.
  • ctest -R libressl: 17/17 pass on main 9857eee + #10915 939a194 + #9448 b2583e3. public_headers: 65/65 pass. check_conventions.py --base origin/main passes.
  • Not tested: no console or game dump, no Windows or macOS build. SHA-1 isn't covered because #9448 doesn't export EVP_sha1.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (#11151, #11128, #10915 and #9448 are merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008468278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11175). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T20:37:30Z. --- ### What Implements `RSA_verify_PKCS1_PSS` and `RSA_padding_add_PKCS1_PSS`, following LibreSSL 2.5.4 [rsa_pss.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_pss.c) and `PKCS1_MGF1` in [rsa_oaep.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_oaep.c). Head `b6882dc6619b295599e5310fc73e16a0041671e4` merges main `e16cf70591044d50e01e7111eadd0a7d0720be7e` without rewriting published history. #11151, #11128, #10915 and #9448 are merged. The common guarded RSA body matches main exactly; the PSS extension and its test stay unchanged. The merge resolves only CMake placement. It keeps the PSS target and main's private-RSA/PKEY targets once. Main's native32/legacy56 PKEY split and the documented forwarding gaps stay unchanged. No unsupported-throw assertion is added. Fresh Linux Release checks: 404-step build passes. RSA, private RSA, PSS, EVP, errors, legacy PKEY lifecycle/RSA and native PKEY lifecycle pass (8/8). The inherited actual-NID binding check and 65 public headers pass. Conventions and whitespace pass. Earlier differential and mutation results below are reused for unchanged feature code. Windows and firmware reports by other contributors remain separate evidence. - Both calls read only `rsa->n` (bit count and `RSA_size`), like 2.5.4. No method slot is involved. A non-normalised or negative `n` behaves the way `BN_num_bits` would. - Digests and MGF1 go through #9448's `EVP_DigestInit_ex`/`Update`/`Final_ex` on a local context, so a guest-defined `EVP_MD` gets the same calls in the same order. `hLen` over 64 returns 0 through `EVP_DigestFinal_ex`, as in 2.5.4. - Same step order as 2.5.4, including the bytes written on failure: add writes `EM[0] = 0` for a modulus with `bits % 8 == 1` before its length check, and a null `Hash` or `sLen < -2` returns 0 before `rsa` or `EM` is read. - The salt comes from host entropy instead of `arc4random_buf`. - Throws where 2.5.4 has UB or loops forever: int overflow in `hLen + sLen + 2`, an `sLen` of -2 that leaves a negative salt (at the salt digest update), an empty masked DB in verify (read past `malloc(0)`), MGF1 with a 0-byte digest, and the null pointers it dereferences. Verify with a masked DB length of -1 returns 0 and queues `ERR_R_MALLOC_FAILURE`, because 2.5.4's `malloc(SIZE_MAX)` fails there. - Each failing call queues the one RSA error 2.5.4 queues at that check (`RSAerror` in `rsa_pss.c`), and a success queues none. The EVP errors 2.5.4 adds for a null or over-64-byte digest are missing, since #9448's EVP functions queue none (in the debt entry). One new debt entry; the merged debt entry has no pending count. Verify accepts a key from `d2i_RSA_PUBKEY` and pairs with `RSA_public_decrypt` using `RSA_NO_PADDING`. Add pairs with `RSA_private_encrypt` from #11151. Main now implements `BN_new` and `BN_bin2bn` for private-key construction. ### Tested The following feature checks predate this alignment. Fresh checks appear above. Ubuntu 24.04 on WSL2, GCC 13.3, Release, on `main` + #11151 + #9448. - Differential against real LibreSSL 2.5.4 static `libcrypto.a`: a host generator recorded 6420 cases. They cover 80 moduli (124 to 4096 bits, every `bits % 8` class near the length limits), MD5/SHA-256/SHA-384/SHA-512, `sLen` -1, -2, 0, small, max and max+1, and single-byte mutations of 2.5.4-made EMs (first octet, trailer, masked DB, H, salt, separator, wrong mHash). A guest harness (not committed) got these results: - add: 667/667 byte-exact with prefilled buffers; 267/267 salted outputs match the fixed bits and round-trip. - verify: 5122/5122 return values match. - 350 deliberate throws (2.5.4 UB, not run there). - Real 2.5.4 `RSA_verify_PKCS1_PSS` accepts all 267 salted outputs from the emulator. - Error queue: the generator also records 2.5.4's queued codes. 0 RSA-code differences, with all 8 codes from BunnyFl's table plus `ERR_R_MALLOC_FAILURE` hit. The only differences are 16 null-digest rows where 2.5.4 also queues the EVP code (the gap above). - New `guest_libressl_rsa_pss` covers: - exact 2.5.4 add bytes with `sLen` 0, and 2.5.4-made EMs with mutations; - salt-length boundaries per key and digest, and round trips on 513/1031/2048/4096-bit keys with all 4 digests; - two salted adds differing; - check order (null `Hash`, `sLen < -2`, trailer before the empty-DB throw); - the exact queued code for every failing check, one code per call, none on success or on a digest failure; - guest `EVP_MD`s whose init fails or whose size is 0, 65 or `INT_MAX`. - Red: with the exports throwing again, the test aborts (`RSA_padding_add_PKCS1_PSS_nid_postfix not implemented`). Each of 15 mutations fails the test, among them: no DB top-bit mask, an MGF1 counter starting at 1, `EM[0]` zeroed only on success, a zero salt, the empty-DB throw moved above the trailer check, and the negative-salt throw moved before the digest calls. - `ctest -R libressl`: 17/17 pass on `main` 9857eee + #10915 939a194 + #9448 b2583e3. `public_headers`: 65/65 pass. `check_conventions.py --base origin/main` passes. - Not tested: no console or game dump, no Windows or macOS build. SHA-1 isn't covered because #9448 doesn't export `EVP_sha1`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#11151, #11128, #10915 and #9448 are merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 20:39:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T20:39:30Z.

Review: COMMENTED


I reviewed the PSS implementation at head edf25645a5b39a8e31de065f47919910f5bd4a38 against the pinned LibreSSL 2.5.4 behavior, including the encoded-message length/top-bit handling, -1 and -2 salt modes, MGF1 counter encoding, and digest input order. I did not find a concrete correctness issue in the reviewed code. The committed tests cover boundary and mutation cases, and the PR reports differential results against the reference implementation.

Independent checks here: tools/check_conventions.py --base upstream/main --head HEAD, git diff --check upstream/main...HEAD, and C++20 syntax compilation of Rsa.cpp plus GuestLibreSslRsaPss.cpp passed. I could not run the guest test binary: this worktree has an uninitialized mbedTLS submodule, so the full runtime test/build remains unverified here. Hosted eligibility, conventions, intake, labels, and report checks passed; product build jobs were skipped. I’m leaving this as a comment rather than approval until runtime/hosted build evidence is available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645149288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11175#pullrequestreview-5645149288). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T20:39:30Z. **Review:** COMMENTED --- I reviewed the PSS implementation at head `edf25645a5b39a8e31de065f47919910f5bd4a38` against the pinned LibreSSL 2.5.4 behavior, including the encoded-message length/top-bit handling, `-1` and `-2` salt modes, MGF1 counter encoding, and digest input order. I did not find a concrete correctness issue in the reviewed code. The committed tests cover boundary and mutation cases, and the PR reports differential results against the reference implementation. Independent checks here: `tools/check_conventions.py --base upstream/main --head HEAD`, `git diff --check upstream/main...HEAD`, and C++20 syntax compilation of `Rsa.cpp` plus `GuestLibreSslRsaPss.cpp` passed. I could not run the guest test binary: this worktree has an uninitialized mbedTLS submodule, so the full runtime test/build remains unverified here. Hosted eligibility, conventions, intake, labels, and report checks passed; product build jobs were skipped. I’m leaving this as a comment rather than approval until runtime/hosted build evidence is available.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!11175
No description provided.