Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions core/libs/prx/libSceLibreSsl/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ int APS5_VABI BIO_write_nid_postfix(LibreSsl::Bio* bio, const void* source, int
return LibreSslBioWrite_nid_no_patch(bio, source, size);
}

int APS5_VABI BN_print_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

void APS5_VABI CONF_modules_free_nid_postfix(void) {}

int APS5_VABI CONF_modules_load_file_nid_postfix(void) {
Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libSceLibreSsl/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ Bignum* APS5_VABI BN_mod_inverse_nid_postfix(Bignum* r, const Bignum* a, const B
int APS5_VABI BN_mul_nid_postfix(Bignum* r, const Bignum* a, const Bignum* b, BignumContext*);
Bignum* APS5_VABI BN_new_nid_postfix(void);
int APS5_VABI BN_num_bits_nid_postfix(const Bignum* a);
int APS5_VABI BN_print_nid_postfix(void);
int APS5_VABI BN_print_nid_postfix(LibreSsl::Bio* bio, const Bignum* a);
int APS5_VABI BN_set_word_nid_postfix(Bignum* a, std::uint64_t w);
int APS5_VABI BN_sub_word_nid_postfix(Bignum* a, std::uint64_t w);
void APS5_VABI CMAC_CTX_free_nid_postfix(LibreSslCmac* context);
Expand Down
17 changes: 17 additions & 0 deletions core/libs/prx/libSceLibreSsl/src/Bignum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ int APS5_VABI BN_mul_nid_postfix(Bignum* r, const Bignum* a, const Bignum* b, Bi
return Store(r, product, x.neg != y.neg);
}

int APS5_VABI BN_print_nid_postfix(LibreSsl::Bio* bio, const Bignum* a) {
static constexpr char digits[] = "0123456789ABCDEF";
Checked(a, __func__);
if (a->neg && LibreSslBioWrite_nid_no_patch(bio, "-", 1) != 1) return 0;
if (a->top == 0 && LibreSslBioWrite_nid_no_patch(bio, "0", 1) != 1) return 0;
bool started = false;
for (int i = a->top - 1; i >= 0; i--) {
for (int shift = 60; shift >= 0; shift -= 4) {
const int digit = static_cast<int>(a->d[i] >> shift) & 15;
if (!started && digit == 0) continue;
if (LibreSslBioWrite_nid_no_patch(bio, &digits[digit], 1) != 1) return 0;
started = true;
}
}
return 1;
}

Bignum* APS5_VABI BN_mod_inverse_nid_postfix(Bignum* r, const Bignum* a, const Bignum* n, BignumContext*) {
const Int value = Load(Checked(a, __func__));
const Int modulus = Load(Checked(n, __func__));
Expand Down
32 changes: 32 additions & 0 deletions core/libs/tests/GuestLibreSslBn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ static std::string Hex(const Bn* value) {
return value->neg ? "-" + text : text;
}

static std::string Printed(const Bn* value, int expected = 1) {
LibreSsl::Bio* bio = BIO_new_nid_postfix(BIO_s_mem_nid_postfix());
Require(bio && BN_print_nid_postfix(bio, value) == expected);
char* data = nullptr;
const auto length = BIO_ctrl_nid_postfix(bio, 3, 0, &data);
std::string text(data, static_cast<std::size_t>(length));
BIO_free_nid_postfix(bio);
return text;
}

static void Print() {
const char* inputs[][2] = {{"0", "0"}, {"-1A", "-1A"}, {"00F", "F"}, {"10000000000000000", "10000000000000000"},
{"123456789ABCDEF0FEDCBA9876543210", "123456789ABCDEF0FEDCBA9876543210"}};
for (const auto& [input, output] : inputs) {
Bn* value = FromHex(input);
Require(Printed(value) == output);
BN_free_nid_postfix(value);
}
std::uint64_t limbs[] = {0x2a, 0};
Bn padded{limbs, 2, 2, 0, 2};
Require(Printed(&padded) == "2A");
Bn negativeZero{limbs, 0, 2, 1, 2};
Require(Printed(&negativeZero) == "-0");
Bn negative{limbs, 1, 2, 1, 2};
LibreSsl::Bio* readOnly = BIO_new_mem_buf_nid_postfix("x", 1);
Require(readOnly && BN_print_nid_postfix(readOnly, &negative) == 0);
BIO_free_nid_postfix(readOnly);
ERR_clear_error_nid_postfix();
Require(BN_print_nid_postfix(nullptr, &negative) == 0);
}

static void InitializeHeap() {
void* const api[10] {};
ApplicationHeapRegister_nid_no_patch(api);
Expand Down Expand Up @@ -230,5 +261,6 @@ int main() {
for (Bn* value : all) BN_free_nid_postfix(value);
}
BN_CTX_free_nid_postfix(ctx);
Print();
return 0;
}
2 changes: 1 addition & 1 deletion docs/dev/debt/libSceLibreSsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

- [CMAC and AES-CBC descriptors](../../../core/libs/prx/libSceLibreSsl/src/Cmac.cpp) have no PS5 equivalence verification. Signatures, the 88-byte cipher method and 168-byte cipher context follow [LibreSSL 3.4.1](https://github.com/libressl/openbsd/blob/078b3d1df8ca2011c1605cd40418c58fa0f5b143/src/lib/libcrypto/cmac/cmac.c); the console version remains unknown. AES cipher data uses Mbed TLS storage instead of LibreSSL's private key schedule. Guest-defined cipher methods run through guest-ABI callbacks. ENGINE-backed methods throw. Null contexts, null nonempty update buffers, null initialized-final length pointers, negative cipher dimensions and missing required callbacks throw where upstream dereferences unchecked pointers. Error metadata names host source locations.

- [BN_new, BN_free, BN_CTX_new, BN_CTX_free, BN_CTX_start, BN_CTX_end, BN_bin2bn, BN_bn2bin, BN_hex2bn, BN_num_bits, BN_set_word, BN_add_word, BN_sub_word, BN_mul and BN_mod_inverse](../../../core/libs/prx/libSceLibreSsl/src/Bignum.cpp) (libSceLibreSsl) - console behaviour is unverified; the `BIGNUM` layout (24 bytes, 64-bit limbs, `top`, `dmax`, `neg`, `flags`) and documented semantics follow the public LibreSSL 2.x API. `BN_CTX` is opaque and only counts `BN_CTX_start`/`BN_CTX_end`, since `BN_CTX_get` is unimplemented, and `BN_mul` and `BN_mod_inverse` ignore it. `BN_mod_inverse` treats a negative modulus by its magnitude and returns 0 for a modulus of 1, uncompared with LibreSSL. `BN_hex2bn` clears or allocates the target on non-empty hex strings without digits, preserving negative zero per LibreSSL 2.5.4 rather than later versions that reject them. Null `BIGNUM` pointers, which LibreSSL does not check, throw. `BN_print` throws because it needs a `BIO`.
- [BN_new, BN_free, BN_CTX_new, BN_CTX_free, BN_CTX_start, BN_CTX_end, BN_bin2bn, BN_bn2bin, BN_hex2bn, BN_num_bits, BN_set_word, BN_add_word, BN_sub_word, BN_mul and BN_mod_inverse](../../../core/libs/prx/libSceLibreSsl/src/Bignum.cpp) (libSceLibreSsl) - console behaviour is unverified; the `BIGNUM` layout (24 bytes, 64-bit limbs, `top`, `dmax`, `neg`, `flags`) and documented semantics follow the public LibreSSL 2.x API. `BN_CTX` is opaque and only counts `BN_CTX_start`/`BN_CTX_end`, since `BN_CTX_get` is unimplemented, and `BN_mul` and `BN_mod_inverse` ignore it. `BN_mod_inverse` treats a negative modulus by its magnitude and returns 0 for a modulus of 1, uncompared with LibreSSL. `BN_hex2bn` clears or allocates the target on non-empty hex strings without digits, preserving negative zero per LibreSSL 2.5.4 rather than later versions that reject them. Null `BIGNUM` pointers, which LibreSSL does not check, throw. `BN_print` writes one hex digit per `BIO_write` call, as `bn_print.c` does.

Check warning on line 77 in docs/dev/debt/libSceLibreSsl.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1002 characters

- [Memory BIOs](../../../core/libs/prx/libSceLibreSsl/src/Bio.cpp) have no console execution verification. [Firmware 13.42 module comparisons on a PC](https://github.com/boykopovar/AnyPS5/pull/9536#issuecomment-6407192928) cover 1,048 sequences with stand-in allocators, including null/zero writes, callbacks and growth wiping. They support dispatching nonnegative writes to the method even with null source or zero length; LibreSSL 3.4.1 instead guards those calls. Negative public lengths retain that source's early return because the module report did not measure them. The 104-byte BIO, 80-byte method and 24-byte buffer use LP64 layouts. Allocator padding is zeroed rather than retaining the module allocator's fill. Other firmware and console imports remain unverified. CRYPTO ex-data, file, socket and SSL BIOs remain unsupported.

Expand Down
Loading