XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
corecrypto: xnulinux's own interface, kernel side and provider, in place of Apple's (#25)
CommaBotc1cda535193e
diff --git a/LICENSE b/LICENSEindex 090eb57..5482762 100644--- a/LICENSE+++ b/LICENSE@@ -6,12 +6,14 @@ Each source file names its license in an SPDX-License-Identifier line; the fullAPSL-2.0 xnu-ko/, xnu-patches/, kexts/ (kexts/free/compat/ too)BSD-2-Clause-Patent kexts/XnuDisplay/, kexts/XnuEthernet/, kexts/XnuInput/ (from macOS-DroidVM),gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalShim.h (from UniXNU/AdrenoMetal)- MPL-2.0 kexts/free/patches/corecrypto-arm64e-sptr.patch (a patch to corecrypto, under its license)MIT gpu/xnu-drm/darwin/, gpu/xnu-drm/test/, gpu/xnu-drm/xnudrm_proto.h; the patches to Mesa and virglrenderer(under their upstream's license, as are the patches to metal2vulkan: GPL-2.0-or-later)docs/images/ holds screenshots of macOS running on xnulinux; the macOS interface and wallpaper in them are Apple's.+Mbed TLS (Apache-2.0 OR GPL-2.0-or-later), which the corecrypto kext (kexts/free/corecrypto/) is built with, is fetched+at build time at a pinned release (kexts/free/corecrypto/mbedtls.rev) and used under Apache-2.0; none of it is kept here.+Files without a comment syntax (JSON manifests, property lists, patches) are under the license of the directory they are in.Copyright (c) 2026 Heyang Zhou and contributors.diff --git a/LICENSES/MPL-2.0.txt b/LICENSES/MPL-2.0.txtdeleted file mode 100644index d0a1fa1..0000000--- a/LICENSES/MPL-2.0.txt+++ /dev/null@@ -1,373 +0,0 @@-Mozilla Public License Version 2.0-==================================--1. Definitions-----------------1.1. "Contributor"- means each individual or legal entity that creates, contributes to- the creation of, or owns Covered Software.--1.2. "Contributor Version"- means the combination of the Contributions of others (if any) used- by a Contributor and that particular Contributor's Contribution.--1.3. "Contribution"- means Covered Software of a particular Contributor.--1.4. "Covered Software"- means Source Code Form to which the initial Contributor has attached- the notice in Exhibit A, the Executable Form of such Source Code- Form, and Modifications of such Source Code Form, in each case- including portions thereof.--1.5. "Incompatible With Secondary Licenses"- means-- (a) that the initial Contributor has attached the notice described- in Exhibit B to the Covered Software; or-- (b) that the Covered Software was made available under the terms of- version 1.1 or earlier of the License, but not also under the- terms of a Secondary License.--1.6. "Executable Form"- means any form of the work other than Source Code Form.--1.7. "Larger Work"- means a work that combines Covered Software with other material, in- a separate file or files, that is not Covered Software.--1.8. "License"- means this document.--1.9. "Licensable"- means having the right to grant, to the maximum extent possible,- whether at the time of the initial grant or subsequently, any and- all of the rights conveyed by this License.--1.10. "Modifications"- means any of the following:-- (a) any file in Source Code Form that results from an addition to,- deletion from, or modification of the contents of Covered- Software; or-- (b) any new file in Source Code Form that contains any Covered- Software.--1.11. "Patent Claims" of a Contributor- means any patent claim(s), including without limitation, method,- process, and apparatus claims, in any patent Licensable by such- Contributor that would be infringed, but for the grant of the- License, by the making, using, selling, offering for sale, having- made, import, or transfer of either its Contributions or its- Contributor Version.--1.12. "Secondary License"- means either the GNU General Public License, Version 2.0, the GNU- Lesser General Public License, Version 2.1, the GNU Affero General- Public License, Version 3.0, or any later versions of those- licenses.--1.13. "Source Code Form"- means the form of the work preferred for making modifications.--1.14. "You" (or "Your")- means an individual or a legal entity exercising rights under this- License. For legal entities, "You" includes any entity that- controls, is controlled by, or is under common control with You. For- purposes of this definition, "control" means (a) the power, direct- or indirect, to cause the direction or management of such entity,- whether by contract or otherwise, or (b) ownership of more than- fifty percent (50%) of the outstanding shares or beneficial- ownership of such entity.--2. License Grants and Conditions-----------------------------------2.1. Grants--Each Contributor hereby grants You a world-wide, royalty-free,-non-exclusive license:--(a) under intellectual property rights (other than patent or trademark)- Licensable by such Contributor to use, reproduce, make available,- modify, display, perform, distribute, and otherwise exploit its- Contributions, either on an unmodified basis, with Modifications, or- as part of a Larger Work; and--(b) under Patent Claims of such Contributor to make, use, sell, offer- for sale, have made, import, and otherwise transfer either its- Contributions or its Contributor Version.--2.2. Effective Date--The licenses granted in Section 2.1 with respect to any Contribution-become effective for each Contribution on the date the Contributor first-distributes such Contribution.--2.3. Limitations on Grant Scope--The licenses granted in this Section 2 are the only rights granted under-this License. No additional rights or licenses will be implied from the-distribution or licensing of Covered Software under this License.-Notwithstanding Section 2.1(b) above, no patent license is granted by a-Contributor:--(a) for any code that a Contributor has removed from Covered Software;- or--(b) for infringements caused by: (i) Your and any other third party's- modifications of Covered Software, or (ii) the combination of its- Contributions with other software (except as part of its Contributor- Version); or--(c) under Patent Claims infringed by Covered Software in the absence of- its Contributions.--This License does not grant any rights in the trademarks, service marks,-or logos of any Contributor (except as may be necessary to comply with-the notice requirements in Section 3.4).--2.4. Subsequent Licenses--No Contributor makes additional grants as a result of Your choice to-distribute the Covered Software under a subsequent version of this-License (see Section 10.2) or under the terms of a Secondary License (if-permitted under the terms of Section 3.3).--2.5. Representation--Each Contributor represents that the Contributor believes its-Contributions are its original creation(s) or it has sufficient rights-to grant the rights to its Contributions conveyed by this License.--2.6. Fair Use--This License is not intended to limit any rights You have under-applicable copyright doctrines of fair use, fair dealing, or other-equivalents.--2.7. Conditions--Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted-in Section 2.1.--3. Responsibilities----------------------3.1. Distribution of Source Form--All distribution of Covered Software in Source Code Form, including any-Modifications that You create or to which You contribute, must be under-the terms of this License. You must inform recipients that the Source-Code Form of the Covered Software is governed by the terms of this-License, and how they can obtain a copy of this License. You may not-attempt to alter or restrict the recipients' rights in the Source Code-Form.--3.2. Distribution of Executable Form--If You distribute Covered Software in Executable Form then:--(a) such Covered Software must also be made available in Source Code- Form, as described in Section 3.1, and You must inform recipients of- the Executable Form how they can obtain a copy of such Source Code- Form by reasonable means in a timely manner, at a charge no more- than the cost of distribution to the recipient; and--(b) You may distribute such Executable Form under the terms of this- License, or sublicense it under different terms, provided that the- license for the Executable Form does not attempt to limit or alter- the recipients' rights in the Source Code Form under this License.--3.3. Distribution of a Larger Work--You may create and distribute a Larger Work under terms of Your choice,-provided that You also comply with the requirements of this License for-the Covered Software. If the Larger Work is a combination of Covered-Software with a work governed by one or more Secondary Licenses, and the-Covered Software is not Incompatible With Secondary Licenses, this-License permits You to additionally distribute such Covered Software-under the terms of such Secondary License(s), so that the recipient of-the Larger Work may, at their option, further distribute the Covered-Software under the terms of either this License or such Secondary-License(s).--3.4. Notices--You may not remove or alter the substance of any license notices-(including copyright notices, patent notices, disclaimers of warranty,-or limitations of liability) contained within the Source Code Form of-the Covered Software, except that You may alter any license notices to-the extent required to remedy known factual inaccuracies.--3.5. Application of Additional Terms--You may choose to offer, and to charge a fee for, warranty, support,-indemnity or liability obligations to one or more recipients of Covered-Software. However, You may do so only on Your own behalf, and not on-behalf of any Contributor. You must make it absolutely clear that any-such warranty, support, indemnity, or liability obligation is offered by-You alone, and You hereby agree to indemnify every Contributor for any-liability incurred by such Contributor as a result of warranty, support,-indemnity or liability terms You offer. You may include additional-disclaimers of warranty and limitations of liability specific to any-jurisdiction.--4. Inability to Comply Due to Statute or Regulation------------------------------------------------------If it is impossible for You to comply with any of the terms of this-License with respect to some or all of the Covered Software due to-statute, judicial order, or regulation then You must: (a) comply with-the terms of this License to the maximum extent possible; and (b)-describe the limitations and the code they affect. Such description must-be placed in a text file included with all distributions of the Covered-Software under this License. Except to the extent prohibited by statute-or regulation, such description must be sufficiently detailed for a-recipient of ordinary skill to be able to understand it.--5. Termination-----------------5.1. The rights granted under this License will terminate automatically-if You fail to comply with any of its terms. However, if You become-compliant, then the rights granted under this License from a particular-Contributor are reinstated (a) provisionally, unless and until such-Contributor explicitly and finally terminates Your grants, and (b) on an-ongoing basis, if such Contributor fails to notify You of the-non-compliance by some reasonable means prior to 60 days after You have-come back into compliance. Moreover, Your grants from a particular-Contributor are reinstated on an ongoing basis if such Contributor-notifies You of the non-compliance by some reasonable means, this is the-first time You have received notice of non-compliance with this License-from such Contributor, and You become compliant prior to 30 days after-Your receipt of the notice.--5.2. If You initiate litigation against any entity by asserting a patent-infringement claim (excluding declaratory judgment actions,-counter-claims, and cross-claims) alleging that a Contributor Version-directly or indirectly infringes any patent, then the rights granted to-You by any and all Contributors for the Covered Software under Section-2.1 of this License shall terminate.--5.3. In the event of termination under Sections 5.1 or 5.2 above, all-end user license agreements (excluding distributors and resellers) which-have been validly granted by You or Your distributors under this License-prior to termination shall survive termination.--************************************************************************-* *-* 6. Disclaimer of Warranty *-* ------------------------- *-* *-* Covered Software is provided under this License on an "as is" *-* basis, without warranty of any kind, either expressed, implied, or *-* statutory, including, without limitation, warranties that the *-* Covered Software is free of defects, merchantable, fit for a *-* particular purpose or non-infringing. The entire risk as to the *-* quality and performance of the Covered Software is with You. *-* Should any Covered Software prove defective in any respect, You *-* (not any Contributor) assume the cost of any necessary servicing, *-* repair, or correction. This disclaimer of warranty constitutes an *-* essential part of this License. No use of any Covered Software is *-* authorized under this License except under this disclaimer. *-* *-************************************************************************--************************************************************************-* *-* 7. Limitation of Liability *-* -------------------------- *-* *-* Under no circumstances and under no legal theory, whether tort *-* (including negligence), contract, or otherwise, shall any *-* Contributor, or anyone who distributes Covered Software as *-* permitted above, be liable to You for any direct, indirect, *-* special, incidental, or consequential damages of any character *-* including, without limitation, damages for lost profits, loss of *-* goodwill, work stoppage, computer failure or malfunction, or any *-* and all other commercial damages or losses, even if such party *-* shall have been informed of the possibility of such damages. This *-* limitation of liability shall not apply to liability for death or *-* personal injury resulting from such party's negligence to the *-* extent applicable law prohibits such limitation. Some *-* jurisdictions do not allow the exclusion or limitation of *-* incidental or consequential damages, so this exclusion and *-* limitation may not apply to You. *-* *-************************************************************************--8. Litigation----------------Any litigation relating to this License may be brought only in the-courts of a jurisdiction where the defendant maintains its principal-place of business and such litigation shall be governed by laws of that-jurisdiction, without reference to its conflict-of-law provisions.-Nothing in this Section shall prevent a party's ability to bring-cross-claims or counter-claims.--9. Miscellaneous-------------------This License represents the complete agreement concerning the subject-matter hereof. If any provision of this License is held to be-unenforceable, such provision shall be reformed only to the extent-necessary to make it enforceable. Any law or regulation which provides-that the language of a contract shall be construed against the drafter-shall not be used to construe this License against a Contributor.--10. Versions of the License------------------------------10.1. New Versions--Mozilla Foundation is the license steward. Except as provided in Section-10.3, no one other than the license steward has the right to modify or-publish new versions of this License. Each version will be given a-distinguishing version number.--10.2. Effect of New Versions--You may distribute the Covered Software under the terms of the version-of the License under which You originally received the Covered Software,-or under the terms of any subsequent version published by the license-steward.--10.3. Modified Versions--If you create software not governed by this License, and you want to-create a new license for such software, you may create and use a-modified version of this License if you rename the license and remove-any references to the name of the license steward (except to note that-such modified license differs from this License).--10.4. Distributing Source Code Form that is Incompatible With Secondary-Licenses--If You choose to distribute Source Code Form that is Incompatible With-Secondary Licenses under the terms of this version of the License, the-notice described in Exhibit B of this License must be attached.--Exhibit A - Source Code Form License Notice---------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public- License, v. 2.0. If a copy of the MPL was not distributed with this- file, You can obtain one at https://mozilla.org/MPL/2.0/.--If it is not possible or desirable to put the notice in a particular-file, then You may include the notice in a location (such as a LICENSE-file in a relevant directory) where a recipient would be likely to look-for such a notice.--You may add additional accurate notices of copyright ownership.--Exhibit B - "Incompatible With Secondary Licenses" Notice------------------------------------------------------------ This Source Code Form is "Incompatible With Secondary Licenses", as- defined by the Mozilla Public License, v. 2.0.diff --git a/docs/corecrypto.md b/docs/corecrypto.mdnew file mode 100644index 0000000..407b0d1--- /dev/null+++ b/docs/corecrypto.md@@ -0,0 +1,53 @@+<!-- SPDX-License-Identifier: GPL-2.0-only -->+# corecrypto: xnulinux's own interface and provider++XNU ships part of Apple's corecrypto: the interface's headers (`EXTERNAL_HEADERS/corecrypto`), a built-in subset compiled+into the kernel (`osfmk/corecrypto`, `osfmk/arm64/corecrypto`), all under Apple's corecrypto internal-use license. None of+it is used here:++| | XNU's | xnulinux's |+|---|---|---|+| The interface (types, layouts, declarations) | `EXTERNAL_HEADERS/corecrypto` (42 headers) | `xnu-patches/corecrypto/include/corecrypto` (written for xnulinux, APSL-2.0) |+| The kernel's side | `osfmk/corecrypto`: SHA-2, HMAC, HKDF, HMAC-DRBG, GCM and CBC code | `xnu-patches/corecrypto/kernel` (`osfmk/xnulinux_crypto`): no algorithm; digests and HMAC go to the registered provider, modes through their tables |+| Early random numbers (`early_random`, `read_erandom`) | an HMAC-DRBG seeded with HMAC-SHA-512 of the booter's seed | the host's generator (Linux's `get_random_bytes`, `LINUX64_CALL_RANDOM`); the boot seeds are the booter's 256 bytes as they are (`xnu-patches/0008`). `xnu.ko` waits for Linux's generator to be seeded before it takes any (`fki_random_wait`) |+| The provider (`com.apple.kec.corecrypto`) in `kc-free.bin` | (Apple's kext, in `kc-apple.bin` only) | `kexts/free/corecrypto`: Mbed TLS behind the interface |++`scripts/vm-xnu-rebuild.sh` removes XNU's three directories and copies xnulinux's in; `0008` takes the built-in files out of+the build and changes `osfmk/prng/prng_random.c`. Without the host the kernel has no early random numbers, so it boots+hosted only.++## The interface's ABI++`kc-apple.bin` still carries Apple's corecrypto kext, which registers with this kernel (`register_crypto_functions`,+`register_and_init_prng`): the structures and context layouts in the new headers are the ones that kext and the kernel's+callers agree on. `scripts/corecrypto-abi-check.sh` checks it: every kernel source that includes `<corecrypto/...>` is+compiled twice with its own recipe, once with XNU's headers (read from the k27 tree's history into a temporary directory,+never kept) and once with xnulinux's, and the objects' code, data, relocations and section sizes must be the same. All 22+such sources this configuration builds are, C and C++, xnulinux's kernel side and the PRNG's registration among them (the+one other, NFS's Kerberos, is not built). The check fails when a source other than that one has no recipe, or nothing was compared.++## The provider++`kexts/free/corecrypto` registers every function `struct crypto_functions` has:++| | |+|---|---|+| Digests | MD5, SHA-1, SHA-224/256/384/512: Mbed TLS's block functions and padding on the interface's digest contexts |+| HMAC | RFC 2104 over those digests, in the interface's HMAC context |+| AES | ECB, CBC, CTR, XTS (IEEE 1619 tweak over Mbed TLS's AES), GCM (Mbed TLS's; tags of 4, 8 and 12 to 16 bytes, SP 800-38D's); arm64 with the CPU's AES and SHA-256 instructions |+| DES, triple DES | ECB, CBC; weak keys and parity; CBC-CS3 (RFC 3962) over any CBC mode |+| ChaCha20-Poly1305 | Mbed TLS's (RFC 8439); not `chacha20poly1305_incnonce`, which nothing in the kernel calls (`CCMODE_NOT_SUPPORTED`) |+| RSA | PKCS #1 v1.5 signature verification (Mbed TLS's) |+| Random numbers | the kernel PRNG and `ccrng`: the host's generator. kmem's (`crypto_random_kmem_*`, which must take no lock once set up): ChaCha20 with fast key erasure, keyed from the host when kmem sets it up |++Mbed TLS is fetched at build time at the release `kexts/free/corecrypto/mbedtls.rev` pins (Apache-2.0); its configuration is+`mbedtls_config_xnulinux.h`. The kext runs known-answer tests when it starts and panics when one fails.++`kexts/free/corecrypto/test/run.sh` builds the same sources for Linux user space with AddressSanitizer and+UndefinedBehaviorSanitizer and tests them through the interface's calls against OpenSSL on random keys, lengths and call+splits: every digest and HMAC, AES in every mode and key size (GCM with random IV lengths, pieces of AAD and text, forged+tags; IPsec's `init_with_iv`, `reset`, `inc_iv` sequence), DES and triple DES, RFC 3962's CTS vectors, ChaCha20-Poly1305,+RSA signatures of 1024 to 4096 bits, and the uniformity of the random numbers; the edge cases (XTS key sizes, unknown digest OIDs, DES weak keys of either parity, ChaCha20-Poly1305 dropped halfway, a fresh digest context's block), and that each free names its allocation's size.++Tested in the krunkit VM: PureDarwin on `kc-free.bin` (the kext's self tests pass, the root mounts, its shell runs), and+macOS on the kernel with Apple's corecrypto kext (`kc-apple.bin`): the desktop in 36 s, `codesign -v` of an app.diff --git a/docs/free-kc.md b/docs/free-kc.mdindex e455f83..09a76d9 100644--- a/docs/free-kc.md+++ b/docs/free-kc.md@@ -11,7 +11,7 @@ dyld, libSystem, launchd, zsh and toybox) to its console shell.| Kext | From | Built as ||---|---|---|| XNU, `XnuCPU`, `XnuDisk`, `XnuDrm` | this repository | as for macOS (`vm-xnu-rebuild.sh`) |-| `com.apple.kec.corecrypto` | PureDarwin's interim corecrypto fork (MPL-2.0), pinned | `kexts/free/patches/corecrypto-arm64e-sptr.patch` |+| `com.apple.kec.corecrypto` | xnulinux (`kexts/free/corecrypto`: Mbed TLS behind the corecrypto interface, `docs/corecrypto.md`) | || `com.apple.kec.amfi` | PureDarwin's permissive AMFI stub | || `com.apple.kec.pthread` | PureDarwin (Apple's libpthread kext) | || `com.apple.iokit.IOStorageFamily`, `HFSEncodings`, `hfs` | PureDarwin (Apple's open-source projects) | |@@ -26,10 +26,8 @@ first makes an empty one: an `MH_FILESET` with the kernel's segments at the addrkernelcache and kernel collection of Apple's hidden from the build (a mount namespace with them replaced by empty files),the result is the same byte for byte. It also boots with no static trust cache (`TRUSTCACHE=`).-PureDarwin's prebuilt kexts cannot be used. They are arm64 and this kernel is arm64e, so calls from the kernel through their C++ vtables and-function pointers would fail pointer authentication. For the same reason the corecrypto patch signs the function-pointers that corecrypto shares with the kernel (digest and mode descriptors, the PRNG's function table, RNG handles) as-Apple's headers declare them (`CC_SPTR`).+PureDarwin's prebuilt kexts cannot be used. They are arm64 and this kernel is arm64e, so calls from the kernel through+their C++ vtables and function pointers would fail pointer authentication.`kexts/free/compat/` fills the few gaps between those sources and this kernel: `OSMalloc`, which this kernel no longerhas; the kernel-internal `kalloc_type_impl` that pthread and AMFI reach for; `devfs_is_ready`, which only PureDarwin's@@ -88,8 +86,6 @@ prelink info XNU's `OSKext` reads):## Not done yet-- corecrypto here is PureDarwin's interim fork. By its own README it is LLM-written and unaudited, and it must not be- relied on to protect anything.- The kernel headers the kexts build against come from this kernel's `installhdrs`. PureDarwin's AMFI stub also needsPureDarwin's CoreEntitlements and TrustCache headers.- Tested in the krunkit VM with 1 and 2 hosted CPUs: console shell, `launchctl list`, writes to the root, `halt`.diff --git a/docs/hosted-service.md b/docs/hosted-service.mdindex 5d5a1ac..e385773 100644--- a/docs/hosted-service.md+++ b/docs/hosted-service.md@@ -12,7 +12,7 @@ for what they install is the user's responsibility.|---|---|---|| Kernel collection | `kc-free.bin`: our XNU, our kexts, PureDarwin's kexts built from source | `kc-apple.bin`: the same kernel and our kexts, plus Apple's kexts, linked on the user's machine || System disk | PureDarwin's userland on HFS+ (we build it) | made by Apple's installer from Apple's IPSW (the user makes it) |-| Licenses | APSL-2.0, BSD, MIT; MPL-2.0 for corecrypto (§2.3) | Apple's, accepted by the user |+| Licenses | APSL-2.0, BSD, MIT; Apache-2.0 for Mbed TLS (corecrypto) | Apple's, accepted by the user |Both boot on the same `xnu.ko` and kernel, so a host can switch between them with `IMAGE=` (§4).@@ -55,11 +55,10 @@ macOS build we support gets its own kit, and `link.sh` refuses any other kernelc- `scripts/puredarwin-root.sh` takes the root file system from PureDarwin's `image-arm64-virt-minimal`, and`scripts/mk-puredarwin-disk.sh` makes the HFS+ disk and its `MAC_CONF`. Pin the PureDarwin revision, and ship itssource references with the disk.-- PureDarwin's kexts in `kc-free.bin` (`IOStorageFamily`, `HFSEncodings`, `hfs`, `pthread`, the AMFI stub,- corecrypto) are built from source as arm64e with this kernel's flags (`docs/free-kc.md`).-- **Blocker for a production launch:** the corecrypto in it is PureDarwin's interim fork. Its own README says it is- LLM-written and unaudited, and it is not to be relied on. Replace it before offering PureDarwin instances to anyone- who would store secrets in them.+- PureDarwin's kexts in `kc-free.bin` (`IOStorageFamily`, `HFSEncodings`, `hfs`, `pthread`, the AMFI stub) are built+ from source as arm64e with this kernel's flags (`docs/free-kc.md`); its corecrypto is xnulinux's, on Mbed TLS+ (`docs/corecrypto.md`).+- PureDarwin's userland still carries PureDarwin's interim `libcorecrypto` (unaudited, by its own README) on its disk.### 2.4 The GPU parts for macOS users@@ -142,7 +141,7 @@ Apple's installer (`tart` or another Virtualization.framework installer).| Gap | Effect on the service ||---|---|-| PureDarwin's corecrypto is unaudited (§2.3) | blocks offering PureDarwin for anything that keeps secrets |+| PureDarwin's userland `libcorecrypto` is PureDarwin's unaudited interim fork (§2.3) | replace it on the PureDarwin disk before offering it for anything that keeps secrets || Making the macOS disk needs a Mac with Xcode and `tart` (`mk-macvm-disk.sh`), and the PureDarwin disk needs `hdiutil` (`mk-puredarwin-disk.sh`) | users need a Mac to make their macOS disk; a Linux disk maker (from the IPSW's APFS images) would remove that. Check `tart`'s own license for commercial use, or use another Virtualization.framework installer. The PureDarwin disk should move to a Linux build (`mkfs.hfsplus`) so the pipeline runs without a Mac || One kit per macOS build, fixed segment slots | each macOS release needs a new kit before users can update || AMFI relaxed (`amfi_get_out_of_my_way=1`) and SIP partly off (`lp-sip0`); the OS cryptex merged into the system volume | the hosted macOS is less locked down than a real Mac. State this in the service's documentation (`docs/production.md` §3) |diff --git a/fki-ko/fki.h b/fki-ko/fki.hindex 20f403e..9a5f215 100644--- a/fki-ko/fki.h+++ b/fki-ko/fki.h@@ -54,6 +54,8 @@ void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);fki_u64 fki_counter_frequency(void);fki_u64 fki_counter(void);void fki_random(void *buf, unsigned long len);+/* sleeps until Linux's generator is seeded (process context): 0, or -errno when a signal comes first */+int fki_random_wait(void);/** Physically contiguous memory with real physical addresses (for a foreign kernel whose own page tables* the hardware walks). va is a kernel linear-map address (RW, not executable). size rounds up to a power of two.diff --git a/fki-ko/fki_mod.c b/fki-ko/fki_mod.cindex 56888e3..a8541dd 100644--- a/fki-ko/fki_mod.c+++ b/fki-ko/fki_mod.c@@ -248,6 +248,12 @@ void fki_random(void *buf, unsigned long len)}EXPORT_SYMBOL(fki_random);+int fki_random_wait(void)+{+ return wait_for_random_bytes();+}+EXPORT_SYMBOL(fki_random_wait);+/** cpu_list: the Linux CPUs of the foreign kernel's CPUs 0 .. n-1 (default: the same numbers). For machines where two* CPUs share a block-device interrupt (an NVMe with fewer queues than CPUs), which deadlocks two foreign CPUs on them.diff --git a/kexts/free/corecrypto/Info.plist b/kexts/free/corecrypto/Info.plistnew file mode 100644index 0000000..54b2aa1--- /dev/null+++ b/kexts/free/corecrypto/Info.plist@@ -0,0 +1,24 @@+<?xml version="1.0" encoding="UTF-8"?>+<!-- SPDX-License-Identifier: APSL-2.0 -->+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">+<plist version="1.0">+<dict>+ <key>CFBundleIdentifier</key>+ <string>com.apple.kec.corecrypto</string>+ <key>CFBundleName</key>+ <string>corecrypto</string>+ <key>CFBundleVersion</key>+ <string>1.0</string>+ <key>OSBundleLibraries</key>+ <dict>+ <key>com.apple.kpi.iokit</key>+ <string>19.0</string>+ <key>com.apple.kpi.libkern</key>+ <string>19.0</string>+ <key>com.apple.kpi.mach</key>+ <string>19.0</string>+ <key>com.apple.kpi.private</key>+ <string>19.0</string>+ </dict>+</dict>+</plist>diff --git a/kexts/free/corecrypto/cc_aes.c b/kexts/free/corecrypto/cc_aes.cnew file mode 100644index 0000000..8fb3ee0--- /dev/null+++ b/kexts/free/corecrypto/cc_aes.c@@ -0,0 +1,503 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * AES in the interface's modes. The block cipher, its key schedule and GCM are Mbed TLS's.+ *+ * ECB, CBC, CTR and XTS keep a compact key (the rounds and round keys of an Mbed TLS context, which fits the kernel's+ * fixed AES contexts: libkern/crypto/aes.h) and run each call on an Mbed TLS context loaded from it. XTS's tweak+ * (IEEE 1619) is the sector IV encrypted with the tweak key, multiplied by x in GF(2^128) after each block.+ */+#include "cc_internal.h"+#include <mbedtls/aes.h>+#include <mbedtls/gcm.h>+#include <mbedtls/platform_util.h>++struct xc_aes_key {+ uint32_t nr;+ uint32_t rk[60];+};++static int+aes_schedule(struct xc_aes_key *k, size_t key_nbytes, const void *key, int enc)+{+ mbedtls_aes_context c;+ int rc;++ if (key_nbytes != 16 && key_nbytes != 24 && key_nbytes != 32) {+ return CCMODE_INVALID_INPUT;+ }+ mbedtls_aes_init(&c);+ rc = enc ? mbedtls_aes_setkey_enc(&c, key, (unsigned)key_nbytes * 8) :+ mbedtls_aes_setkey_dec(&c, key, (unsigned)key_nbytes * 8);+ if (rc == 0) {+ k->nr = (uint32_t)c.nr;+ memcpy(k->rk, c.buf + c.rk_offset, (k->nr + 1) * 16);+ }+ mbedtls_aes_free(&c);+ mbedtls_platform_zeroize(&c, sizeof(c));+ return rc ? CCMODE_INTERNAL_ERROR : 0;+}++static void+aes_load(mbedtls_aes_context *c, const struct xc_aes_key *k)+{+ c->nr = (int)k->nr;+ c->rk_offset = 0;+ memcpy(c->buf, k->rk, (k->nr + 1) * 16);+}++static void+aes_blocks(const struct xc_aes_key *k, int mode, size_t nblocks, const uint8_t *in, uint8_t *out)+{+ mbedtls_aes_context c;++ aes_load(&c, k);+ for (; nblocks; nblocks--, in += 16, out += 16) {+ mbedtls_aes_crypt_ecb(&c, mode, in, out);+ }+ mbedtls_platform_zeroize(&c, sizeof(c));+}++/* ECB */+static int+ecb_init_enc(const struct ccmode_ecb *m, ccecb_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return aes_schedule((struct xc_aes_key *)ctx, n, key, 1);+}++static int+ecb_init_dec(const struct ccmode_ecb *m, ccecb_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return aes_schedule((struct xc_aes_key *)ctx, n, key, 0);+}++static int+ecb_enc(const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out)+{+ aes_blocks((const struct xc_aes_key *)ctx, MBEDTLS_AES_ENCRYPT, nblocks, in, out);+ return 0;+}++static int+ecb_dec(const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out)+{+ aes_blocks((const struct xc_aes_key *)ctx, MBEDTLS_AES_DECRYPT, nblocks, in, out);+ return 0;+}++static const struct ccmode_ecb aes_ecb_enc = {+ .size = sizeof(struct xc_aes_key), .block_size = 16, .init = ecb_init_enc, .ecb = ecb_enc,+};+static const struct ccmode_ecb aes_ecb_dec = {+ .size = sizeof(struct xc_aes_key), .block_size = 16, .init = ecb_init_dec, .ecb = ecb_dec,+};++/* CBC: the IV is the caller's and is left as the last ciphertext block */+static int+cbc_init_enc(const struct ccmode_cbc *m, cccbc_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return aes_schedule((struct xc_aes_key *)ctx, n, key, 1);+}++static int+cbc_init_dec(const struct ccmode_cbc *m, cccbc_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return aes_schedule((struct xc_aes_key *)ctx, n, key, 0);+}++static int+cbc_run(const cccbc_ctx *ctx, int mode, cccbc_iv *iv, size_t nblocks, const void *in, void *out)+{+ mbedtls_aes_context c;+ int rc;++ if (nblocks == 0) {+ return 0;+ }+ if (nblocks > SIZE_MAX / 16) {+ return CCMODE_INVALID_INPUT;+ }+ aes_load(&c, (const struct xc_aes_key *)ctx);+ rc = mbedtls_aes_crypt_cbc(&c, mode, nblocks * 16, (unsigned char *)iv, in, out);+ mbedtls_platform_zeroize(&c, sizeof(c));+ return rc ? CCMODE_INTERNAL_ERROR : 0;+}++static int+cbc_enc(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out)+{+ return cbc_run(ctx, MBEDTLS_AES_ENCRYPT, iv, nblocks, in, out);+}++static int+cbc_dec(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out)+{+ return cbc_run(ctx, MBEDTLS_AES_DECRYPT, iv, nblocks, in, out);+}++static const struct ccmode_cbc aes_cbc_enc = {+ .size = sizeof(struct xc_aes_key), .block_size = 16, .init = cbc_init_enc, .cbc = cbc_enc,+};+static const struct ccmode_cbc aes_cbc_dec = {+ .size = sizeof(struct xc_aes_key), .block_size = 16, .init = cbc_init_dec, .cbc = cbc_dec,+};++/* CTR: a 128-bit big-endian counter block; the unused keystream of the last block carries over between calls */+struct xc_ctr {+ struct xc_aes_key key;+ uint8_t counter[16];+ uint8_t stream[16];+ size_t offset;+};++static int+ctr_setctr(const struct ccmode_ctr *m, ccctr_ctx *ctx, const void *ctr)+{+ struct xc_ctr *c = (struct xc_ctr *)ctx;++ (void)m;+ memcpy(c->counter, ctr, 16);+ c->offset = 0;+ return 0;+}++static int+ctr_init(const struct ccmode_ctr *m, ccctr_ctx *ctx, size_t n, const void *key, const void *iv)+{+ int rc = aes_schedule(&((struct xc_ctr *)ctx)->key, n, key, 1);+ return rc ? rc : ctr_setctr(m, ctx, iv);+}++static int+ctr_crypt(ccctr_ctx *ctx, size_t nbytes, const void *in, void *out)+{+ struct xc_ctr *c = (struct xc_ctr *)ctx;+ mbedtls_aes_context a;+ int rc;++ aes_load(&a, &c->key);+ rc = mbedtls_aes_crypt_ctr(&a, nbytes, &c->offset, c->counter, c->stream, in, out);+ mbedtls_platform_zeroize(&a, sizeof(a));+ return rc ? CCMODE_INTERNAL_ERROR : 0;+}++static const struct ccmode_ctr aes_ctr = {+ .size = sizeof(struct xc_ctr), .block_size = 1, .ecb_block_size = 16, .init = ctr_init, .setctr = ctr_setctr,+ .ctr = ctr_crypt,+};++/* XTS */+struct xc_xts {+ struct xc_aes_key data;+ struct xc_aes_key tweak;+};++static int+xts_init_dir(ccxts_ctx *ctx, size_t n, const void *dk, const void *tk, int enc)+{+ struct xc_xts *x = (struct xc_xts *)ctx;+ int rc;++ if (n != 16 && n != 32) { /* XTS-AES-128 and -256 only */+ return CCERR_PARAMETER;+ }+ if (cc_cmp_safe(n, dk, tk) == 0) {+ return CCERR_XTS_KEYS_EQUAL;+ }+ rc = aes_schedule(&x->data, n, dk, enc);+ return rc ? rc : aes_schedule(&x->tweak, n, tk, 1);+}++static int+xts_init_enc(const struct ccmode_xts *m, ccxts_ctx *ctx, size_t n, const void *dk, const void *tk)+{+ (void)m;+ return xts_init_dir(ctx, n, dk, tk, 1);+}++static int+xts_init_dec(const struct ccmode_xts *m, ccxts_ctx *ctx, size_t n, const void *dk, const void *tk)+{+ (void)m;+ return xts_init_dir(ctx, n, dk, tk, 0);+}++static void+xts_key_sched_enc(const struct ccmode_xts *m, ccxts_ctx *ctx, size_t n, const void *dk, const void *tk)+{+ (void)xts_init_enc(m, ctx, n, dk, tk);+}++static void+xts_key_sched_dec(const struct ccmode_xts *m, ccxts_ctx *ctx, size_t n, const void *dk, const void *tk)+{+ (void)xts_init_dec(m, ctx, n, dk, tk);+}++static int+xts_set_tweak(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv)+{+ aes_blocks(&((const struct xc_xts *)ctx)->tweak, MBEDTLS_AES_ENCRYPT, 1, iv, (uint8_t *)tweak);+ return 0;+}++/* T = T * x in GF(2^128), T little-endian (IEEE 1619) */+static void+xts_next(uint8_t t[16])+{+ uint8_t carry = t[15] >> 7;++ for (int i = 15; i > 0; i--) {+ t[i] = (uint8_t)((t[i] << 1) | (t[i - 1] >> 7));+ }+ t[0] = (uint8_t)((t[0] << 1) ^ (0x87 & -carry));+}++static void *+xts_run(const ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in, void *out, int mode)+{+ const struct xc_xts *x = (const struct xc_xts *)ctx;+ uint8_t *t = (uint8_t *)tweak, b[16];+ const uint8_t *p = in;+ uint8_t *q = out;+ mbedtls_aes_context c;++ aes_load(&c, &x->data);+ for (; nblocks; nblocks--, p += 16, q += 16) {+ for (int i = 0; i < 16; i++) {+ b[i] = p[i] ^ t[i];+ }+ mbedtls_aes_crypt_ecb(&c, mode, b, b);+ for (int i = 0; i < 16; i++) {+ q[i] = b[i] ^ t[i];+ }+ xts_next(t);+ }+ mbedtls_platform_zeroize(&c, sizeof(c));+ mbedtls_platform_zeroize(b, sizeof(b));+ return tweak;+}++static void *+xts_enc(const ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in, void *out)+{+ return xts_run(ctx, tweak, nblocks, in, out, MBEDTLS_AES_ENCRYPT);+}++static void *+xts_dec(const ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in, void *out)+{+ return xts_run(ctx, tweak, nblocks, in, out, MBEDTLS_AES_DECRYPT);+}++static const struct ccmode_xts aes_xts_enc = {+ .size = sizeof(struct xc_xts), .tweak_size = 16, .block_size = 16, .init = xts_init_enc,+ .key_sched = xts_key_sched_enc, .set_tweak = xts_set_tweak, .xts = xts_enc,+};+static const struct ccmode_xts aes_xts_dec = {+ .size = sizeof(struct xc_xts), .tweak_size = 16, .block_size = 16, .init = xts_init_dec,+ .key_sched = xts_key_sched_dec, .set_tweak = xts_set_tweak, .xts = xts_dec,+};++/*+ * GCM: Mbed TLS's context, the IV (kept for inc_iv) and where in the call order the context is. A decryptor's finalize+ * compares the tag given with the one computed.+ */+enum { GCM_KEY, GCM_IV, GCM_AAD, GCM_TEXT, GCM_DONE };++struct xc_gcm {+ mbedtls_gcm_context g;+ uint8_t iv[CCGCM_IV_NBYTES];+ uint8_t state;+ uint8_t with_iv;+ uint8_t decrypt;+};++static int+gcm_init_dir(ccgcm_ctx *ctx, size_t n, const void *key, int decrypt)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;++ if (n != 16 && n != 24 && n != 32) {+ return CCMODE_INVALID_INPUT;+ }+ memset(c, 0, sizeof(*c));+ mbedtls_gcm_init(&c->g);+ if (mbedtls_gcm_setkey(&c->g, MBEDTLS_CIPHER_ID_AES, key, (unsigned)n * 8) != 0) {+ return CCMODE_INTERNAL_ERROR;+ }+ c->decrypt = (uint8_t)decrypt;+ c->state = GCM_IV;+ return 0;+}++static int+gcm_init_enc(const struct ccmode_gcm *m, ccgcm_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return gcm_init_dir(ctx, n, key, 0);+}++static int+gcm_init_dec(const struct ccmode_gcm *m, ccgcm_ctx *ctx, size_t n, const void *key)+{+ (void)m;+ return gcm_init_dir(ctx, n, key, 1);+}++static int+gcm_start(struct xc_gcm *c, size_t n, const void *iv)+{+ if (mbedtls_gcm_starts(&c->g, c->decrypt ? MBEDTLS_GCM_DECRYPT : MBEDTLS_GCM_ENCRYPT, iv, n) != 0) {+ return CCMODE_INVALID_INPUT;+ }+ c->state = GCM_AAD;+ return 0;+}++static int+gcm_set_iv(ccgcm_ctx *ctx, size_t n, const void *iv)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;++ if (c->state != GCM_IV || n == 0) {+ return c->state != GCM_IV ? CCMODE_INVALID_CALL_SEQUENCE : CCMODE_INVALID_INPUT;+ }+ if (n == CCGCM_IV_NBYTES) {+ memcpy(c->iv, iv, n);+ }+ return gcm_start(c, n, iv);+}++static int+gcm_aad(ccgcm_ctx *ctx, size_t n, const void *in)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;++ if (c->state != GCM_AAD) {+ return CCMODE_INVALID_CALL_SEQUENCE;+ }+ if (n == 0) {+ return 0;+ }+ return mbedtls_gcm_update_ad(&c->g, in, n) ? CCMODE_INVALID_INPUT : 0;+}++static int+gcm_crypt(ccgcm_ctx *ctx, size_t n, const void *in, void *out)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;+ size_t done;++ if (c->state != GCM_AAD && c->state != GCM_TEXT) {+ return CCMODE_INVALID_CALL_SEQUENCE;+ }+ c->state = GCM_TEXT;+ if (n == 0) {+ return 0;+ }+ if (mbedtls_gcm_update(&c->g, in, n, out, n, &done) != 0 || done != n) {+ return CCMODE_INVALID_INPUT;+ }+ return 0;+}++static int+gcm_finalize(ccgcm_ctx *ctx, size_t tag_nbytes, void *tag)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;+ uint8_t t[16];+ size_t none;+ int rc = 0;++ if (c->state != GCM_AAD && c->state != GCM_TEXT) {+ return CCMODE_INVALID_CALL_SEQUENCE;+ }+ /* the tag lengths of NIST SP 800-38D (5.2.1.2): 12 to 16 bytes, and 4 or 8 */+ if (tag_nbytes != 4 && tag_nbytes != 8 && (tag_nbytes < 12 || tag_nbytes > sizeof(t))) {+ return CCMODE_INVALID_INPUT;+ }+ if (mbedtls_gcm_finish(&c->g, NULL, 0, &none, t, sizeof(t)) != 0) {+ return CCMODE_INTERNAL_ERROR;+ }+ if (c->decrypt) {+ rc = cc_cmp_safe(tag_nbytes, t, tag) ? CCMODE_INTEGRITY_FAILURE : 0;+ } else {+ memcpy(tag, t, tag_nbytes);+ }+ mbedtls_platform_zeroize(t, sizeof(t));+ c->state = GCM_DONE;+ return rc;+}++/* back to before the IV, the key and the IV kept */+static int+gcm_reset(ccgcm_ctx *ctx)+{+ ((struct xc_gcm *)ctx)->state = GCM_IV;+ return 0;+}++static const struct ccmode_gcm aes_gcm_enc = {+ .size = sizeof(struct xc_gcm), .encdec = CCMODE_GCM_ENCRYPTOR, .block_size = 1, .init = gcm_init_enc,+ .set_iv = gcm_set_iv, .gmac = gcm_aad, .gcm = gcm_crypt, .finalize = gcm_finalize, .reset = gcm_reset,+};+static const struct ccmode_gcm aes_gcm_dec = {+ .size = sizeof(struct xc_gcm), .encdec = CCMODE_GCM_DECRYPTOR, .block_size = 1, .init = gcm_init_dec,+ .set_iv = gcm_set_iv, .gmac = gcm_aad, .gcm = gcm_crypt, .finalize = gcm_finalize, .reset = gcm_reset,+};++int+xc_gcm_init_with_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key, const void *iv)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;+ int rc = mode->init(mode, ctx, key_nbytes, key);++ if (rc == 0) {+ rc = gcm_set_iv(ctx, CCGCM_IV_NBYTES, iv);+ }+ if (rc == 0) {+ c->with_iv = 1;+ }+ return rc;+}++/* the next IV: the last 8 bytes of the IV counted up (big-endian); after a reset, on a context made with its IV */+int+xc_gcm_inc_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv)+{+ struct xc_gcm *c = (struct xc_gcm *)ctx;++ (void)mode;+ if (c->state != GCM_IV || !c->with_iv) {+ return CCMODE_INVALID_CALL_SEQUENCE;+ }+ for (int i = CCGCM_IV_NBYTES - 1; i >= CCGCM_IV_NBYTES - 8; i--) {+ if (++c->iv[i] != 0) {+ break;+ }+ }+ memcpy(iv, c->iv, CCGCM_IV_NBYTES);+ return gcm_start(c, CCGCM_IV_NBYTES, c->iv);+}++const struct ccmode_ecb *xc_aes_ecb_encrypt(void) { return &aes_ecb_enc; }+const struct ccmode_ecb *xc_aes_ecb_decrypt(void) { return &aes_ecb_dec; }+const struct ccmode_cbc *xc_aes_cbc_encrypt(void) { return &aes_cbc_enc; }+const struct ccmode_cbc *xc_aes_cbc_decrypt(void) { return &aes_cbc_dec; }+const struct ccmode_ctr *xc_aes_ctr(void) { return &aes_ctr; }+const struct ccmode_xts *xc_aes_xts_encrypt(void) { return &aes_xts_enc; }+const struct ccmode_xts *xc_aes_xts_decrypt(void) { return &aes_xts_dec; }+const struct ccmode_gcm *xc_aes_gcm_encrypt(void) { return &aes_gcm_enc; }+const struct ccmode_gcm *xc_aes_gcm_decrypt(void) { return &aes_gcm_dec; }diff --git a/kexts/free/corecrypto/cc_chachapoly.c b/kexts/free/corecrypto/cc_chachapoly.cnew file mode 100644index 0000000..53261a6--- /dev/null+++ b/kexts/free/corecrypto/cc_chachapoly.c@@ -0,0 +1,148 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ChaCha20-Poly1305 (RFC 8439): Mbed TLS's, its context in the caller's ccchacha20poly1305_ctx. Mbed TLS fixes the+ * direction when the nonce is set; the interface learns it from the first encrypt or decrypt call, so the nonce starts+ * an encryption and a decryption that comes before any text turns it around (the direction only decides which side of+ * the cipher Poly1305 reads).+ */+#include "cc_internal.h"+#include <mbedtls/chachapoly.h>+#include <mbedtls/platform_util.h>++_Static_assert(sizeof(mbedtls_chachapoly_context) <= sizeof(ccchacha20poly1305_ctx),+ "Mbed TLS's ChaCha20-Poly1305 context must fit the interface's");++#define C(ctx) ((mbedtls_chachapoly_context *)(void *)(ctx))++/* Mbed TLS's states of a message (library/chachapoly.c of the pinned release) */+enum { CCP_INIT = 0, CCP_AAD = 1, CCP_TEXT = 2 };++static const struct ccchacha20poly1305_info xc_ccp_info_value;++const struct ccchacha20poly1305_info *+xc_ccp_info(void)+{+ return &xc_ccp_info_value;+}++int+xc_ccp_init(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *key)+{+ (void)info;+ mbedtls_chachapoly_init(C(ctx));+ return mbedtls_chachapoly_setkey(C(ctx), key) ? CCERR_PARAMETER : 0;+}++/* the key kept, the message dropped: nothing but setnonce goes on from here */+int+xc_ccp_reset(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx)+{+ mbedtls_chachapoly_context *c = C(ctx);++ (void)info;+ mbedtls_platform_zeroize(&c->poly1305_ctx, sizeof(c->poly1305_ctx));+ c->aad_len = 0;+ c->ciphertext_len = 0;+ c->state = CCP_INIT;+ return 0;+}++int+xc_ccp_setnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *nonce)+{+ (void)info;+ return mbedtls_chachapoly_starts(C(ctx), nonce, MBEDTLS_CHACHAPOLY_ENCRYPT) ? CCERR_PARAMETER : 0;+}++/*+ * Not provided: chacha20poly1305_incnonce (libkern/crypto/chacha20poly1305.h) returns CCMODE_NOT_SUPPORTED. Nothing in+ * the kernel calls it; ESP sets each packet's nonce (setnonce).+ */+int+xc_ccp_incnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *nonce)+{+ (void)info;+ (void)ctx;+ (void)nonce;+ return CCMODE_NOT_SUPPORTED;+}++int+xc_ccp_aad(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *aad)+{+ (void)info;+ return mbedtls_chachapoly_update_aad(C(ctx), aad, nbytes) ? CCERR_CALL_SEQUENCE : 0;+}++static int+crypt(ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *in, void *out, mbedtls_chachapoly_mode_t mode)+{+ mbedtls_chachapoly_context *c = C(ctx);++ if (c->mode != mode) {+ if (c->ciphertext_len != 0) {+ return CCERR_CALL_SEQUENCE;+ }+ c->mode = mode;+ }+ return mbedtls_chachapoly_update(c, nbytes, in, out) ? CCERR_CALL_SEQUENCE : 0;+}++int+xc_ccp_encrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ptext, void *ctext)+{+ (void)info;+ return crypt(ctx, nbytes, ptext, ctext, MBEDTLS_CHACHAPOLY_ENCRYPT);+}++int+xc_ccp_decrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ctext, void *ptext)+{+ (void)info;+ return crypt(ctx, nbytes, ctext, ptext, MBEDTLS_CHACHAPOLY_DECRYPT);+}++/* a message ends once: Mbed TLS would finish a finished one again (state 3) */+static int+finish(ccchacha20poly1305_ctx *ctx, uint8_t tag[CCPOLY1305_TAG_NBYTES])+{+ mbedtls_chachapoly_context *c = C(ctx);++ if (c->state != CCP_AAD && c->state != CCP_TEXT) { /* a nonce set, the message not ended */+ return CCERR_CALL_SEQUENCE;+ }+ return mbedtls_chachapoly_finish(c, tag) ? CCERR_CALL_SEQUENCE : 0;+}++int+xc_ccp_finalize(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *tag)+{+ (void)info;+ return finish(ctx, tag);+}++/* 0 only when the tag is the message's */+int+xc_ccp_verify(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *tag)+{+ uint8_t t[CCPOLY1305_TAG_NBYTES];+ int rc;++ (void)info;+ if (finish(ctx, t)) {+ return CCERR_CALL_SEQUENCE;+ }+ rc = cc_cmp_safe(sizeof(t), t, tag) ? CCMODE_INTEGRITY_FAILURE : 0;+ mbedtls_platform_zeroize(t, sizeof(t));+ return rc;+}diff --git a/kexts/free/corecrypto/cc_des.c b/kexts/free/corecrypto/cc_des.cnew file mode 100644index 0000000..0fff955--- /dev/null+++ b/kexts/free/corecrypto/cc_des.c@@ -0,0 +1,231 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * DES and triple DES (Mbed TLS's), for Kerberos and the other protocols that still name them, and CBC with ciphertext+ * stealing (CS3: the last two blocks always swapped, RFC 3962) over any CBC mode.+ */+#include "cc_internal.h"+#include <mbedtls/des.h>+#include <mbedtls/platform_util.h>++/* DES */+static int+des_init_enc(size_t n, void *ctx, const void *key)+{+ return n == 8 && mbedtls_des_setkey_enc(ctx, key) == 0 ? 0 : CCMODE_INVALID_INPUT;+}++static int+des_init_dec(size_t n, void *ctx, const void *key)+{+ return n == 8 && mbedtls_des_setkey_dec(ctx, key) == 0 ? 0 : CCMODE_INVALID_INPUT;+}++static int des_ecb_init_enc(const struct ccmode_ecb *m, ccecb_ctx *c, size_t n, const void *k) { (void)m; return des_init_enc(n, c, k); }+static int des_ecb_init_dec(const struct ccmode_ecb *m, ccecb_ctx *c, size_t n, const void *k) { (void)m; return des_init_dec(n, c, k); }+static int des_cbc_init_enc(const struct ccmode_cbc *m, cccbc_ctx *c, size_t n, const void *k) { (void)m; return des_init_enc(n, c, k); }+static int des_cbc_init_dec(const struct ccmode_cbc *m, cccbc_ctx *c, size_t n, const void *k) { (void)m; return des_init_dec(n, c, k); }++static int+des_ecb(const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out)+{+ const uint8_t *p = in;+ uint8_t *q = out;++ for (; nblocks; nblocks--, p += 8, q += 8) {+ mbedtls_des_crypt_ecb((mbedtls_des_context *)ctx, p, q);+ }+ return 0;+}++static int+des_cbc(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out, int mode)+{+ if (nblocks == 0) {+ return 0;+ }+ if (nblocks > SIZE_MAX / 8) {+ return CCMODE_INVALID_INPUT;+ }+ return mbedtls_des_crypt_cbc((mbedtls_des_context *)ctx, mode, nblocks * 8, (unsigned char *)iv, in, out) ?+ CCMODE_INTERNAL_ERROR : 0;+}++static int des_cbc_enc(const cccbc_ctx *c, cccbc_iv *iv, size_t n, const void *in, void *out) { return des_cbc(c, iv, n, in, out, MBEDTLS_DES_ENCRYPT); }+static int des_cbc_dec(const cccbc_ctx *c, cccbc_iv *iv, size_t n, const void *in, void *out) { return des_cbc(c, iv, n, in, out, MBEDTLS_DES_DECRYPT); }++/* triple DES: two keys (16 bytes) or three (24) */+static int+des3_init(size_t n, void *ctx, const void *key, int enc)+{+ int rc;++ if (n == 16) {+ rc = enc ? mbedtls_des3_set2key_enc(ctx, key) : mbedtls_des3_set2key_dec(ctx, key);+ } else if (n == 24) {+ rc = enc ? mbedtls_des3_set3key_enc(ctx, key) : mbedtls_des3_set3key_dec(ctx, key);+ } else {+ return CCMODE_INVALID_INPUT;+ }+ return rc ? CCMODE_INVALID_INPUT : 0;+}++static int des3_ecb_init_enc(const struct ccmode_ecb *m, ccecb_ctx *c, size_t n, const void *k) { (void)m; return des3_init(n, c, k, 1); }+static int des3_ecb_init_dec(const struct ccmode_ecb *m, ccecb_ctx *c, size_t n, const void *k) { (void)m; return des3_init(n, c, k, 0); }+static int des3_cbc_init_enc(const struct ccmode_cbc *m, cccbc_ctx *c, size_t n, const void *k) { (void)m; return des3_init(n, c, k, 1); }+static int des3_cbc_init_dec(const struct ccmode_cbc *m, cccbc_ctx *c, size_t n, const void *k) { (void)m; return des3_init(n, c, k, 0); }++static int+des3_ecb(const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out)+{+ const uint8_t *p = in;+ uint8_t *q = out;++ for (; nblocks; nblocks--, p += 8, q += 8) {+ mbedtls_des3_crypt_ecb((mbedtls_des3_context *)ctx, p, q);+ }+ return 0;+}++static int+des3_cbc(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out, int mode)+{+ if (nblocks == 0) {+ return 0;+ }+ if (nblocks > SIZE_MAX / 8) {+ return CCMODE_INVALID_INPUT;+ }+ return mbedtls_des3_crypt_cbc((mbedtls_des3_context *)ctx, mode, nblocks * 8, (unsigned char *)iv, in, out) ?+ CCMODE_INTERNAL_ERROR : 0;+}++static int des3_cbc_enc(const cccbc_ctx *c, cccbc_iv *iv, size_t n, const void *in, void *out) { return des3_cbc(c, iv, n, in, out, MBEDTLS_DES_ENCRYPT); }+static int des3_cbc_dec(const cccbc_ctx *c, cccbc_iv *iv, size_t n, const void *in, void *out) { return des3_cbc(c, iv, n, in, out, MBEDTLS_DES_DECRYPT); }++#define DES_ECB(name, sz, i, f) static const struct ccmode_ecb name = { .size = sz, .block_size = 8, .init = i, .ecb = f }+#define DES_CBC(name, sz, i, f) static const struct ccmode_cbc name = { .size = sz, .block_size = 8, .init = i, .cbc = f }+DES_ECB(des_ecb_enc_mode, sizeof(mbedtls_des_context), des_ecb_init_enc, des_ecb);+DES_ECB(des_ecb_dec_mode, sizeof(mbedtls_des_context), des_ecb_init_dec, des_ecb);+DES_CBC(des_cbc_enc_mode, sizeof(mbedtls_des_context), des_cbc_init_enc, des_cbc_enc);+DES_CBC(des_cbc_dec_mode, sizeof(mbedtls_des_context), des_cbc_init_dec, des_cbc_dec);+DES_ECB(des3_ecb_enc_mode, sizeof(mbedtls_des3_context), des3_ecb_init_enc, des3_ecb);+DES_ECB(des3_ecb_dec_mode, sizeof(mbedtls_des3_context), des3_ecb_init_dec, des3_ecb);+DES_CBC(des3_cbc_enc_mode, sizeof(mbedtls_des3_context), des3_cbc_init_enc, des3_cbc_enc);+DES_CBC(des3_cbc_dec_mode, sizeof(mbedtls_des3_context), des3_cbc_init_dec, des3_cbc_dec);++const struct ccmode_ecb *xc_des_ecb_encrypt(void) { return &des_ecb_enc_mode; }+const struct ccmode_ecb *xc_des_ecb_decrypt(void) { return &des_ecb_dec_mode; }+const struct ccmode_cbc *xc_des_cbc_encrypt(void) { return &des_cbc_enc_mode; }+const struct ccmode_cbc *xc_des_cbc_decrypt(void) { return &des_cbc_dec_mode; }+const struct ccmode_ecb *xc_des3_ecb_encrypt(void) { return &des3_ecb_enc_mode; }+const struct ccmode_ecb *xc_des3_ecb_decrypt(void) { return &des3_ecb_dec_mode; }+const struct ccmode_cbc *xc_des3_cbc_encrypt(void) { return &des3_cbc_enc_mode; }+const struct ccmode_cbc *xc_des3_cbc_decrypt(void) { return &des3_cbc_dec_mode; }++/* nonzero when any 8-byte key in key is weak or semi-weak */+int+xc_des_key_is_weak(void *key, unsigned long length)+{+ const uint8_t *k = key;+ unsigned char b[MBEDTLS_DES_KEY_SIZE];+ int weak = 0;++ /* the table holds the keys with odd parity, and the parity bits are not part of the key */+ for (unsigned long i = 0; !weak && i + 8 <= length; i += 8) {+ memcpy(b, k + i, sizeof(b));+ mbedtls_des_key_set_parity(b);+ weak = mbedtls_des_key_check_weak(b);+ }+ mbedtls_platform_zeroize(b, sizeof(b));+ return weak != 0;+}++void+xc_des_key_set_odd_parity(void *key, unsigned long length)+{+ uint8_t *k = key;++ for (unsigned long i = 0; i + 8 <= length; i += 8) {+ mbedtls_des_key_set_parity(k + i);+ }+}++/*+ * CBC-CS3. Up to the last two blocks it is CBC; the last full plaintext block and the (zero-padded) partial one are+ * CBC-encrypted to Ca, Cb and written as Cb, then Ca cut to the partial length. One block is plain CBC. The IV is left+ * as CBC would leave it (Cb).+ */+#define CTS_MAX_BLOCK 16++size_t+xc_cts3_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in, void *out)+{+ size_t b = cbc->block_size, r, m;+ const uint8_t *p = in;+ uint8_t *q = out, last2[2 * CTS_MAX_BLOCK] = {0}, c[2 * CTS_MAX_BLOCK];++ if (b > CTS_MAX_BLOCK || nbytes < b) {+ return 0;+ }+ if (nbytes == b) {+ return cbc->cbc(ctx, iv, 1, p, q) ? 0 : nbytes;+ }+ r = nbytes % b ? nbytes % b : b;+ m = (nbytes - r) / b; /* full blocks, the last of them in the swapped pair */+ memcpy(last2, p + (m - 1) * b, b + r);+ if ((m > 1 && cbc->cbc(ctx, iv, m - 1, p, q)) || cbc->cbc(ctx, iv, 2, last2, c)) {+ return 0;+ }+ memcpy(q + (m - 1) * b, c + b, b);+ memcpy(q + m * b, c, r);+ mbedtls_platform_zeroize(last2, sizeof(last2));+ return nbytes;+}++size_t+xc_cts3_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in, void *out)+{+ size_t b = cbc->block_size, r, m;+ const uint8_t *p = in;+ uint8_t *q = out, cb[CTS_MAX_BLOCK], ca[CTS_MAX_BLOCK], z[CTS_MAX_BLOCK], pl[CTS_MAX_BLOCK];+ uint8_t zero_iv[CTS_MAX_BLOCK] CC_ALIGNED(16);++ if (b > CTS_MAX_BLOCK || nbytes < b) {+ return 0;+ }+ if (nbytes == b) {+ return cbc->cbc(ctx, iv, 1, p, q) ? 0 : nbytes;+ }+ r = nbytes % b ? nbytes % b : b;+ m = (nbytes - r) / b;+ memcpy(cb, p + (m - 1) * b, b); /* CBC's last block */+ memcpy(ca, p + m * b, r); /* CBC's second-last block, cut */+ if (m > 1 && cbc->cbc(ctx, iv, m - 1, p, q)) {+ return 0;+ }+ /* Dec(Cb) = padded last plaintext ^ Ca: its tail is Ca's missing tail, its head the last plaintext */+ memset(zero_iv, 0, sizeof(zero_iv));+ if (cbc->cbc(ctx, (cccbc_iv *)zero_iv, 1, cb, z)) {+ return 0;+ }+ for (size_t i = 0; i < r; i++) {+ pl[i] = z[i] ^ ca[i];+ }+ memcpy(ca + r, z + r, b - r);+ if (cbc->cbc(ctx, iv, 1, ca, q + (m - 1) * b)) {+ return 0;+ }+ memcpy(q + m * b, pl, r);+ memcpy(iv, cb, b);+ mbedtls_platform_zeroize(z, sizeof(z));+ mbedtls_platform_zeroize(pl, sizeof(pl));+ return nbytes;+}diff --git a/kexts/free/corecrypto/cc_digest.c b/kexts/free/corecrypto/cc_digest.cnew file mode 100644index 0000000..37383f6--- /dev/null+++ b/kexts/free/corecrypto/cc_digest.c@@ -0,0 +1,352 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * Digests and HMAC. A digest's state in the interface's context is Mbed TLS's state array as it is; compressing blocks+ * and finishing load it into an Mbed TLS context and run Mbed TLS's own block function and padding.+ */+#include "cc_internal.h"+#include <mbedtls/md5.h>+#include <mbedtls/sha1.h>+#include <mbedtls/sha256.h>+#include <mbedtls/sha512.h>+#include <mbedtls/platform_util.h>++static uint64_t+total_bytes(const struct ccdigest_info *di, ccdigest_ctx_t ctx)+{+ return ccdigest_nbits(di, ctx) / 8 + ccdigest_num(di, ctx);+}++/* MD5 */+static uint32_t md5_iv[4];++static void+md5_compress(ccdigest_state_t state, size_t nblocks, const void *data)+{+ mbedtls_md5_context c;+ const uint8_t *p = data;++ memcpy(c.state, state, sizeof(c.state));+ for (; nblocks; nblocks--, p += 64) {+ mbedtls_internal_md5_process(&c, p);+ }+ memcpy(state, c.state, sizeof(c.state));+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+md5_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ mbedtls_md5_context c;+ uint64_t n = total_bytes(di, ctx);++ memcpy(c.state, ccdigest_state(di, ctx), sizeof(c.state));+ c.total[0] = (uint32_t)n;+ c.total[1] = (uint32_t)(n >> 32);+ memcpy(c.buffer, ccdigest_data(di, ctx), ccdigest_num(di, ctx));+ mbedtls_md5_finish(&c, out);+ mbedtls_platform_zeroize(&c, sizeof(c));+}++/* SHA-1 */+static uint32_t sha1_iv[5];++static void+sha1_compress(ccdigest_state_t state, size_t nblocks, const void *data)+{+ mbedtls_sha1_context c;+ const uint8_t *p = data;++ memcpy(c.state, state, sizeof(c.state));+ for (; nblocks; nblocks--, p += 64) {+ mbedtls_internal_sha1_process(&c, p);+ }+ memcpy(state, c.state, sizeof(c.state));+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+sha1_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ mbedtls_sha1_context c;+ uint64_t n = total_bytes(di, ctx);++ memcpy(c.state, ccdigest_state(di, ctx), sizeof(c.state));+ c.total[0] = (uint32_t)n;+ c.total[1] = (uint32_t)(n >> 32);+ memcpy(c.buffer, ccdigest_data(di, ctx), ccdigest_num(di, ctx));+ mbedtls_sha1_finish(&c, out);+ mbedtls_platform_zeroize(&c, sizeof(c));+}++/* SHA-224 and SHA-256: one block function, the output differs */+static uint32_t sha224_iv[8], sha256_iv[8];++static void+sha256_compress(ccdigest_state_t state, size_t nblocks, const void *data)+{+ mbedtls_sha256_context c;+ const uint8_t *p = data;++ memcpy(c.state, state, sizeof(c.state));+ for (; nblocks; nblocks--, p += 64) {+ mbedtls_internal_sha256_process(&c, p);+ }+ memcpy(state, c.state, sizeof(c.state));+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+sha256_final_is224(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out, int is224)+{+ mbedtls_sha256_context c;+ uint64_t n = total_bytes(di, ctx);++ memcpy(c.state, ccdigest_state(di, ctx), sizeof(c.state));+ c.total[0] = (uint32_t)n;+ c.total[1] = (uint32_t)(n >> 32);+ c.is224 = is224;+ memcpy(c.buffer, ccdigest_data(di, ctx), ccdigest_num(di, ctx));+ mbedtls_sha256_finish(&c, out);+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+sha224_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ sha256_final_is224(di, ctx, out, 1);+}++static void+sha256_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ sha256_final_is224(di, ctx, out, 0);+}++/* SHA-384 and SHA-512 */+static uint64_t sha384_iv[8], sha512_iv[8];++static void+sha512_compress(ccdigest_state_t state, size_t nblocks, const void *data)+{+ mbedtls_sha512_context c;+ const uint8_t *p = data;++ memcpy(c.state, state, sizeof(c.state));+ for (; nblocks; nblocks--, p += 128) {+ mbedtls_internal_sha512_process(&c, p);+ }+ memcpy(state, c.state, sizeof(c.state));+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+sha512_final_is384(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out, int is384)+{+ mbedtls_sha512_context c;++ memcpy(c.state, ccdigest_state(di, ctx), sizeof(c.state));+ c.total[0] = total_bytes(di, ctx);+ c.total[1] = 0;+ c.is384 = is384;+ memcpy(c.buffer, ccdigest_data(di, ctx), ccdigest_num(di, ctx));+ mbedtls_sha512_finish(&c, out);+ mbedtls_platform_zeroize(&c, sizeof(c));+}++static void+sha384_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ sha512_final_is384(di, ctx, out, 1);+}++static void+sha512_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *out)+{+ sha512_final_is384(di, ctx, out, 0);+}++#define DI(name, out, state, block, oid_der, oid_len, iv, comp, fin) \+ static const struct ccdigest_info name = { \+ .output_size = out, .state_size = state, .block_size = block, .oid_size = oid_len, .oid = oid_der, \+ .initial_state = iv, .compress = comp, .final = fin, \+ }++DI(md5_di, 16, 16, 64, CC_DIGEST_OID_MD5, 10, md5_iv, md5_compress, md5_final);+DI(sha1_di, 20, 20, 64, CC_DIGEST_OID_SHA1, 7, sha1_iv, sha1_compress, sha1_final);+DI(sha224_di, 28, 32, 64, CC_DIGEST_OID_SHA224, 11, sha224_iv, sha256_compress, sha224_final);+DI(sha256_di, 32, 32, 64, CC_DIGEST_OID_SHA256, 11, sha256_iv, sha256_compress, sha256_final);+DI(sha384_di, 48, 64, 128, CC_DIGEST_OID_SHA384, 11, sha384_iv, sha512_compress, sha384_final);+DI(sha512_di, 64, 64, 128, CC_DIGEST_OID_SHA512, 11, sha512_iv, sha512_compress, sha512_final);++const struct ccdigest_info *xc_md5_di(void) { return &md5_di; }+const struct ccdigest_info *xc_sha1_di(void) { return &sha1_di; }+const struct ccdigest_info *xc_sha224_di(void) { return &sha224_di; }+const struct ccdigest_info *xc_sha256_di(void) { return &sha256_di; }+const struct ccdigest_info *xc_sha384_di(void) { return &sha384_di; }+const struct ccdigest_info *xc_sha512_di(void) { return &sha512_di; }++/* the initial states, as Mbed TLS starts each hash (before anything is registered) */+void+xc_digest_setup(void)+{+ mbedtls_md5_context m;+ mbedtls_sha1_context s1;+ mbedtls_sha256_context s2;+ mbedtls_sha512_context s5;++ mbedtls_md5_init(&m);+ mbedtls_md5_starts(&m);+ memcpy(md5_iv, m.state, sizeof(md5_iv));+ mbedtls_sha1_init(&s1);+ mbedtls_sha1_starts(&s1);+ memcpy(sha1_iv, s1.state, sizeof(sha1_iv));+ mbedtls_sha256_init(&s2);+ mbedtls_sha256_starts(&s2, 1);+ memcpy(sha224_iv, s2.state, sizeof(sha224_iv));+ mbedtls_sha256_starts(&s2, 0);+ memcpy(sha256_iv, s2.state, sizeof(sha256_iv));+ mbedtls_sha512_init(&s5);+ mbedtls_sha512_starts(&s5, 1);+ memcpy(sha384_iv, s5.state, sizeof(sha384_iv));+ mbedtls_sha512_starts(&s5, 0);+ memcpy(sha512_iv, s5.state, sizeof(sha512_iv));+}++/*+ * The generic digest calls. The context's bit count is what has been compressed; the pending bytes (fewer than a block)+ * wait in its buffer until a block fills or the digest finishes.+ */+void+xc_digest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx)+{+ memcpy(ccdigest_state(di, ctx), di->initial_state, di->state_size);+ ccdigest_nbits(di, ctx) = 0;+ memset(ccdigest_data(di, ctx), 0, di->block_size); /* MD5Init and SHA1Init copy the whole block out */+ ccdigest_num(di, ctx) = 0;+}++void+xc_digest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned long len, const void *data)+{+ const uint8_t *p = data;+ size_t bs = di->block_size;+ unsigned int num = ccdigest_num(di, ctx);++ if (num >= bs) {+ xc_panic("corecrypto: digest context corrupt (%u pending bytes)", num);+ }+ if (num > 0) {+ size_t take = bs - num < len ? bs - num : len;+ memcpy(ccdigest_data(di, ctx) + num, p, take);+ num += take;+ p += take;+ len -= take;+ if (num == bs) {+ di->compress(ccdigest_state(di, ctx), 1, ccdigest_data(di, ctx));+ ccdigest_nbits(di, ctx) += bs * 8;+ num = 0;+ }+ }+ if (len >= bs) {+ size_t n = len / bs;+ di->compress(ccdigest_state(di, ctx), n, p);+ ccdigest_nbits(di, ctx) += (uint64_t)n * bs * 8;+ p += n * bs;+ len -= n * bs;+ }+ if (len > 0) {+ memcpy(ccdigest_data(di, ctx), p, len);+ num = (unsigned int)len;+ }+ ccdigest_num(di, ctx) = num;+}++void+xc_digest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, void *digest)+{+ di->final(di, ctx, digest);+}++void+xc_digest(const struct ccdigest_info *di, unsigned long len, const void *data, void *digest)+{+ ccdigest_di_decl(di, ctx);+ xc_digest_init(di, ctx);+ xc_digest_update(di, ctx, len, data);+ di->final(di, ctx, digest);+ ccdigest_di_clear(di, ctx);+}++/*+ * HMAC (RFC 2104) over any of the digests: the inner hash runs in the context's digest part from the start; the+ * outer hash's state after its key block waits in the context's ostate.+ */+#define XC_MAX_BLOCK 128+#define XC_MAX_OUTPUT 64++void+xc_hmac_init(const struct ccdigest_info *di, cchmac_ctx_t hc, unsigned long key_len, const void *key)+{+ uint8_t k[XC_MAX_BLOCK] = {0};+ size_t bs = di->block_size;++ if (bs > XC_MAX_BLOCK || di->output_size > XC_MAX_OUTPUT) {+ xc_panic("corecrypto: HMAC over a digest with a %zu-byte block", bs);+ }+ if (key_len > bs) {+ xc_digest(di, key_len, key, k);+ } else if (key_len > 0) {+ memcpy(k, key, key_len);+ }+ for (size_t i = 0; i < bs; i++) {+ k[i] ^= 0x5c;+ }+ memcpy(cchmac_ostate(di, hc), di->initial_state, di->state_size);+ di->compress(cchmac_ostate(di, hc), 1, k);+ for (size_t i = 0; i < bs; i++) {+ k[i] ^= 0x5c ^ 0x36;+ }+ xc_digest_init(di, cchmac_digest_ctx(di, hc));+ xc_digest_update(di, cchmac_digest_ctx(di, hc), bs, k);+ mbedtls_platform_zeroize(k, sizeof(k));+}++void+xc_hmac_update(const struct ccdigest_info *di, cchmac_ctx_t hc, unsigned long data_len, const void *data)+{+ xc_digest_update(di, cchmac_digest_ctx(di, hc), data_len, data);+}++void+xc_hmac_final(const struct ccdigest_info *di, cchmac_ctx_t hc, unsigned char *mac)+{+ uint8_t inner[XC_MAX_OUTPUT];+ ccdigest_ctx_t ctx = cchmac_digest_ctx(di, hc);++ di->final(di, ctx, inner);+ memcpy(ccdigest_state(di, ctx), cchmac_ostate(di, hc), di->state_size);+ ccdigest_nbits(di, ctx) = di->block_size * 8;+ ccdigest_num(di, ctx) = 0;+ xc_digest_update(di, ctx, di->output_size, inner);+ di->final(di, ctx, mac);+ mbedtls_platform_zeroize(inner, sizeof(inner));+}++void+xc_hmac(const struct ccdigest_info *di, unsigned long key_len, const void *key, unsigned long data_len,+ const void *data, unsigned char *mac)+{+ cchmac_di_decl(di, hc);+ xc_hmac_init(di, hc, key_len, key);+ xc_hmac_update(di, hc, data_len, data);+ xc_hmac_final(di, hc, mac);+ cchmac_di_clear(di, hc);+}diff --git a/kexts/free/corecrypto/cc_internal.h b/kexts/free/corecrypto/cc_internal.hnew file mode 100644index 0000000..9d81029--- /dev/null+++ b/kexts/free/corecrypto/cc_internal.h@@ -0,0 +1,136 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * xnulinux's corecrypto kext: the corecrypto interface's provider for the kernel collection without Apple's kexts+ * (kc-free.bin). Every primitive is Mbed TLS's; this kext adapts it to the interface's tables and context layouts. Random+ * numbers are the host's (Linux's get_random_bytes): the kext has no generator of its own.+ *+ * The same sources build for user space (test/): CC_TEST_USERSPACE replaces the kernel's services.+ */+#ifndef XNULINUX_CC_INTERNAL_H+#define XNULINUX_CC_INTERNAL_H++#define MBEDTLS_ALLOW_PRIVATE_ACCESS+#ifndef MBEDTLS_CONFIG_FILE+#define MBEDTLS_CONFIG_FILE "mbedtls_config_xnulinux.h"+#endif++#include <corecrypto/cc.h>+#include <corecrypto/ccdigest.h>+#include <corecrypto/cchmac.h>+#include <corecrypto/ccmode.h>+#include <corecrypto/ccaes.h>+#include <corecrypto/ccdes.h>+#include <corecrypto/ccpad.h>+#include <corecrypto/ccsha1.h>+#include <corecrypto/ccsha2.h>+#include <corecrypto/ccmd5.h>+#include <corecrypto/ccrng.h>+#include <corecrypto/ccrsa.h>+#include <corecrypto/ccchacha20poly1305.h>+#include <corecrypto/cckprng.h>++#if CC_TEST_USERSPACE+#include <stdio.h>+#include <stdlib.h>+#define xc_panic(...) do { fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); abort(); } while (0)+void xc_random(void *buf, size_t nbytes);+/* the tests' allocator: each free must name the size its allocation had, as IOFreeData does */+void xc_test_alloc(void *p, size_t bytes);+void xc_test_free(void *p, size_t bytes);+#else+#include <kern/debug.h>+#include <linux64/linux64.h>+#define xc_panic(...) panic(__VA_ARGS__)+/* the host's generator (Linux's get_random_bytes), through LINUX64_CALL_RANDOM */+static inline void+xc_random(void *buf, size_t nbytes)+{+ if (!linux64_hook) {+ panic("corecrypto: no host to take random numbers from");+ }+ linux64_random(buf, nbytes);+}+#endif++/* every function of the interface this kext provides, by name, for the registration (cc_kext.c) and the tests */+const struct ccdigest_info *xc_md5_di(void);+const struct ccdigest_info *xc_sha1_di(void);+const struct ccdigest_info *xc_sha224_di(void);+const struct ccdigest_info *xc_sha256_di(void);+const struct ccdigest_info *xc_sha384_di(void);+const struct ccdigest_info *xc_sha512_di(void);+void xc_digest_setup(void);+void xc_digest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx);+void xc_digest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned long len, const void *data);+void xc_digest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, void *digest);+void xc_digest(const struct ccdigest_info *di, unsigned long len, const void *data, void *digest);+void xc_hmac_init(const struct ccdigest_info *di, cchmac_ctx_t ctx, unsigned long key_len, const void *key);+void xc_hmac_update(const struct ccdigest_info *di, cchmac_ctx_t ctx, unsigned long data_len, const void *data);+void xc_hmac_final(const struct ccdigest_info *di, cchmac_ctx_t ctx, unsigned char *mac);+void xc_hmac(const struct ccdigest_info *di, unsigned long key_len, const void *key, unsigned long data_len,+ const void *data, unsigned char *mac);++const struct ccmode_ecb *xc_aes_ecb_encrypt(void);+const struct ccmode_ecb *xc_aes_ecb_decrypt(void);+const struct ccmode_cbc *xc_aes_cbc_encrypt(void);+const struct ccmode_cbc *xc_aes_cbc_decrypt(void);+const struct ccmode_ctr *xc_aes_ctr(void);+const struct ccmode_xts *xc_aes_xts_encrypt(void);+const struct ccmode_xts *xc_aes_xts_decrypt(void);+const struct ccmode_gcm *xc_aes_gcm_encrypt(void);+const struct ccmode_gcm *xc_aes_gcm_decrypt(void);+int xc_gcm_init_with_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key,+ const void *iv);+int xc_gcm_inc_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv);++const struct ccmode_ecb *xc_des_ecb_encrypt(void);+const struct ccmode_ecb *xc_des_ecb_decrypt(void);+const struct ccmode_cbc *xc_des_cbc_encrypt(void);+const struct ccmode_cbc *xc_des_cbc_decrypt(void);+const struct ccmode_ecb *xc_des3_ecb_encrypt(void);+const struct ccmode_ecb *xc_des3_ecb_decrypt(void);+const struct ccmode_cbc *xc_des3_cbc_encrypt(void);+const struct ccmode_cbc *xc_des3_cbc_decrypt(void);+int xc_des_key_is_weak(void *key, unsigned long length);+void xc_des_key_set_odd_parity(void *key, unsigned long length);+size_t xc_cts3_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in,+ void *out);+size_t xc_cts3_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in,+ void *out);++const struct ccchacha20poly1305_info *xc_ccp_info(void);+int xc_ccp_init(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *key);+int xc_ccp_reset(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx);+int xc_ccp_setnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *nonce);+int xc_ccp_incnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *nonce);+int xc_ccp_aad(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *aad);+int xc_ccp_encrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ptext, void *ctext);+int xc_ccp_finalize(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *tag);+int xc_ccp_decrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ctext, void *ptext);+int xc_ccp_verify(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *tag);++int xc_rsa_make_pub(ccrsa_pub_ctx_t pubk, size_t exp_nbytes, const uint8_t *exp, size_t mod_nbytes,+ const uint8_t *mod);+int xc_rsa_verify_pkcs1v15(ccrsa_pub_ctx_t key, const uint8_t *oid, size_t digest_len, const uint8_t *digest,+ size_t sig_len, const uint8_t *sig, bool *valid);++struct ccrng_state *xc_rng(int *error);+int xc_rng_uniform(struct ccrng_state *rng, uint64_t bound, uint64_t *rand);+/* kmem's generator, a ccrng that takes no lock once initialized (crypto_random_kmem_*) */+size_t xc_kmem_rng_size(void);+void xc_kmem_rng_init(void *ctx);++/* known-answer tests of every primitive, through the interface (cc_selftest.c): 0 when all pass */+int xc_selftest(void);++#endif /* XNULINUX_CC_INTERNAL_H */diff --git a/kexts/free/corecrypto/cc_kext.c b/kexts/free/corecrypto/cc_kext.cnew file mode 100644index 0000000..eeeaf7b--- /dev/null+++ b/kexts/free/corecrypto/cc_kext.c@@ -0,0 +1,358 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * The kext: its self tests, then its functions registered with the kernel (register_crypto_functions) and the kernel's+ * random generator registered as the host's (register_and_init_prng).+ */+#include "cc_internal.h"+#include <libkern/crypto/register_crypto.h>+#include <prng/random.h>+#include <mach/kmod.h>+#include <libkern/libkern.h>++kern_return_t corecrypto_start(kmod_info_t *ki, void *d);+kern_return_t corecrypto_stop(kmod_info_t *ki, void *d);++static const struct ccchacha20poly1305_fns chachapoly_fns = {+ .info = xc_ccp_info, .init = xc_ccp_init, .reset = xc_ccp_reset, .setnonce = xc_ccp_setnonce,+ .incnonce = xc_ccp_incnonce, .aad = xc_ccp_aad, .encrypt = xc_ccp_encrypt, .finalize = xc_ccp_finalize,+ .decrypt = xc_ccp_decrypt, .verify = xc_ccp_verify,+};++/* the crypto_digest_* and crypto_hmac_* calls: the ccdigest and cchmac contexts of the algorithm's digest */+static const struct ccdigest_info *+alg_di(crypto_digest_alg_t alg)+{+ switch (alg) {+ case CRYPTO_DIGEST_ALG_MD5: return xc_md5_di();+ case CRYPTO_DIGEST_ALG_SHA1: return xc_sha1_di();+ case CRYPTO_DIGEST_ALG_SHA256: return xc_sha256_di();+ case CRYPTO_DIGEST_ALG_SHA384: return xc_sha384_di();+ case CRYPTO_DIGEST_ALG_SHA512: return xc_sha512_di();+ default: xc_panic("corecrypto: unknown digest algorithm %u", (unsigned)alg);+ }+}++static void+check_ctx(size_t want, size_t have)+{+ if (want != have) {+ xc_panic("corecrypto: context of %zu bytes where %zu are needed", have, want);+ }+}++static void+check_out(const struct ccdigest_info *di, size_t size)+{+ if (size > di->output_size) {+ xc_panic("corecrypto: %zu bytes asked of a %zu-byte digest", size, di->output_size);+ }+}++static size_t+digest_ctx_size(crypto_digest_alg_t alg)+{+ return ccdigest_di_size(alg_di(alg));+}++static void+digest_init(crypto_digest_alg_t alg, void *ctx, size_t ctx_size)+{+ check_ctx(digest_ctx_size(alg), ctx_size);+ xc_digest_init(alg_di(alg), ctx);+}++static void+digest_update(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, const void *data, size_t data_size)+{+ check_ctx(digest_ctx_size(alg), ctx_size);+ xc_digest_update(alg_di(alg), ctx, data_size, data);+}++static void+digest_final(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, void *digest, size_t digest_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];++ check_ctx(digest_ctx_size(alg), ctx_size);+ check_out(di, digest_size);+ xc_digest_final(di, ctx, out);+ memcpy(digest, out, digest_size);+ cc_clear(sizeof(out), out);+}++static void+digest_oneshot(crypto_digest_alg_t alg, const void *data, size_t data_size, void *digest, size_t digest_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];++ check_out(di, digest_size);+ xc_digest(di, data_size, data, out);+ memcpy(digest, out, digest_size);+ cc_clear(sizeof(out), out);+}++static size_t+hmac_ctx_size(crypto_digest_alg_t alg)+{+ return cchmac_di_size(alg_di(alg));+}++static void+hmac_init(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, const void *key, size_t key_size)+{+ check_ctx(hmac_ctx_size(alg), ctx_size);+ xc_hmac_init(alg_di(alg), ctx, key_size, key);+}++static void+hmac_update(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, const void *data, size_t data_size)+{+ check_ctx(hmac_ctx_size(alg), ctx_size);+ xc_hmac_update(alg_di(alg), ctx, data_size, data);+}++static void+hmac_final_generate(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, void *tag, size_t tag_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];++ check_ctx(hmac_ctx_size(alg), ctx_size);+ check_out(di, tag_size);+ xc_hmac_final(di, ctx, out);+ memcpy(tag, out, tag_size);+ cc_clear(sizeof(out), out);+}++static bool+hmac_final_verify(crypto_digest_alg_t alg, void *ctx, size_t ctx_size, const void *tag, size_t tag_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];+ bool ok;++ check_ctx(hmac_ctx_size(alg), ctx_size);+ check_out(di, tag_size);+ xc_hmac_final(di, ctx, out);+ ok = cc_cmp_safe(tag_size, tag, out) == 0;+ cc_clear(sizeof(out), out);+ return ok;+}++static void+hmac_generate(crypto_digest_alg_t alg, const void *key, size_t key_size, const void *data, size_t data_size, void *tag,+ size_t tag_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];++ check_out(di, tag_size);+ xc_hmac(di, key_size, key, data_size, data, out);+ memcpy(tag, out, tag_size);+ cc_clear(sizeof(out), out);+}++static bool+hmac_verify(crypto_digest_alg_t alg, const void *key, size_t key_size, const void *data, size_t data_size,+ const void *tag, size_t tag_size)+{+ const struct ccdigest_info *di = alg_di(alg);+ uint8_t out[64];+ bool ok;++ check_out(di, tag_size);+ xc_hmac(di, key_size, key, data_size, data, out);+ ok = cc_cmp_safe(tag_size, tag, out) == 0;+ cc_clear(sizeof(out), out);+ return ok;+}++/* random numbers: a context is kmem's generator (the only kind crypto_random_* is given); none, the host's */+static struct ccrng_state *+random_rng(crypto_random_ctx_t ctx)+{+ return ctx != NULL ? (struct ccrng_state *)ctx : xc_rng(NULL);+}++static void+random_generate(crypto_random_ctx_t ctx, void *random, size_t random_size)+{+ (void)ccrng_generate(random_rng(ctx), random_size, random);+}++static void+random_uniform(crypto_random_ctx_t ctx, uint64_t bound, uint64_t *random)+{+ if (xc_rng_uniform(random_rng(ctx), bound, random) != 0) {+ xc_panic("corecrypto: crypto_random_uniform with bound 0");+ }+}++static size_t+random_kmem_ctx_size(void)+{+ return xc_kmem_rng_size();+}++static void+random_kmem_init(crypto_random_ctx_t ctx)+{+ xc_kmem_rng_init(ctx);+}++static struct crypto_functions functions = {+ .ccdigest_init_fn = xc_digest_init,+ .ccdigest_update_fn = xc_digest_update,+ .ccdigest_final_fn = xc_digest_final,+ .ccdigest_fn = xc_digest,+ .cchmac_init_fn = xc_hmac_init,+ .cchmac_update_fn = xc_hmac_update,+ .cchmac_final_fn = xc_hmac_final,+ .cchmac_fn = xc_hmac,+ .ccgcm_init_with_iv_fn = xc_gcm_init_with_iv,+ .ccgcm_inc_iv_fn = xc_gcm_inc_iv,+ .ccchacha20poly1305_fns = &chachapoly_fns,+ .ccdes_key_is_weak_fn = xc_des_key_is_weak,+ .ccdes_key_set_odd_parity_fn = xc_des_key_set_odd_parity,+ .ccpad_cts3_encrypt_fn = xc_cts3_encrypt,+ .ccpad_cts3_decrypt_fn = xc_cts3_decrypt,+ .ccrng_fn = xc_rng,+ .ccrsa_make_pub_fn = xc_rsa_make_pub,+ .ccrsa_verify_pkcs1v15_fn = xc_rsa_verify_pkcs1v15,+ .random_generate_fn = random_generate,+ .random_uniform_fn = random_uniform,+ .random_kmem_ctx_size_fn = random_kmem_ctx_size,+ .random_kmem_init_fn = random_kmem_init,+ .digest_ctx_size_fn = digest_ctx_size,+ .digest_init_fn = digest_init,+ .digest_update_fn = digest_update,+ .digest_final_fn = digest_final,+ .digest_fn = digest_oneshot,+ .hmac_ctx_size_fn = hmac_ctx_size,+ .hmac_init_fn = hmac_init,+ .hmac_update_fn = hmac_update,+ .hmac_final_generate_fn = hmac_final_generate,+ .hmac_final_verify_fn = hmac_final_verify,+ .hmac_generate_fn = hmac_generate,+ .hmac_verify_fn = hmac_verify,+};++/* the kernel PRNG: the host's generator; nothing to seed, nothing per CPU */+struct cckprng_ctx {+ int unused;+};+static struct cckprng_ctx kprng_ctx;++static void+kprng_init(struct cckprng_ctx *ctx, size_t seed_nbytes, const void *seed, size_t nonce_nbytes, const void *nonce,+ cckprng_getentropy getentropy, void *arg)+{+ (void)ctx, (void)seed_nbytes, (void)seed, (void)nonce_nbytes, (void)nonce, (void)getentropy, (void)arg;+}++static void+kprng_init_with_getentropy(struct cckprng_ctx *ctx, unsigned max_ngens, size_t seed_nbytes, const void *seed,+ size_t nonce_nbytes, const void *nonce, cckprng_getentropy getentropy, void *arg)+{+ (void)max_ngens;+ kprng_init(ctx, seed_nbytes, seed, nonce_nbytes, nonce, getentropy, arg);+}++static void+kprng_initgen(struct cckprng_ctx *ctx, unsigned gen_idx)+{+ (void)ctx, (void)gen_idx;+}++/* seeds the kernel offers (/dev/random writes among them): the host's generator has its own entropy */+static void+kprng_reseed(struct cckprng_ctx *ctx, size_t nbytes, const void *seed)+{+ (void)ctx, (void)nbytes, (void)seed;+}++static void+kprng_refresh(struct cckprng_ctx *ctx)+{+ (void)ctx;+}++static void+kprng_generate(struct cckprng_ctx *ctx, unsigned gen_idx, size_t nbytes, void *out)+{+ (void)ctx, (void)gen_idx;+ xc_random(out, nbytes);+}++static const struct cckprng_funcs kprng_funcs = {+ .init = kprng_init,+ .initgen = kprng_initgen,+ .reseed = kprng_reseed,+ .refresh = kprng_refresh,+ .generate = kprng_generate,+ .init_with_getentropy = kprng_init_with_getentropy,+};++static void+register_modes(void)+{+ functions.ccmd5_di = xc_md5_di();+ functions.ccsha1_di = xc_sha1_di();+ functions.ccsha256_di = xc_sha256_di();+ functions.ccsha384_di = xc_sha384_di();+ functions.ccsha512_di = xc_sha512_di();+ functions.ccaes_ecb_encrypt = xc_aes_ecb_encrypt();+ functions.ccaes_ecb_decrypt = xc_aes_ecb_decrypt();+ functions.ccaes_cbc_encrypt = xc_aes_cbc_encrypt();+ functions.ccaes_cbc_decrypt = xc_aes_cbc_decrypt();+ functions.ccaes_ctr_crypt = xc_aes_ctr();+ functions.ccaes_xts_encrypt = xc_aes_xts_encrypt();+ functions.ccaes_xts_decrypt = xc_aes_xts_decrypt();+ functions.ccaes_gcm_encrypt = xc_aes_gcm_encrypt();+ functions.ccaes_gcm_decrypt = xc_aes_gcm_decrypt();+ functions.ccdes_ecb_encrypt = xc_des_ecb_encrypt();+ functions.ccdes_ecb_decrypt = xc_des_ecb_decrypt();+ functions.ccdes_cbc_encrypt = xc_des_cbc_encrypt();+ functions.ccdes_cbc_decrypt = xc_des_cbc_decrypt();+ functions.cctdes_ecb_encrypt = xc_des3_ecb_encrypt();+ functions.cctdes_ecb_decrypt = xc_des3_ecb_decrypt();+ functions.cctdes_cbc_encrypt = xc_des3_cbc_encrypt();+ functions.cctdes_cbc_decrypt = xc_des3_cbc_decrypt();+}++kern_return_t+corecrypto_start(kmod_info_t *ki, void *d)+{+ int failed;++ (void)ki, (void)d;+ xc_digest_setup();+ failed = xc_selftest();+ if (failed) {+ panic("corecrypto: self test %d failed", failed);+ }+ register_modes();+ if (register_crypto_functions(&functions) != 0) {+ panic("corecrypto: another crypto provider is registered");+ }+ register_and_init_prng(&kprng_ctx, &kprng_funcs);+ printf("corecrypto: xnulinux's (Mbed TLS), self tests passed; random numbers from the host\n");+ return KERN_SUCCESS;+}++kern_return_t+corecrypto_stop(kmod_info_t *ki, void *d)+{+ (void)ki, (void)d;+ return KERN_FAILURE; /* the kernel's crypto cannot go away */+}diff --git a/kexts/free/corecrypto/cc_platform.c b/kexts/free/corecrypto/cc_platform.cnew file mode 100644index 0000000..f4822c3--- /dev/null+++ b/kexts/free/corecrypto/cc_platform.c@@ -0,0 +1,173 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* Memory for Mbed TLS (RSA's big numbers), and the random numbers: the host's. */+#include "cc_internal.h"+#include <mbedtls/chacha20.h>+#include <mbedtls/platform_util.h>+#if !CC_TEST_USERSPACE+#include <IOKit/IOLib.h>+#endif++/* each block carries its size, which IOFreeData needs */+struct block {+ size_t size;+ uint64_t data[];+};++void *+xnulinux_cc_calloc(size_t n, size_t size)+{+ struct block *b;+ size_t bytes;++ if (n != 0 && size > (SIZE_MAX - sizeof(struct block)) / n) {+ return NULL;+ }+ bytes = sizeof(struct block) + n * size;+#if CC_TEST_USERSPACE+ b = calloc(1, bytes);+#else+ b = IOMallocZeroData(bytes);+#endif+ if (b == NULL) {+ return NULL;+ }+#if CC_TEST_USERSPACE+ xc_test_alloc(b, bytes);+#endif+ b->size = bytes;+ return b->data;+}++void+xnulinux_cc_free(void *p)+{+ struct block *b;+ size_t bytes;++ if (p == NULL) {+ return;+ }+ b = (struct block *)((uint8_t *)p - offsetof(struct block, data));+ bytes = b->size; /* cc_clear zeroes the size too */+ cc_clear(bytes, b);+#if CC_TEST_USERSPACE+ xc_test_free(b, bytes);+#else+ IOFreeData(b, bytes);+#endif+}++/* Mbed TLS formats OIDs as text only for display, which nothing here asks for */+int+xnulinux_cc_snprintf(char *s, size_t n, const char *format, ...)+{+ (void)format;+ if (n > 0) {+ s[0] = 0;+ }+ return -1;+}++/* the generator every ccrng caller gets: the host's */+static int+host_generate(struct ccrng_state *rng, size_t outlen, void *out)+{+ (void)rng;+ xc_random(out, outlen);+ return 0;+}++static struct ccrng_state host_rng = { .generate = host_generate };++struct ccrng_state *+xc_rng(int *error)+{+ if (error != NULL) {+ *error = 0;+ }+ return &host_rng;+}++/* uniform in [0, bound), by rejection (no modulo bias) */+int+xc_rng_uniform(struct ccrng_state *rng, uint64_t bound, uint64_t *rand)+{+ uint64_t v, limit;++ if (bound == 0) {+ return CCERR_PARAMETER;+ }+ limit = -bound % bound;+ do {+ int rc = ccrng_generate(rng, sizeof(v), &v);+ if (rc != 0) {+ return rc;+ }+ } while (v < limit);+ *rand = v % bound;+ return 0;+}++/*+ * kmem's generator (crypto_random_kmem_*): its contract is that generating takes no lock, which a call to the host+ * cannot promise. Keyed from the host at init (which may lock); then ChaCha20 with fast key erasure: each block's first+ * half is the next key, its second half the output. One per CPU, used with preemption off (osfmk/vm/vm_map_store.c).+ */+struct kmem_rng {+ struct ccrng_state rng; /* first: the context is the ccrng */+ uint8_t key[32];+ uint8_t out[32];+ uint32_t left; /* unused bytes at the end of out */+};++static int+kmem_generate(struct ccrng_state *rng, size_t outlen, void *out)+{+ static const uint8_t nonce[12], zero[64];+ struct kmem_rng *k = (struct kmem_rng *)rng;+ uint8_t *p = out, block[64];++ while (outlen > 0) {+ size_t n;+ if (k->left == 0) {+ (void)mbedtls_chacha20_crypt(k->key, nonce, 0, sizeof(block), zero, block);+ memcpy(k->key, block, sizeof(k->key));+ memcpy(k->out, block + sizeof(k->key), sizeof(k->out));+ mbedtls_platform_zeroize(block, sizeof(block));+ k->left = sizeof(k->out);+ }+ n = outlen < k->left ? outlen : k->left;+ memcpy(p, k->out + sizeof(k->out) - k->left, n);+ mbedtls_platform_zeroize(k->out + sizeof(k->out) - k->left, n);+ k->left -= (uint32_t)n;+ p += n;+ outlen -= n;+ }+ return 0;+}++_Static_assert(sizeof(struct kmem_rng) <= 256, "kmem's generator must fit CRYPTO_RANDOM_MAX_CTX_SIZE");++size_t+xc_kmem_rng_size(void)+{+ return sizeof(struct kmem_rng);+}++void+xc_kmem_rng_init(void *ctx)+{+ struct kmem_rng *k = ctx;++ k->rng.generate = kmem_generate;+ xc_random(k->key, sizeof(k->key));+ k->left = 0;+}diff --git a/kexts/free/corecrypto/cc_rsa.c b/kexts/free/corecrypto/cc_rsa.cnew file mode 100644index 0000000..de08b7e--- /dev/null+++ b/kexts/free/corecrypto/cc_rsa.c@@ -0,0 +1,103 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * RSA PKCS #1 v1.5 signature verification (Mbed TLS's). ccrsa_make_pub keeps the key's modulus and exponent, as bytes,+ * in the caller's context after its cczp header (whose n, set by the caller, bounds them); a verification imports them+ * into an Mbed TLS context.+ */+#include "cc_internal.h"+#include <mbedtls/rsa.h>+#include <mbedtls/md.h>++struct xc_rsa_key {+ uint32_t mod_nbytes;+ uint32_t exp_nbytes;+ uint8_t bytes[]; /* modulus, then exponent */+};++#define KEY(pubk) ((struct xc_rsa_key *)(void *)(pubk)->pb_ccn)++int+xc_rsa_make_pub(ccrsa_pub_ctx_t pubk, size_t exp_nbytes, const uint8_t *exp, size_t mod_nbytes, const uint8_t *mod)+{+ size_t room = 3 * ccn_sizeof_n(ccrsa_ctx_n(pubk)); /* the units after the header */++ while (mod_nbytes > 0 && mod[0] == 0) {+ mod++, mod_nbytes--;+ }+ while (exp_nbytes > 0 && exp[0] == 0) {+ exp++, exp_nbytes--;+ }+ if (mod_nbytes == 0 || exp_nbytes == 0 || mod_nbytes > ccn_sizeof_n(ccrsa_ctx_n(pubk)) ||+ sizeof(struct xc_rsa_key) + mod_nbytes + exp_nbytes > room) {+ return CCRSA_INVALID_INPUT;+ }+ KEY(pubk)->mod_nbytes = (uint32_t)mod_nbytes;+ KEY(pubk)->exp_nbytes = (uint32_t)exp_nbytes;+ memcpy(KEY(pubk)->bytes, mod, mod_nbytes);+ memcpy(KEY(pubk)->bytes + mod_nbytes, exp, exp_nbytes);+ pubk->pb_bitlen = (cc_unit)mod_nbytes * 8;+ return 0;+}++static const struct {+ const unsigned char *oid;+ size_t oid_size;+ mbedtls_md_type_t md;+ size_t len;+} digests[] = {+ { CC_DIGEST_OID_SHA1, 7, MBEDTLS_MD_SHA1, 20 },+ { CC_DIGEST_OID_SHA224, 11, MBEDTLS_MD_SHA224, 28 },+ { CC_DIGEST_OID_SHA256, 11, MBEDTLS_MD_SHA256, 32 },+ { CC_DIGEST_OID_SHA384, 11, MBEDTLS_MD_SHA384, 48 },+ { CC_DIGEST_OID_SHA512, 11, MBEDTLS_MD_SHA512, 64 },+ { CC_DIGEST_OID_MD5, 10, MBEDTLS_MD_MD5, 16 },+};++/* oid: the digest's DER object identifier (tag and length first); NULL: the digest is signed as it is */+int+xc_rsa_verify_pkcs1v15(ccrsa_pub_ctx_t key, const uint8_t *oid, size_t digest_len, const uint8_t *digest,+ size_t sig_len, const uint8_t *sig, bool *valid)+{+ const struct xc_rsa_key *k = KEY(key);+ mbedtls_md_type_t md = MBEDTLS_MD_NONE;+ mbedtls_rsa_context rsa;+ int rc = 0;++ *valid = false;+ if (oid != NULL) {+ size_t i;+ /* a DER object identifier: tag 6 and a short-form length, then that many bytes */+ if (oid[0] != 0x06 || oid[1] > 0x7f) {+ return CCRSA_INVALID_INPUT;+ }+ for (i = 0; i < sizeof(digests) / sizeof(digests[0]); i++) {+ if ((size_t)oid[1] + 2 == digests[i].oid_size && memcmp(oid, digests[i].oid, digests[i].oid_size) == 0) {+ break;+ }+ }+ if (i == sizeof(digests) / sizeof(digests[0]) || digests[i].len != digest_len) {+ return CCRSA_INVALID_INPUT;+ }+ md = digests[i].md;+ }+ if (sig_len != k->mod_nbytes || digest_len > UINT32_MAX) {+ return 0; /* a signature of the wrong size is just not valid */+ }+ mbedtls_rsa_init(&rsa);+ if (mbedtls_rsa_import_raw(&rsa, k->bytes, k->mod_nbytes, NULL, 0, NULL, 0, NULL, 0,+ k->bytes + k->mod_nbytes, k->exp_nbytes) != 0 || mbedtls_rsa_complete(&rsa) != 0) {+ rc = CCRSA_KEY_ERROR;+ } else {+ *valid = mbedtls_rsa_rsassa_pkcs1_v15_verify(&rsa, md, (unsigned int)digest_len, digest, sig) == 0;+ }+ mbedtls_rsa_free(&rsa);+ return rc;+}diff --git a/kexts/free/corecrypto/cc_selftest.c b/kexts/free/corecrypto/cc_selftest.cnew file mode 100644index 0000000..67bb861--- /dev/null+++ b/kexts/free/corecrypto/cc_selftest.c@@ -0,0 +1,199 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * Known-answer tests run when the kext starts, through the same calls the kernel makes: the digests on "abc" (FIPS+ * 180-4 / RFC 1321), HMAC-SHA-256 (RFC 4231 case 1), AES-128 (FIPS 197 C.1) in ECB, CBC and GCM (the GCM+ * specification's test case 2), and round trips of the modes without a short standard vector here. The full test+ * vectors run in user space (test/).+ */+#include "cc_internal.h"++static int+hexeq(const uint8_t *got, const char *hex, size_t n)+{+ for (size_t i = 0; i < n; i++) {+ unsigned v;+ char c[3] = { hex[2 * i], hex[2 * i + 1], 0 };+ v = (unsigned)((c[0] <= '9' ? c[0] - '0' : (c[0] | 0x20) - 'a' + 10) << 4 |+ (c[1] <= '9' ? c[1] - '0' : (c[1] | 0x20) - 'a' + 10));+ if (got[i] != v) {+ return 0;+ }+ }+ return 1;+}++static int+digest_abc(const struct ccdigest_info *di, const char *hex)+{+ uint8_t out[64];++ xc_digest(di, 3, "abc", out);+ return hexeq(out, hex, di->output_size);+}++int+xc_selftest(void)+{+ static const uint8_t k128[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };+ static const uint8_t pt[16] = {+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,+ };+ uint8_t out[64], back[64], key[20], tag[16], iv[16];++ if (!digest_abc(xc_md5_di(), "900150983cd24fb0d6963f7d28e17f72")) {+ return 1;+ }+ if (!digest_abc(xc_sha1_di(), "a9993e364706816aba3e25717850c26c9cd0d89d")) {+ return 2;+ }+ if (!digest_abc(xc_sha224_di(), "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")) {+ return 3;+ }+ if (!digest_abc(xc_sha256_di(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")) {+ return 4;+ }+ if (!digest_abc(xc_sha384_di(), "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"+ "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7")) {+ return 5;+ }+ if (!digest_abc(xc_sha512_di(), "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")) {+ return 6;+ }+ memset(key, 0x0b, sizeof(key));+ xc_hmac(xc_sha256_di(), sizeof(key), key, 8, "Hi There", out);+ if (!hexeq(out, "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", 32)) {+ return 7;+ }++ /* AES-128 */+ {+ const struct ccmode_ecb *e = xc_aes_ecb_encrypt(), *d = xc_aes_ecb_decrypt();+ ccecb_ctx_decl(e->size, ce);+ ccecb_ctx_decl(d->size, cd);+ if (e->init(e, ce, 16, k128) || e->ecb(ce, 1, pt, out) || !hexeq(out, "69c4e0d86a7b0430d8cdb78070b4c55a", 16)) {+ return 8;+ }+ if (d->init(d, cd, 16, k128) || d->ecb(cd, 1, out, back) || memcmp(back, pt, 16) != 0) {+ return 9;+ }+ }+ {+ /* CBC with a zero IV: the first block is ECB's */+ const struct ccmode_cbc *e = xc_aes_cbc_encrypt(), *d = xc_aes_cbc_decrypt();+ cccbc_ctx_decl(e->size, ce);+ cccbc_ctx_decl(d->size, cd);+ uint8_t two[32];+ memcpy(two, pt, 16);+ memcpy(two + 16, pt, 16);+ memset(iv, 0, 16);+ if (e->init(e, ce, 16, k128) || e->cbc(ce, (cccbc_iv *)iv, 2, two, out) ||+ !hexeq(out, "69c4e0d86a7b0430d8cdb78070b4c55a", 16) || memcmp(iv, out + 16, 16) != 0) {+ return 10;+ }+ memset(iv, 0, 16);+ if (d->init(d, cd, 16, k128) || d->cbc(cd, (cccbc_iv *)iv, 2, out, back) || memcmp(back, two, 32) != 0) {+ return 11;+ }+ }+ {+ /* GCM test case 2: zero key, zero 96-bit IV, one zero block */+ const struct ccmode_gcm *e = xc_aes_gcm_encrypt(), *d = xc_aes_gcm_decrypt();+ ccgcm_ctx_decl(e->size, ge);+ ccgcm_ctx_decl(d->size, gd);+ uint8_t zero[16] = {0};+ if (e->init(e, ge, 16, zero) || e->set_iv(ge, 12, zero) || e->gcm(ge, 16, zero, out) ||+ e->finalize(ge, 16, tag) || !hexeq(out, "0388dace60b6a392f328c2b971b2fe78", 16) ||+ !hexeq(tag, "ab6e47d42cec13bdf53a67b21257bddf", 16)) {+ return 12;+ }+ if (d->init(d, gd, 16, zero) || d->set_iv(gd, 12, zero) || d->gcm(gd, 16, out, back) ||+ d->finalize(gd, 16, tag) || memcmp(back, zero, 16) != 0) {+ return 13;+ }+ tag[0] ^= 1;+ if (d->reset(gd) || d->set_iv(gd, 12, zero) || d->gcm(gd, 16, out, back) ||+ d->finalize(gd, 16, tag) != CCMODE_INTEGRITY_FAILURE) {+ return 14;+ }+ }+ {+ /* XTS and CTR: back to the plaintext */+ const struct ccmode_xts *e = xc_aes_xts_encrypt(), *d = xc_aes_xts_decrypt();+ ccxts_ctx_decl(e->size, xe);+ ccxts_ctx_decl(d->size, xd);+ ccxts_tweak_decl(e->tweak_size, t);+ uint8_t k2[16], data[64];+ for (int i = 0; i < 16; i++) {+ k2[i] = (uint8_t)(0xf0 + i);+ }+ for (int i = 0; i < 64; i++) {+ data[i] = (uint8_t)i;+ }+ memset(iv, 0, 16);+ if (e->init(e, xe, 16, k128, k2) || e->set_tweak(xe, t, iv) || !e->xts(xe, t, 4, data, out) ||+ memcmp(out, data, 64) == 0) {+ return 15;+ }+ if (d->init(d, xd, 16, k128, k2) || d->set_tweak(xd, t, iv) || !d->xts(xd, t, 4, out, back) ||+ memcmp(back, data, 64) != 0) {+ return 16;+ }+ const struct ccmode_ctr *c = xc_aes_ctr();+ ccctr_ctx_decl(c->size, cc);+ if (c->init(c, cc, 16, k128, iv) || c->ctr(cc, 7, data, out) || c->ctr(cc, 57, data + 7, out + 7) ||+ c->setctr(c, cc, iv) || c->ctr(cc, 64, out, back) || memcmp(back, data, 64) != 0) {+ return 17;+ }+ }+ {+ /* ChaCha20-Poly1305 and triple DES with CTS3: round trips, and a forged tag refused */+ ccchacha20poly1305_ctx cp;+ uint8_t k32[32], nonce[12] = {0}, msg[40];+ for (int i = 0; i < 32; i++) {+ k32[i] = (uint8_t)i;+ }+ for (int i = 0; i < 40; i++) {+ msg[i] = (uint8_t)(i * 7);+ }+ if (xc_ccp_init(NULL, &cp, k32) || xc_ccp_setnonce(NULL, &cp, nonce) || xc_ccp_aad(NULL, &cp, 3, "abc") ||+ xc_ccp_encrypt(NULL, &cp, 40, msg, out) || xc_ccp_finalize(NULL, &cp, tag)) {+ return 18;+ }+ if (xc_ccp_reset(NULL, &cp) || xc_ccp_setnonce(NULL, &cp, nonce) || xc_ccp_aad(NULL, &cp, 3, "abc") ||+ xc_ccp_decrypt(NULL, &cp, 40, out, back) || xc_ccp_verify(NULL, &cp, tag) || memcmp(back, msg, 40) != 0) {+ return 19;+ }+ tag[15] ^= 0x80;+ if (xc_ccp_reset(NULL, &cp) || xc_ccp_setnonce(NULL, &cp, nonce) || xc_ccp_aad(NULL, &cp, 3, "abc") ||+ xc_ccp_decrypt(NULL, &cp, 40, out, back) || xc_ccp_verify(NULL, &cp, tag) != CCMODE_INTEGRITY_FAILURE) {+ return 20;+ }+ const struct ccmode_cbc *e = xc_des3_cbc_encrypt(), *d = xc_des3_cbc_decrypt();+ cccbc_ctx_decl(e->size, de);+ cccbc_ctx_decl(d->size, dd);+ uint8_t k24[24];+ for (int i = 0; i < 24; i++) {+ k24[i] = (uint8_t)(0x13 * i + 1);+ }+ xc_des_key_set_odd_parity(k24, 24);+ memset(iv, 0, 8);+ if (e->init(e, de, 24, k24) || xc_cts3_encrypt(e, de, (cccbc_iv *)iv, 21, msg, out) != 21) {+ return 21;+ }+ memset(iv, 0, 8);+ if (d->init(d, dd, 24, k24) || xc_cts3_decrypt(d, dd, (cccbc_iv *)iv, 21, out, back) != 21 ||+ memcmp(back, msg, 21) != 0) {+ return 22;+ }+ }+ return 0;+}diff --git a/kexts/free/corecrypto/compat/assert.h b/kexts/free/corecrypto/compat/assert.hnew file mode 100644index 0000000..4e0d70f--- /dev/null+++ b/kexts/free/corecrypto/compat/assert.h@@ -0,0 +1,11 @@+// SPDX-License-Identifier: APSL-2.0+/* For Mbed TLS in the kernel (-idirafter): its asserts are debugging aids, off as in a release build. */+#ifndef XNULINUX_CC_COMPAT_ASSERT_H+#define XNULINUX_CC_COMPAT_ASSERT_H+#ifndef assert+#define assert(x) ((void)0)+#endif+#ifndef static_assert+#define static_assert _Static_assert+#endif+#endifdiff --git a/kexts/free/corecrypto/compat/stdio.h b/kexts/free/corecrypto/compat/stdio.hnew file mode 100644index 0000000..47387c6--- /dev/null+++ b/kexts/free/corecrypto/compat/stdio.h@@ -0,0 +1,6 @@+// SPDX-License-Identifier: APSL-2.0+/* For Mbed TLS in the kernel (-idirafter): oid.c includes <stdio.h> for snprintf, which the config replaces. */+#ifndef XNULINUX_CC_COMPAT_STDIO_H+#define XNULINUX_CC_COMPAT_STDIO_H+#include <stddef.h>+#endifdiff --git a/kexts/free/corecrypto/compat/stdlib.h b/kexts/free/corecrypto/compat/stdlib.hnew file mode 100644index 0000000..86ed0ef--- /dev/null+++ b/kexts/free/corecrypto/compat/stdlib.h@@ -0,0 +1,6 @@+// SPDX-License-Identifier: APSL-2.0+/* For Mbed TLS in the kernel (-idirafter): it includes <stdlib.h> and uses nothing of it but what <stddef.h> has. */+#ifndef XNULINUX_CC_COMPAT_STDLIB_H+#define XNULINUX_CC_COMPAT_STDLIB_H+#include <stddef.h>+#endifdiff --git a/kexts/free/corecrypto/mbedtls.rev b/kexts/free/corecrypto/mbedtls.revnew file mode 100644index 0000000..1a8f34b--- /dev/null+++ b/kexts/free/corecrypto/mbedtls.rev@@ -0,0 +1,6 @@+# SPDX-License-Identifier: GPL-2.0-only+# Mbed TLS (Apache-2.0 OR GPL-2.0-or-later; used under Apache-2.0), fetched at build time, never kept in this+# repository: the release, by commit, and the library sources the kext compiles (mbedtls_config_xnulinux.h's modules).+MBEDTLS_VERSION=3.6.8+MBEDTLS_REV=82e69999f91126589a28b70dd75b69fbcabe2711+MBEDTLS_SOURCES=aes.c aesce.c block_cipher.c gcm.c des.c chacha20.c poly1305.c chachapoly.c md5.c sha1.c sha256.c sha512.c md.c rsa.c rsa_alt_helpers.c bignum.c bignum_core.c constant_time.c oid.c asn1parse.c asn1write.c platform.c platform_util.cdiff --git a/kexts/free/corecrypto/mbedtls_config_xnulinux.h b/kexts/free/corecrypto/mbedtls_config_xnulinux.hnew file mode 100644index 0000000..749b46e--- /dev/null+++ b/kexts/free/corecrypto/mbedtls_config_xnulinux.h@@ -0,0 +1,64 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * Mbed TLS (scripts/free-kc/build.sh fetches it at a pinned release) as this kext uses it: the primitives behind the+ * corecrypto interface, nothing else. No I/O, no time, no threads; memory from the kext (cc_platform.c).+ */+#ifndef MBEDTLS_CONFIG_XNULINUX_H+#define MBEDTLS_CONFIG_XNULINUX_H++#define MBEDTLS_PLATFORM_C+#define MBEDTLS_PLATFORM_MEMORY+#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS+#define MBEDTLS_PLATFORM_CALLOC_MACRO xnulinux_cc_calloc+#define MBEDTLS_PLATFORM_FREE_MACRO xnulinux_cc_free+#define MBEDTLS_PLATFORM_SNPRINTF_MACRO xnulinux_cc_snprintf /* OIDs as text: never needed here */+#define MBEDTLS_DEPRECATED_REMOVED++/* digests */+#define MBEDTLS_MD_C+#define MBEDTLS_MD5_C+#define MBEDTLS_SHA1_C+#define MBEDTLS_SHA224_C+#define MBEDTLS_SHA256_C+#define MBEDTLS_SHA384_C+#define MBEDTLS_SHA512_C++/* ciphers and modes */+#define MBEDTLS_AES_C+#define MBEDTLS_CIPHER_MODE_CBC+#define MBEDTLS_CIPHER_MODE_CTR+#define MBEDTLS_GCM_C+#define MBEDTLS_DES_C+#define MBEDTLS_CHACHA20_C+#define MBEDTLS_POLY1305_C+#define MBEDTLS_CHACHAPOLY_C++/* RSA PKCS #1 v1.5 signature verification */+#define MBEDTLS_BIGNUM_C+#define MBEDTLS_RSA_C+#define MBEDTLS_PKCS1_V15+#define MBEDTLS_OID_C+#define MBEDTLS_NO_UDBL_DIVISION /* no 128-bit division in the kernel (__udivti3) */+#define MBEDTLS_ASN1_PARSE_C++/* arm64: AES and SHA-256 with the CPU's crypto instructions only (every host xnulinux runs on has them) */+#if defined(__aarch64__)+#define MBEDTLS_AESCE_C+#define MBEDTLS_AES_USE_HARDWARE_ONLY+#define MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY+#endif++#include <stddef.h>+void *xnulinux_cc_calloc(size_t n, size_t size);+void xnulinux_cc_free(void *p);+int xnulinux_cc_snprintf(char *s, size_t n, const char *format, ...);++#endif /* MBEDTLS_CONFIG_XNULINUX_H */diff --git a/kexts/free/corecrypto/test/.gitignore b/kexts/free/corecrypto/test/.gitignorenew file mode 100644index 0000000..567609b--- /dev/null+++ b/kexts/free/corecrypto/test/.gitignore@@ -0,0 +1 @@+build/diff --git a/kexts/free/corecrypto/test/rfc3962.h b/kexts/free/corecrypto/test/rfc3962.hnew file mode 100644index 0000000..5bb4c50--- /dev/null+++ b/kexts/free/corecrypto/test/rfc3962.h@@ -0,0 +1,22 @@+// SPDX-License-Identifier: APSL-2.0+/* RFC 3962 Appendix B: AES-128 CBC with ciphertext stealing, IV 0, key "chicken teriyaki" */+static const struct { const char *in, *out, *next_iv; } rfc3962[] = {+ { "4920776f756c64206c696b652074686520",+ "c6353568f2bf8cb4d8a580362da7ff7f97",+ "c6353568f2bf8cb4d8a580362da7ff7f" },+ { "4920776f756c64206c696b65207468652047656e6572616c20476175277320",+ "fc00783e0efdb2c1d445d4c8eff7ed2297687268d6ecccc0c07b25e25ecfe5",+ "fc00783e0efdb2c1d445d4c8eff7ed22" },+ { "4920776f756c64206c696b65207468652047656e6572616c2047617527732043",+ "39312523a78662d5be7fcbcc98ebf5a897687268d6ecccc0c07b25e25ecfe584",+ "39312523a78662d5be7fcbcc98ebf5a8" },+ { "4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c",+ "97687268d6ecccc0c07b25e25ecfe584b3fffd940c16a18c1b5549d2f838029e39312523a78662d5be7fcbcc98ebf5",+ "b3fffd940c16a18c1b5549d2f838029e" },+ { "4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c20",+ "97687268d6ecccc0c07b25e25ecfe5849dad8bbb96c4cdc03bc103e1a194bbd839312523a78662d5be7fcbcc98ebf5a8",+ "9dad8bbb96c4cdc03bc103e1a194bbd8" },+ { "4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c20616e6420776f6e746f6e20736f75702e",+ "97687268d6ecccc0c07b25e25ecfe58439312523a78662d5be7fcbcc98ebf5a84807efe836ee89a526730dbc2f7bc8409dad8bbb96c4cdc03bc103e1a194bbd8",+ "4807efe836ee89a526730dbc2f7bc840" },+};diff --git a/kexts/free/corecrypto/test/run.sh b/kexts/free/corecrypto/test/run.shnew file mode 100755index 0000000..70dc18a--- /dev/null+++ b/kexts/free/corecrypto/test/run.sh@@ -0,0 +1,24 @@+#!/bin/bash+# SPDX-License-Identifier: GPL-2.0-only+# Build the corecrypto kext's sources for user space with Mbed TLS (MBEDTLS: a checkout of the pinned release; fetched+# when not given) and run test.c against OpenSSL, with AddressSanitizer and UndefinedBehaviorSanitizer.+set -euo pipefail+D=$(cd "$(dirname "$0")" && pwd); K=$D/..; I=$(cd "$K/../../../xnu-patches/corecrypto/include" && pwd)+MBEDTLS_REV=$(sed -n 's/^MBEDTLS_REV=//p' "$K/mbedtls.rev")+W=${W:-$D/build}; mkdir -p "$W"+M=${MBEDTLS:-$W/mbedtls-$MBEDTLS_REV} # the cache is per pinned commit+if [ ! -d "$M/library" ] && [ -z "${MBEDTLS:-}" ]; then+ rm -rf "$M"; git init -q "$M" && git -C "$M" fetch -q --depth 1 https://github.com/Mbed-TLS/mbedtls "$MBEDTLS_REV" &&+ git -C "$M" checkout -q FETCH_HEAD+fi+HEAD=$(git -C "$M" rev-parse HEAD 2>/dev/null || echo "(not a git checkout)")+[ "$HEAD" = "$MBEDTLS_REV" ] || { echo "Mbed TLS at $M is $HEAD, mbedtls.rev pins $MBEDTLS_REV"; exit 1; }+LIB=$(sed -n 's/^MBEDTLS_SOURCES=//p' "$K/mbedtls.rev")+CC=${CC:-clang}+$CC -std=c11 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -Wall -Wextra -Wno-unused-parameter \+ -DCC_TEST_USERSPACE=1 -D_GNU_SOURCE -DMBEDTLS_CONFIG_FILE='"mbedtls_config_xnulinux.h"' -I"$I" -I"$K" -I"$M/include" -I"$M/library" \+ "$D/test.c" "$K"/cc_digest.c "$K"/cc_aes.c "$K"/cc_des.c "$K"/cc_chachapoly.c "$K"/cc_rsa.c "$K"/cc_platform.c \+ "$K"/cc_selftest.c \+ $(for f in $LIB; do echo "$M/library/$f"; done) -lcrypto -o "$W/cctest" > "$W/build.log" 2>&1 ||+ { head -60 "$W/build.log"; exit 1; }+"$W/cctest"diff --git a/kexts/free/corecrypto/test/test.c b/kexts/free/corecrypto/test/test.cnew file mode 100644index 0000000..8ee7cce--- /dev/null+++ b/kexts/free/corecrypto/test/test.c@@ -0,0 +1,575 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * The corecrypto kext's primitives in user space, through the interface's calls: its own self tests, RFC 3962's CTS+ * vectors, and every mode against OpenSSL on random keys, lengths and call splits. run.sh builds and runs it.+ */+#include "../cc_internal.h"+#include <openssl/evp.h>+#include <openssl/hmac.h>+#include <openssl/rsa.h>+#include <openssl/bn.h>+#include <openssl/core_names.h>+#include <openssl/provider.h>+#include <sys/random.h>+#include "rfc3962.h"++/* the kernel's (osfmk/xnulinux_crypto) */+void+cc_clear(size_t len, void *dst)+{+ memset(dst, 0, len);+ __asm__ __volatile__ ("" : : "r"(dst) : "memory");+}++int+cc_cmp_safe(size_t num, const void *a, const void *b)+{+ const volatile uint8_t *x = a, *y = b;+ uint8_t d = 0;+ for (size_t i = 0; i < num; i++) {+ d |= x[i] ^ y[i];+ }+ return num == 0 || d != 0;+}++static unsigned long host_calls; /* how often the kext asked the host for random numbers */+void+xc_random(void *buf, size_t n)+{+ host_calls++;+ if (getrandom(buf, n, 0) != (ssize_t)n) {+ abort();+ }+}++/* the allocations live now and their sizes: a free of another size fails the test, as IOFreeData's size must match */+static struct { void *p; size_t n; } live[4096];+static int frees_wrong;+void+xc_test_alloc(void *p, size_t n)+{+ for (size_t i = 0; i < sizeof(live) / sizeof(live[0]); i++) {+ if (live[i].p == NULL) {+ live[i].p = p, live[i].n = n;+ return;+ }+ }+ abort();+}+void+xc_test_free(void *p, size_t n)+{+ for (size_t i = 0; i < sizeof(live) / sizeof(live[0]); i++) {+ if (live[i].p == p) {+ frees_wrong += live[i].n != n;+ live[i].p = NULL;+ free(p);+ return;+ }+ }+ abort();+}++/* the context macros take expressions as sizes */+_Static_assert(cc_ctx_n(ccecb_ctx, 1 ? 17 : 32) == 2, "cc_ctx_n with a conditional size");+_Static_assert(cc_pad_align(1 ? 1 : 2) == CC_MAX_ALIGNMENT, "cc_pad_align with a conditional size");++static uint64_t seed = 0x9e3779b97f4a7c15ull;+static uint64_t+rnd(void)+{+ seed ^= seed << 13, seed ^= seed >> 7, seed ^= seed << 17;+ return seed;+}+static size_t rndn(size_t n) { return n ? (size_t)(rnd() % n) : 0; }+static void fill(uint8_t *p, size_t n) { while (n--) *p++ = (uint8_t)rnd(); }++static int failures;+#define CHECK(cond, ...) do { if (!(cond)) { failures++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)++static void+unhex(const char *h, uint8_t *out, size_t *n)+{+ size_t i = 0;+ for (; h[2 * i]; i++) {+ sscanf(h + 2 * i, "%2hhx", &out[i]);+ }+ *n = i;+}++/* digests and HMAC, in random pieces */+static void+test_digests(void)+{+ static const struct { const struct ccdigest_info *(*di)(void); const char *name; } d[] = {+ { xc_md5_di, "MD5" }, { xc_sha1_di, "SHA1" }, { xc_sha224_di, "SHA224" }, { xc_sha256_di, "SHA256" },+ { xc_sha384_di, "SHA384" }, { xc_sha512_di, "SHA512" },+ };+ static uint8_t data[5000], key[300];++ for (size_t a = 0; a < sizeof(d) / sizeof(d[0]); a++) {+ const struct ccdigest_info *di = d[a].di();+ const EVP_MD *md = EVP_get_digestbyname(d[a].name);+ for (int it = 0; it < 400; it++) {+ size_t len = it < 300 ? (size_t)it : rndn(sizeof(data)), klen = rndn(sizeof(key)), off = 0;+ uint8_t ours[64], theirs[64];+ unsigned tl;+ fill(data, len);+ fill(key, klen);+ ccdigest_di_decl(di, ctx);+ memset(ctx, 0xa5, ccdigest_di_size(di));+ xc_digest_init(di, ctx);+ /* MD5Init and SHA1Init copy the whole pending block out of a fresh context */+ CHECK(memcmp(ccdigest_data(di, ctx), (uint8_t[128]){0}, di->block_size) == 0, "%s init leaves the block", d[a].name);+ while (off < len) {+ size_t n = 1 + rndn(len - off);+ xc_digest_update(di, ctx, n, data + off);+ off += n;+ }+ xc_digest_final(di, ctx, ours);+ EVP_Digest(data, len, theirs, &tl, md, NULL);+ CHECK(tl == di->output_size && memcmp(ours, theirs, tl) == 0, "%s of %zu bytes", d[a].name, len);+ cchmac_di_decl(di, hc);+ xc_hmac_init(di, hc, klen, key);+ for (off = 0; off < len;) {+ size_t n = 1 + rndn(len - off);+ xc_hmac_update(di, hc, n, data + off);+ off += n;+ }+ xc_hmac_final(di, hc, ours);+ HMAC(md, key, (int)klen, data, len, theirs, &tl);+ CHECK(memcmp(ours, theirs, tl) == 0, "HMAC-%s key %zu data %zu", d[a].name, klen, len);+ }+ }+}++static int+evp_crypt(const EVP_CIPHER *c, int enc, const uint8_t *key, const uint8_t *iv, const uint8_t *in, size_t n, uint8_t *out)+{+ EVP_CIPHER_CTX *x = EVP_CIPHER_CTX_new();+ int l1 = 0, l2 = 0;+ EVP_CipherInit_ex(x, c, NULL, key, iv, enc);+ EVP_CIPHER_CTX_set_padding(x, 0);+ EVP_CipherUpdate(x, out, &l1, in, (int)n);+ EVP_CipherFinal_ex(x, out + l1, &l2);+ EVP_CIPHER_CTX_free(x);+ return l1 + l2;+}++static void+test_aes(void)+{+ static uint8_t key[32], key2[32], iv[16], ivc[16], pt[4096], ours[4096], theirs[4096], back[4096];+ const char *bits[] = { "128", "192", "256" };++ for (int k = 0; k < 3; k++) {+ size_t kn = 16 + 8 * (size_t)k;+ char name[32];+ for (int it = 0; it < 200; it++) {+ size_t nb = 1 + rndn(64), n = nb * 16;+ fill(key, kn), fill(key2, kn), fill(iv, 16), fill(pt, n);+ /* ECB */+ const struct ccmode_ecb *ee = xc_aes_ecb_encrypt(), *ed = xc_aes_ecb_decrypt();+ ccecb_ctx_decl(ee->size, ce);+ ccecb_ctx_decl(ed->size, cd);+ ee->init(ee, ce, kn, key), ee->ecb(ce, nb, pt, ours);+ snprintf(name, sizeof(name), "AES-%s-ECB", bits[k]);+ evp_crypt(EVP_get_cipherbyname(name), 1, key, NULL, pt, n, theirs);+ CHECK(memcmp(ours, theirs, n) == 0, "%s %zu", name, n);+ ed->init(ed, cd, kn, key), ed->ecb(cd, nb, ours, back);+ CHECK(memcmp(back, pt, n) == 0, "%s decrypt", name);+ /* CBC, in two calls, the IV carried */+ const struct ccmode_cbc *be = xc_aes_cbc_encrypt(), *bd = xc_aes_cbc_decrypt();+ cccbc_ctx_decl(be->size, cbe);+ cccbc_ctx_decl(bd->size, cbd);+ size_t split = rndn(nb + 1);+ memcpy(ivc, iv, 16);+ be->init(be, cbe, kn, key);+ be->cbc(cbe, (cccbc_iv *)ivc, split, pt, ours), be->cbc(cbe, (cccbc_iv *)ivc, nb - split, pt + split * 16, ours + split * 16);+ snprintf(name, sizeof(name), "AES-%s-CBC", bits[k]);+ evp_crypt(EVP_get_cipherbyname(name), 1, key, iv, pt, n, theirs);+ CHECK(memcmp(ours, theirs, n) == 0 && memcmp(ivc, theirs + n - 16, 16) == 0, "%s %zu", name, n);+ memcpy(ivc, iv, 16);+ memcpy(back, ours, n);+ bd->init(bd, cbd, kn, key), bd->cbc(cbd, (cccbc_iv *)ivc, nb, back, back); /* in place */+ CHECK(memcmp(back, pt, n) == 0, "%s decrypt in place", name);+ /* CTR in byte pieces */+ const struct ccmode_ctr *ct = xc_aes_ctr();+ ccctr_ctx_decl(ct->size, cc);+ size_t len = rndn(n + 1), off = 0;+ ct->init(ct, cc, kn, key, iv);+ while (off < len) {+ size_t m = 1 + rndn(len - off);+ ct->ctr(cc, m, pt + off, ours + off);+ off += m;+ }+ snprintf(name, sizeof(name), "AES-%s-CTR", bits[k]);+ evp_crypt(EVP_get_cipherbyname(name), 1, key, iv, pt, len, theirs);+ CHECK(memcmp(ours, theirs, len) == 0, "%s %zu", name, len);+ /* XTS (128 and 256), the sector in two calls */+ if (kn != 24) {+ const struct ccmode_xts *xe = xc_aes_xts_encrypt(), *xd = xc_aes_xts_decrypt();+ ccxts_ctx_decl(xe->size, cxe);+ ccxts_ctx_decl(xd->size, cxd);+ ccxts_tweak_decl(xe->tweak_size, tw);+ uint8_t k2[64];+ memcpy(k2, key, kn), memcpy(k2 + kn, key2, kn);+ xe->init(xe, cxe, kn, key, key2), xe->set_tweak(cxe, tw, iv);+ xe->xts(cxe, tw, split, pt, ours), xe->xts(cxe, tw, nb - split, pt + split * 16, ours + split * 16);+ snprintf(name, sizeof(name), "AES-%s-XTS", kn == 16 ? "128" : "256");+ evp_crypt(EVP_get_cipherbyname(name), 1, k2, iv, pt, n, theirs);+ CHECK(memcmp(ours, theirs, n) == 0, "%s %zu", name, n);+ xd->init(xd, cxd, kn, key, key2), xd->set_tweak(cxd, tw, iv), xd->xts(cxd, tw, nb, ours, back);+ CHECK(memcmp(back, pt, n) == 0, "%s decrypt", name);+ }+ }+ }+ /* XTS takes 16- and 32-byte keys, and two different ones */+ {+ const struct ccmode_xts *xe = xc_aes_xts_encrypt();+ ccxts_ctx_decl(xe->size, cx);+ uint8_t k1[32], k2[32];+ fill(k1, 32), fill(k2, 32);+ CHECK(xe->init(xe, cx, 24, k1, k2) != 0, "XTS with 24-byte keys accepted");+ CHECK(xe->init(xe, cx, (size_t)-16, k1, k2) != 0, "XTS with a negative key length accepted");+ CHECK(xe->init(xe, cx, 0, k1, k2) != 0, "XTS with no key accepted");+ CHECK(xe->init(xe, cx, 32, k1, k1) == CCERR_XTS_KEYS_EQUAL, "XTS with equal keys accepted");+ CHECK(xe->init(xe, cx, 32, k1, k2) == 0, "XTS-256 init");+ }+ /* a block count whose bytes size_t cannot hold: refused, nothing done */+ {+ const struct ccmode_cbc *ce = xc_aes_cbc_encrypt();+ cccbc_ctx_decl(ce->size, cc);+ uint8_t k1[16] = {0}, ivb[16] = {0}, buf[16] = {0};+ ce->init(ce, cc, 16, k1);+ CHECK(ce->cbc(cc, (cccbc_iv *)ivb, SIZE_MAX / 16 + 1, buf, buf) != 0, "AES-CBC with a block count past size_t");+ const struct ccmode_cbc *de = xc_des_cbc_encrypt(), *d3 = xc_des3_cbc_encrypt();+ cccbc_ctx_decl(de->size, dc);+ cccbc_ctx_decl(d3->size, d3c);+ uint8_t dk[24] = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,+ 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 };+ de->init(de, dc, 8, dk), d3->init(d3, d3c, 24, dk);+ CHECK(de->cbc(dc, (cccbc_iv *)ivb, SIZE_MAX / 8 + 1, buf, buf) != 0, "DES-CBC with a block count past size_t");+ CHECK(d3->cbc(d3c, (cccbc_iv *)ivb, SIZE_MAX / 8 + 1, buf, buf) != 0, "3DES-CBC with a block count past size_t");+ CHECK(memcmp(buf, (uint8_t[16]){0}, 16) == 0, "CBC wrote with a block count past size_t");+ }+}++static void+test_gcm(void)+{+ static uint8_t key[32], iv[64], aad[300], pt[3000], ours[3000], theirs[3000], back[3000];++ for (int it = 0; it < 600; it++) {+ size_t kn = 16 + 8 * rndn(3), ivn = it % 3 ? 12 : 1 + rndn(64), an = rndn(sizeof(aad)), n = rndn(sizeof(pt));+ size_t tn = 12 + rndn(5), off;+ uint8_t tag[16], ttag[16];+ int l;+ fill(key, kn), fill(iv, ivn), fill(aad, an), fill(pt, n);+ const struct ccmode_gcm *e = xc_aes_gcm_encrypt(), *d = xc_aes_gcm_decrypt();+ ccgcm_ctx_decl(e->size, ge);+ ccgcm_ctx_decl(d->size, gd);+ CHECK(e->init(e, ge, kn, key) == 0 && e->set_iv(ge, ivn, iv) == 0, "GCM init");+ for (off = 0; off < an;) {+ size_t m = 1 + rndn(an - off);+ e->gmac(ge, m, aad + off);+ off += m;+ }+ for (off = 0; off < n;) {+ size_t m = 1 + rndn(n - off);+ e->gcm(ge, m, pt + off, ours + off);+ off += m;+ }+ CHECK(e->finalize(ge, tn, tag) == 0, "GCM finalize");+ EVP_CIPHER_CTX *x = EVP_CIPHER_CTX_new();+ EVP_EncryptInit_ex(x, kn == 16 ? EVP_aes_128_gcm() : kn == 24 ? EVP_aes_192_gcm() : EVP_aes_256_gcm(), NULL, NULL, NULL);+ EVP_CIPHER_CTX_ctrl(x, EVP_CTRL_GCM_SET_IVLEN, (int)ivn, NULL);+ EVP_EncryptInit_ex(x, NULL, NULL, key, iv);+ if (an) EVP_EncryptUpdate(x, NULL, &l, aad, (int)an);+ if (n) EVP_EncryptUpdate(x, theirs, &l, pt, (int)n);+ EVP_EncryptFinal_ex(x, theirs, &l);+ EVP_CIPHER_CTX_ctrl(x, EVP_CTRL_GCM_GET_TAG, 16, ttag);+ EVP_CIPHER_CTX_free(x);+ CHECK(memcmp(ours, theirs, n) == 0 && memcmp(tag, ttag, tn) == 0, "GCM k%zu iv%zu aad%zu n%zu", kn, ivn, an, n);+ d->init(d, gd, kn, key), d->set_iv(gd, ivn, iv), d->gmac(gd, an, aad), d->gcm(gd, n, ours, back);+ CHECK(d->finalize(gd, tn, tag) == 0 && memcmp(back, pt, n) == 0, "GCM decrypt");+ tag[rndn(tn)] ^= (uint8_t)(1 << rndn(8));+ d->reset(gd), d->set_iv(gd, ivn, iv), d->gmac(gd, an, aad), d->gcm(gd, n, ours, back);+ CHECK(d->finalize(gd, tn, tag) == CCMODE_INTEGRITY_FAILURE, "GCM forged tag accepted");+ /* tag lengths: SP 800-38D's only, both ways */+ size_t bad = (size_t[]){ 0, 1, 3, 5, 7, 9, 11, 17 }[rndn(8)], good = it & 1 ? 4 : 8;+ e->reset(ge), e->set_iv(ge, ivn, iv), e->gmac(ge, an, aad), e->gcm(ge, n, pt, ours);+ CHECK(e->finalize(ge, bad, tag) == CCMODE_INVALID_INPUT, "GCM encrypt with a %zu-byte tag", bad);+ d->reset(gd), d->set_iv(gd, ivn, iv), d->gmac(gd, an, aad), d->gcm(gd, n, theirs, back);+ CHECK(d->finalize(gd, bad, ttag) == CCMODE_INVALID_INPUT, "GCM decrypt with a %zu-byte tag", bad);+ d->reset(gd), d->set_iv(gd, ivn, iv), d->gmac(gd, an, aad), d->gcm(gd, n, theirs, back);+ CHECK(d->finalize(gd, good, ttag) == 0, "GCM decrypt with a %zu-byte tag", good);+ }+ /* IPsec's sequence: init with an IV, then for each packet reset, inc_iv, aad, text, finalize */+ {+ const struct ccmode_gcm *e = xc_aes_gcm_encrypt();+ ccgcm_ctx_decl(e->size, ge);+ uint8_t iv0[12], cur[12], tag[16], ttag[16];+ int l;+ fill(key, 32), fill(iv0, 12), fill(pt, 100);+ memcpy(cur, iv0, 12);+ CHECK(xc_gcm_init_with_iv(e, ge, 32, key, iv0) == 0, "init_with_iv");+ CHECK(xc_gcm_inc_iv(e, ge, iv) == CCMODE_INVALID_CALL_SEQUENCE, "inc_iv before reset");+ for (int p = 0; p < 300; p++) {+ e->reset(ge);+ CHECK(xc_gcm_inc_iv(e, ge, iv) == 0, "inc_iv");+ for (int i = 11; i >= 4; i--) if (++cur[i]) break;+ CHECK(memcmp(iv, cur, 12) == 0, "inc_iv counts the last 8 bytes up (packet %d)", p);+ e->gmac(ge, 8, aad), e->gcm(ge, 100, pt, ours), e->finalize(ge, 16, tag);+ EVP_CIPHER_CTX *x = EVP_CIPHER_CTX_new();+ EVP_EncryptInit_ex(x, EVP_aes_256_gcm(), NULL, key, cur);+ EVP_EncryptUpdate(x, NULL, &l, aad, 8), EVP_EncryptUpdate(x, theirs, &l, pt, 100), EVP_EncryptFinal_ex(x, theirs, &l);+ EVP_CIPHER_CTX_ctrl(x, EVP_CTRL_GCM_GET_TAG, 16, ttag);+ EVP_CIPHER_CTX_free(x);+ CHECK(memcmp(ours, theirs, 100) == 0 && memcmp(tag, ttag, 16) == 0, "IPsec packet %d", p);+ }+ /* the count stays in the last 8 bytes: it wraps there */+ memset(iv0, 0x5a, 4), memset(iv0 + 4, 0xff, 8);+ xc_gcm_init_with_iv(e, ge, 32, key, iv0), e->reset(ge);+ CHECK(xc_gcm_inc_iv(e, ge, iv) == 0 && memcmp(iv, "\x5a\x5a\x5a\x5a\0\0\0\0\0\0\0\0", 12) == 0, "inc_iv wraps");+ }+}++static void+test_des(void)+{+ static uint8_t key[24], iv[8], ivc[8], pt[800], ours[800], theirs[800], back[800];++ for (int it = 0; it < 300; it++) {+ size_t nb = 1 + rndn(100), n = nb * 8;+ fill(key, 24), fill(iv, 8), fill(pt, n);+ xc_des_key_set_odd_parity(key, 24);+ struct { const struct ccmode_cbc *e, *d; size_t kn; const char *name; } m[] = {+ { xc_des_cbc_encrypt(), xc_des_cbc_decrypt(), 8, "DES-CBC" },+ { xc_des3_cbc_encrypt(), xc_des3_cbc_decrypt(), 24, "DES-EDE3-CBC" },+ { xc_des3_cbc_encrypt(), xc_des3_cbc_decrypt(), 16, "DES-EDE-CBC" },+ };+ for (int j = 0; j < 3; j++) {+ cccbc_ctx_decl(m[j].e->size, ce);+ cccbc_ctx_decl(m[j].d->size, cd);+ memcpy(ivc, iv, 8);+ m[j].e->init(m[j].e, ce, m[j].kn, key), m[j].e->cbc(ce, (cccbc_iv *)ivc, nb, pt, ours);+ const EVP_CIPHER *c = EVP_CIPHER_fetch(NULL, m[j].name, NULL);+ CHECK(c != NULL, "OpenSSL has no %s", m[j].name);+ if (c) {+ evp_crypt(c, 1, key, iv, pt, n, theirs);+ CHECK(memcmp(ours, theirs, n) == 0, "%s %zu", m[j].name, n);+ EVP_CIPHER_free((EVP_CIPHER *)c);+ }+ memcpy(ivc, iv, 8);+ m[j].d->init(m[j].d, cd, m[j].kn, key), m[j].d->cbc(cd, (cccbc_iv *)ivc, nb, ours, back);+ CHECK(memcmp(back, pt, n) == 0, "%s decrypt", m[j].name);+ }+ const struct ccmode_ecb *ee = xc_des3_ecb_encrypt();+ ccecb_ctx_decl(ee->size, ce);+ ee->init(ee, ce, 24, key), ee->ecb(ce, nb, pt, ours);+ evp_crypt(EVP_des_ede3_ecb(), 1, key, NULL, pt, n, theirs);+ CHECK(memcmp(ours, theirs, n) == 0, "DES-EDE3-ECB");+ }+ static const uint8_t weak[8] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };+ uint8_t k[8] = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };+ CHECK(xc_des_key_is_weak((void *)weak, 8) && !xc_des_key_is_weak(k, 8), "DES weak keys");+ /* the same weak and semi-weak keys with their parity bits flipped: the bits are not part of the key */+ static const uint8_t weak_even[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };+ static const uint8_t semi[8] = { 0x01, 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e };+ uint8_t semi_even[8], k3[24];+ for (int i = 0; i < 8; i++) semi_even[i] = semi[i] ^ 1;+ CHECK(xc_des_key_is_weak((void *)weak_even, 8), "DES weak key without odd parity");+ CHECK(xc_des_key_is_weak((void *)semi, 8) && xc_des_key_is_weak(semi_even, 8), "DES semi-weak key, both parities");+ memcpy(k3, k, 8), memcpy(k3 + 8, k, 8), memcpy(k3 + 16, weak_even, 8);+ CHECK(xc_des_key_is_weak(k3, 24), "triple DES key with a weak third part");+ CHECK(memcmp(weak_even, (uint8_t[8]){0}, 8) == 0, "the key checked is left as it was");+}++static void+test_cts3(void)+{+ uint8_t key[16], in[64], out[64], ours[64], back[64], next[16], iv[16];+ size_t n, kn;+ unhex("636869636b656e207465726979616b69", key, &kn);+ const struct ccmode_cbc *e = xc_aes_cbc_encrypt(), *d = xc_aes_cbc_decrypt();+ cccbc_ctx_decl(e->size, ce);+ cccbc_ctx_decl(d->size, cd);+ e->init(e, ce, 16, key), d->init(d, cd, 16, key);+ for (size_t i = 0; i < sizeof(rfc3962) / sizeof(rfc3962[0]); i++) {+ size_t on, nn;+ unhex(rfc3962[i].in, in, &n), unhex(rfc3962[i].out, out, &on), unhex(rfc3962[i].next_iv, next, &nn);+ memset(iv, 0, 16);+ CHECK(xc_cts3_encrypt(e, ce, (cccbc_iv *)iv, n, in, ours) == n && memcmp(ours, out, n) == 0, "RFC 3962 case %zu", i);+ CHECK(memcmp(iv, next, 16) == 0, "RFC 3962 case %zu next IV", i);+ memset(iv, 0, 16);+ CHECK(xc_cts3_decrypt(d, cd, (cccbc_iv *)iv, n, out, back) == n && memcmp(back, in, n) == 0, "RFC 3962 case %zu decrypt", i);+ CHECK(memcmp(iv, next, 16) == 0, "RFC 3962 case %zu decrypt next IV", i);+ }+}++static void+test_chachapoly(void)+{+ static uint8_t key[32], nonce[12], aad[200], pt[2000], ours[2000], theirs[2000], back[2000];+ for (int it = 0; it < 400; it++) {+ size_t an = rndn(sizeof(aad)), n = rndn(sizeof(pt)), off;+ uint8_t tag[16], ttag[16];+ int l;+ ccchacha20poly1305_ctx c;+ fill(key, 32), fill(nonce, 12), fill(aad, an), fill(pt, n);+ xc_ccp_init(NULL, &c, key), xc_ccp_setnonce(NULL, &c, nonce);+ for (off = 0; off < an;) { size_t m = 1 + rndn(an - off); xc_ccp_aad(NULL, &c, m, aad + off); off += m; }+ for (off = 0; off < n;) { size_t m = 1 + rndn(n - off); xc_ccp_encrypt(NULL, &c, m, pt + off, ours + off); off += m; }+ CHECK(xc_ccp_finalize(NULL, &c, tag) == 0, "ChaCha20-Poly1305 finalize");+ EVP_CIPHER_CTX *x = EVP_CIPHER_CTX_new();+ EVP_EncryptInit_ex(x, EVP_chacha20_poly1305(), NULL, key, nonce);+ if (an) EVP_EncryptUpdate(x, NULL, &l, aad, (int)an);+ if (n) EVP_EncryptUpdate(x, theirs, &l, pt, (int)n);+ EVP_EncryptFinal_ex(x, theirs, &l);+ EVP_CIPHER_CTX_ctrl(x, EVP_CTRL_AEAD_GET_TAG, 16, ttag);+ EVP_CIPHER_CTX_free(x);+ CHECK(memcmp(ours, theirs, n) == 0 && memcmp(tag, ttag, 16) == 0, "ChaCha20-Poly1305 aad %zu n %zu", an, n);+ xc_ccp_reset(NULL, &c), xc_ccp_setnonce(NULL, &c, nonce), xc_ccp_aad(NULL, &c, an, aad);+ xc_ccp_decrypt(NULL, &c, n, ours, back);+ CHECK(xc_ccp_verify(NULL, &c, tag) == 0 && memcmp(back, pt, n) == 0, "ChaCha20-Poly1305 decrypt");+ tag[rndn(16)] ^= 4;+ xc_ccp_reset(NULL, &c), xc_ccp_setnonce(NULL, &c, nonce), xc_ccp_aad(NULL, &c, an, aad);+ xc_ccp_decrypt(NULL, &c, n, ours, back);+ CHECK(xc_ccp_verify(NULL, &c, tag) == CCMODE_INTEGRITY_FAILURE, "ChaCha20-Poly1305 forged tag accepted");+ /* a message dropped halfway: after reset only setnonce goes on, and the next message is as if it were the first */+ xc_ccp_reset(NULL, &c), xc_ccp_setnonce(NULL, &c, nonce), xc_ccp_aad(NULL, &c, an, aad);+ if (n > 0) xc_ccp_encrypt(NULL, &c, rndn(n) + 1, pt, back);+ xc_ccp_reset(NULL, &c);+ CHECK(xc_ccp_encrypt(NULL, &c, 1, pt, back) != 0, "ChaCha20-Poly1305 encrypt after reset without a nonce");+ CHECK(xc_ccp_aad(NULL, &c, 1, aad) != 0, "ChaCha20-Poly1305 aad after reset without a nonce");+ uint8_t t2[16];+ CHECK(xc_ccp_finalize(NULL, &c, t2) != 0, "ChaCha20-Poly1305 finalize after reset without a nonce");+ CHECK(xc_ccp_verify(NULL, &c, ttag) == CCERR_CALL_SEQUENCE, "ChaCha20-Poly1305 verify after reset without a nonce");+ xc_ccp_reset(NULL, &c), xc_ccp_setnonce(NULL, &c, nonce), xc_ccp_aad(NULL, &c, an, aad);+ xc_ccp_encrypt(NULL, &c, n, pt, back);+ CHECK(xc_ccp_finalize(NULL, &c, t2) == 0 && memcmp(back, theirs, n) == 0 && memcmp(t2, ttag, 16) == 0,+ "ChaCha20-Poly1305 message after a dropped one");+ /* a message ends once */+ CHECK(xc_ccp_finalize(NULL, &c, t2) == CCERR_CALL_SEQUENCE, "ChaCha20-Poly1305 finalized twice");+ CHECK(xc_ccp_verify(NULL, &c, ttag) == CCERR_CALL_SEQUENCE, "ChaCha20-Poly1305 verify after finalize");+ CHECK(xc_ccp_encrypt(NULL, &c, 1, pt, back) != 0, "ChaCha20-Poly1305 encrypt after finalize");+ }+}++static void+test_rsa(void)+{+ static const struct { const char *md; const struct ccdigest_info *(*di)(void); const unsigned char *oid; } h[] = {+ { "SHA1", xc_sha1_di, CC_DIGEST_OID_SHA1 }, { "SHA256", xc_sha256_di, CC_DIGEST_OID_SHA256 },+ { "SHA384", xc_sha384_di, CC_DIGEST_OID_SHA384 }, { "SHA512", xc_sha512_di, CC_DIGEST_OID_SHA512 },+ };+ int sizes[] = { 1024, 2048, 3072, 4096 };+ for (int s = 0; s < 4; s++) {+ EVP_PKEY *pk = EVP_RSA_gen((unsigned)sizes[s]);+ BIGNUM *bn = NULL, *be = NULL;+ uint8_t mod[512], ex[8], dig[64], sig[512], msg[100];+ EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &bn);+ EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_E, &be);+ int mn = BN_bn2bin(bn, mod), en = BN_bn2bin(be, ex);+ /* the kernel's context: room for 4096 bits (libkern/crypto/rsa.h) */+ ccrsa_pub_ctx_decl(ccn_sizeof(4096), key);+ ccrsa_ctx_n(key) = ccn_nof(4096);+ CHECK(xc_rsa_make_pub(key, (size_t)en, ex, (size_t)mn, mod) == 0, "make_pub %d", sizes[s]);+ for (int j = 0; j < 4; j++) {+ const struct ccdigest_info *di = h[j].di();+ size_t sl = sizeof(sig);+ bool valid;+ fill(msg, sizeof(msg));+ xc_digest(di, sizeof(msg), msg, dig);+ EVP_PKEY_CTX *c = EVP_PKEY_CTX_new(pk, NULL);+ EVP_PKEY_sign_init(c);+ EVP_PKEY_CTX_set_rsa_padding(c, RSA_PKCS1_PADDING);+ EVP_PKEY_CTX_set_signature_md(c, EVP_get_digestbyname(h[j].md));+ EVP_PKEY_sign(c, sig, &sl, dig, di->output_size);+ EVP_PKEY_CTX_free(c);+ CHECK(xc_rsa_verify_pkcs1v15(key, h[j].oid, di->output_size, dig, sl, sig, &valid) == 0 && valid,+ "RSA-%d %s valid signature", sizes[s], h[j].md);+ sig[rndn(sl)] ^= 1;+ CHECK(xc_rsa_verify_pkcs1v15(key, h[j].oid, di->output_size, dig, sl, sig, &valid) == 0 && !valid,+ "RSA-%d %s forged signature accepted", sizes[s], h[j].md);+ sig[0] ^= 0, dig[0] ^= 1;+ }+ BN_free(bn), BN_free(be), EVP_PKEY_free(pk);+ }+ /* an object identifier that is no digest's, shorter than any: refused, and read no further than its own length */+ {+ ccrsa_pub_ctx_decl(ccn_sizeof(4096), key);+ uint8_t *oid = malloc(5), dig[32] = {0}, sig[512] = {0};+ bool valid = true;+ memcpy(oid, (uint8_t[]){ 0x06, 0x03, 0x2b, 0x0e, 0x03 }, 5);+ ccrsa_ctx_n(key) = ccn_nof(4096);+ CHECK(xc_rsa_verify_pkcs1v15(key, oid, 32, dig, 512, sig, &valid) == CCRSA_INVALID_INPUT && !valid,+ "RSA with an unknown short OID");+ oid[0] = 0x04;+ CHECK(xc_rsa_verify_pkcs1v15(key, oid, 32, dig, 512, sig, &valid) == CCRSA_INVALID_INPUT, "RSA with no OID tag");+ free(oid);+ }+ CHECK(frees_wrong == 0, "%d frees named another size than their allocation", frees_wrong);+}++static void+test_rng(void)+{+ struct ccrng_state *r = xc_rng(NULL);+ uint64_t v, counts[10] = {0};+ for (int i = 0; i < 100000; i++) {+ CHECK(xc_rng_uniform(r, 10, &v) == 0 && v < 10, "uniform");+ counts[v]++;+ }+ for (int i = 0; i < 10; i++) {+ CHECK(counts[i] > 9000 && counts[i] < 11000, "uniform bucket %d: %llu", i, (unsigned long long)counts[i]);+ }+ /* kmem's generator: uniform, any lengths, and two of them differ */+ _Alignas(16) uint8_t a[256], b[256];+ CHECK(xc_kmem_rng_size() <= sizeof(a), "kmem generator size");+ unsigned long calls = host_calls;+ xc_kmem_rng_init(a), xc_kmem_rng_init(b);+ CHECK(host_calls == calls + 2, "kmem generator keyed from the host at init");+ calls = host_calls;+ memset(counts, 0, sizeof(counts));+ for (int i = 0; i < 100000; i++) {+ CHECK(xc_rng_uniform((struct ccrng_state *)a, 10, &v) == 0 && v < 10, "kmem uniform");+ counts[v]++;+ }+ for (int i = 0; i < 10; i++) {+ CHECK(counts[i] > 9000 && counts[i] < 11000, "kmem uniform bucket %d: %llu", i, (unsigned long long)counts[i]);+ }+ uint8_t x[1000], y[1000];+ for (size_t off = 0; off < sizeof(x);) {+ size_t m = 1 + rndn(sizeof(x) - off);+ ccrng_generate((struct ccrng_state *)a, m, x + off);+ off += m;+ }+ ccrng_generate((struct ccrng_state *)b, sizeof(y), y);+ CHECK(host_calls == calls, "kmem generator asked the host %lu times after init", host_calls - calls);+ CHECK(memcmp(x, y, sizeof(x)) != 0, "two kmem generators gave the same bytes");+ size_t ones = 0;+ for (size_t i = 0; i < sizeof(x); i++) ones += (size_t)__builtin_popcount(x[i]);+ CHECK(ones > 3700 && ones < 4300, "kmem generator bits: %zu ones of 8000", ones);+}++int+main(void)+{+ OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy"), *dflt = OSSL_PROVIDER_load(NULL, "default");+ xc_digest_setup();+ int st = xc_selftest();+ CHECK(st == 0, "self test %d", st);+ test_digests();+ test_aes();+ test_gcm();+ test_des();+ test_cts3();+ test_chachapoly();+ test_rsa();+ test_rng();+ OSSL_PROVIDER_unload(legacy), OSSL_PROVIDER_unload(dflt);+ printf("%s: %d failure(s)\n", failures ? "FAILED" : "PASSED", failures);+ return failures != 0;+}diff --git a/kexts/free/patches/corecrypto-arm64e-sptr.patch b/kexts/free/patches/corecrypto-arm64e-sptr.patchdeleted file mode 100644index 6df7a5d..0000000--- a/kexts/free/patches/corecrypto-arm64e-sptr.patch+++ /dev/null@@ -1,351 +0,0 @@-diff --git a/cc/include/corecrypto/cc_config.h b/cc/include/corecrypto/cc_config.h-index e32f0d3..753e64e 100644---- a/cc/include/corecrypto/cc_config.h-+++ b/cc/include/corecrypto/cc_config.h-@@ -235,4 +235,20 @@- #define CC_SMALL_CODE 0- #endif--+-+/*-+ * xnulinux: function pointers in the structures the kernel shares with corecrypto (digest and mode descriptors, the-+ * kernel PRNG's function table) are signed on arm64e kernels as Apple's corecrypto headers declare them (CC_SPTR), so-+ * the kernel's authenticated calls through them check out.-+ */-+#ifndef CC_SPTR-+#if __has_feature(ptrauth_calls) && defined(KERNEL)-+#include <ptrauth.h>-+#define CC_SPTR(_sn_, _n_) \-+ __ptrauth(ptrauth_key_process_independent_code, 1, ptrauth_string_discriminator("cc_" #_sn_ #_n_)) _n_-+#else-+#define CC_SPTR(_sn_, _n_) _n_-+#endif-+#endif-+- #endif /* __CORECRYPTO_CC_CONFIG_H__ */-diff --git a/ccdigest/include/corecrypto/ccdigest.h b/ccdigest/include/corecrypto/ccdigest.h-index 3c77856..7249bc2 100644---- a/ccdigest/include/corecrypto/ccdigest.h-+++ b/ccdigest/include/corecrypto/ccdigest.h-@@ -76,11 +76,11 @@ struct ccdigest_info {- /* the initial state of the hash */- const void *initial_state;--- void (*compress)(ccdigest_state_t state,-+ void (*CC_SPTR(ccdigest_info, compress))(ccdigest_state_t state,- size_t nblocks,- const void *in);--- void (*final)(const struct ccdigest_info *di,-+ void (*CC_SPTR(ccdigest_info, final))(const struct ccdigest_info *di,- ccdigest_ctx_t ctx,- void *digest);--diff --git a/ccdrbg/include/corecrypto/ccdrbg_impl.h b/ccdrbg/include/corecrypto/ccdrbg_impl.h-index ec1c390..95bcc50 100644---- a/ccdrbg/include/corecrypto/ccdrbg_impl.h-+++ b/ccdrbg/include/corecrypto/ccdrbg_impl.h-@@ -16,7 +16,7 @@ struct ccdrbg_state;- struct ccdrbg_info {- size_t size;--- ccdrbg_status_t (*init)(const struct ccdrbg_info *info,-+ ccdrbg_status_t (*CC_SPTR(ccdrbg_info, init))(const struct ccdrbg_info *info,- struct ccdrbg_state *state,- size_t entropy_length,- const void *entropy,-@@ -25,19 +25,19 @@ struct ccdrbg_info {- size_t ps_length,- const void *ps);--- ccdrbg_status_t (*reseed)(struct ccdrbg_state *state,-+ ccdrbg_status_t (*CC_SPTR(ccdrbg_info, reseed))(struct ccdrbg_state *state,- size_t entropy_length,- const void *entropy,- size_t ad_length,- const void *ad);--- ccdrbg_status_t (*generate)(struct ccdrbg_state *state,-+ ccdrbg_status_t (*CC_SPTR(ccdrbg_info, generate))(struct ccdrbg_state *state,- size_t out_length,- void *out,- size_t ad_length,- const void *ad);--- void (*done)(struct ccdrbg_state *state);-+ void (*CC_SPTR(ccdrbg_info, done))(struct ccdrbg_state *state);-- const void *custom;- };-diff --git a/cckprng/include/corecrypto/cckprng.h b/cckprng/include/corecrypto/cckprng.h-index ab86b8e..87ef125 100644---- a/cckprng/include/corecrypto/cckprng.h-+++ b/cckprng/include/corecrypto/cckprng.h-@@ -45,13 +45,13 @@ struct cckprng_ctx {- };-- struct cckprng_funcs {-- void (*init)(struct cckprng_ctx *ctx, size_t seed_nbytes, const void *seed, size_t nonce_nbytes, const void *nonce,-+ void (*CC_SPTR(cckprng_funcs, init))(struct cckprng_ctx *ctx, size_t seed_nbytes, const void *seed, size_t nonce_nbytes, const void *nonce,- cckprng_getentropy getentropy, void *getentropy_arg);-- void (*initgen)(struct cckprng_ctx *ctx, unsigned gen_idx);-- void (*reseed)(struct cckprng_ctx *ctx, size_t nbytes, const void *seed);-- void (*refresh)(struct cckprng_ctx *ctx);-- void (*generate)(struct cckprng_ctx *ctx, unsigned gen_idx, size_t nbytes, void *out);-- void (*init_with_getentropy)(struct cckprng_ctx *ctx, unsigned max_ngens, size_t seed_nbytes, const void *seed,-+ void (*CC_SPTR(cckprng_funcs, initgen))(struct cckprng_ctx *ctx, unsigned gen_idx);-+ void (*CC_SPTR(cckprng_funcs, reseed))(struct cckprng_ctx *ctx, size_t nbytes, const void *seed);-+ void (*CC_SPTR(cckprng_funcs, refresh))(struct cckprng_ctx *ctx);-+ void (*CC_SPTR(cckprng_funcs, generate))(struct cckprng_ctx *ctx, unsigned gen_idx, size_t nbytes, void *out);-+ void (*CC_SPTR(cckprng_funcs, init_with_getentropy))(struct cckprng_ctx *ctx, unsigned max_ngens, size_t seed_nbytes, const void *seed,- size_t nonce_nbytes, const void *nonce, cckprng_getentropy getentropy, void *getentropy_arg);- };--diff --git a/ccmode/include/corecrypto/ccmode_impl.h b/ccmode/include/corecrypto/ccmode_impl.h-index 4178f73..d0e6018 100644---- a/ccmode/include/corecrypto/ccmode_impl.h-+++ b/ccmode/include/corecrypto/ccmode_impl.h-@@ -87,12 +87,12 @@ struct ccmode_ecb {- size_t size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_ecb *ecb,-+ cc_error_t (*CC_SPTR(ccmode_ecb, init))(const struct ccmode_ecb *ecb,- ccecb_ctx *ctx,- size_t key_size,- const void *key);--- cc_error_t (*ecb)(const ccecb_ctx *ctx, size_t nblocks, const void *in,-+ cc_error_t (*CC_SPTR(ccmode_ecb, ecb))(const ccecb_ctx *ctx, size_t nblocks, const void *in,- void *out);-- void *pad;-@@ -151,12 +151,12 @@ struct ccmode_cbc {- size_t size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_cbc *cbc,-+ cc_error_t (*CC_SPTR(ccmode_cbc, init))(const struct ccmode_cbc *cbc,- cccbc_ctx *ctx,- size_t key_size,- const void *key);--- cc_error_t (*cbc)(const cccbc_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_cbc, cbc))(const cccbc_ctx *ctx,- cccbc_iv *iv,- size_t nblocks,- const void *in,-@@ -177,13 +177,13 @@ struct ccmode_cfb {- size_t size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_cfb *ofb,-+ cc_error_t (*CC_SPTR(ccmode_cfb, init))(const struct ccmode_cfb *ofb,- cccfb_ctx *ctx,- size_t key_size,- const void *key,- const void *iv);--- cc_error_t (*cfb)(cccfb_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_cfb, cfb))(cccfb_ctx *ctx,- size_t nbytes,- const void *in,- void *out);-@@ -200,13 +200,13 @@ struct ccmode_cfb8 {- size_t size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_cfb8 *ofb,-+ cc_error_t (*CC_SPTR(ccmode_cfb8, init))(const struct ccmode_cfb8 *ofb,- cccfb8_ctx *ctx,- size_t key_size,- const void *key,- const void *iv);--- cc_error_t (*cfb8)(cccfb8_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_cfb8, cfb8))(cccfb8_ctx *ctx,- size_t nbytes,- const void *in,- void *out);-@@ -223,13 +223,13 @@ struct ccmode_ofb {- size_t size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_ofb *ofb,-+ cc_error_t (*CC_SPTR(ccmode_ofb, init))(const struct ccmode_ofb *ofb,- ccofb_ctx *ctx,- size_t key_size,- const void *key,- const void *iv);--- cc_error_t (*ofb)(ccofb_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ofb, ofb))(ccofb_ctx *ctx,- size_t nbytes,- const void *in,- void *out);-@@ -249,17 +249,17 @@ struct ccmode_ctr {- size_t block_size;- size_t ecb_block_size;--- cc_error_t (*init)(const struct ccmode_ctr *ctr,-+ cc_error_t (*CC_SPTR(ccmode_ctr, init))(const struct ccmode_ctr *ctr,- ccctr_ctx *ctx,- size_t key_size,- const void *key,- const void *iv);--- cc_error_t (*setctr)(const struct ccmode_ctr *ctr,-+ cc_error_t (*CC_SPTR(ccmode_ctr, setctr))(const struct ccmode_ctr *ctr,- ccctr_ctx *ctx,- const void *counter);--- cc_error_t (*ctr)(ccctr_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ctr, ctr))(ccctr_ctx *ctx,- size_t nbytes,- const void *in,- void *out);-@@ -287,29 +287,29 @@ struct ccmode_gcm {- int encdec;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_gcm *gcm,-+ cc_error_t (*CC_SPTR(ccmode_gcm, init))(const struct ccmode_gcm *gcm,- ccgcm_ctx *cyx,- size_t key_size,- const void *key);--- cc_error_t (*set_iv)(ccgcm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_gcm, set_iv))(ccgcm_ctx *ctx,- size_t iv_size,- const void *iv);--- cc_error_t (*gmac)(ccgcm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_gcm, gmac))(ccgcm_ctx *ctx,- size_t nbytes,- const void *in);--- cc_error_t (*gcm)(ccgcm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_gcm, gcm))(ccgcm_ctx *ctx,- size_t nbytes,- const void *in,- void *out);--- cc_error_t (*finalize)(ccgcm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_gcm, finalize))(ccgcm_ctx *ctx,- size_t tag_size,- void *tag);--- cc_error_t (*reset)(ccgcm_ctx *ctx);-+ cc_error_t (*CC_SPTR(ccmode_gcm, reset))(ccgcm_ctx *ctx);-- const void *custom;- };-@@ -327,23 +327,23 @@ struct ccmode_xts {- size_t tweak_size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_xts *xts,-+ cc_error_t (*CC_SPTR(ccmode_xts, init))(const struct ccmode_xts *xts,- ccxts_ctx *ctx,- size_t key_size,- const void *data_key,- const void *tweak_key);--- cc_error_t (*key_sched)(const struct ccmode_xts *xts,-+ cc_error_t (*CC_SPTR(ccmode_xts, key_sched))(const struct ccmode_xts *xts,- ccxts_ctx *ctx,- size_t key_size,- const void *data_key,- const void *tweak_key);--- cc_error_t (*set_tweak)(const ccxts_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_xts, set_tweak))(const ccxts_ctx *ctx,- ccxts_tweak *tweak,- const void *iv);--- void *(*xts)(const ccxts_ctx *ctx,-+ void *(*CC_SPTR(ccmode_xts, xts))(const ccxts_ctx *ctx,- ccxts_tweak *tweak,- size_t nblocks,- const void *in,-@@ -368,12 +368,12 @@ struct ccmode_ccm {- size_t nonce_size;- size_t block_size;--- cc_error_t (*init)(const struct ccmode_ccm *ccm,-+ cc_error_t (*CC_SPTR(ccmode_ccm, init))(const struct ccmode_ccm *ccm,- ccccm_ctx *ctx,- size_t key_size,- const void *key);--- cc_error_t (*set_iv)(ccccm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ccm, set_iv))(ccccm_ctx *ctx,- ccccm_nonce *nonce_ctx,- size_t nonce_len,- const void *nonce,-@@ -381,22 +381,22 @@ struct ccmode_ccm {- size_t auth_len,- size_t data_len);--- cc_error_t (*cbcmac)(ccccm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ccm, cbcmac))(ccccm_ctx *ctx,- ccccm_nonce *nonce,- size_t nbytes,- const void *in);--- cc_error_t (*ccm)(ccccm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ccm, ccm))(ccccm_ctx *ctx,- ccccm_nonce *nonce,- size_t nbytes,- const void *in,- void *out);--- cc_error_t (*finalize)(ccccm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ccm, finalize))(ccccm_ctx *ctx,- ccccm_nonce *nonce,- void *mac);--- cc_error_t (*reset)(ccccm_ctx *ctx,-+ cc_error_t (*CC_SPTR(ccmode_ccm, reset))(ccccm_ctx *ctx,- ccccm_nonce *nonce);-- const void *custom;-diff --git a/ccrng/include/corecrypto/ccrng.h b/ccrng/include/corecrypto/ccrng.h-index 8cf93c8..94d5400 100644---- a/ccrng/include/corecrypto/ccrng.h-+++ b/ccrng/include/corecrypto/ccrng.h-@@ -14,7 +14,7 @@- CC_BEGIN_DECLS-- struct ccrng_state {-- cc_error_t (*generate)(struct ccrng_state *rng,-+ cc_error_t (*CC_SPTR(ccrng_state, generate))(struct ccrng_state *rng,- size_t out_length,- void *out);- };-diff --git a/ccrng/include/corecrypto/ccrng_system.h b/ccrng/include/corecrypto/ccrng_system.h-index 16d83d0..6f3843c 100644---- a/ccrng/include/corecrypto/ccrng_system.h-+++ b/ccrng/include/corecrypto/ccrng_system.h-@@ -15,7 +15,7 @@ CC_BEGIN_DECLS-- // deprecated wrapper over ccrng(), kept for existing callers- struct ccrng_system_state {-- cc_error_t (*generate)(struct ccrng_state *rng, size_t out_length, void *out);-+ cc_error_t (*CC_SPTR(ccrng_state, generate))(struct ccrng_state *rng, size_t out_length, void *out); /* xnulinux: read as a ccrng_state */- int fd;- };--diff --git a/corecrypto_kext/main.c b/corecrypto_kext/main.c-index 3e992cb..a3a859c 100644---- a/corecrypto_kext/main.c-+++ b/corecrypto_kext/main.c-@@ -173,7 +173,7 @@ static bool crypto_hmac_verify(crypto_digest_alg_t alg, const void *key, size_t-- // pd: random handles are ccrng_state shaped, the kmem one is a lock-free integer-only chacha20 stream- struct kmem_rng {-- cc_error_t (*generate)(struct ccrng_state *rng, size_t nbytes, void *out);-+ cc_error_t (*CC_SPTR(ccrng_state, generate))(struct ccrng_state *rng, size_t nbytes, void *out); /* xnulinux: read as a ccrng_state */- ccchacha20_ctx chacha;- };-diff --git a/linux/drivers/fki/fki.c b/linux/drivers/fki/fki.cindex fb7bd7f..a065f69 100644--- a/linux/drivers/fki/fki.c+++ b/linux/drivers/fki/fki.c@@ -161,6 +161,12 @@ void fki_random(void *buf, unsigned long len)}EXPORT_SYMBOL(fki_random);+int fki_random_wait(void)+{+ return wait_for_random_bytes();+}+EXPORT_SYMBOL(fki_random_wait);+int fki_bind_cpu(int cpu){if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu))diff --git a/linux/include/linux/fki.h b/linux/include/linux/fki.hindex 3c7fc25..f6de5c3 100644--- a/linux/include/linux/fki.h+++ b/linux/include/linux/fki.h@@ -52,6 +52,8 @@ void fki_pac_swap(const struct fki_pac_keys *load, struct fki_pac_keys *save);fki_u64 fki_counter_frequency(void);fki_u64 fki_counter(void);void fki_random(void *buf, unsigned long len);+/* sleeps until Linux's generator is seeded (process context): 0, or -errno when a signal comes first */+int fki_random_wait(void);/** Physically contiguous memory with real physical addresses (for a foreign kernel whose own page tables* the hardware walks). va is a kernel linear-map address (RW, not executable). size rounds up to a power of two.diff --git a/scripts/apple-kc/package.sh b/scripts/apple-kc/package.shindex 184a912..0488216 100755--- a/scripts/apple-kc/package.sh+++ b/scripts/apple-kc/package.sh@@ -12,13 +12,15 @@set -euo pipefailPOC=$(cd "$(dirname "$0")/../.." && pwd)DVM=${DVM:-$HOME/w/dvm}; KIT=${1:-$HOME/w/apple-kc-kit}-[ -s $DVM/out/k27/natives.txt ] || { echo "$DVM/out/k27/natives.txt: run scripts/vm-xnu-rebuild.sh (with out/kc/kc27.bin) first" >&2; exit 1; }-rm -rf $KIT; mkdir -p $KIT/kexts-cp $DVM/out/k27/kernel-k27.macho $KIT/kernel.macho-[ -e /lib/firmware/xnu/kc-free.bin ] && cp /lib/firmware/xnu/kc-free.bin $KIT/kc-free.bin-python3 - $DVM $KIT "$(sha256sum $DVM/out/kc/kc27.bin | cut -d' ' -f1)" \- "$(git -C $POC rev-parse HEAD 2>/dev/null || echo unknown)" "${AMFI_PLATFORM:-26}" $(cat $DVM/out/k27/natives.txt) -- \- $(grep -v '^#' $POC/scripts/apple-kc/keep.txt) -- $(grep -v '^#' $POC/scripts/apple-kc/rebind.txt) <<'PY'+[ -s "$DVM/out/k27/natives.txt" ] || { echo "$DVM/out/k27/natives.txt: run scripts/vm-xnu-rebuild.sh (with out/kc/kc27.bin) first" >&2; exit 1; }+mapfile -t lines < "$DVM/out/k27/natives.txt" # one kext description a line, paths whole (a file not read stops here)+natives=(); for l in "${lines[@]}"; do [ -n "$l" ] && natives+=("$l"); done+rm -rf "$KIT"; mkdir -p "$KIT/kexts"+cp "$DVM/out/k27/kernel-k27.macho" "$KIT/kernel.macho"+[ -e /lib/firmware/xnu/kc-free.bin ] && cp /lib/firmware/xnu/kc-free.bin "$KIT/kc-free.bin"+python3 - "$DVM" "$KIT" "$(sha256sum "$DVM/out/kc/kc27.bin" | cut -d' ' -f1)" \+ "$(git -C "$POC" rev-parse HEAD 2>/dev/null || echo unknown)" "${AMFI_PLATFORM:-26}" "${natives[@]}" -- \+ $(grep -v '^#' "$POC/scripts/apple-kc/keep.txt") -- $(grep -v '^#' "$POC/scripts/apple-kc/rebind.txt") <<'PY'import json, os, shutil, sysfrom pathlib import Pathdvm, kit, kc_sha, poc_rev, amfi = sys.argv[1:6]@@ -56,4 +58,4 @@ keep = [k for k in keep if k and not (k in seen or seen.add(k))]}, indent=1) + '\n')print(f'{kit}: kernel, {len(specs)} kexts of ours, {len(keep)} Apple kexts to keep')PY-du -sh $KIT+du -sh "$KIT"diff --git a/scripts/corecrypto-abi-check.sh b/scripts/corecrypto-abi-check.shnew file mode 100755index 0000000..9867a31--- /dev/null+++ b/scripts/corecrypto-abi-check.sh@@ -0,0 +1,74 @@+#!/bin/bash+# SPDX-License-Identifier: GPL-2.0-only+# Where the kernel is built (after scripts/vm-xnu-rebuild.sh): xnulinux's corecrypto headers (xnu-patches/corecrypto)+# against the ones XNU ships, for the interface's ABI. Every kernel source that includes <corecrypto/...> is compiled+# twice with its own recipe (the k27 object tree's .o.json for C, .cpo.json for C++), once with each set of headers+# first in the include path;+# the objects' code, data, relocations and section sizes (debug information aside) must be the same. XNU's headers are read from the k27+# tree's git history into a temporary directory, never kept.+# scripts/corecrypto-abi-check.sh (DVM: ~/w/dvm)+set -euo pipefail+POC=$(cd "$(dirname "$0")/.." && pwd)+DVM=${DVM:-$HOME/w/dvm}; T=$DVM/out/k27/xnu+OBJ=$DVM/out/x12377/build/xnu/k27-obj/DEVELOPMENT_ARM64_BCM2837+export PATH=$PATH:/usr/lib/llvm-21/bin+W=$(mktemp -d); trap 'rm -rf $W' EXIT+[ -d $OBJ ] || { echo "no k27 objects at $OBJ: build the kernel first (scripts/vm-xnu-rebuild.sh)"; exit 1; }+mkdir -p $W/old $W/new/corecrypto+git -C $T archive HEAD EXTERNAL_HEADERS/corecrypto | tar -x -C $W/old --strip-components=1+cp $POC/xnu-patches/corecrypto/include/corecrypto/*.h $W/new/corecrypto/+# the sources that include the interface, xnulinux's kernel side (osfmk/xnulinux_crypto) and the PRNG's registration+# (osfmk/prng) among them+SRCS=$(cd $T && grep -rlE '#include <corecrypto/' --include=*.c --include=*.cpp bsd osfmk libkern iokit security san pexpert |+ grep -vE '^osfmk/(arm64/)?corecrypto/' | sort)+python3 - $W $OBJ $SRCS <<'PY'+import json, os, subprocess, sys+from pathlib import Path+w, obj, srcs = Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3:]+recipes = {}+for j in [*obj.rglob('*.o.json'), *obj.rglob('*.cpo.json')]:+ r = json.JSONDecoder().raw_decode(j.read_text())[0]+ recipes.setdefault(os.path.normpath(os.path.join(r['directory'], r['file'])), r)+# the one source the k27 configuration does not build (NFS's Kerberos); every other one must be compared+UNBUILT = {'bsd/nfs/gss/ccrypto.c'}+same = differ = missing = 0+for s in srcs:+ r = next((v for k, v in recipes.items() if k.endswith('/' + s)), None)+ if r is None:+ if s in UNBUILT:+ print(f' - {s} (not built in this configuration)')+ else:+ missing += 1; print(f' MISSING {s}: no recipe in {obj}')+ continue+ outs = {}+ for side in ('old', 'new'):+ args, cmd, i = r['arguments'], [], 0+ while i < len(args):+ a = args[i]+ if a == '-o': cmd += ['-o', str(w / f'{side}.o')]; i += 2; continue+ if a in ('-MF', '-MJ', '-MT', '-MQ'): i += 2; continue+ if a in ('-MD', '-MMD', '-MP'): i += 1; continue+ cmd.append(a); i += 1+ cmd.insert(1, f'-I{w / side}')+ p = subprocess.run(cmd, cwd=r['directory'], capture_output=True, text=True)+ if p.returncode:+ print(f' ERR {s} ({side} headers):\n' + '\n'.join(p.stderr.splitlines()[:15])); sys.exit(1)+ # code, data and relocations of every section but the debug ones (__DWARF: their sizes move the zero-fill+ # sections' addresses, which are left out), and the sections' sizes+ o = str(w / f'{side}.s.o')+ subprocess.run(['llvm-objcopy', '--strip-debug', str(w / f'{side}.o'), o], check=True)+ dump = subprocess.run(['llvm-objdump', '-d', '-r', '-s', '--no-show-raw-insn', o], capture_output=True, text=True,+ check=True).stdout.splitlines()[2:]+ dump = [l for l in dump if not l.startswith('<skipping contents of bss section')]+ heads = subprocess.run(['llvm-objdump', '-h', o], capture_output=True, text=True, check=True).stdout.splitlines()+ heads = [' '.join(l.split()[1:3]) for l in heads if l.strip()[:1].isdigit() and '__DWARF' not in l and 'debug' not in l]+ outs[side] = '\n'.join(heads + dump)+ if outs['old'] == outs['new']:+ same += 1; print(f' ok {s}')+ else:+ differ += 1; print(f' DIFF {s}')+ for side in ('old', 'new'):+ (w.parent / f'abi-{Path(s).name}.{side}.txt').write_text(outs[side])+print(f'{same} the same, {differ} different' + (f', {missing} without a recipe' if missing else ''))+sys.exit(1 if differ or missing or same == 0 else 0)+PYdiff --git a/scripts/free-kc/build.sh b/scripts/free-kc/build.shindex 4a7c5ae..34bf106 100755--- a/scripts/free-kc/build.sh+++ b/scripts/free-kc/build.sh@@ -2,10 +2,11 @@# SPDX-License-Identifier: GPL-2.0-only# In the VM, after scripts/vm-xnu-rebuild.sh: a kernel collection with none of Apple's kexts. The hosted kernel and xnulinux's# own kexts (XnuCPU, XnuDisk, XnuDrm), plus the kexts a Darwin userland needs, built from PureDarwin's sources as arm64e with-# this kernel's own compile flags and headers (scripts/free-kc/kcc.py): corecrypto, a permissive AMFI, pthread,-# IOStorageFamily, HFSEncodings and hfs. It boots PureDarwin's userland from an HFS+ disk (scripts/mk-puredarwin-disk.sh):+# this kernel's own compile flags and headers (scripts/free-kc/kcc.py): a permissive AMFI, pthread, IOStorageFamily,+# HFSEncodings and hfs; and xnulinux's own corecrypto kext (kexts/free/corecrypto: Mbed TLS behind the kernel's corecrypto+# interface). It boots PureDarwin's userland from an HFS+ disk (scripts/mk-puredarwin-disk.sh):# IMAGE=xnu/kc-free.bin DISK=... ROOT_UUID=... scripts/xnulinux-boot.sh-# Sources are fetched at pinned revisions; corecrypto gets kexts/free/patches/corecrypto-arm64e-sptr.patch.+# Sources are fetched at pinned revisions (Mbed TLS: kexts/free/corecrypto/mbedtls.rev).## Nothing of Apple's is read: the collection is linked into an empty one that mk-template.py makes from layout numbers# (layout.json) and XNU's own pseudo-extensions.@@ -13,7 +14,7 @@ set -euo pipefailPOC=$(cd "$(dirname "$0")/../.." && pwd)DVM=${DVM:-$HOME/w/dvm}; W=${FREE_KC_WORK:-$HOME/w/free-kc}; OUT=${OUT:-/lib/firmware/xnu/kc-free.bin}PD_URL=https://github.com/PureDarwin/PureDarwin PD_REV=0c1990c803c13b68cad4df3f6dc34ba4306fc789-CC_URL=https://github.com/Vali0004/corecrypto CC_REV=fa94ec601f8d6b3a2a39b974f29c5acaf8a9fa2d+MBEDTLS_URL=https://github.com/Mbed-TLS/mbedtls MBEDTLS_REV=$(sed -n 's/^MBEDTLS_REV=//p' $POC/kexts/free/corecrypto/mbedtls.rev)export DVM NETINC=${NETINC:-$POC/build/netinc}K="python3 $POC/scripts/free-kc/kcc.py --macos"; O=$W/obj; S=$W/src; F=$POC/kexts/freemkdir -p $S $O@@ -28,8 +29,7 @@ fetch() { # repo rev dir [paths...]: a shallow checkout of one revision, only thE=src/Kernel/Extensionsfetch $PD_URL $PD_REV $S/pd /$E/IOStorageFamily/ /$E/HFSEncodings/ /$E/hfs/ /$E/pthread/ /$E/amfi/ /src/Kernel/xnu/EXTERNAL_HEADERS/ \/src/Libraries/libSystem/pthread/include/ /src/Libraries/libSystem/pthread/private/-fetch $CC_URL $CC_REV $S/corecrypto-git -C $S/corecrypto apply --check $F/patches/corecrypto-arm64e-sptr.patch 2>/dev/null && git -C $S/corecrypto apply $F/patches/corecrypto-arm64e-sptr.patch+fetch $MBEDTLS_URL $MBEDTLS_REV $S/mbedtls /include/ /library/E=$S/pd/src/Kernel/Extensions# this kernel's Kernel.framework (xnu installhdrs into objects of its own, k27h-*), what --kext-view builds against@@ -44,15 +44,11 @@ if [ ! -d $KF/PrivateHeaders ]; thenfi# the kexts (each step stops the build when a source fails)-( cd $S/corecrypto && $K $O/corecrypto --kext-view $(for c in */include; do echo -I $PWD/$c; done) -I $PWD/corecrypto_kext \- --cflag=-Wno-nullability-completeness $(python3 - <<'PY'-import re-comps = "cc ccaes ccansikdf ccasn1 ccblowfish cccast ccchacha20poly1305 cccmac ccdes ccdigest ccdrbg cchkdf cchmac ccmd2 ccmd4 ccmd5 ccmode ccn ccnistkdf ccpad ccpbkdf2 ccrc2 ccrc4 ccripemd ccrng ccsha1 ccsha2 ccwrap cckprng ccpost ccrsa cczp".split()-for c in comps: # pd/corecrypto-pd.cmake's PD_CORECRYPTO_KERNEL_COMPONENTS- s = open(f'cmake/components/{c}.cmake').read()- print(' '.join(f'{c}/src/{f}' for f in re.search(r'set\(CORECRYPTO_%s_SOURCES(.*?)\)' % c, s, re.S).group(1).split()))-PY-) corecrypto_kext/main.c )+# corecrypto: the kernel's own view of its headers (it registers with libkern's and osfmk's interfaces), not the macOS target+C=$F/corecrypto+( cd $C && python3 $POC/scripts/free-kc/kcc.py $O/corecrypto -I $C -I $S/mbedtls/include -I $S/mbedtls/library --after $C/compat \+ -D 'MBEDTLS_CONFIG_FILE="mbedtls_config_xnulinux.h"' cc_kext.c cc_digest.c cc_aes.c cc_des.c cc_chachapoly.c cc_rsa.c \+ cc_platform.c cc_selftest.c $(for f in $(sed -n 's/^MBEDTLS_SOURCES=//p' $C/mbedtls.rev); do echo $S/mbedtls/library/$f; done) )# PureDarwin's AMFI stub, with PureDarwin's CoreEntitlements headers (it declares their coreentitlements_t itself) and# TrustCache headers (after this kernel's own)mkdir -p $W/ce-inc; ln -sfn $S/pd/src/Kernel/xnu/EXTERNAL_HEADERS/CoreEntitlements $W/ce-inc/CoreEntitlementsdiff --git a/scripts/free-kc/genspec.py b/scripts/free-kc/genspec.pyindex 03f330f..9fdcf48 100644--- a/scripts/free-kc/genspec.py+++ b/scripts/free-kc/genspec.py@@ -15,7 +15,8 @@ from pathlib import PathSRC, O, OUT = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])OUT.mkdir(parents=True, exist_ok=True)KEXTS = { # name: (Info.plist below SRC, bundle id when the plist leaves it to the build, start, stop, compat objects)- 'corecrypto': ('corecrypto/corecrypto_kext/Info.plist', 'com.apple.kec.corecrypto', '_corecrypto_start', '_corecrypto_stop', []),+ 'corecrypto': (str(Path(__file__).resolve().parents[2] / 'kexts/free/corecrypto/Info.plist'), None, '_corecrypto_start',+ '_corecrypto_stop', []),'amfi': ('pd/src/Kernel/Extensions/amfi/Info.plist', None, '_amfi_kext_start', '_amfi_kext_stop', ['kalloc-type']),'pthread': ('pd/src/Kernel/Extensions/pthread/pthread-Info.plist', None, '_pthread_start', '_pthread_stop', ['kalloc-type']),'IOStorageFamily': ('pd/src/Kernel/Extensions/IOStorageFamily/Info.plist', None, None, None, ['osmalloc']),diff --git a/scripts/free-kc/kcc.py b/scripts/free-kc/kcc.pyindex 1c56d44..6543fd9 100644--- a/scripts/free-kc/kcc.py+++ b/scripts/free-kc/kcc.py@@ -20,7 +20,7 @@ import argparse, json, os, subprocess, sysfrom pathlib import Pathfrom concurrent.futures import ThreadPoolExecutorDVM = Path(os.environ.get('DVM', Path.home() / 'w/dvm'))-NETINC = Path(os.environ.get('NETINC', Path.home() / 'w/poc/build/netinc')) # TargetConditionals.h (vm-xnu-rebuild.sh)+NETINC = Path(os.environ.get('NETINC', Path(__file__).resolve().parents[2] / 'build/netinc')) # TargetConditionals.h (vm-xnu-rebuild.sh)KF = Path(os.environ.get('KERNEL_FRAMEWORK', DVM / 'out/x12377/build/xnu/k27h-dst/System/Library/Frameworks/Kernel.framework/Versions/A'))tree = DVM / 'out/x12377/build/xnu/k27-obj/DEVELOPMENT_ARM64_BCM2837'p = argparse.ArgumentParser()diff --git a/scripts/vm-xnu-build.sh b/scripts/vm-xnu-build.shindex 143675f..d3c0bdb 100755--- a/scripts/vm-xnu-build.sh+++ b/scripts/vm-xnu-build.sh@@ -3,6 +3,7 @@# Inside the arm64 Ubuntu VM: the DroidVM Linux toolchain (native compile, no Darling: the kernel build does not need it)# and the xnu-12377 kernel from source.set -ex+POC=$(cd "$(dirname "$0")/.." && pwd)export DEBIAN_FRONTEND=noninteractiveif [ ! -x /usr/lib/llvm-21/bin/clang ]; thenwget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc >/dev/null@@ -43,15 +44,19 @@ patch_cctools() {}python3 scripts/setup-env.py toolchain --jobs 12patch_cctools-# decomment keeps getc() in a char: EOF never matches where char is unsigned (aarch64) and it spins forever-D=out/k27/xnu/SETUP/decomment/decomment.c-[ -f $D ] && sed -i 's/^\tchar bufchar;/\tint bufchar;/' $D && grep -n 'bufchar;' $D && rm -f out/x12377/build/xnu/hdrs-obj/SETUP/decomment/decomment-pkill -x decomment || true# libclang_rt.cc_kext comes from the CLTools package (unpacked only; Darling is not needed for it)A=toolchains/linux/appleclang; mkdir -p $Apython3 compat/xnu-linux/scripts/setup-appleclang.py fetch --directory $A[ -d $A/apple-tools ] || python3 compat/xnu-linux/scripts/setup-appleclang.py extract --directory $Als $A/apple-tools/Library/Developer/CommandLineTools/usr/lib/clang/*/lib/darwin/ | grep cc_kext+# the k27 tree first (setup-env's x12377 step makes it and runs its installhdrs at once), for decomment: it keeps getc()+# in a char, so EOF never matches where char is unsigned (aarch64) and it spins forever+python3 -c "import importlib.util as u; s = u.spec_from_file_location('setup_env', 'scripts/setup-env.py'); m = u.module_from_spec(s); s.loader.exec_module(m); m.kernel_tree()"+D=out/k27/xnu/SETUP/decomment/decomment.c+sed -i -E 's/^([[:space:]]*)char([[:space:]]+bufchar;)/\1int\2/' $D+grep -qE '^[[:space:]]*int[[:space:]]+bufchar;' $D || { echo "decomment.c: no int bufchar after the patch" >&2; exit 1; }+rm -f out/x12377/build/xnu/hdrs-obj/SETUP/decomment/decommentpython3 scripts/setup-env.py x12377 --jobs 12-JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -30-ls -la out/k27/kernel-k27.macho+# the first kernel build is the rebuild's: xnu-patches and xnulinux's corecrypto go into the tree before anything of it+# is compiled (XNU's own corecrypto never is)+bash "$POC"/scripts/vm-xnu-rebuild.shdiff --git a/scripts/vm-xnu-rebuild.sh b/scripts/vm-xnu-rebuild.shindex e7d21cb..66947e4 100755--- a/scripts/vm-xnu-rebuild.sh+++ b/scripts/vm-xnu-rebuild.sh@@ -4,24 +4,36 @@# kernelcache is there) relink the kernel collection with Apple's kexts. The kernel's segment slots inside the collection# (k27/segaddrs.sh) follow the kernel's size: when kc27-layout.py moves them, the kernel is linked again.set -e -o pipefail # a failed kernel build stops here: never repackage the previous kernel+POC=${POC:-$(cd "$(dirname "$0")/.." && pwd)} # this repository: its patches, kexts and linkercd ~/w/dvmexport PATH=$PATH:/usr/lib/llvm-21/binT=out/k27/xnu# back to the tree's own sources: every file the patches touched last time (.linux64-patched) or touch now, then the# patches again. A file that comes out as it was keeps its old time, so the build recompiles only what changed (assert.h# and cpu_data.h are included everywhere: touching them rebuilds the whole kernel)-P=~/w/poc/xnu-patches-FILES=$(cat $T/.linux64-patched 2>/dev/null; grep -h '^+++ b/' $P/*.patch | sed 's|^+++ b/||')+P="$POC/xnu-patches"+FILES=$(cat $T/.linux64-patched 2>/dev/null; grep -h '^+++ b/' "$P"/*.patch | sed 's|^+++ b/||')SNAP=$(mktemp -d)for f in $FILES; do [ -e $T/$f ] && mkdir -p $SNAP/$(dirname $f) && cp -p $T/$f $SNAP/$f; donefor f in $FILES; do git -C $T checkout -q -- $f 2>/dev/null || rm -f $T/$f; done-for p in $P/*.patch; do git -C $T apply $p; echo "applied $(basename $p)"; done+for p in "$P"/*.patch; do git -C $T apply "$p"; echo "applied $(basename "$p")"; donefor f in $FILES; do [ -e $SNAP/$f ] && cmp -s $SNAP/$f $T/$f && touch -r $SNAP/$f $T/$f; donerm -rf $SNAP-grep -h '^+++ b/' $P/*.patch | sed 's|^+++ b/||' > $T/.linux64-patched+grep -h '^+++ b/' "$P"/*.patch | sed 's|^+++ b/||' > $T/.linux64-patched# the machine layer's headers (shared with xnu.ko), where every component's sources find themmkdir -p $T/EXTERNAL_HEADERS/linux64-for h in $P/include/*.h; do cmp -s $h $T/EXTERNAL_HEADERS/linux64/$(basename $h) || cp $h $T/EXTERNAL_HEADERS/linux64/; done+for h in "$P"/include/*.h; do cmp -s "$h" $T/EXTERNAL_HEADERS/linux64/$(basename "$h") || cp "$h" $T/EXTERNAL_HEADERS/linux64/; done+# the corecrypto interface: xnulinux's own headers and kernel side (xnu-patches/corecrypto) in place of the tree's+# corecrypto, whose files are under Apple's corecrypto internal-use license (0008 drops them from the build). Files that+# come out the same keep their times.+mirror() { # mirror SRC DST: DST holds exactly SRC's files+ mkdir -p "$2"+ for f in $(ls -A "$2"); do [ -e "$1/$f" ] || rm -rf "$2/$f"; done+ for f in $(ls -A "$1"); do cmp -s "$1/$f" "$2/$f" || cp "$1/$f" "$2/$f"; done+}+rm -rf $T/osfmk/corecrypto $T/osfmk/arm64/corecrypto+mirror "$P"/corecrypto/include/corecrypto $T/EXTERNAL_HEADERS/corecrypto+mirror "$P"/corecrypto/kernel $T/osfmk/xnulinux_cryptot=$SECONDS; JOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -15; echo "kernel: $((SECONDS - t)) s"if [ -e out/kc/kc27.bin ]; thencp k27/segaddrs.sh /tmp/segaddrs.before@@ -31,39 +43,39 @@ if [ -e out/kc/kc27.bin ]; thenJOBS=12 bash k27/build-kernel27.sh 2>&1 | tail -3fi# xnulinux's own kexts (kexts/*/NAME.json + NAME.cpp), built against the k27 kernel's objects and linked into the collection- NATIVE=+ NATIVE=()# IONetworkingFamily's kernel headers (IOKit/network/*.h) for XnuEthernet: Apple's open source IONetworkingFamily-188 (APSL,# fetched here, not kept in the repository), whose __PRIVATE_SPI__ declarations name the reserved slots 2-5 the shipped family# uses (the SDK's headers leave slot 5 a reserved stub the collection does not export)- NETINC=~/w/poc/build/netinc; NETSRC=~/w/poc/build/IONetworkingFamily-188; mkdir -p $NETINC/IOKit- [ -e $NETSRC/IOEthernetController.h ] || git clone -q --depth 1 --branch IONetworkingFamily-188 https://github.com/apple-oss-distributions/IONetworkingFamily.git $NETSRC- ln -sfn $NETSRC $NETINC/IOKit/network+ NETINC="$POC/build/netinc"; NETSRC="$POC/build/IONetworkingFamily-188"; mkdir -p "$NETINC/IOKit"+ [ -e "$NETSRC/IOEthernetController.h" ] || git clone -q --depth 1 --branch IONetworkingFamily-188 https://github.com/apple-oss-distributions/IONetworkingFamily.git "$NETSRC"+ ln -sfn "$NETSRC" "$NETINC/IOKit/network"# IOHIDFamily's kernel headers (IOKit/hid/*.h) for XnuInput and IOStorageFamily's (IOKit/storage/*.h) for XnuDisk: the SDK's# Kernel.framework has themfor h in toolchains/sdks/*.sdk/System/Library/Frameworks/Kernel.framework/Headers/IOKit/hid; do- [ -e $h/IOHIDDevice.h ] && { ln -sfn $PWD/$h $NETINC/IOKit/hid; ln -sfn $PWD/$h/../hidsystem $NETINC/IOKit/hidsystem; ln -sfn $PWD/$h/../graphics $NETINC/IOKit/graphics; ln -sfn $PWD/$h/../storage $NETINC/IOKit/storage- ln -sfn $PWD/${h%%/System/*}/usr/include/TargetConditionals.h $NETINC/TargetConditionals.h- ln -sfn $PWD/$h/../../HIDDriverKit $NETINC/HIDDriverKit; break; } # IOHIDDevice.h includes HIDDriverKit/IOHIDDevice.h+ [ -e $h/IOHIDDevice.h ] && { ln -sfn $PWD/$h "$NETINC/IOKit/hid"; ln -sfn $PWD/$h/../hidsystem "$NETINC/IOKit/hidsystem"; ln -sfn $PWD/$h/../graphics "$NETINC/IOKit/graphics"; ln -sfn $PWD/$h/../storage "$NETINC/IOKit/storage"+ ln -sfn $PWD/${h%%/System/*}/usr/include/TargetConditionals.h "$NETINC/TargetConditionals.h"+ ln -sfn $PWD/$h/../../HIDDriverKit "$NETINC/HIDDriverKit"; break; } # IOHIDDevice.h includes HIDDriverKit/IOHIDDevice.hdone- for j in ~/w/poc/kexts/*/*.json; do+ for j in "$POC"/kexts/*/*.json; do[ -e "$j" ] || continue- n=$(basename $j .json)+ n=$(basename "$j" .json)# a kext that does not build is left out of the collection (reported), not the whole rebuild- python3 scripts/build-native-kext.py $n $(dirname $j)/$n.cpp -I $NETINC --obj ${KEXT_OBJ:-k27-obj} || { echo "kext $n failed to build: left out"; continue; }- NATIVE="$NATIVE $j"+ python3 scripts/build-native-kext.py $n "$(dirname "$j")/$n.cpp" -I "$NETINC" --obj ${KEXT_OBJ:-k27-obj} || { echo "kext $n failed to build: left out"; continue; }+ NATIVE+=("$j")done- printf '%s\n' $NATIVE > out/k27/natives.txt # the kexts of ours in this collection, for scripts/apple-kc/package.sh+ printf '%s\n' "${NATIVE[@]}" > out/k27/natives.txt # the kexts of ours in this collection, for scripts/apple-kc/package.sh# the collection (scripts/kclink): the kernel, xnulinux's kexts, and the Apple kexts of scripts/apple-kc/keep.txt from# macOS 27.2's kernelcachet=$SECONDS- python3 ~/w/poc/scripts/kclink/kclink.py out/kc/kc27.bin out/k27/kernel-k27.macho out/k27/kc-k27.bin \- --keep $(grep -v '^#' ~/w/poc/scripts/apple-kc/keep.txt) --native $NATIVE \- --rebind $(grep -v '^#' ~/w/poc/scripts/apple-kc/rebind.txt) > out/k27/kclink.log 2>&1 ||+ python3 "$POC"/scripts/kclink/kclink.py out/kc/kc27.bin out/k27/kernel-k27.macho out/k27/kc-k27.bin \+ --keep $(grep -v '^#' "$POC"/scripts/apple-kc/keep.txt) --native "${NATIVE[@]}" \+ --rebind $(grep -v '^#' "$POC"/scripts/apple-kc/rebind.txt) > out/k27/kclink.log 2>&1 ||{ tail -5 out/k27/kclink.log; echo "kclink failed: out/k27/kclink.log"; exit 1; }grep -E '^wrote|vtable check' out/k27/kclink.log; echo "collection: $((SECONDS - t)) s"# AMFI of the collection's macOS expects the code-signature platform version of that build; the system disk's binaries# carry their own (macOS 27.0.1: 26 where 27.2's AMFI expects 27)- python3 ~/w/poc/scripts/kc-amfi-platform.py out/k27/kc-k27.bin ${AMFI_PLATFORM:-26}+ python3 "$POC"/scripts/kc-amfi-platform.py out/k27/kc-k27.bin ${AMFI_PLATFORM:-26}sudo cp out/k27/kc-k27.bin /lib/firmware/xnu/kc.binfils -la out/k27/kernel-k27.machodiff --git a/scripts/xnulinux-boot.sh b/scripts/xnulinux-boot.shindex cc91c29..7f0cd5c 100755--- a/scripts/xnulinux-boot.sh+++ b/scripts/xnulinux-boot.sh@@ -54,7 +54,8 @@ sudo dmesg -n "${CONLEVEL:-1}"# fki has the hosted kernel's DRAM from its load at boot (scripts/vm-install-fki-service.sh): reload it only when it changed,# with the same carve-outCARVE="carve_base=$(cat /sys/module/fki/parameters/carve_base 2>/dev/null || echo 0x200000000) carve_mb=$(cat /sys/module/fki/parameters/carve_mb 2>/dev/null || echo 8156)"-CPULIST=$(cat /sys/module/fki/parameters/cpu_list 2>/dev/null | tr -d '\n'); [ -z "$CPULIST" ] || CARVE="$CARVE cpu_list=$CPULIST"+# (an fki.ko from before cpu_list has none: set -e must not stop here)+CPULIST=$(tr -d '\n' 2>/dev/null < /sys/module/fki/parameters/cpu_list || true); [ -z "$CPULIST" ] || CARVE="$CARVE cpu_list=$CPULIST"if [ "$(cat /sys/module/fki/srcversion 2>/dev/null)" != "$(modinfo -F srcversion fki-ko/fki.ko)" ]; thensudo systemctl stop xnudrmd 2>/dev/null || true # it holds fki's /dev/xnu-hostcallsudo rmmod fki 2>/dev/null || true@@ -77,7 +78,7 @@ NCPU=$(nproc --all)HOSTCPUS= # Linux's CPUs that XNU does not takeif [ "$NCPU" -gt "$CPUS" ]; then# XNU's CPUs: Linux CPUs 0..CPUS-1, or the first CPUS of fki's cpu_list- XCPUS=$(cat /sys/module/fki/parameters/cpu_list 2>/dev/null | tr -d '\n')+ XCPUS=$(tr -d '\n' 2>/dev/null < /sys/module/fki/parameters/cpu_list || true)MASK=$(python3 -c "l = [int(c) for c in '$XCPUS'.split(',') if c][:$CPUS] or list(range($CPUS))m = (1 << $NCPU) - 1@@ -121,7 +122,7 @@ if [ "$GPU" != off ] && [ -e /dev/dri/renderD128 ]; thenfifi# Chip name for System Information (override: SOC_NAME=)-XCPU0=$(cat /sys/module/fki/parameters/cpu_list 2>/dev/null | tr -d '\n' | cut -d, -f1); XCPU0=${XCPU0:-0}+XCPU0=$( (tr -d '\n' 2>/dev/null < /sys/module/fki/parameters/cpu_list || true) | cut -d, -f1); XCPU0=${XCPU0:-0}SOC_NAME=${SOC_NAME:-$(lscpu -e=CPU,MODELNAME 2>/dev/null | awk -v c="$XCPU0" '$1 == c { $1 = ""; sub(/^ +/, ""); print }' || true)}[ "$SOC_NAME" = - ] && SOC_NAME= # lscpu's "-": the CPU has no model name (a guest)NAMES=()diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.cindex a66152d..70b086d 100644--- a/xnu-ko/xnu_main.c+++ b/xnu-ko/xnu_main.c@@ -1325,6 +1325,9 @@ static int __init xnu_init(void)if (fki_version() != 0)return -22;+ /* every random number XNU takes comes from Linux's generator (the boot seeds, LINUX64_CALL_RANDOM): seeded first */+ err = fki_random_wait();+ if (err) { logkv("xnu: Linux's random number generator is not seeded, err", (u64_t)err); return err; }if (ramdisk) {err = fki_blob_get(ramdisk, &rd_data, &rd_size, &rd_blob);if (err) { logkv("xnu: cannot read the RAM disk, err", (u64_t)err); return err; }diff --git a/xnu-patches/0008-linux64-own-corecrypto.patch b/xnu-patches/0008-linux64-own-corecrypto.patchnew file mode 100644index 0000000..3048f94--- /dev/null+++ b/xnu-patches/0008-linux64-own-corecrypto.patch@@ -0,0 +1,345 @@+xnulinux's own corecrypto interface in place of the corecrypto XNU ships.++scripts/vm-xnu-rebuild.sh removes EXTERNAL_HEADERS/corecrypto, osfmk/corecrypto and osfmk/arm64/corecrypto (files under+Apple's corecrypto internal-use license) and copies in xnu-patches/corecrypto: the interface's headers, written for+xnulinux with the layouts a crypto kext built for this kernel or for macOS agrees on, and osfmk/xnulinux_crypto, the+kernel's side of it. The kernel keeps no cryptographic algorithm: digests and HMAC go to the registered provider (the+corecrypto kext), modes through their tables, and the early random numbers (early_random, read_erandom before the kernel+PRNG registers) come from the host's generator; the four boot seeds are the booter's 256 bytes as they are (xnu.ko fills+them from Linux's get_random_bytes). Early random numbers need the host: this kernel no longer boots on hardware.+++diff --git a/osfmk/conf/files b/osfmk/conf/files+index 95f9d89..6f2f051 100644+--- a/osfmk/conf/files++++ b/osfmk/conf/files+@@ -374,38 +374,8 @@ osfmk/kern/telemetry.c optional config_telemetry+ osfmk/kern/trap_telemetry.c optional config_telemetry+ osfmk/kern/bootprofile.c optional config_telemetry++-# Built-in corecrypto for early_random():+-osfmk/corecrypto/cc_clear.c standard+-osfmk/corecrypto/cc_cmp_safe.c standard+-osfmk/corecrypto/cc_abort.c standard+-osfmk/corecrypto/cc_try_abort.c standard+-osfmk/corecrypto/cc_dit.c standard+-osfmk/corecrypto/cccbc.c standard+-osfmk/corecrypto/ccdrbg.c standard+-osfmk/corecrypto/ccdrbg_nisthmac.c standard+-osfmk/corecrypto/ccdigest_init.c standard+-osfmk/corecrypto/ccdigest_update.c standard+-osfmk/corecrypto/ccgcm.c standard+-osfmk/corecrypto/ccmode_gcm_gf_mult.c standard+-osfmk/corecrypto/ccmode_gcm_mult_h.c standard+-osfmk/corecrypto/cchkdf.c standard+-osfmk/corecrypto/cchmac.c standard+-osfmk/corecrypto/cchmac_init.c standard+-osfmk/corecrypto/cchmac_update.c standard+-osfmk/corecrypto/cchmac_final.c standard+-osfmk/corecrypto/ccdigest_final_64be.c standard+-+-osfmk/corecrypto/ccsha256_di.c standard+-osfmk/corecrypto/ccsha256_initial_state.c standard+-osfmk/corecrypto/ccsha256_K.c standard+-osfmk/corecrypto/ccsha256_ltc_compress.c standard+-osfmk/corecrypto/ccsha256_ltc_di.c standard+-+-osfmk/corecrypto/ccsha512_di.c standard+-osfmk/corecrypto/ccsha512_initial_state.c standard+-osfmk/corecrypto/ccsha512_K.c standard+-osfmk/corecrypto/ccsha512_ltc_compress.c standard+-osfmk/corecrypto/ccsha512_ltc_di.c standard++# LINUX64 (xnulinux): the corecrypto interface's kernel side (xnulinux's own, in place of the built-in corecrypto)++osfmk/xnulinux_crypto/xnulinux_crypto.c standard++ osfmk/prng/prng_random.c standard+ osfmk/prng/entropy.c standard+diff --git a/osfmk/conf/files.arm64 b/osfmk/conf/files.arm64+index d24eea1..9a1d5d7 100644+--- a/osfmk/conf/files.arm64++++ b/osfmk/conf/files.arm64+@@ -120,7 +120,6 @@ osfmk/arm64/mte.c standard+ osfmk/arm64/vm_mte_compress.c standard+ osfmk/arm64/sme.c standard+ osfmk/arm64/static_if.c standard+-osfmk/arm64/corecrypto/sha256_compress_arm64.s standard+ osfmk/arm64/x86_64_compat.c standard++ osfmk/arm/counter.c standard+diff --git a/osfmk/prng/prng_random.c b/osfmk/prng/prng_random.c+index 4ce279b..74ae060 100644+--- a/osfmk/prng/prng_random.c++++ b/osfmk/prng/prng_random.c+@@ -38,12 +38,7 @@ extern volatile unsigned long long port_crumb; /* port bring-up breadcrumb (link+ #include <sys/random.h>+ #include <prng/random.h>+ #include <prng/entropy.h>+-#include <corecrypto/ccdigest.h>+-#include <corecrypto/ccdigest_priv.h>+-#include <corecrypto/ccdrbg.h>+ #include <corecrypto/cckprng.h>+-#include <corecrypto/ccsha2.h>+-#include <corecrypto/cchmac.h>++ static struct cckprng_ctx *prng_ctx;++@@ -52,12 +47,6 @@ static SECURITY_READ_ONLY_LATE(int) prng_ready;++ #define SEED_SIZE SHA512_DIGEST_LENGTH++-#if defined(__x86_64__)+- #define BOOTLOADER_ENTROPY_REQUEST_SIZE 64 // efiboot max+-#else+- #define BOOTLOADER_ENTROPY_REQUEST_SIZE 4*SEED_SIZE+-#endif+-+ static uint8_t kprngseed[SEED_SIZE];+ static uint8_t earlyseed[SEED_SIZE];+ static uint8_t entropyseed[SEED_SIZE];+@@ -78,157 +67,31 @@ bootseed_init_bootloader(uint32_t request_size, uint8_t *dst)+ }+ }++-#if defined(__x86_64__)+-#include <i386/cpuid.h>+-+-static void+-bootseed_init_native(uint32_t request_size, uint8_t *dst)+-{+- uint64_t x;+- uint8_t ok;+- size_t i = 0;+- size_t n;+-+- assert3u(request_size % sizeof(x), ==, 0);+-+- if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_RDSEED) {+- n = request_size / sizeof(x);+-+- while (i < n) {+- asm volatile ("rdseed %0; setc %1" : "=r"(x), "=qm"(ok) : : "cc");+- if (ok) {+- cc_memcpy(&dst[i * sizeof(x)], &x, sizeof(x));+- i += 1;+- } else {+- // Intel recommends to pause between unsuccessful rdseed attempts.+- cpu_pause();+- }+- }+- } else if (cpuid_features() & CPUID_FEATURE_RDRAND) {+- // The Intel documentation guarantees a reseed every 512 rdrand calls.+- n = (request_size / sizeof(x)) * 512;+-+- while (i < n) {+- asm volatile ("rdrand %0; setc %1" : "=r"(x), "=qm"(ok) : : "cc");+- if (ok) {+- if (i % 512 == 0) {+- cc_memcpy(&dst[(i / 512) * sizeof(x)], &x, sizeof(x));+- }+- i += 1;+- } else {+- // Intel does not recommend pausing between unsuccessful rdrand attempts.+- }+- }+- }+-+- cc_clear(sizeof(x), &x);+-}+-+-#else+-+-static void+-bootseed_init_native(uint32_t request_size, uint8_t *dst)+-{+- // Even if we don't have any input, the second input needs to be a fixed input of the same size+- // to maintain dual-PRF security for HKDF/HMAC. All zero is fine as long as it is fixed.+- cc_clear(request_size, dst);+-}+-+-#endif+-++/*++ * LINUX64 (xnulinux): the booter (xnu.ko) passes 256 bytes from Linux's generator (get_random_bytes); they are the++ * four seeds as they are, with no hashing (nothing of a hardware source needs mixing in), and early random numbers come++ * from Linux's generator too (LINUX64_CALL_RANDOM), so this kernel has no DRBG of its own.++ */+ static void+ bootseed_init(void)+ {+- // Request our starting entropy from the bootloader+ uint8_t bootloader_rand[4 * SEED_SIZE];+- bootseed_init_bootloader(BOOTLOADER_ENTROPY_REQUEST_SIZE, bootloader_rand);+-+- #if defined(__x86_64__) && BOOTLOADER_ENTROPY_REQUEST_SIZE == 64+- // efiboot can only provide 64 bytes of entropy, so fill the rest of the buffer with zero.+- // We compensate for this by sampling from RDRAND/RDSEED in bootseed_init_native.+- cc_clear(sizeof(bootloader_rand) - BOOTLOADER_ENTROPY_REQUEST_SIZE, bootloader_rand + BOOTLOADER_ENTROPY_REQUEST_SIZE);+- #endif+-+- /*+- * First, we copy out a direct seed for the kprng from the TRNG packet then clear it to prevent reuse.+- * | BOOTLOADER_ENTROPY_REQUEST_SIZE |+- * | KPRNG | OPAQUE ENTROPY |+- * | 00000 | OPAQUE ENTROPY |+- */+- memcpy(kprngseed, bootloader_rand, SEED_SIZE);+- cc_clear(SEED_SIZE, bootloader_rand);++- uint8_t native_rand[3 * SEED_SIZE];+- bootseed_init_native(sizeof(native_rand), native_rand);+-+- const struct ccdigest_info *di = &ccsha512_ltc_di;+- assert3u(SEED_SIZE, <, di->block_size);+-+- uint8_t zero_salt[SEED_SIZE] = {0};+- uint8_t combined_input[2 * SEED_SIZE];+-+- // First subkey: bootloader_rand[64-127] || native_rand[0-63]+- memcpy(combined_input, &bootloader_rand[1 * SEED_SIZE], SEED_SIZE);+- memcpy(&combined_input[SEED_SIZE], &native_rand[0 * SEED_SIZE], SEED_SIZE);+- cchmac(di, SEED_SIZE, zero_salt, 2 * SEED_SIZE, combined_input, earlyseed);+-+- // Second subkey: bootloader_rand[128-191] || native_rand[64-127]+- memcpy(combined_input, &bootloader_rand[2 * SEED_SIZE], SEED_SIZE);+- memcpy(&combined_input[SEED_SIZE], &native_rand[1 * SEED_SIZE], SEED_SIZE);+- cchmac(di, SEED_SIZE, zero_salt, 2 * SEED_SIZE, combined_input, entropyseed);+-+- // Third subkey: bootloader_rand[192-255] || native_rand[128-191]+- memcpy(combined_input, &bootloader_rand[3 * SEED_SIZE], SEED_SIZE);+- memcpy(&combined_input[SEED_SIZE], &native_rand[2 * SEED_SIZE], SEED_SIZE);+- cchmac(di, SEED_SIZE, zero_salt, 2 * SEED_SIZE, combined_input, kprng_reseed);+-+- cc_clear(sizeof(combined_input), combined_input);+-+- cc_clear(BOOTLOADER_ENTROPY_REQUEST_SIZE, bootloader_rand);+- cc_clear(BOOTLOADER_ENTROPY_REQUEST_SIZE, native_rand);++ bootseed_init_bootloader(sizeof(bootloader_rand), bootloader_rand);++ memcpy(kprngseed, &bootloader_rand[0 * SEED_SIZE], SEED_SIZE);++ memcpy(earlyseed, &bootloader_rand[1 * SEED_SIZE], SEED_SIZE);++ memcpy(entropyseed, &bootloader_rand[2 * SEED_SIZE], SEED_SIZE);++ memcpy(kprng_reseed, &bootloader_rand[3 * SEED_SIZE], SEED_SIZE);++ cc_clear(sizeof(bootloader_rand), bootloader_rand);+ }++-#define EARLY_RANDOM_STATE_STATIC_SIZE (264)+-+-static struct {+- uint8_t drbg_state[EARLY_RANDOM_STATE_STATIC_SIZE];+- struct ccdrbg_info drbg_info;+- const struct ccdrbg_nisthmac_custom drbg_custom;+-} erandom = {.drbg_custom = {+- .di = &ccsha256_ltc_di,+- .strictFIPS = 0,+- }};+-+ __attribute__((noinline))+ static void+ early_random_init(void)+ {+- uint64_t nonce;+- int rc;+- const char ps[] = "xnu early random";+-+ port_crumb = 60;+ bootseed_init();+- port_crumb = 61;+-+- /* Init DRBG for NIST HMAC */+- ccdrbg_factory_nisthmac(&erandom.drbg_info, &erandom.drbg_custom);+- port_crumb = 62;+- assert3u(erandom.drbg_info.size, <=, sizeof(erandom.drbg_state));+-+- /*+- * Init our DBRG from the boot entropy and a timestamp as nonce+- * and the cpu number as personalization.+- */+- assert3u(sizeof(earlyseed), >, sizeof(nonce));+- nonce = ml_get_timebase();+- rc = ccdrbg_init(&erandom.drbg_info, (struct ccdrbg_state *)erandom.drbg_state, sizeof(earlyseed), earlyseed, sizeof(nonce), &nonce, sizeof(ps) - 1, ps);+ port_crumb = 63;+- if (rc != CCDRBG_STATUS_OK) {+- panic("ccdrbg_init() returned %d", rc);+- }+-+- cc_clear(sizeof(nonce), &nonce);+ cc_clear(sizeof(earlyseed), earlyseed);+ }++@@ -240,30 +103,9 @@ __static_testable void read_erandom(void * buf, size_t nbytes);+ * This interface should have minimal dependencies on kernel services,+ * and thus be available very early in the life of the kernel.+ *+- * This provides cryptographically secure randomness contingent on the+- * quality of the seed. It is seeded (lazily) with entropy provided by+- * the Booter.+- *+- * The implementation is a NIST HMAC-SHA256 DRBG instance used as+- * follows:+- *+- * - When first called (on macOS this is very early while page tables+- * are being built) early_random() calls ccdrbg_factory_hmac() to+- * set-up a ccdbrg info structure.+- *+- * - A 64-byte seed from the bootloader is hashed with HMAC-SHA512.+- * Where available, hardware RNG outputs are mixed into the seed.+- * (See bootseed_init.) The resulting seed is 64 bytes.+- *+- * - The ccdrbg state structure is a statically allocated area which+- * is then initialized by calling the ccdbrg_init method. The+- * initial entropy is the 64-byte seed described above. The nonce+- * is an 8-byte timestamp from ml_get_timebase(). The+- * personalization data provided is a fixed string.+- *+- * - 64-bit outputs are generated via read_erandom, a wrapper around+- * the ccdbrg_generate method. (Since "strict FIPS" is disabled,+- * the DRBG will never request a reseed.)++ * LINUX64 (xnulinux): the numbers come from the host's generator (Linux's++ * get_random_bytes, through LINUX64_CALL_RANDOM) until the kernel PRNG is++ * registered.+ *+ * - After the kernel PRNG is initialized, read_erandom defers+ * generation to it via read_random_generate. (Note that this+@@ -288,31 +130,15 @@ early_random(void)+ static void+ read_random_generate(uint8_t *buffer, size_t numbytes);++-// This code is used only during early boot (until corecrypto kext is+-// loaded), so it's better not to inline it.++// This code is used only during early boot (until the corecrypto kext registers the kernel PRNG).+ __attribute__((noinline))+ static void+ read_erandom_generate(void * buf, size_t nbytes)+ {+- uint8_t * buffer_bytes = buf;+- size_t n;+- int rc;+-+- // The DBRG request size is limited, so we break the request into+- // chunks.+- while (nbytes > 0) {+- n = MIN(nbytes, PAGE_SIZE);+-+- // Since "strict FIPS" is disabled, the DRBG will never+- // request a reseed; therefore, we panic on any error+- rc = ccdrbg_generate(&erandom.drbg_info, (struct ccdrbg_state *)erandom.drbg_state, n, buffer_bytes, 0, NULL);+- if (rc != CCDRBG_STATUS_OK) {+- panic("read_erandom ccdrbg error %d", rc);+- }+-+- buffer_bytes += n;+- nbytes -= n;++ if (!linux64_hook) {++ panic("read_erandom: no host to take random numbers from");+ }++ linux64_random(buf, nbytes);+ }++ __static_testable __mockable void+@@ -357,7 +183,6 @@ register_and_init_prng(struct cckprng_ctx *ctx, const struct cckprng_funcs *func+ cc_clear(sizeof(entropyseed), entropyseed);+ cc_clear(sizeof(kprngseed), kprngseed);+ cc_clear(sizeof(kprng_reseed), kprng_reseed);+- cc_clear(sizeof(erandom), &erandom);+ }++ voiddiff --git a/xnu-patches/corecrypto/include/corecrypto/cc.h b/xnu-patches/corecrypto/include/corecrypto/cc.hnew file mode 100644index 0000000..4f9a5bb--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cc.h@@ -0,0 +1,86 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* cc.h: alignment, context storage, pointer signing, and the two helpers every user needs. */+#ifndef _CORECRYPTO_CC_H_+#define _CORECRYPTO_CC_H_++#include <corecrypto/cc_config.h>+#include <corecrypto/cc_error.h>+#include <string.h>+#include <stdint.h>+#include <stdbool.h>+#include <stddef.h>+#include <stdarg.h>+#if CC_KERNEL+#include <kern/assert.h>+#else+#include <assert.h>+#endif++#define cc_concat_(a, b) a##b+#define cc_concat(a, b) cc_concat_(a, b)+#define cc_static_assert(e, m) _Static_assert(e, m)+#define cc_assert(x)++/* which implementation a mode or digest is (its value is the provider's business) */+typedef enum cc_impl {+ CC_IMPL_UNKNOWN = 0,+} cc_impl_t;++/*+ * Function pointers in the interface's tables are signed, with the process-independent code key and a discriminator+ * of "cc_" followed by the struct's and the field's names, when the kernel uses pointer authentication.+ */+#if __has_feature(ptrauth_calls) && CC_KERNEL+#include <ptrauth.h>+#define CC_SPTR(_sn_, _n_) \+ __ptrauth(ptrauth_key_process_independent_code, 1, ptrauth_string_discriminator("cc_" #_sn_ #_n_)) _n_+#else+#define CC_SPTR(_sn_, _n_) _n_+#endif++/* an opaque type of _alignment_ bytes, aligned to them: contexts are arrays of it */+#define cc_aligned_struct(_alignment_) \+ typedef struct { uint8_t b[_alignment_]; } CC_ALIGNED(_alignment_)++#if defined(__BIGGEST_ALIGNMENT__)+#define CC_MAX_ALIGNMENT ((size_t)__BIGGEST_ALIGNMENT__)+#else+#define CC_MAX_ALIGNMENT ((size_t)16)+#endif+#define cc_pad_align(_size_) (((_size_) + CC_MAX_ALIGNMENT - 1) & (~(CC_MAX_ALIGNMENT - 1)))++/* how many _type_ hold _size_ bytes, and their size */+#define cc_ctx_n(_type_, _size_) (((_size_) + sizeof(_type_) - 1) / sizeof(_type_))++#define CC_IGNORE_VLA_WARNINGS _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wvla\"")+#define CC_RESTORE_VLA_WARNINGS _Pragma("GCC diagnostic pop")++#define cc_ctx_sizeof(_type_, _size_) \+ CC_IGNORE_VLA_WARNINGS sizeof(_type_[cc_ctx_n(_type_, _size_)]) CC_RESTORE_VLA_WARNINGS++/* a context of _size_ bytes on the stack, or as a struct field */+#define cc_ctx_decl(_type_, _size_, _name_) \+ CC_IGNORE_VLA_WARNINGS _type_ _name_[cc_ctx_n(_type_, _size_)]; CC_RESTORE_VLA_WARNINGS+#define cc_ctx_decl_field(_type_, _size_, _name_) _type_ _name_[cc_ctx_n(_type_, _size_)]+#define cc_ctx_decl_vla(_type_, _size_, _name_) \+ CC_IGNORE_VLA_WARNINGS cc_ctx_decl(_type_, _size_, _name_); CC_RESTORE_VLA_WARNINGS++/* zero len bytes at dst, in a way the compiler keeps */+CC_NONNULL((2))+void cc_clear(size_t len, void *dst);++#define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)++/* 0 when the num bytes at ptr1 and ptr2 are equal (num > 0), otherwise 1; the time taken depends on num only */+CC_NONNULL((2, 3))+int cc_cmp_safe(size_t num, const void *ptr1, const void *ptr2);++#endif /* _CORECRYPTO_CC_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cc_config.h b/xnu-patches/corecrypto/include/corecrypto/cc_config.hnew file mode 100644index 0000000..5858a83--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cc_config.h@@ -0,0 +1,54 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * xnulinux's corecrypto interface: the types, macros and declarations through which XNU, its kexts and a crypto kext+ * (com.apple.kec.corecrypto, which registers with register_crypto_functions()) talk to each other. Written for xnulinux;+ * the layouts are those that interface has (a crypto kext built for macOS and this kernel must agree on them).+ *+ * cc_config.h: the build environment.+ */+#ifndef _CORECRYPTO_CC_CONFIG_H_+#define _CORECRYPTO_CC_CONFIG_H_++#if defined(KERNEL) && (KERNEL)+#define CC_KERNEL 1+#else+#define CC_KERNEL 0+#endif++#if defined(__LP64__) && __LP64__+#define CCN_UNIT_SIZE 8+#else+#define CCN_UNIT_SIZE 4+#endif++#define CC_ALIGNED(x) __attribute__((aligned(x)))+#define CC_INLINE static inline+#define CC_NONNULL(N) __attribute__((__nonnull__ N))+#define CC_NONNULL_ALL __attribute__((__nonnull__))+#define CC_UNUSED __attribute__((unused))+#define CC_PURE __attribute__((__pure__))+#define CC_WARN_RESULT __attribute__((__warn_unused_result__))++/* bounds annotations: not used by this kernel's build */+#define CC_PTRCHECK 0+#define CC_PTRCHECK_CAPABLE_HEADER()+#define cc_counted_by(x)+#define cc_sized_by(x)+#define cc_ended_by(x)+#define cc_indexable+#define cc_bidi_indexable+#define cc_single+#define cc_unsafe_indexable+#define cc_cstring+#define cc_unsafe_forge_bidi_indexable(p, n) (p)+#define cc_unsafe_forge_single(p) (p)++#endif /* _CORECRYPTO_CC_CONFIG_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cc_error.h b/xnu-patches/corecrypto/include/corecrypto/cc_error.hnew file mode 100644index 0000000..03eccee--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cc_error.h@@ -0,0 +1,49 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* cc_error.h: the status codes the interface's functions return (0 is success). */+#ifndef _CORECRYPTO_CC_ERROR_H_+#define _CORECRYPTO_CC_ERROR_H_++enum {+ CCERR_OK = 0,+ CCERR_INTERNAL = -1,+ CCERR_INTEGRITY = -2,+ CCERR_PARAMETER = -7,+ CCERR_MEMORY = -8,+ CCERR_OUT_OF_ENTROPY = -10,+ CCERR_OVERFLOW = -12,+ CCRSA_INVALID_INPUT = -23,+ CCRSA_INVALID_CONFIG = -24,+ CCRSA_ENCODING_ERROR = -25,+ CCRSA_DECODING_ERROR = -26,+ CCRSA_KEY_ERROR = -28,+ CCDRBG_STATUS_ERROR = -61,+ CCDRBG_STATUS_NEED_RESEED = -62,+ CCDRBG_STATUS_PARAM_ERROR = -63,+ CCDRBG_STATUS_ABORT = -64,+ CCKPRNG_NEED_ENTROPY = -65,+ CCKPRNG_ABORT = -66,+ CCMODE_INVALID_INPUT = -67,+ CCMODE_INVALID_CALL_SEQUENCE = -68,+ CCMODE_INTEGRITY_FAILURE = -69,+ CCMODE_NOT_SUPPORTED = -70,+ CCMODE_INTERNAL_ERROR = -71,+ CCERR_CALL_SEQUENCE = -86,+ CCERR_INVALID_SIGNATURE = -146,+ CCERR_BUFFER_TOO_SMALL = -163,+ CCERR_XTS_KEYS_EQUAL = -164,+ CCERR_NOT_SUPPORTED = -173,+};++#define CCERR_VALID_SIGNATURE CCERR_OK+#define CCDRBG_STATUS_OK CCERR_OK+#define CCKPRNG_OK CCERR_OK++#endif /* _CORECRYPTO_CC_ERROR_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cc_priv.h b/xnu-patches/corecrypto/include/corecrypto/cc_priv.hnew file mode 100644index 0000000..02c9c44--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cc_priv.h@@ -0,0 +1,14 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* cc_priv.h: kept for the sources that include it; everything they use is in cc.h. */+#ifndef _CORECRYPTO_CC_PRIV_H_+#define _CORECRYPTO_CC_PRIV_H_+#include <corecrypto/cc.h>+#endif /* _CORECRYPTO_CC_PRIV_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccaes.h b/xnu-patches/corecrypto/include/corecrypto/ccaes.hnew file mode 100644index 0000000..6c3c42f--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccaes.h@@ -0,0 +1,32 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccaes.h: AES (FIPS 197) in the modes of ccmode.h. */+#ifndef _CORECRYPTO_CCAES_H_+#define _CORECRYPTO_CCAES_H_++#include <corecrypto/cc_config.h>+#include <corecrypto/ccmode.h>++#define CCAES_BLOCK_SIZE 16+#define CCAES_KEY_SIZE_128 16+#define CCAES_KEY_SIZE_192 24+#define CCAES_KEY_SIZE_256 32++const struct ccmode_ecb *ccaes_ecb_encrypt_mode(void);+const struct ccmode_ecb *ccaes_ecb_decrypt_mode(void);+const struct ccmode_cbc *ccaes_cbc_encrypt_mode(void);+const struct ccmode_cbc *ccaes_cbc_decrypt_mode(void);+const struct ccmode_ctr *ccaes_ctr_crypt_mode(void);+const struct ccmode_xts *ccaes_xts_encrypt_mode(void);+const struct ccmode_xts *ccaes_xts_decrypt_mode(void);+const struct ccmode_gcm *ccaes_gcm_encrypt_mode(void);+const struct ccmode_gcm *ccaes_gcm_decrypt_mode(void);++#endif /* _CORECRYPTO_CCAES_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccchacha20poly1305.h b/xnu-patches/corecrypto/include/corecrypto/ccchacha20poly1305.hnew file mode 100644index 0000000..9d8e4e4--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccchacha20poly1305.h@@ -0,0 +1,74 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccchacha20poly1305.h: ChaCha20-Poly1305 (RFC 8439). The context is the callers' storage for a provider's state; its+ * layout (ChaCha20's state and buffer, Poly1305's, the lengths and the call state) sets its size.+ */+#ifndef _CORECRYPTO_CCCHACHA20POLY1305_H_+#define _CORECRYPTO_CCCHACHA20POLY1305_H_++#include <corecrypto/cc.h>++#define CCCHACHA20_KEY_NBYTES 32+#define CCCHACHA20_BLOCK_NBYTES 64+#define CCCHACHA20_BLOCK_NBITS (CCCHACHA20_BLOCK_NBYTES * 8)+#define CCCHACHA20_NONCE_NBYTES 12++typedef struct {+ uint32_t state[16];+ uint8_t buffer[CCCHACHA20_BLOCK_NBYTES];+ size_t leftover;+} ccchacha20_ctx;++#define CCPOLY1305_TAG_NBYTES 16+#define CCPOLY1305_KEY_NBYTES 32++typedef struct {+ uint32_t r0, r1, r2, r3, r4;+ uint32_t s1, s2, s3, s4;+ uint32_t h0, h1, h2, h3, h4;+ uint8_t buf[16];+ size_t buf_used;+ uint8_t key[16];+} ccpoly1305_ctx;++#define CCCHACHA20POLY1305_KEY_NBYTES (CCCHACHA20_KEY_NBYTES)+#define CCCHACHA20POLY1305_NONCE_NBYTES (CCCHACHA20_NONCE_NBYTES)+#define CCCHACHA20POLY1305_TAG_NBYTES (CCPOLY1305_TAG_NBYTES)+#define CCCHACHA20POLY1305_TEXT_MAX_NBYTES ((1ULL << 38) - 64ULL)++typedef struct {+ ccchacha20_ctx chacha20_ctx;+ ccpoly1305_ctx poly1305_ctx;+ uint64_t aad_nbytes;+ uint64_t text_nbytes;+ uint8_t state;+} ccchacha20poly1305_ctx;++struct ccchacha20poly1305_info {+};++const struct ccchacha20poly1305_info *ccchacha20poly1305_info(void);+int ccchacha20poly1305_init(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *key);+int ccchacha20poly1305_reset(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx);+int ccchacha20poly1305_setnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx,+ const uint8_t *nonce);+int ccchacha20poly1305_incnonce(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *nonce);+int ccchacha20poly1305_aad(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *aad);+int ccchacha20poly1305_encrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ptext, void *ctext);+int ccchacha20poly1305_finalize(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *tag);+int ccchacha20poly1305_decrypt(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes,+ const void *ctext, void *ptext);+int ccchacha20poly1305_verify(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx,+ const uint8_t *tag);++#endif /* _CORECRYPTO_CCCHACHA20POLY1305_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccder.h b/xnu-patches/corecrypto/include/corecrypto/ccder.hnew file mode 100644index 0000000..4ae18cb--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccder.h@@ -0,0 +1,31 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccder.h: DER as the kernel's CoreEntitlements headers see it (EXTERNAL_HEADERS/CoreEntitlements/der_vm.h): a tag, and+ * a buffer being written or read as a pair of pointers. The DER functions themselves are not part of this interface.+ */+#ifndef _CORECRYPTO_CCDER_H_+#define _CORECRYPTO_CCDER_H_++#include <corecrypto/cc.h>++typedef unsigned long ccder_tag;++typedef struct ccder_blob {+ uint8_t *der;+ uint8_t *der_end;+} ccder_blob;++typedef struct ccder_read_blob {+ const uint8_t *der;+ const uint8_t *der_end;+} ccder_read_blob;++#endif /* _CORECRYPTO_CCDER_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccder_blob.h b/xnu-patches/corecrypto/include/corecrypto/ccder_blob.hnew file mode 100644index 0000000..e5de4a2--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccder_blob.h@@ -0,0 +1,14 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccder_blob.h: see ccder.h. */+#ifndef _CORECRYPTO_CCDER_BLOB_H_+#define _CORECRYPTO_CCDER_BLOB_H_+#include <corecrypto/ccder.h>+#endif /* _CORECRYPTO_CCDER_BLOB_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccdes.h b/xnu-patches/corecrypto/include/corecrypto/ccdes.hnew file mode 100644index 0000000..fa3860f--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccdes.h@@ -0,0 +1,33 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccdes.h: DES and two/three-key triple DES (FIPS 46-3), for the protocols that still name them. */+#ifndef _CORECRYPTO_CCDES_H_+#define _CORECRYPTO_CCDES_H_++#include <corecrypto/ccmode.h>++#define CCDES_BLOCK_SIZE 8+#define CCDES_KEY_SIZE 8++const struct ccmode_ecb *ccdes_ecb_encrypt_mode(void);+const struct ccmode_ecb *ccdes_ecb_decrypt_mode(void);+const struct ccmode_cbc *ccdes_cbc_encrypt_mode(void);+const struct ccmode_cbc *ccdes_cbc_decrypt_mode(void);+const struct ccmode_ecb *ccdes3_ecb_encrypt_mode(void);+const struct ccmode_ecb *ccdes3_ecb_decrypt_mode(void);+const struct ccmode_cbc *ccdes3_cbc_encrypt_mode(void);+const struct ccmode_cbc *ccdes3_cbc_decrypt_mode(void);++/* nonzero when a DES key is one of the weak or semi-weak keys */+int ccdes_key_is_weak(void *key, size_t length);+/* set each key byte's low bit so the byte has odd parity */+void ccdes_key_set_odd_parity(void *key, size_t length);++#endif /* _CORECRYPTO_CCDES_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccdigest.h b/xnu-patches/corecrypto/include/corecrypto/ccdigest.hnew file mode 100644index 0000000..21394d8--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccdigest.h@@ -0,0 +1,92 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccdigest.h: a hash function as a table (struct ccdigest_info) of its sizes, initial state and compression function,+ * and the context every caller allocates for it:+ * [ uint64_t bits hashed ][ state (state_size) ][ pending input (block_size) ][ unsigned int pending bytes ]+ */+#ifndef _CORECRYPTO_CCDIGEST_H_+#define _CORECRYPTO_CCDIGEST_H_++#include <corecrypto/cc.h>+#include <corecrypto/ccn.h>++struct ccdigest_ctx {+ uint8_t state[1];+} CC_ALIGNED(8);+typedef struct ccdigest_ctx *ccdigest_ctx_t;++struct ccdigest_state {+ uint8_t state[1];+} CC_ALIGNED(8);+typedef struct ccdigest_state *ccdigest_state_t;++struct ccdigest_info {+ size_t output_size;+ size_t state_size;+ size_t block_size;+ size_t oid_size;+ const unsigned char *oid;+ const void *initial_state;+ void (*CC_SPTR(ccdigest_info, compress))(ccdigest_state_t state, size_t nblocks, const void *data);+ void (*CC_SPTR(ccdigest_info, final))(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest);+ cc_impl_t impl;+ void (*CC_SPTR(ccdigest_info, compress_parallel))(ccdigest_state_t state1, size_t nblocks1, const void *data1,+ ccdigest_state_t state2, size_t nblocks2, const void *data2);+};++#define ccdigest_ctx_size(_state_size_, _block_size_) \+ ((_state_size_) + sizeof(uint64_t) + (_block_size_) + sizeof(unsigned int))+#define ccdigest_di_size(_di_) (ccdigest_ctx_size((_di_)->state_size, (_di_)->block_size))++#define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) \+ cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_)+#define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) \+ cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_)+#define ccdigest_di_decl(_di_, _name_) cc_ctx_decl_vla(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_)+#define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_)++/* the fields of a context */+#define ccdigest_nbits(_di_, _ctx_) (*((uint64_t *)((ccdigest_ctx_t)(_ctx_))->state))+#define ccdigest_state(_di_, _ctx_) ((ccdigest_state_t)(((ccdigest_ctx_t)(_ctx_))->state + sizeof(uint64_t)))+#define ccdigest_data(_di_, _ctx_) (((ccdigest_ctx_t)(_ctx_))->state + (_di_)->state_size + sizeof(uint64_t))+#define ccdigest_num(_di_, _ctx_) \+ (*((unsigned int *)(((ccdigest_ctx_t)(_ctx_))->state + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size)))++#define ccdigest_u8(_state_) ((uint8_t *)((ccdigest_state_t)(_state_)))+#define ccdigest_u32(_state_) ((uint32_t *)((ccdigest_state_t)(_state_)))+#define ccdigest_u64(_state_) ((uint64_t *)((ccdigest_state_t)(_state_)))+#define ccdigest_ccn(_state_) ((cc_unit *)((ccdigest_state_t)(_state_)))+#define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_)))+#define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_)))+#define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_)))+#define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_)))++void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx);+void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, size_t len, const void *data);++CC_INLINE+void+ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest)+{+ di->final(di, ctx, digest);+}++void ccdigest(const struct ccdigest_info *di, size_t len, const void *data, void *digest);++/* DER-encoded object identifiers of the digests (tag and length included) */+#define CC_DIGEST_OID_MD5 ((const unsigned char *)"\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05")+#define CC_DIGEST_OID_SHA1 ((const unsigned char *)"\x06\x05\x2b\x0e\x03\x02\x1a")+#define CC_DIGEST_OID_SHA224 ((const unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")+#define CC_DIGEST_OID_SHA256 ((const unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")+#define CC_DIGEST_OID_SHA384 ((const unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")+#define CC_DIGEST_OID_SHA512 ((const unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")++#endif /* _CORECRYPTO_CCDIGEST_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccdigest_priv.h b/xnu-patches/corecrypto/include/corecrypto/ccdigest_priv.hnew file mode 100644index 0000000..52d812d--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccdigest_priv.h@@ -0,0 +1,17 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccdigest_priv.h: kept for the sources that include it; the digest interface is in ccdigest.h. */+#ifndef _CORECRYPTO_CCDIGEST_PRIV_H_+#define _CORECRYPTO_CCDIGEST_PRIV_H_++#include <corecrypto/cc_priv.h>+#include <corecrypto/ccdigest.h>++#endif /* _CORECRYPTO_CCDIGEST_PRIV_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cchmac.h b/xnu-patches/corecrypto/include/corecrypto/cchmac.hnew file mode 100644index 0000000..298d755--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cchmac.h@@ -0,0 +1,55 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * cchmac.h: HMAC over a ccdigest_info. Its context is a digest context (the inner hash), padded to CC_MAX_ALIGNMENT,+ * followed by the outer hash's state.+ */+#ifndef _CORECRYPTO_CCHMAC_H_+#define _CORECRYPTO_CCHMAC_H_++#include <corecrypto/cc.h>+#include <corecrypto/ccdigest.h>++struct cchmac_ctx {+ uint8_t b[1];+} CC_ALIGNED(8);+typedef struct cchmac_ctx *cchmac_ctx_t;++#define cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE) \+ (cc_pad_align(ccdigest_ctx_size(STATE_SIZE, BLOCK_SIZE)) + (STATE_SIZE))+#define cchmac_di_size(_di_) (cchmac_ctx_size((_di_)->state_size, (_di_)->block_size))+#define cchmac_ctx_n(STATE_SIZE, BLOCK_SIZE) ccn_nof_size(cchmac_ctx_size((STATE_SIZE), (BLOCK_SIZE)))++#define cchmac_ctx_decl(STATE_SIZE, BLOCK_SIZE, _name_) \+ cc_ctx_decl_vla(struct cchmac_ctx, cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_)+#define cchmac_ctx_clear(STATE_SIZE, BLOCK_SIZE, _name_) cc_clear(cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_)+#define cchmac_di_decl(_di_, _name_) cchmac_ctx_decl((_di_)->state_size, (_di_)->block_size, _name_)+#define cchmac_di_clear(_di_, _name_) cchmac_ctx_clear((_di_)->state_size, (_di_)->block_size, _name_)++#define cchmac_digest_ctx(_di_, HC) ((ccdigest_ctx_t)(HC))+#define cchmac_ostate(_di_, HC) ((ccdigest_state_t)(((cchmac_ctx_t)(HC))->b + cc_pad_align(ccdigest_di_size(_di_))))+#define cchmac_ostate8(_di_, HC) (ccdigest_u8(cchmac_ostate(_di_, HC)))+#define cchmac_ostate32(_di_, HC) (ccdigest_u32(cchmac_ostate(_di_, HC)))+#define cchmac_ostate64(_di_, HC) (ccdigest_u64(cchmac_ostate(_di_, HC)))+#define cchmac_istate(_di_, HC) ccdigest_state(_di_, ((ccdigest_ctx_t)(HC)))+#define cchmac_istate8(_di_, HC) ccdigest_u8(cchmac_istate(_di_, HC))+#define cchmac_istate32(_di_, HC) ccdigest_u32(cchmac_istate(_di_, HC))+#define cchmac_istate64(_di_, HC) ccdigest_u64(cchmac_istate(_di_, HC))+#define cchmac_data(_di_, HC) ccdigest_data(_di_, ((ccdigest_ctx_t)(HC)))+#define cchmac_num(_di_, HC) ccdigest_num(_di_, ((ccdigest_ctx_t)(HC)))+#define cchmac_nbits(_di_, HC) ccdigest_nbits(_di_, ((ccdigest_ctx_t)(HC)))++void cchmac_init(const struct ccdigest_info *di, cchmac_ctx_t ctx, size_t key_len, const void *key);+void cchmac_update(const struct ccdigest_info *di, cchmac_ctx_t ctx, size_t data_len, const void *data);+void cchmac_final(const struct ccdigest_info *di, cchmac_ctx_t ctx, unsigned char *mac);+void cchmac(const struct ccdigest_info *di, size_t key_len, const void *key, size_t data_len, const void *data,+ unsigned char *mac);++#endif /* _CORECRYPTO_CCHMAC_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cckprng.h b/xnu-patches/corecrypto/include/corecrypto/cckprng.hnew file mode 100644index 0000000..d7944c6--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cckprng.h@@ -0,0 +1,42 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * cckprng.h: the kernel's random generator, as a provider registers it (register_and_init_prng()): a table of functions+ * on a context the provider owns. The kernel keeps only a pointer to the context.+ */+#ifndef _CORECRYPTO_CCKPRNG_H_+#define _CORECRYPTO_CCKPRNG_H_++#include <corecrypto/cc.h>+#if CC_KERNEL+#include <kern/locks.h> /* the kernel's PRNG users (osfmk/prng/random.h and those who include it) take its locks from here */+#endif++/* fills entropy with up to *entropy_nbytes bytes and sets *entropy_nbytes to how many it wrote; arg is the provider's */+typedef int32_t (*cckprng_getentropy)(size_t *entropy_nbytes, void *entropy, void *arg);++#define CCKPRNG_SEED_NBYTES 32+#define CCKPRNG_GENERATE_MAX_NBYTES 256++struct cckprng_ctx;++struct cckprng_funcs {+ void (*CC_SPTR(cckprng_funcs, init))(struct cckprng_ctx *ctx, size_t seed_nbytes, const void *seed,+ size_t nonce_nbytes, const void *nonce, cckprng_getentropy getentropy, void *getentropy_arg);+ void (*CC_SPTR(cckprng_funcs, initgen))(struct cckprng_ctx *ctx, unsigned gen_idx);+ void (*CC_SPTR(cckprng_funcs, reseed))(struct cckprng_ctx *ctx, size_t nbytes, const void *seed);+ void (*CC_SPTR(cckprng_funcs, refresh))(struct cckprng_ctx *ctx);+ void (*CC_SPTR(cckprng_funcs, generate))(struct cckprng_ctx *ctx, unsigned gen_idx, size_t nbytes, void *out);+ void (*CC_SPTR(cckprng_funcs, init_with_getentropy))(struct cckprng_ctx *ctx, unsigned max_ngens,+ size_t seed_nbytes, const void *seed, size_t nonce_nbytes, const void *nonce, cckprng_getentropy getentropy,+ void *getentropy_arg);+};++#endif /* _CORECRYPTO_CCKPRNG_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccmd5.h b/xnu-patches/corecrypto/include/corecrypto/ccmd5.hnew file mode 100644index 0000000..a0f4891--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccmd5.h@@ -0,0 +1,22 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccmd5.h: MD5 (RFC 1321), for the protocols that still name it. */+#ifndef _CORECRYPTO_CCMD5_H_+#define _CORECRYPTO_CCMD5_H_++#include <corecrypto/ccdigest.h>++#define CCMD5_BLOCK_SIZE 64+#define CCMD5_OUTPUT_SIZE 16+#define CCMD5_STATE_SIZE 16++const struct ccdigest_info *ccmd5_di(void);++#endif /* _CORECRYPTO_CCMD5_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccmode.h b/xnu-patches/corecrypto/include/corecrypto/ccmode.hnew file mode 100644index 0000000..6628fac--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccmode.h@@ -0,0 +1,82 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccmode.h: storage for mode contexts, and the calls that go through a mode's table. */+#ifndef _CORECRYPTO_CCMODE_H_+#define _CORECRYPTO_CCMODE_H_++#include <corecrypto/cc.h>+#include <corecrypto/ccmode_impl.h>++/* ECB */+#define ccecb_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccecb_ctx, _size_, _name_)+#define ccecb_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)+size_t ccecb_context_size(const struct ccmode_ecb *mode);+size_t ccecb_block_size(const struct ccmode_ecb *mode);+int ccecb_init(const struct ccmode_ecb *mode, ccecb_ctx *ctx, size_t key_len, const void *key);+int ccecb_update(const struct ccmode_ecb *mode, const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out);++/* CBC */+#define cccbc_ctx_decl(_size_, _name_) cc_ctx_decl_vla(cccbc_ctx, _size_, _name_)+#define cccbc_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)+#define cccbc_iv_decl(_size_, _name_) cc_ctx_decl_vla(cccbc_iv, _size_, _name_)+#define cccbc_iv_clear(_size_, _name_) cc_clear(_size_, _name_)+size_t cccbc_context_size(const struct ccmode_cbc *mode);+size_t cccbc_block_size(const struct ccmode_cbc *mode);+int cccbc_init(const struct ccmode_cbc *mode, cccbc_ctx *ctx, size_t key_len, const void *key);+int cccbc_copy_iv(cccbc_iv *iv_ctx, const void *iv, size_t len);+int cccbc_clear_iv(cccbc_iv *iv_ctx, size_t len);+int cccbc_set_iv(const struct ccmode_cbc *mode, cccbc_iv *iv_ctx, const void *iv);+int cccbc_update(const struct ccmode_cbc *mode, const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in,+ void *out);++/* CTR */+#define ccctr_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccctr_ctx, _size_, _name_)+#define ccctr_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)+size_t ccctr_context_size(const struct ccmode_ctr *mode);+size_t ccctr_block_size(const struct ccmode_ctr *mode);+int ccctr_init(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t key_len, const void *key, const void *iv);+int ccctr_update(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t nbytes, const void *in, void *out);++/* XTS */+#define ccxts_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccxts_ctx, _size_, _name_)+#define ccxts_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)+#define ccxts_tweak_decl(_size_, _name_) cc_ctx_decl_vla(ccxts_tweak, _size_, _name_)+#define ccxts_tweak_clear(_size_, _name_) cc_clear(_size_, _name_)+size_t ccxts_context_size(const struct ccmode_xts *mode);+size_t ccxts_block_size(const struct ccmode_xts *mode);+int ccxts_init(const struct ccmode_xts *mode, ccxts_ctx *ctx, size_t key_nbytes, const void *data_key,+ const void *tweak_key);+int ccxts_set_tweak(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv);+void *ccxts_update(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in,+ void *out);++/*+ * GCM. Call order: init, set_iv (or init_with_iv), aad*, update*, finalize, then reset before the next IV. An encryptor's+ * finalize writes the tag; a decryptor's compares it with the one given and fails when they differ. A context made by+ * init_with_iv takes its next IV from inc_iv (the IV's last 8 bytes counted up, big-endian) after a reset.+ */+#define ccgcm_ctx_decl(_size_, _name_) cc_ctx_decl_vla(ccgcm_ctx, _size_, _name_)+#define ccgcm_ctx_clear(_size_, _name_) cc_clear(_size_, _name_)+#define CCGCM_IV_NBYTES 12+#define CCGCM_BLOCK_NBYTES 16+#define CCGCM_TEXT_MAX_NBYTES ((1ULL << 36) - 32ULL)+size_t ccgcm_context_size(const struct ccmode_gcm *mode);+size_t ccgcm_block_size(const struct ccmode_gcm *mode);+int ccgcm_init(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key);+int ccgcm_init_with_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key,+ const void *iv);+int ccgcm_set_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t iv_nbytes, const void *iv);+int ccgcm_inc_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv);+int ccgcm_aad(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *additional_data);+int ccgcm_update(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *in, void *out);+int ccgcm_finalize(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t tag_nbytes, void *tag);+int ccgcm_reset(const struct ccmode_gcm *mode, ccgcm_ctx *ctx);++#endif /* _CORECRYPTO_CCMODE_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccmode_impl.h b/xnu-patches/corecrypto/include/corecrypto/ccmode_impl.hnew file mode 100644index 0000000..24578bb--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccmode_impl.h@@ -0,0 +1,102 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccmode_impl.h: a block cipher mode as a table of its context sizes and functions. A provider fills these; callers+ * allocate a context of `size` bytes (ccmode.h's *_ctx_decl) and go through the table, or the wrappers in ccmode.h.+ */+#ifndef _CORECRYPTO_CCMODE_IMPL_H_+#define _CORECRYPTO_CCMODE_IMPL_H_++#include <corecrypto/cc.h>++#define CCMODE_MAX_BLOCK_SIZE 16++/* ECB: a key schedule, then blocks */+cc_aligned_struct(16) ccecb_ctx;+struct ccmode_ecb {+ size_t size;+ size_t block_size;+ int (*CC_SPTR(ccmode_ecb, init))(const struct ccmode_ecb *ecb, ccecb_ctx *ctx, size_t key_nbytes, const void *key);+ int (*CC_SPTR(ccmode_ecb, ecb))(const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out);+ void (*CC_SPTR(ccmode_ecb, roundkey))(const ccecb_ctx *ctx, unsigned r, void *key);+ cc_impl_t impl;+};++/* CBC: the IV lives with the caller and is updated by each call */+cc_aligned_struct(16) cccbc_ctx;+cc_aligned_struct(16) cccbc_iv;+#define CCCBC_MAX_CTX_SIZE 512+struct ccmode_cbc {+ size_t size;+ size_t block_size;+ int (*CC_SPTR(ccmode_cbc, init))(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, size_t key_len, const void *key);+ int (*CC_SPTR(ccmode_cbc, cbc))(const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in, void *out);+ const void *custom;+};++/* CTR: a byte stream; block_size is 1, ecb_block_size the cipher's */+cc_aligned_struct(16) ccctr_ctx;+struct ccmode_ctr {+ size_t size;+ size_t block_size;+ size_t ecb_block_size;+ int (*CC_SPTR(ccmode_ctr, init))(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t key_len, const void *key,+ const void *iv);+ int (*CC_SPTR(ccmode_ctr, setctr))(const struct ccmode_ctr *mode, ccctr_ctx *ctx, const void *ctr);+ int (*CC_SPTR(ccmode_ctr, ctr))(ccctr_ctx *ctx, size_t nbytes, const void *in, void *out);+ const void *custom;+};++/* XTS: a data key and a tweak key; the tweak (set from the sector's IV) lives with the caller */+cc_aligned_struct(16) ccxts_ctx;+cc_aligned_struct(16) ccxts_tweak;+struct ccmode_xts {+ size_t size;+ size_t tweak_size;+ size_t block_size;+ int (*CC_SPTR(ccmode_xts, init))(const struct ccmode_xts *xts, ccxts_ctx *ctx, size_t key_nbytes,+ const void *data_key, const void *tweak_key);+ void (*CC_SPTR(ccmode_xts, key_sched))(const struct ccmode_xts *xts, ccxts_ctx *ctx, size_t key_nbytes,+ const void *data_key, const void *tweak_key);+ int (*CC_SPTR(ccmode_xts, set_tweak))(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv);+ void *(*CC_SPTR(ccmode_xts, xts))(const ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in,+ void *out);+ const void *custom;+ const void *custom1;+ cc_impl_t impl;+};++/* GCM: one context per key, in one direction (encdec) */+cc_aligned_struct(16) ccgcm_ctx;+#define CCMODE_GCM_DECRYPTOR 78647+#define CCMODE_GCM_ENCRYPTOR 4073947+struct ccmode_gcm {+ size_t size;+ int encdec;+ size_t block_size;+ int (*CC_SPTR(ccmode_gcm, init))(const struct ccmode_gcm *gcm, ccgcm_ctx *ctx, size_t key_nbytes, const void *key);+ int (*CC_SPTR(ccmode_gcm, set_iv))(ccgcm_ctx *ctx, size_t iv_nbytes, const void *iv);+ int (*CC_SPTR(ccmode_gcm, gmac))(ccgcm_ctx *ctx, size_t nbytes, const void *in);+ int (*CC_SPTR(ccmode_gcm, gcm))(ccgcm_ctx *ctx, size_t nbytes, const void *in, void *out);+ int (*CC_SPTR(ccmode_gcm, finalize))(ccgcm_ctx *key, size_t tag_nbytes, void *tag);+ int (*CC_SPTR(ccmode_gcm, reset))(ccgcm_ctx *ctx);+ const void *custom;+};++/* modes this interface names but does not use */+struct ccmode_cfb;+struct ccmode_cfb8;+struct ccmode_ofb;+struct ccmode_ccm;+struct ccmode_omac;+struct ccmode_siv;+struct ccmode_siv_hmac;++#endif /* _CORECRYPTO_CCMODE_IMPL_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccn.h b/xnu-patches/corecrypto/include/corecrypto/ccn.hnew file mode 100644index 0000000..6b9c44e--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccn.h@@ -0,0 +1,37 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccn.h: multi-precision integers as arrays of units, and the sizes of such arrays. */+#ifndef _CORECRYPTO_CCN_H_+#define _CORECRYPTO_CCN_H_++#include <corecrypto/cc.h>++typedef size_t cc_size;+#if CCN_UNIT_SIZE == 8+typedef uint64_t cc_unit;+typedef int64_t cc_int;+#define CCN_LOG2_BITS_PER_UNIT 6+#else+typedef uint32_t cc_unit;+typedef int32_t cc_int;+#define CCN_LOG2_BITS_PER_UNIT 5+#endif++#define CCN_UNIT_BITS (sizeof(cc_unit) * 8)+#define ccn_sizeof_n(_n_) (sizeof(cc_unit) * (_n_))+#define ccn_nof(_bits_) (((_bits_) + CCN_UNIT_BITS - 1) >> CCN_LOG2_BITS_PER_UNIT)+#define ccn_sizeof(_bits_) (ccn_sizeof_n(ccn_nof(_bits_)))+#define ccn_nof_size(_size_) (((_size_) + sizeof(cc_unit) - 1) / sizeof(cc_unit))+#define ccn_nof_sizeof(_expr_) ccn_nof_size(sizeof(_expr_))+#define ccn_bitsof_n(_n_) ((_n_) * CCN_UNIT_BITS)+#define ccn_bitsof_size(_size_) ((_size_) * 8)+#define ccn_sizeof_size(_size_) ccn_sizeof_n(ccn_nof_size(_size_))++#endif /* _CORECRYPTO_CCN_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccpad.h b/xnu-patches/corecrypto/include/corecrypto/ccpad.hnew file mode 100644index 0000000..4d5f01b--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccpad.h@@ -0,0 +1,24 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccpad.h: CBC with ciphertext stealing, CS3 of NIST SP 800-38A's addendum (Kerberos' variant: the last two blocks are+ * always swapped). They return the number of bytes processed.+ */+#ifndef _CORECRYPTO_CCPAD_H_+#define _CORECRYPTO_CCPAD_H_++#include <corecrypto/ccmode.h>++size_t ccpad_cts3_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in,+ void *out);+size_t ccpad_cts3_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv, size_t nbytes, const void *in,+ void *out);++#endif /* _CORECRYPTO_CCPAD_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccrng.h b/xnu-patches/corecrypto/include/corecrypto/ccrng.hnew file mode 100644index 0000000..ae41280--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccrng.h@@ -0,0 +1,31 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccrng.h: a random generator is a struct whose first member is its generate function. */+#ifndef _CORECRYPTO_CCRNG_H_+#define _CORECRYPTO_CCRNG_H_++#include <corecrypto/cc.h>++#define CCRNG_STATE_COMMON \+ int (*CC_SPTR(ccrng_state, generate))(struct ccrng_state *rng, size_t outlen, void *out);++struct ccrng_state {+ CCRNG_STATE_COMMON+};++/* the provider's generator (NULL with *error set on failure) */+struct ccrng_state *ccrng(int *error);++#define ccrng_generate(rng, outlen, out) ((rng)->generate((struct ccrng_state *)(rng), (outlen), (out)))++/* a uniform value in [0, bound) */+int ccrng_uniform(struct ccrng_state *rng, uint64_t bound, uint64_t *rand);++#endif /* _CORECRYPTO_CCRNG_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccrsa.h b/xnu-patches/corecrypto/include/corecrypto/ccrsa.hnew file mode 100644index 0000000..5ae30f5--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccrsa.h@@ -0,0 +1,39 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * ccrsa.h: an RSA public key. Its context is the modulus' cczp header and units, then (in units) m0inv, the Montgomery+ * constant and the exponent; ccrsa_ctx_n() must be set (to the units the context has room for) before+ * ccrsa_make_pub() fills it.+ */+#ifndef _CORECRYPTO_CCRSA_H_+#define _CORECRYPTO_CCRSA_H_++#include <corecrypto/cc.h>+#include <corecrypto/ccn.h>+#include <corecrypto/cczp.h>++struct ccrsa_pub_ctx {+ __CCZP_ELEMENTS_DEFINITIONS(pb_)+} CC_ALIGNED(CCN_UNIT_SIZE);+typedef struct ccrsa_pub_ctx *ccrsa_pub_ctx_t;++#define ccrsa_pub_ctx_size(_nbytes_) (sizeof(struct cczp) + CCN_UNIT_SIZE + 3 * ccn_sizeof_size(_nbytes_))+#define ccrsa_pub_ctx_decl(_nbytes_, _name_) cc_ctx_decl(struct ccrsa_pub_ctx, ccrsa_pub_ctx_size(_nbytes_), _name_)+#define ccrsa_pub_ctx_clear(_nbytes_, _name_) cc_clear(ccrsa_pub_ctx_size(_nbytes_), _name_)++#define ccrsa_ctx_zm(_ctx_) ((struct cczp *)(_ctx_))+#define ccrsa_ctx_n(_ctx_) (ccrsa_ctx_zm(_ctx_)->n)++int ccrsa_make_pub(ccrsa_pub_ctx_t pubk, size_t exp_nbytes, const uint8_t *exp, size_t mod_nbytes, const uint8_t *mod);+/* PKCS #1 v1.5 (RFC 8017): *valid tells whether sig signs digest, of the hash with DER OID oid */+int ccrsa_verify_pkcs1v15(ccrsa_pub_ctx_t key, const uint8_t *oid, size_t digest_len, const uint8_t *digest,+ size_t sig_len, const uint8_t *sig, bool *valid);++#endif /* _CORECRYPTO_CCRSA_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccsha1.h b/xnu-patches/corecrypto/include/corecrypto/ccsha1.hnew file mode 100644index 0000000..19fca00--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccsha1.h@@ -0,0 +1,25 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccsha1.h: SHA-1 (FIPS 180-4). */+#ifndef _CORECRYPTO_CCSHA1_H_+#define _CORECRYPTO_CCSHA1_H_++#include <corecrypto/ccdigest.h>++#define CCSHA1_BLOCK_SIZE 64+#define CCSHA1_OUTPUT_SIZE 20+#define CCSHA1_STATE_SIZE 20++const struct ccdigest_info *ccsha1_di(void);++#define ccoid_sha1 ((unsigned char *)"\x06\x05\x2b\x0e\x03\x02\x1a")+#define ccoid_sha1_len 7++#endif /* _CORECRYPTO_CCSHA1_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/ccsha2.h b/xnu-patches/corecrypto/include/corecrypto/ccsha2.hnew file mode 100644index 0000000..a2c9ea6--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/ccsha2.h@@ -0,0 +1,39 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* ccsha2.h: the SHA-2 family (FIPS 180-4). */+#ifndef _CORECRYPTO_CCSHA2_H_+#define _CORECRYPTO_CCSHA2_H_++#include <corecrypto/ccdigest.h>++const struct ccdigest_info *ccsha224_di(void);+const struct ccdigest_info *ccsha256_di(void);+const struct ccdigest_info *ccsha384_di(void);+const struct ccdigest_info *ccsha512_di(void);++#define CCSHA224_OUTPUT_SIZE 28+#define CCSHA256_BLOCK_SIZE 64+#define CCSHA256_OUTPUT_SIZE 32+#define CCSHA256_STATE_SIZE 32+#define CCSHA384_OUTPUT_SIZE 48+#define CCSHA512_BLOCK_SIZE 128+#define CCSHA512_OUTPUT_SIZE 64+#define CCSHA512_STATE_SIZE 64++#define ccoid_sha224 ((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")+#define ccoid_sha224_len 11+#define ccoid_sha256 ((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")+#define ccoid_sha256_len 11+#define ccoid_sha384 ((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")+#define ccoid_sha384_len 11+#define ccoid_sha512 ((unsigned char *)"\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")+#define ccoid_sha512_len 11++#endif /* _CORECRYPTO_CCSHA2_H_ */diff --git a/xnu-patches/corecrypto/include/corecrypto/cczp.h b/xnu-patches/corecrypto/include/corecrypto/cczp.hnew file mode 100644index 0000000..4e985a5--- /dev/null+++ b/xnu-patches/corecrypto/include/corecrypto/cczp.h@@ -0,0 +1,33 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/* cczp.h: the header of a modulus (its size in units, bit length and arithmetic), followed by its units. */+#ifndef _CORECRYPTO_CCZP_H_+#define _CORECRYPTO_CCZP_H_++#include <corecrypto/ccn.h>++struct cczp_funcs;+typedef const struct cczp_funcs *cczp_funcs_t;++#define __CCZP_HEADER_ELEMENTS_DEFINITIONS(pre) \+ cc_size pre##n; \+ cc_unit pre##bitlen; \+ cczp_funcs_t pre##funcs;+#define __CCZP_ELEMENTS_DEFINITIONS(pre) \+ __CCZP_HEADER_ELEMENTS_DEFINITIONS(pre) \+ cc_unit pre##ccn[];++struct cczp {+ __CCZP_ELEMENTS_DEFINITIONS()+} CC_ALIGNED(CCN_UNIT_SIZE);+typedef struct cczp *cczp_t;+typedef const struct cczp *cczp_const_t;++#endif /* _CORECRYPTO_CCZP_H_ */diff --git a/xnu-patches/corecrypto/kernel/xnulinux_crypto.c b/xnu-patches/corecrypto/kernel/xnulinux_crypto.cnew file mode 100644index 0000000..9b45426--- /dev/null+++ b/xnu-patches/corecrypto/kernel/xnulinux_crypto.c@@ -0,0 +1,327 @@+// SPDX-License-Identifier: APSL-2.0+/*+ * Copyright (c) 2026 xnulinux contributors. All rights reserved.+ *+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@+ * This file contains Original Code and/or Modifications of Original Code as defined in and that are subject to the Apple+ * Public Source License Version 2.0 (the 'License'). You may not use this file except in compliance with the License.+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@+ */+/*+ * The kernel side of the corecrypto interface: the calls XNU's own code makes (bsd/, libkern/, osfmk/) that go to the+ * registered crypto provider (register_crypto_functions(): the corecrypto kext). The kernel has no cryptographic+ * algorithm of its own: digests and HMAC are the provider's (g_crypto_funcs), modes go through the mode's table, and+ * early random numbers come from the host (osfmk/prng/prng_random.c).+ */+#include <corecrypto/cc.h>+#include <corecrypto/ccdigest.h>+#include <corecrypto/cchmac.h>+#include <corecrypto/ccmode.h>+#include <corecrypto/ccsha1.h>+#include <corecrypto/ccsha2.h>+#include <corecrypto/ccmd5.h>+#include <libkern/crypto/register_crypto.h>+#include <libkern/crypto/crypto_internal.h>+#include <kern/debug.h>++static crypto_functions_t+provider(void)+{+ crypto_functions_t f = g_crypto_funcs;++ if (__improbable(f == NULL)) {+ panic("corecrypto: used before a crypto provider registered (register_crypto_functions)");+ }+ return f;+}++void+cc_clear(size_t len, void *dst)+{+ memset(dst, 0, len);+ /* the stores happen: the compiler must assume the memory is read */+ __asm__ __volatile__ ("" : : "r"(dst) : "memory");+}++int+cc_cmp_safe(size_t num, const void *ptr1, const void *ptr2)+{+ const volatile uint8_t *a = ptr1, *b = ptr2;+ uint8_t d = 0;++ if (num == 0) {+ return 1;+ }+ for (size_t i = 0; i < num; i++) {+ d |= a[i] ^ b[i];+ }+ return d != 0;+}++/* digests and HMAC: the provider's */++const struct ccdigest_info *+ccmd5_di(void)+{+ return provider()->ccmd5_di;+}++const struct ccdigest_info *+ccsha1_di(void)+{+ return provider()->ccsha1_di;+}++const struct ccdigest_info *+ccsha256_di(void)+{+ return provider()->ccsha256_di;+}++const struct ccdigest_info *+ccsha384_di(void)+{+ return provider()->ccsha384_di;+}++const struct ccdigest_info *+ccsha512_di(void)+{+ return provider()->ccsha512_di;+}++void+ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx)+{+ provider()->ccdigest_init_fn(di, ctx);+}++void+ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, size_t len, const void *data)+{+ provider()->ccdigest_update_fn(di, ctx, len, data);+}++void+ccdigest(const struct ccdigest_info *di, size_t len, const void *data, void *digest)+{+ provider()->ccdigest_fn(di, len, data, digest);+}++void+cchmac_init(const struct ccdigest_info *di, cchmac_ctx_t ctx, size_t key_len, const void *key)+{+ provider()->cchmac_init_fn(di, ctx, key_len, key);+}++void+cchmac_update(const struct ccdigest_info *di, cchmac_ctx_t ctx, size_t data_len, const void *data)+{+ provider()->cchmac_update_fn(di, ctx, data_len, data);+}++void+cchmac_final(const struct ccdigest_info *di, cchmac_ctx_t ctx, unsigned char *mac)+{+ provider()->cchmac_final_fn(di, ctx, mac);+}++void+cchmac(const struct ccdigest_info *di, size_t key_len, const void *key, size_t data_len, const void *data,+ unsigned char *mac)+{+ provider()->cchmac_fn(di, key_len, key, data_len, data, mac);+}++/* modes: through the mode's own table */++size_t+ccecb_context_size(const struct ccmode_ecb *mode)+{+ return mode->size;+}++size_t+ccecb_block_size(const struct ccmode_ecb *mode)+{+ return mode->block_size;+}++int+ccecb_init(const struct ccmode_ecb *mode, ccecb_ctx *ctx, size_t key_len, const void *key)+{+ return mode->init(mode, ctx, key_len, key);+}++int+ccecb_update(const struct ccmode_ecb *mode, const ccecb_ctx *ctx, size_t nblocks, const void *in, void *out)+{+ return mode->ecb(ctx, nblocks, in, out);+}++size_t+cccbc_context_size(const struct ccmode_cbc *mode)+{+ return mode->size;+}++size_t+cccbc_block_size(const struct ccmode_cbc *mode)+{+ return mode->block_size;+}++int+cccbc_init(const struct ccmode_cbc *mode, cccbc_ctx *ctx, size_t key_len, const void *key)+{+ return mode->init(mode, ctx, key_len, key);+}++int+cccbc_copy_iv(cccbc_iv *iv_ctx, const void *iv, size_t len)+{+ memcpy(iv_ctx, iv, len);+ return 0;+}++int+cccbc_clear_iv(cccbc_iv *iv_ctx, size_t len)+{+ cc_clear(len, iv_ctx);+ return 0;+}++/* a NULL iv is all zeroes */+int+cccbc_set_iv(const struct ccmode_cbc *mode, cccbc_iv *iv_ctx, const void *iv)+{+ if (iv != NULL) {+ memcpy(iv_ctx, iv, mode->block_size);+ } else {+ cc_clear(mode->block_size, iv_ctx);+ }+ return 0;+}++int+cccbc_update(const struct ccmode_cbc *mode, const cccbc_ctx *ctx, cccbc_iv *iv, size_t nblocks, const void *in,+ void *out)+{+ return mode->cbc(ctx, iv, nblocks, in, out);+}++size_t+ccctr_context_size(const struct ccmode_ctr *mode)+{+ return mode->size;+}++size_t+ccctr_block_size(const struct ccmode_ctr *mode)+{+ return mode->block_size;+}++int+ccctr_init(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t key_len, const void *key, const void *iv)+{+ return mode->init(mode, ctx, key_len, key, iv);+}++int+ccctr_update(const struct ccmode_ctr *mode, ccctr_ctx *ctx, size_t nbytes, const void *in, void *out)+{+ return mode->ctr(ctx, nbytes, in, out);+}++size_t+ccxts_context_size(const struct ccmode_xts *mode)+{+ return mode->size;+}++size_t+ccxts_block_size(const struct ccmode_xts *mode)+{+ return mode->block_size;+}++int+ccxts_init(const struct ccmode_xts *mode, ccxts_ctx *ctx, size_t key_nbytes, const void *data_key, const void *tweak_key)+{+ return mode->init(mode, ctx, key_nbytes, data_key, tweak_key);+}++int+ccxts_set_tweak(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv)+{+ return mode->set_tweak(ctx, tweak, iv);+}++void *+ccxts_update(const struct ccmode_xts *mode, ccxts_ctx *ctx, ccxts_tweak *tweak, size_t nblocks, const void *in,+ void *out)+{+ return mode->xts(ctx, tweak, nblocks, in, out);+}++size_t+ccgcm_context_size(const struct ccmode_gcm *mode)+{+ return mode->size;+}++size_t+ccgcm_block_size(const struct ccmode_gcm *mode)+{+ return mode->block_size;+}++int+ccgcm_init(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key)+{+ return mode->init(mode, ctx, key_nbytes, key);+}++/* these two depend on the provider's context layout: the provider's own */+int+ccgcm_init_with_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t key_nbytes, const void *key, const void *iv)+{+ return provider()->ccgcm_init_with_iv_fn(mode, ctx, key_nbytes, key, iv);+}++int+ccgcm_inc_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv)+{+ return provider()->ccgcm_inc_iv_fn(mode, ctx, iv);+}++int+ccgcm_set_iv(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t iv_nbytes, const void *iv)+{+ return mode->set_iv(ctx, iv_nbytes, iv);+}++int+ccgcm_aad(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *additional_data)+{+ return mode->gmac(ctx, nbytes, additional_data);+}++int+ccgcm_update(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t nbytes, const void *in, void *out)+{+ return mode->gcm(ctx, nbytes, in, out);+}++int+ccgcm_finalize(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, size_t tag_nbytes, void *tag)+{+ return mode->finalize(ctx, tag_nbytes, tag);+}++int+ccgcm_reset(const struct ccmode_gcm *mode, ccgcm_ctx *ctx)+{+ return mode->reset(ctx);+}