diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2c3759f0f..41859770d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -129,6 +129,11 @@ jobs: run: | clang++ -std=c++17 -O1 tools/test_update_tags.cpp -o build/test-update-tags build/test-update-tags + - name: Self-updater (manifest, signature, swap and rollback) + run: | + clang++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update + build/test-update + tools/test_update_e2e.sh - name: Controller rumble (HID bridge to SDL, fake gamepad) run: | clang -std=gnu11 -O2 -I"$HOME/sdl3/include" tools/test_rumble.c -L"$HOME/sdl3/lib" -Wl,-rpath,"$HOME/sdl3/lib" -lSDL3 -lm -o build/test-rumble @@ -265,6 +270,11 @@ jobs: run: | clang++ -std=c++17 -O1 tools/test_update_tags.cpp -o build/test-update-tags build/test-update-tags + - name: Self-updater (manifest, signature, swap and rollback) + run: | + clang++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update + build/test-update + tools/test_update_e2e.sh - name: Controller rumble (HID bridge to SDL, fake gamepad) run: | clang -std=gnu11 -O2 -I"$HOME/sdl3/include" tools/test_rumble.c -L"$HOME/sdl3/lib" -Wl,-rpath,"$HOME/sdl3/lib" -lSDL3 -lm -o build/test-rumble @@ -351,6 +361,11 @@ jobs: run: | clang++ -std=c++17 -O1 tools/test_update_tags.cpp -o build/test-update-tags build/test-update-tags + - name: Self-updater (manifest, signature, swap and rollback) + run: | + clang++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update + build/test-update + tools/test_update_e2e.sh - name: Controller rumble (HID bridge to SDL, fake gamepad) run: | clang -std=gnu11 -O2 -I"$(brew --prefix sdl3)/include" -L"$(brew --prefix sdl3)/lib" tools/test_rumble.c -lSDL3 -o build/test-rumble @@ -507,6 +522,10 @@ jobs: run: python3 tools/test_delete_guard.py - name: Data folder move (cut off at every step) run: python3 tools/test_data_move.py + - name: Self-updater (manifest, signature, swap and rollback; Windows file rules) + run: | + clang++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update.exe + build/test-update.exe - name: Movie extractor lifecycle regression run: | clang -std=gnu11 -O1 tools/test_movie_extractor.c -lavformat -lavcodec -lavutil -lswscale -o build/test-movie-extractor.exe @@ -623,6 +642,10 @@ jobs: run: python3 tools/test_delete_guard.py - name: Data folder move (cut off at every step) run: python3 tools/test_data_move.py + - name: Self-updater (manifest, signature, swap and rollback; Windows file rules) + run: | + clang++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update.exe + build/test-update.exe - name: Movie extractor lifecycle regression run: | clang -pthread -std=gnu11 -O1 tools/test_movie_extractor.c -lavformat -lavcodec -lavutil -lswscale -o build/test-movie-extractor.exe diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index fa8db3dfa..a5a80a6f8 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -78,9 +78,23 @@ jobs: echo "- Linux: \`linux-x86_64.AppImage\`, \`linux-aarch64.AppImage\`" echo "- Android: \`android.apk\`, installed as its own app, LAS Recomp Nightly, beside the release one" echo "- iPhone and iPad: \`iOS.laskit\`, made into the app with a nightly desktop build of the same date" + # Changes: the pull requests merged since the previous round, newest round first, with the earlier + # rounds' lists kept (the last 30), so a player several nightlies behind sees everything in the start + # screen's update dialog. Each round starts with a "nightly-changes: " line; the dialog shows the + # rounds newer than its own build (runtime/ui/update_notes.h). + CHANGES= if [ -n "$RSHA" ]; then - MERGES=$(gh api "repos/$GH_REPO/compare/$RSHA...$SHA" --jq '.commits[] | select(.parents | length > 1) | .commit.message | split("\n") | .[0]' 2>/dev/null | sed -n 's/^Merge pull request \(#[0-9]*\).*/\1/p' | tr '\n' ' ' || true) - [ -n "$MERGES" ] && { echo; echo "New since the previous nightly: $MERGES"; } + CHANGES=$(gh api "repos/$GH_REPO/compare/$RSHA...$SHA" --jq '.commits[] | select(.parents | length > 1) | .commit.message | capture("^Merge pull request #(?[0-9]+)[^\n]*\n\n(?[^\n]+)") | "- \(.t) (#\(.n))"' 2>/dev/null | tac || true) + fi + OLD=$(awk '/^$/ { n++ } n && n < 30 && !/^To report a problem/' <<< "$BODY" | sed '/^" + echo "**$(date -u -d "${STAMP::8}" '+%Y-%m-%d')**"; echo + echo "$CHANGES"; echo + fi + [ -n "$OLD" ] && echo "$OLD" fi echo echo "To report a problem, open an [issue]($URL/issues), say it's a nightly and which date, and attach the log (\`lasrecomp.log\` in the data folder)." @@ -161,6 +175,9 @@ jobs: find "$A" -type f \( -perm +111 -o -name '*.dylib' -o -name '*.so' \) | while read -r f; do if nm "$f" 2>/dev/null | grep ' _\{0,1\}f_70' >/dev/null; then echo "::error::game code in $f"; exit 1; fi done + - name: Sparkle feed + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}' } + run: tools/sparkle_appcast.sh nightly build/dist/*-macOS.dmg - name: Publish env: { GH_TOKEN: '${{ github.token }}', GH_REPO: '${{ github.repository }}' } run: tools/nightly_publish.sh build/dist/*-macOS.dmg macOS.dmg @@ -283,6 +300,9 @@ jobs: bad=$(find "$D" -type d \( -name extracted -o -name 'generated*' -o -name exefs -o -name romfs \); find "$D" -name '*.c' ! -path '*/zig/lib/*' ! -path '*/gamelib-src/gamelib_entry.c'; find "$D" -name 'lasgame*') [ -z "$bad" ] || { echo "::error::must not ship: $bad"; exit 1; } + - name: Update manifest + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}', VERSION: '${{ needs.plan.outputs.version }}' } + run: tools/update_manifest.sh "$VERSION" windows build/dist/*-windows.zip "build/dist/LAS Recomp" - name: Publish shell: bash # (Git Bash: gh is on its PATH, not MSYS2's) env: { GH_TOKEN: '${{ github.token }}', GH_REPO: '${{ github.repository }}' } @@ -349,6 +369,9 @@ jobs: bad=$(find "$D" -type d \( -name extracted -o -name 'generated*' -o -name exefs -o -name romfs \); find "$D" -name '*.c' ! -path '*/zig/lib/*' ! -path '*/gamelib-src/gamelib_entry.c'; find "$D" -name 'lasgame*') [ -z "$bad" ] || { echo "::error::must not ship: $bad"; exit 1; } + - name: Update manifest + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}', VERSION: '${{ needs.plan.outputs.version }}' } + run: tools/update_manifest.sh "$VERSION" windows-arm64 build/dist/*-windows-arm64.zip "build/dist/LAS Recomp" - name: Publish shell: bash # (Git Bash: gh is on its PATH, not MSYS2's) env: { GH_TOKEN: '${{ github.token }}', GH_REPO: '${{ github.repository }}' } @@ -404,6 +427,9 @@ jobs: find "$A" -type f \( -perm /111 -o -name '*.so*' \) | while read -r f; do if readelf -Ws --wide "$f" 2>/dev/null | grep ' f_70' >/dev/null; then echo "::error::game code in $f"; exit 1; fi done + - name: Update manifest + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}', VERSION: '${{ needs.plan.outputs.version }}' } + run: tools/update_manifest.sh "$VERSION" linux-${{ matrix.arch }} build/dist/*-linux-${{ matrix.arch }}.AppImage - name: Publish env: { GH_TOKEN: '${{ github.token }}', GH_REPO: '${{ github.repository }}' } run: tools/nightly_publish.sh build/dist/*-linux-${{ matrix.arch }}.AppImage linux-${{ matrix.arch }}.AppImage diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a640a0155..1ba7415ad 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -156,10 +156,18 @@ jobs: if nm "$f" 2>/dev/null | grep ' _\{0,1\}f_70' >/dev/null; then echo "::error::game code in $f"; exit 1; fi done ls -la build/dist + # Sparkle's feed for the macOS app (tools/sparkle_appcast.sh; none without the key) + - name: Sparkle feed + env: + UPDATE_SIGNING_KEY: ${{ secrets.UPDATE_SIGNING_KEY }} + TAG: ${{ inputs.tag || github.event.release.tag_name }} + run: tools/sparkle_appcast.sh "$TAG" build/dist/*-macOS.dmg - uses: actions/upload-artifact@v4 with: name: macos - path: build/dist/*.dmg + path: | + build/dist/*.dmg + build/dist/appcast-*.xml if-no-files-found: error compression-level: 0 # (already compressed) @@ -225,10 +233,16 @@ jobs: find "$D" -name '*.c' ! -path '*/zig/lib/*' ! -path '*/gamelib-src/gamelib_entry.c'; find "$D" -name 'lasgame*') [ -z "$bad" ] || { echo "::error::must not ship: $bad"; exit 1; } ls -la build/dist + # the self-updater's signed manifest (tools/update_manifest.sh; none without the key: Download only) + - name: Update manifest + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}', VERSION: '${{ needs.check.outputs.version }}' } + run: tools/update_manifest.sh "$VERSION" windows build/dist/*-windows.zip "build/dist/LAS Recomp" - uses: actions/upload-artifact@v4 with: name: windows - path: build/dist/*.zip + path: | + build/dist/*.zip + build/dist/update-*.txt if-no-files-found: error compression-level: 0 @@ -439,10 +453,15 @@ jobs: if readelf -Ws --wide "$f" 2>/dev/null | grep ' f_70' >/dev/null; then echo "::error::game code in $f"; exit 1; fi done ls -la build/dist + - name: Update manifest + env: { UPDATE_SIGNING_KEY: '${{ secrets.UPDATE_SIGNING_KEY }}', VERSION: '${{ needs.check.outputs.version }}' } + run: tools/update_manifest.sh "$VERSION" linux-${{ matrix.arch }} build/dist/*-linux-${{ matrix.arch }}.AppImage - uses: actions/upload-artifact@v4 with: name: linux-${{ matrix.arch }} - path: build/dist/*.AppImage + path: | + build/dist/*.AppImage + build/dist/update-*.txt if-no-files-found: error compression-level: 0 @@ -548,7 +567,8 @@ jobs: GH_REPO: ${{ github.repository }} run: | cd dist - ls -la + mkdir ../manifests && mv update-*.txt appcast-*.xml ../manifests/ 2>/dev/null || true # (the updaters', beside the files) + ls -la . ../manifests N=6; [ "$ARM64" = true ] && N=7 [ "$(ls | wc -l)" = $N ] || { echo "::error::expected $N files"; exit 1; } # (a test build: only the platforms its fix targets ship; the others were built as a compile check) @@ -559,11 +579,17 @@ jobs: *) echo "::error::unknown file $f"; exit 1 ;; esac [[ " $PLATFORMS " == *" $p "* ]] || { echo "built, not shipped: $f"; rm "$f"; } done + for m in ../manifests/*; do # (a manifest or feed goes up only with its platform's file) + [ -f "$m" ] || continue + plat=$(basename "$m"); plat=${plat#update-}; plat=${plat#appcast-}; plat=${plat%.*} + ls ./*-"$plat".zip ./*-"$plat".AppImage ./*-"$plat".dmg >/dev/null 2>&1 || rm "$m" + done + MANIFESTS=(); for m in ../manifests/*; do [ -f "$m" ] && MANIFESTS+=("$m"); done if [ "$EVENT" = release ]; then # (published on GitHub: attach the files, the notes get the sha256 table) gh release view "$TAG" --json body -q .body | grep -v '^> The download files are being built' > ../body.md || true { cat ../body.md; echo; echo "| File | sha256 |"; echo "|---|---|" for f in *; do echo "| \`${f// /.}\` | \`$(sha256sum "$f" | cut -d' ' -f1)\` |"; done; } > ../notes.md - gh release upload "$TAG" --clobber * + gh release upload "$TAG" --clobber * "${MANIFESTS[@]}" gh release edit "$TAG" --notes-file ../notes.md gh release view "$TAG" --json url,assets -q '.url, (.assets[] | "\(.name) \(.size)")' exit 0 @@ -584,9 +610,9 @@ jobs: if DRAFT=$(gh release view "$TAG" --json isDraft -q .isDraft 2>/dev/null); then [ "$DRAFT" = true ] || { echo "::error::release $TAG is already published: not touched"; exit 1; } gh release edit "$TAG" --draft --target "$SHA" --notes-file ../notes.md "${PRE[@]}" - gh release upload "$TAG" --clobber * + gh release upload "$TAG" --clobber * "${MANIFESTS[@]}" else - gh release create "$TAG" --draft --target "$SHA" --title "$TAG" --notes-file ../notes.md "${PRE[@]}" * + gh release create "$TAG" --draft --target "$SHA" --title "$TAG" --notes-file ../notes.md "${PRE[@]}" * "${MANIFESTS[@]}" fi gh release view "$TAG" --json url,isDraft,assets -q '.url, .isDraft, (.assets[] | "\(.name) \(.size)")' diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b172fac58..0bdd10878 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -132,6 +132,9 @@ items picked up anywhere, floor key spots included, get a message naming their d `python tools/test_text_speed.py` checks the Text speed hooks (Normal, Fast, Instant) without game data. +The updater: `tools/test_update.cpp` (manifest, signature, the swap and its rollback; build line at its top) and +`tools/test_update_e2e.sh` (download, check, swap and restart against a local web server), without game data or network. + Build-cache regression tests use synthetic inputs and the production scheduler: `python tools/test_build_cache.py` and `python tools/test_gamebuild_resume.py`. On Windows, also run `python tools/test_windows_processes.py` to check independent diff --git a/README.md b/README.md index d01816d85..f8bc4dd5c 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,9 @@ none of their files are used): - [BIZ UDPGothic](https://github.com/googlefonts/morisawa-biz-ud-gothic) by Morisawa - the game's text on Linux when you haven't imported the Switch fonts: a free font from the same universal-design gothic family as the Switch's UD Shin Go (SIL Open Font License 1.1, `third_party/fonts/BIZUDPGothic/OFL.txt`) - [DejaVu Sans](https://dejavu-fonts.github.io) - the in-game menu and button letters when no system font fits, and the game text's last resort (Bitstream Vera / DejaVu license, `third_party/fonts/DejaVu/LICENSE`) - [Vulkan Memory Allocator](https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator) - Windows and Linux renderer (MIT) +- [Monocypher](https://monocypher.org) by Loup Vaillant and contributors - checks the signature of updates on Windows and + Linux (CC0, `third_party/monocypher/LICENCE.md`) +- [Sparkle](https://sparkle-project.org) - the macOS app's updater (MIT, `Contents/Resources/Sparkle-LICENSE.txt` in the app) - [SMAA](https://github.com/iryoku/smaa) by Jorge Jimenez, Jose I. Echevarria, Belen Masia, Fernando Navarro and Diego Gutierrez - the anti-aliasing option (MIT, `third_party/smaa/LICENSE.txt`) - [AMD FidelityFX Super Resolution 1](https://github.com/GPUOpen-Effects/FidelityFX-FSR) - the Windows and Linux upscaler @@ -348,7 +351,7 @@ LAS Recomp's own code (the recompiler tools, the runtime, the randomizer additio Vulkan Memory Allocator (MIT), SMAA (MIT), AMD FSR 1 (MIT), the Baloo 2 and BIZ UDPGothic fonts (SIL OFL 1.1), the DejaVu Sans font (Bitstream Vera / DejaVu license), SDL 3 (zlib), FFmpeg (LGPL 2.1+, linked dynamically, and statically in the iOS app), the Zig compiler (MIT; its LLVM / Clang / LLD: Apache 2.0 with LLVM exceptions; its C library headers keep their own licenses), the iOS kit's Mach-O linker (LLD as built by the Rust project: Apache 2.0 with LLVM exceptions), miniz -(MIT: the iOS kit and the .ipa it makes), and the generator helpers' bundled libraries listed under Credits (oead / evfl / Keystone in the seed +(MIT: the iOS kit and the .ipa it makes), Monocypher (CC0: update signatures), Sparkle (MIT: the macOS updater), and the generator helpers' bundled libraries listed under Credits (oead / evfl / Keystone in the seed generator and Unicorn in the game code generator are under the GPL, versions in the table below: each helper is a separate program the game runs, and its sources are this repository plus those projects' published sources). The iOS app is the exception: iOS runs no helper processes, so it embeds CPython and the seed generator's packages - oead, evfl and Keystone (GPL 2.0+ / GPL 2.0), diff --git a/docs/debug-hooks.md b/docs/debug-hooks.md index 2706b2f1d..2cdeb68c0 100644 --- a/docs/debug-hooks.md +++ b/docs/debug-hooks.md @@ -154,6 +154,14 @@ Use it with `LARECOMP_INPUT` (to get into a save) and `LARECOMP_SAVE_DIR` (so th | `LARECOMP_MENU_BAR` | entry number | Open the menu bar alone, its cursor on that entry: `0` Settings, `1` Cosmetics, `2` Randomizer, `3` Warp, `4` Save Editor, `5` Cheats, `6` Reset, `7` Quit, `8` the close button. | | `LARECOMP_TOUCH_SHOW_INPUT` | 0 / 1 | iPhone, iPad and Android: show scripted input (`LARECOMP_INPUT`) on the touch controls, the buttons lit and the stick moved. | | `LARECOMP_UPDATE_TAGS` | `tag,tag,...` | The update check sees these release tags instead of asking GitHub (no network). | +| `LARECOMP_UPDATE_JSON` | file | The update check reads this saved releases list instead of asking GitHub (the update dialog's changelog too). | +| `LARECOMP_UPDATE_OPEN` | `1` / `update` | The update dialog opens as soon as the update check finds one; `update` also presses Update. | +| `LARECOMP_UPDATE_URL` | URL | Update downloads `//update-.txt` and the file it names from here instead of GitHub (`http://127.0.0.1:...`, `file:///...`). | +| `LARECOMP_UPDATE_KEY` | 64 hex | With `LARECOMP_UPDATE_URL`: the public key manifests are checked with (a test key; `tools/update_sign.cpp keygen`). | +| `LARECOMP_UPDATE_APP` | folder | Windows: the app folder Update replaces files in (default: the exe's). Linux updates `$APPIMAGE`. | +| `LARECOMP_UPDATE_NO_RESTART` | `1` | After Update installs (or rolls back), quit instead of starting the app again. | +| `LARECOMP_SPARKLE_FEED` | URL | macOS: Sparkle reads this appcast instead of the build's, and runs even in builds whose channel has no Update yet. | +| `LARECOMP_SPARKLE_AUTOINSTALL` | `1` | macOS: an update Sparkle downloaded installs at once and the app restarts, as its Install and Relaunch button does (no window to click). | | `LARECOMP_FONT` | font file | Use this file for the system fonts the game asks for (.ttf, .otf, .ttc, .bfttf). | ## Logging and diagnostics diff --git a/runtime/main.c b/runtime/main.c index 9250ac43d..f16975836 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -16,6 +16,7 @@ #include "hle.h" #include "kernel.h" #include "rando.h" +#include "update.h" const char *g_romfs_dir; @@ -324,6 +325,8 @@ int main(int argc, char **argv) { #else setvbuf(stderr, NULL, _IOLBF, 0); #endif + update_boot(); // (an update's swap that was cut off, or a new version that never reached the start screen: the + // old one back, and started instead of this process. runtime/update.h) const char *root = "extracted"; // (in the data root) // LARECOMP_NO_LAUNCHER=1 (tools/game_slot.sh sets it for every test run): never the start screen - it can // delete game files, and a test's extracted/ may be a link to someone else's. Where it would come up (a forced @@ -368,6 +371,7 @@ int main(int argc, char **argv) { launcher_run(root); } boot(root); + update_reached_start_screen(); // (--play skips the start screen: the game booting counts the same) int kind = ui_run(); // window and input on the main thread; returns when a reboot is requested teardown(); title = kind == RT_REBOOT_TITLE; diff --git a/runtime/ui/lang/de.inc b/runtime/ui/lang/de.inc index 60c5acdcd..392a5fb5e 100644 --- a/runtime/ui/lang/de.inc +++ b/runtime/ui/lang/de.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Dynamische Auflösung"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Hebt und senkt die Auflösung nach Bedarf, um deine Ziel-FPS zu halten, bis zur Auflösung deines Bildschirms. Die Einstellung „Auflösung“ ruht, solange es an ist."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Wirkt nur, wenn das Spiel unter der Auflösung deines Bildschirms rendert: bei einer niedrigeren Auflösung oder mit dynamischer Auflösung."}, +{"Checking the update", "Update wird geprüft"}, +{"Downloading", "Wird heruntergeladen"}, +{"Checking the download", "Download wird geprüft"}, +{"Unpacking", "Wird entpackt"}, +{"Installing", "Wird installiert"}, +{"No details for this update.", "Keine Details zu diesem Update."}, +{"Update available", "Update verfügbar"}, +{"Update can't change the app's files here. Use Download.", "Update kann die Dateien der App hier nicht ändern. Nutze Herunterladen."}, +{"Update", "Aktualisieren"}, +{"Download", "Herunterladen"}, diff --git a/runtime/ui/lang/es.inc b/runtime/ui/lang/es.inc index fe37e4514..ae0bef743 100644 --- a/runtime/ui/lang/es.inc +++ b/runtime/ui/lang/es.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Resolución dinámica"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Sube y baja la resolución según haga falta para mantener tus FPS objetivo, hasta la resolución de tu pantalla. El ajuste Resolución queda en pausa mientras está activada."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Solo tiene efecto si el juego se renderiza por debajo de la resolución de tu pantalla: una Resolución más baja o la Resolución dinámica."}, +{"Checking the update", "Comprobando la actualización"}, +{"Downloading", "Descargando"}, +{"Checking the download", "Comprobando la descarga"}, +{"Unpacking", "Descomprimiendo"}, +{"Installing", "Instalando"}, +{"No details for this update.", "No hay detalles de esta actualización."}, +{"Update available", "Actualización disponible"}, +{"Update can't change the app's files here. Use Download.", "Actualizar no puede cambiar aquí los archivos de la app. Usa Descargar."}, +{"Update", "Actualizar"}, +{"Download", "Descargar"}, diff --git a/runtime/ui/lang/fr.inc b/runtime/ui/lang/fr.inc index 2dcb0d93d..0df49c403 100644 --- a/runtime/ui/lang/fr.inc +++ b/runtime/ui/lang/fr.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Résolution dynamique"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Monte et baisse la résolution selon les besoins pour tenir tes FPS cibles, jusqu'à la résolution de ton écran. Le réglage Résolution est mis de côté tant que c'est activé."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Sans effet sauf si le jeu est rendu sous la résolution de ton écran : une Résolution plus basse, ou la Résolution dynamique."}, +{"Checking the update", "Vérification de la mise à jour"}, +{"Downloading", "Téléchargement"}, +{"Checking the download", "Vérification du téléchargement"}, +{"Unpacking", "Décompression"}, +{"Installing", "Installation"}, +{"No details for this update.", "Aucun détail pour cette mise à jour."}, +{"Update available", "Mise à jour disponible"}, +{"Update can't change the app's files here. Use Download.", "Mettre à jour ne peut pas modifier les fichiers de l'app ici. Utilise Télécharger."}, +{"Update", "Mettre à jour"}, +{"Download", "Télécharger"}, diff --git a/runtime/ui/lang/it.inc b/runtime/ui/lang/it.inc index 5527dc096..e9e65eb2b 100644 --- a/runtime/ui/lang/it.inc +++ b/runtime/ui/lang/it.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Risoluzione dinamica"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Alza e abbassa la risoluzione quanto serve per mantenere i tuoi FPS obiettivo, fino alla risoluzione del tuo schermo. L'impostazione Risoluzione viene messa da parte mentre è attiva."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Non ha effetto a meno che il gioco non venga renderizzato sotto la risoluzione del tuo schermo: una Risoluzione più bassa o la Risoluzione dinamica."}, +{"Checking the update", "Verifica dell'aggiornamento"}, +{"Downloading", "Download in corso"}, +{"Checking the download", "Verifica del download"}, +{"Unpacking", "Estrazione"}, +{"Installing", "Installazione"}, +{"No details for this update.", "Nessun dettaglio per questo aggiornamento."}, +{"Update available", "Aggiornamento disponibile"}, +{"Update can't change the app's files here. Use Download.", "Aggiorna non può modificare qui i file dell'app. Usa Scarica."}, +{"Update", "Aggiorna"}, +{"Download", "Scarica"}, diff --git a/runtime/ui/lang/ja.inc b/runtime/ui/lang/ja.inc index f6d3366f3..533ffc57d 100644 --- a/runtime/ui/lang/ja.inc +++ b/runtime/ui/lang/ja.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "可変解像度"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "目標FPSを保てるよう、必要に応じて解像度を上げ下げします(最大で画面の解像度まで)。オンの間は「解像度」の設定は使われません。"}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "ゲームが画面の解像度より低く描画されるとき(低い「解像度」か「可変解像度」)だけ効果があります。"}, +{"Checking the update", "アップデートを確認中"}, +{"Downloading", "ダウンロード中"}, +{"Checking the download", "ダウンロードを確認中"}, +{"Unpacking", "展開中"}, +{"Installing", "インストール中"}, +{"No details for this update.", "このアップデートの詳細はありません。"}, +{"Update available", "アップデートがあります"}, +{"Update can't change the app's files here. Use Download.", "ここではアップデートでアプリのファイルを変更できません。ダウンロードを使ってください。"}, +{"Update", "アップデート"}, +{"Download", "ダウンロード"}, diff --git a/runtime/ui/lang/ko.inc b/runtime/ui/lang/ko.inc index 98006d04c..04eff6e78 100644 --- a/runtime/ui/lang/ko.inc +++ b/runtime/ui/lang/ko.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "동적 해상도"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "목표 FPS를 유지하도록 필요에 따라 해상도를 올리고 내립니다(최대 화면 해상도까지). 켜져 있는 동안 '해상도' 설정은 쓰이지 않습니다."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "게임이 화면 해상도보다 낮게 렌더링될 때(낮은 '해상도' 또는 '동적 해상도')만 효과가 있습니다."}, +{"Checking the update", "업데이트 확인 중"}, +{"Downloading", "다운로드 중"}, +{"Checking the download", "다운로드 확인 중"}, +{"Unpacking", "압축 해제 중"}, +{"Installing", "설치 중"}, +{"No details for this update.", "이 업데이트에 대한 정보가 없습니다."}, +{"Update available", "업데이트 가능"}, +{"Update can't change the app's files here. Use Download.", "여기서는 업데이트로 앱 파일을 바꿀 수 없습니다. 다운로드를 사용하세요."}, +{"Update", "업데이트"}, +{"Download", "다운로드"}, diff --git a/runtime/ui/lang/nl.inc b/runtime/ui/lang/nl.inc index db379ba53..f2241747a 100644 --- a/runtime/ui/lang/nl.inc +++ b/runtime/ui/lang/nl.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Dynamische resolutie"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Verhoogt en verlaagt de resolutie waar nodig om je FPS-doel te halen, tot de resolutie van je scherm. De instelling Resolutie wordt genegeerd zolang dit aan staat."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Doet alleen iets als het spel onder de resolutie van je scherm rendert: een lagere Resolutie, of Dynamische resolutie."}, +{"Checking the update", "Update controleren"}, +{"Downloading", "Downloaden"}, +{"Checking the download", "Download controleren"}, +{"Unpacking", "Uitpakken"}, +{"Installing", "Installeren"}, +{"No details for this update.", "Geen details voor deze update."}, +{"Update available", "Update beschikbaar"}, +{"Update can't change the app's files here. Use Download.", "Bijwerken kan de bestanden van de app hier niet wijzigen. Gebruik Downloaden."}, +{"Update", "Bijwerken"}, +{"Download", "Downloaden"}, diff --git a/runtime/ui/lang/pt-BR.inc b/runtime/ui/lang/pt-BR.inc index b8b434d81..d34343597 100644 --- a/runtime/ui/lang/pt-BR.inc +++ b/runtime/ui/lang/pt-BR.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Resolução dinâmica"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Aumenta e reduz a resolução conforme necessário para manter sua meta de FPS, até a resolução da sua tela. A opção Resolução fica de lado enquanto estiver ligado."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Só tem efeito quando o jogo renderiza abaixo da resolução da sua tela: uma Resolução menor ou a Resolução dinâmica."}, +{"Checking the update", "Verificando a atualização"}, +{"Downloading", "Baixando"}, +{"Checking the download", "Verificando o download"}, +{"Unpacking", "Descompactando"}, +{"Installing", "Instalando"}, +{"No details for this update.", "Sem detalhes para esta atualização."}, +{"Update available", "Atualização disponível"}, +{"Update can't change the app's files here. Use Download.", "Atualizar não pode alterar os arquivos do app aqui. Use Baixar."}, +{"Update", "Atualizar"}, +{"Download", "Baixar"}, diff --git a/runtime/ui/lang/ru.inc b/runtime/ui/lang/ru.inc index 64bae57be..ff600a246 100644 --- a/runtime/ui/lang/ru.inc +++ b/runtime/ui/lang/ru.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "Динамическое разрешение"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "Повышает и понижает разрешение по мере необходимости, чтобы держать целевой FPS, вплоть до разрешения экрана. Пока включено, настройка «Разрешение» не используется."}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "Действует, только если игра рендерится ниже разрешения экрана: при меньшем «Разрешении» или с «Динамическим разрешением»."}, +{"Checking the update", "Проверка обновления"}, +{"Downloading", "Загрузка"}, +{"Checking the download", "Проверка загрузки"}, +{"Unpacking", "Распаковка"}, +{"Installing", "Установка"}, +{"No details for this update.", "Нет сведений об этом обновлении."}, +{"Update available", "Доступно обновление"}, +{"Update can't change the app's files here. Use Download.", "Обновить не может изменить файлы приложения здесь. Используйте Скачать."}, +{"Update", "Обновить"}, +{"Download", "Скачать"}, diff --git a/runtime/ui/lang/zh-Hans.inc b/runtime/ui/lang/zh-Hans.inc index c77efb838..5ecca24e9 100644 --- a/runtime/ui/lang/zh-Hans.inc +++ b/runtime/ui/lang/zh-Hans.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "动态分辨率"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "按需升降分辨率以保持目标帧率,最高到屏幕的分辨率。开启时不使用“分辨率”设置。"}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "只有在游戏以低于屏幕的分辨率渲染时才有作用:较低的“分辨率”或“动态分辨率”。"}, +{"Checking the update", "正在检查更新"}, +{"Downloading", "正在下载"}, +{"Checking the download", "正在检查下载"}, +{"Unpacking", "正在解压"}, +{"Installing", "正在安装"}, +{"No details for this update.", "此更新没有详细信息。"}, +{"Update available", "有可用更新"}, +{"Update can't change the app's files here. Use Download.", "此处无法通过更新修改应用文件。请使用下载。"}, +{"Update", "更新"}, +{"Download", "下载"}, diff --git a/runtime/ui/lang/zh-Hant.inc b/runtime/ui/lang/zh-Hant.inc index e7e2225f5..11c560d56 100644 --- a/runtime/ui/lang/zh-Hant.inc +++ b/runtime/ui/lang/zh-Hant.inc @@ -2147,3 +2147,13 @@ {"Dynamic resolution", "動態解析度"}, {"Raises and lowers the resolution as needed to hold your FPS target, up to your screen's resolution. The Resolution setting is set aside while it's on.", "依需要升降解析度以維持目標影格率,最高到螢幕的解析度。開啟時不使用「解析度」設定。"}, {"Does nothing unless the game renders below your screen's resolution: a lower Resolution, or Dynamic resolution.", "只有在遊戲以低於螢幕的解析度繪製時才有作用:較低的「解析度」或「動態解析度」。"}, +{"Checking the update", "正在檢查更新"}, +{"Downloading", "正在下載"}, +{"Checking the download", "正在檢查下載"}, +{"Unpacking", "正在解壓縮"}, +{"Installing", "正在安裝"}, +{"No details for this update.", "此更新沒有詳細資訊。"}, +{"Update available", "有可用更新"}, +{"Update can't change the app's files here. Use Download.", "此處無法透過更新修改應用程式檔案。請使用下載。"}, +{"Update", "更新"}, +{"Download", "下載"}, diff --git a/runtime/ui/launcher.mm b/runtime/ui/launcher.mm index 5dc39e466..6be39b9f6 100644 --- a/runtime/ui/launcher.mm +++ b/runtime/ui/launcher.mm @@ -125,6 +125,9 @@ static void save_png(id tex, const char *path) { // Nightly builds (.github/workflows/nightly.yml sets this): the build's time (YYYYMMDDHHMM, UTC; 0: not a nightly). // They only look for a newer nightly build of their own platform, never a release or an experimental build. #define LARECOMP_NIGHTLY_STAMP 0LL +// Update in the update dialog (runtime/update.h; macOS: Sparkle): nightlies have it; release builds only once this +// is 1, after it has worked on nightlies for a while on every platform. Until then they offer Download only. +#define LARECOMP_UPDATE_RELEASES 0 // An experimental platform that joins the normal releases: from this release on, its experimental builds offer it // (and any later one) ahead of a newer experimental build. Android ships in Beta 6. #ifdef __ANDROID__ @@ -159,14 +162,24 @@ static void save_png(id tex, const char *path) { #define RELEASES_URL "https://api.github.com/repos/peiikai/LASRecomp/releases?per_page=100" #define RELEASE_PAGE "https://github.com/peiikai/LASRecomp/releases/tag/" -#include "update_tags.h" // tag_rank, tag_label +#include "update_notes.h" // tag_rank, tag_label; the update dialog's changelog +#include "update.h" +#include "sparkle.h" -static std::string g_update_tag; // a newer release's tag (g_update_lock) +static std::string g_update_tag; // a newer release's tag (g_update_lock) +static std::string g_update_json; // the releases list it came from (the dialog's changelog) static pthread_mutex_t g_update_lock = PTHREAD_MUTEX_INITIALIZER; static void *update_thread(void *) { std::string body; - if (const char *t = getenv("LARECOMP_UPDATE_TAGS")) { // (test) "beta_2,v1.0.0": these releases, no network + if (const char *t = getenv("LARECOMP_UPDATE_JSON")) { // (test) a saved releases list instead of GitHub's + if (FILE *f = fopen(t, "rb")) { + char buf[8192]; + size_t n; + while ((n = fread(buf, 1, sizeof buf, f)) > 0) body.append(buf, n); + fclose(f); + } + } else if (const char *t = getenv("LARECOMP_UPDATE_TAGS")) { // (test) "beta_2,v1.0.0": these releases, no network for (const char *p = t; *p;) { const char *c = strchr(p, ','); std::string t(p, c ? c - p : strlen(p)); // (LAS.Recomp-*: an asset name) @@ -239,6 +252,7 @@ static void save_png(id tex, const char *path) { if (!best.empty()) fprintf(stderr, "[launcher] update available: %s (this build %s)\n", best.c_str(), LARECOMP_VERSION); pthread_mutex_lock(&g_update_lock); g_update_tag = best; + if (!best.empty()) g_update_json.swap(body); pthread_mutex_unlock(&g_update_lock); return NULL; } @@ -1047,6 +1061,142 @@ static void notice_popup(float s) { ImGui::PopFont(); ImGui::EndPopup(); } +// ------------------------------------------------------------------ update dialog +// The start screen's update link opens it: what's new since this build, then Update (this platform updates itself: +// runtime/update.h; macOS: Sparkle), Download (the release page: always there, also if Update ever fails) and Back. +// Phones and tablets: no Update. Release builds: no Update until LARECOMP_UPDATE_RELEASES. +static bool g_update_open; +static std::string g_update_notes, g_update_title; +// (the updater's steps, for the translation tables: runtime/update.cpp sets them in English) +static const char *const UPDATE_STEPS[] = {LN("Checking the update"), LN("Downloading"), LN("Checking the download"), + LN("Unpacking"), LN("Installing")}; +// this build's channel has Update at all: nightlies, and releases once LARECOMP_UPDATE_RELEASES is on (never test builds) +static bool update_channel_on(void) { + if (PLAT_TOUCH) return false; + return LARECOMP_NIGHTLY_STAMP || (LARECOMP_UPDATE_RELEASES && strncmp(LARECOMP_VERSION, "test-", 5)); +} +static bool update_button_shown(const std::string &tag) { + (void)UPDATE_STEPS; + if (!update_channel_on() && !getenv("LARECOMP_SPARKLE_FEED")) return false; +#if defined(__APPLE__) && !PLAT_IOS + (void)tag; + return sparkle_available(); // (Sparkle finds the newest of its channel itself) +#else + if (!LARECOMP_SELF_UPDATE) return false; + return LARECOMP_NIGHTLY_STAMP ? tag == "nightly" : tag_rank(tag) >= 0; +#endif +} +static void update_open(const std::string &tag) { + pthread_mutex_lock(&g_update_lock); + std::vector rel = json_releases(g_update_json); + pthread_mutex_unlock(&g_update_lock); + if (tag == "nightly") { + g_update_notes = nightly_changelog(rel, LARECOMP_NIGHTLY_STAMP); + g_update_title = tag_label("nightly", false); + } else { + long long own = strncmp(LARECOMP_VERSION, "test-", 5) ? tag_rank(LARECOMP_VERSION) : tag_rank(LARECOMP_TEST_OF) - 1; + g_update_notes = tag_rank(tag) >= 0 ? release_changelog(rel, own, tag) : ""; + g_update_title = tag_label(tag, false); + } + g_update_open = true; +} +// the changelog: headings in the title colour, list items with a bullet, the rest as paragraphs +static void update_notes_draw(float sc) { + const PlasticTheme &th = plastic_theme(); + if (g_update_notes.empty()) { + ImGui::TextColored(plastic_col(th.text_dim), "%s", L("No details for this update.")); + return; + } + ImGui::PushTextWrapPos(0); + for (size_t at = 0; at <= g_update_notes.size();) { + size_t nl = g_update_notes.find('\n', at); + if (nl == std::string::npos) nl = g_update_notes.size(); + std::string l = g_update_notes.substr(at, nl - at); + at = nl + 1; + size_t h = 0; + while (h < l.size() && l[h] == '#') h++; + if (l.empty()) ImGui::Dummy(ImVec2(0, 6 * sc)); + else if (h && h < l.size() && l[h] == ' ') { + if (h == 1) ImGui::PushFont(g_font_menu); + ImGui::TextColored(plastic_col(th.title), "%s", md_inline(l.substr(h + 1)).c_str()); + if (h == 1) ImGui::PopFont(); + } else if (l.size() > 2 && (l[0] == '-' || l[0] == '*') && l[1] == ' ') { + ImGui::Bullet(); + ImGui::TextUnformatted(md_inline(l.substr(2)).c_str()); + } else ImGui::TextUnformatted(md_inline(l).c_str()); + } + ImGui::PopTextWrapPos(); +} +static void update_dialog(float s) { + float sc = s * 720.0f / 1080.0f; + pthread_mutex_lock(&g_update_lock); + std::string tag = g_update_tag; + pthread_mutex_unlock(&g_update_lock); + if (g_update_open && !ImGui::IsPopupOpen("##update")) ImGui::OpenPopup("##update"); + ImVec2 ds = ImGui::GetIO().DisplaySize; + ImGui::SetNextWindowPos(ImVec2(ds.x * 0.5f, ds.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowSize(ImVec2(fminf(ds.x * 0.9f, 1300 * sc), 0)); + if (!ImGui::BeginPopupModal("##update", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) + return; + const PlasticTheme &th = plastic_theme(); + double frac = -1; + char msg[256] = ""; + int st = update_status(&frac, msg, sizeof msg); + bool running = st == UPDATE_RUNNING || st == UPDATE_DONE; + ImGui::PushFont(g_font_small); + ImGui::PushFont(g_font_menu); + ImGui::TextColored(plastic_col(th.title), "%s", L("Update available")); + ImGui::PopFont(); + ImGui::TextColored(plastic_col(th.text_dim), "%s", g_update_title.c_str()); // (the release's name stays English) + ImGui::Spacing(); + ImGui::BeginChild("##notes", ImVec2(0, fminf(ds.y * 0.55f, 900 * sc)), ImGuiChildFlags_FrameStyle); + update_notes_draw(sc); + ImGui::EndChild(); + ImGui::Spacing(); + bool updater = update_button_shown(tag), sparkle = updater && sparkle_available(); + const char *why = NULL; + bool can = updater && (sparkle || update_available(&why)); + if (updater && !can && st == UPDATE_IDLE) { + static std::string logged; + if (logged != tag) logged = tag, fprintf(stderr, "[launcher] Update off here: %s\n", why ? why : "?"); + ImGui::TextColored(plastic_col(th.text_dim), "%s", L("Update can't change the app's files here. Use Download.")); + } + if (running) { + ImGui::TextUnformatted(st == UPDATE_DONE ? L("Restarting...") : L(msg)); + plastic_progress_bar(st == UPDATE_DONE ? 1.0f : frac < 0 ? 0.0f : (float)frac, ImVec2(-1, 0), NULL, sc); + } else if (st == UPDATE_FAILED) ImGui::TextColored(plastic_col(th.warn), "%s", L(msg)); + ImGui::Spacing(); + bool esc = ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false) || ImGui::IsKeyPressed(ImGuiKey_Escape, false); + ImVec2 bw(300 * sc, 0); + if (running) { + ImGui::BeginDisabled(st == UPDATE_DONE); + if (ImGui::Button(L("Cancel"), bw) || esc) update_cancel(); + ImGui::EndDisabled(); + } else { + if (updater) { + ImGui::BeginDisabled(!can); + ImGui::SetItemDefaultFocus(); + if (ImGui::Button(L("Update"), bw)) { + if (sparkle) sparkle_check_now(), g_update_open = false, ImGui::CloseCurrentPopup(); // (its own window) + else update_start(tag.c_str(), LARECOMP_PLATFORM, LARECOMP_NIGHTLY_STAMP); + } + ImGui::EndDisabled(); + ImGui::SameLine(); + } + if (!updater || !can) ImGui::SetItemDefaultFocus(); + if (ImGui::Button(L("Download"), bw)) SDL_OpenURL((RELEASE_PAGE + tag).c_str()); + ImGui::SameLine(); + if (ImGui::Button(L("Back"), bw) || esc) g_update_open = false, ImGui::CloseCurrentPopup(); + } + ImGui::PopFont(); + ImGui::EndPopup(); + if (st == UPDATE_DONE) { // (one frame of "Restarting..." first) + static int frames; + if (++frames > 2) settings_save(), update_restart(); + } +} + // First PLAY ever, before the menu note: authentic 16:9 or the unlocked (fill the window) aspect ratio. static bool aspect_prompt(float s) { bool done = false; @@ -2354,8 +2504,28 @@ static void load_fonts(void) { const char *play_at_env = getenv("LARECOMP_LAUNCHER_PLAY_AT"), *skip_at_env = getenv("LARECOMP_LAUNCHER_SKIP_AT"); uint64_t loop_t0 = SDL_GetTicks(), last_frame = loop_t0, bg_gap = 0, bg_frames = 0; bool bg_logged = false; + update_reached_start_screen(); // (a new version after an update got this far: it's kept. runtime/update.h) + { // macOS: Sparkle, with its one check of this launch (not again after a "Title screen" reboot) + static bool sparkle_once; + if (!sparkle_once && (update_channel_on() || getenv("LARECOMP_SPARKLE_FEED")) && !getenv("LARECOMP_NO_UPDATE_CHECK")) + sparkle_start(true); + sparkle_once = true; + } + // (test) LARECOMP_UPDATE_OPEN=1: the update dialog opens as soon as the check found one; =update: and Update is + // pressed + const char *upd_open_env = getenv("LARECOMP_UPDATE_OPEN"); for (;;) { uint64_t now = SDL_GetTicks(); + if (upd_open_env && !g_update_open) { + pthread_mutex_lock(&g_update_lock); + std::string t = g_update_tag; + pthread_mutex_unlock(&g_update_lock); + if (!t.empty()) { + update_open(t); + if (!strcmp(upd_open_env, "update") && update_button_shown(t)) update_start(t.c_str(), LARECOMP_PLATFORM, LARECOMP_NIGHTLY_STAMP); + upd_open_env = NULL; + } + } if (g_bg_prep) bg_frames++, bg_gap = std::max(bg_gap, now - last_frame); else if (!bg_logged && bg_frames) { // (how the start screen kept up meanwhile) bg_logged = true; @@ -3124,7 +3294,7 @@ static void load_fonts(void) { dl->AddLine(ImVec2(lp.x, lp.y + lsz.y), ImVec2(lp.x + lsz.x, lp.y + lsz.y), col, 1.5f * s); ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); } - if (click) SDL_OpenURL((RELEASE_PAGE + upd).c_str()); + if (click) update_open(upd); } } ImGui::PopFont(); @@ -3135,6 +3305,7 @@ static void load_fonts(void) { menu_style_begin(ds.y / 1080.0f), plastic_widgets(true), pieces = true; data_folder_popup(s); notice_popup(s); + update_dialog(s); plastic_widgets(false), menu_style_end(); } ImGui::End(); diff --git a/runtime/ui/sparkle.h b/runtime/ui/sparkle.h new file mode 100644 index 000000000..80617c22b --- /dev/null +++ b/runtime/ui/sparkle.h @@ -0,0 +1,9 @@ +// macOS: the Sparkle updater (runtime/ui/sparkle.mm). Elsewhere these do nothing. +#pragma once +#include "../platform.h" + +// Loads Sparkle (when the app has it) and starts it; check_now: the one background check of this launch. false: none. +bool sparkle_start(bool check_now); +bool sparkle_available(void); +// The update dialog's Update: Sparkle checks now and shows its own window +void sparkle_check_now(void); diff --git a/runtime/ui/sparkle.mm b/runtime/ui/sparkle.mm new file mode 100644 index 000000000..8835da25a --- /dev/null +++ b/runtime/ui/sparkle.mm @@ -0,0 +1,85 @@ +// macOS: Sparkle 2 (MIT, tools/fetch_sparkle.sh) is the app's updater, with its own windows. tools/bundle_macos.sh puts +// Sparkle.framework in the app with the feed of the build's channel (the releases' or the nightlies' appcast, made by +// tools/sparkle_appcast.sh) and the public key the appcast's EdDSA signatures are checked with: the same key as the +// Windows and Linux manifests (runtime/update.cpp). The framework is loaded when the start screen first comes up (a +// build from source has none: no Sparkle, the update dialog offers Download). It checks once, at launch, in the +// background: never again while the app runs. The update dialog's Update button asks it to check now, which shows +// Sparkle's own window with the update. +#include "sparkle.h" + +#if defined(__APPLE__) && !PLAT_IOS +#import + +#include + +// (Sparkle's classes by name: the app doesn't link the framework, it loads it when it's there) +@interface NSObject (LASSparkle) +- (instancetype)initWithStartingUpdater:(BOOL)start updaterDelegate:(id)d userDriverDelegate:(id)u; +- (id)updater; +- (void)checkForUpdates:(id)sender; +- (void)checkForUpdatesInBackground; +- (BOOL)canCheckForUpdates; +@end + +static id g_controller; + +// (test) LARECOMP_SPARKLE_FEED=: Sparkle reads this feed instead of the build's (a local server); with it Sparkle +// also runs in builds whose channel has no Update, and in a bundle without a feed in its Info.plist +@interface LASSparkleDelegate : NSObject +@end +@implementation LASSparkleDelegate +- (NSString *)feedURLStringForUpdater:(id)updater { + (void)updater; + const char *f = getenv("LARECOMP_SPARKLE_FEED"); + return f && *f ? [NSString stringWithUTF8String:f] : nil; +} +// (test) LARECOMP_SPARKLE_AUTOINSTALL=1: an update downloaded in the background installs at once, as Sparkle's +// "Install and Relaunch" does (the app is asked to quit, then started again), with no window to click +- (BOOL)updater:(id)updater willInstallUpdateOnQuit:(id)item immediateInstallationBlock:(void (^)(void))install { + (void)updater, (void)item; + if (!getenv("LARECOMP_SPARKLE_AUTOINSTALL")) return NO; + fprintf(stderr, "[sparkle] installing now (test)\n"); + install(); + return YES; +} +@end +static LASSparkleDelegate *g_delegate; + +bool sparkle_start(bool check_now) { + if (g_controller) return true; + NSBundle *app = [NSBundle mainBundle]; + const char *test_feed = getenv("LARECOMP_SPARKLE_FEED"); + if ((!test_feed && ![app objectForInfoDictionaryKey:@"SUFeedURL"]) || ![app objectForInfoDictionaryKey:@"SUPublicEDKey"]) { + fprintf(stderr, "[sparkle] not in this build (no feed or key)\n"); + return false; + } + NSString *path = [[app privateFrameworksPath] stringByAppendingPathComponent:@"Sparkle.framework"]; + NSBundle *fw = [NSBundle bundleWithPath:path]; + NSError *err = nil; + if (!fw || ![fw loadAndReturnError:&err]) { + fprintf(stderr, "[sparkle] can't load Sparkle.framework: %s\n", err ? err.localizedDescription.UTF8String : "missing"); + return false; + } + Class cls = NSClassFromString(@"SPUStandardUpdaterController"); + if (!cls) { fprintf(stderr, "[sparkle] no SPUStandardUpdaterController\n"); return false; } + // (Sparkle's own schedule is off - SUEnableAutomaticChecks false in Info.plist - so it never asks for permission + // and never checks while the app runs; this is the one check at launch) + g_delegate = [LASSparkleDelegate new]; + g_controller = [[cls alloc] initWithStartingUpdater:YES updaterDelegate:g_delegate userDriverDelegate:nil]; + if (!g_controller) { fprintf(stderr, "[sparkle] the updater didn't start\n"); return false; } + fprintf(stderr, "[sparkle] started, feed %s\n", test_feed ? test_feed : [[app objectForInfoDictionaryKey:@"SUFeedURL"] UTF8String]); + if (check_now) [[g_controller updater] checkForUpdatesInBackground]; + return true; +} + +bool sparkle_available(void) { return g_controller != nil; } + +void sparkle_check_now(void) { + if (!g_controller) return; + [g_controller checkForUpdates:nil]; +} +#else +bool sparkle_start(bool) { return false; } +bool sparkle_available(void) { return false; } +void sparkle_check_now(void) {} +#endif diff --git a/runtime/ui/update_notes.h b/runtime/ui/update_notes.h new file mode 100644 index 000000000..16b4c581d --- /dev/null +++ b/runtime/ui/update_notes.h @@ -0,0 +1,204 @@ +// The update dialog's changelog (runtime/ui/launcher.mm), from the releases JSON the update check already fetched. +// Releases: the notes of every release between this build and the one offered, newest first, without the warning +// banner on top or the "Built from" line and sha256 table at the bottom (release.yml adds those). Nightlies: the +// rounds of the nightly page's "Changes" list newer than this build (nightly.yml writes one per round, each starting +// with a "" line). Test: tools/test_update_tags.cpp (no network). +#pragma once +#include +#include +#include + +#include + +#include +#include + +#include "update_tags.h" + +struct ReleaseInfo { + std::string tag, body; +}; + +// A JSON string starting at s[i] == '"' -> its text (\uXXXX as UTF-8); i ends after the closing quote. false: broken. +static inline bool json_string(const std::string &s, size_t &i, std::string *out) { + if (i >= s.size() || s[i] != '"') return false; + for (i++; i < s.size(); i++) { + char c = s[i]; + if (c == '"') { i++; return true; } + if (c != '\\') { if (out) *out += c; continue; } + if (++i >= s.size()) return false; + char e = s[i]; + if (e == 'u') { + if (i + 4 >= s.size()) return false; + unsigned v = (unsigned)strtoul(s.substr(i + 1, 4).c_str(), NULL, 16); + i += 4; + if (v >= 0xd800 && v < 0xdc00 && i + 6 < s.size() && s[i + 1] == '\\' && s[i + 2] == 'u') { // (a pair) + unsigned lo = (unsigned)strtoul(s.substr(i + 3, 4).c_str(), NULL, 16); + if (lo >= 0xdc00 && lo < 0xe000) v = 0x10000 + ((v - 0xd800) << 10) + (lo - 0xdc00), i += 6; + } + if (!out) continue; + if (v < 0x80) *out += (char)v; + else if (v < 0x800) *out += (char)(0xc0 | v >> 6), *out += (char)(0x80 | (v & 63)); + else if (v < 0x10000) *out += (char)(0xe0 | v >> 12), *out += (char)(0x80 | (v >> 6 & 63)), *out += (char)(0x80 | (v & 63)); + else *out += (char)(0xf0 | v >> 18), *out += (char)(0x80 | (v >> 12 & 63)), *out += (char)(0x80 | (v >> 6 & 63)), *out += (char)(0x80 | (v & 63)); + } else if (out) *out += e == 'n' ? '\n' : e == 't' ? '\t' : e == 'r' ? '\r' : e == 'b' ? '\b' : e == 'f' ? '\f' : e; + } + return false; +} +// Skips one JSON value at s[i] (whitespace first); false: broken +static inline bool json_skip(const std::string &s, size_t &i) { + while (i < s.size() && strchr(" \t\r\n", s[i])) i++; + if (i >= s.size()) return false; + if (s[i] == '"') return json_string(s, i, NULL); + if (s[i] == '{' || s[i] == '[') { + int depth = 0; + for (; i < s.size(); i++) { + if (s[i] == '"') { if (!json_string(s, i, NULL)) return false; i--; continue; } + if (s[i] == '{' || s[i] == '[') depth++; + else if ((s[i] == '}' || s[i] == ']') && --depth == 0) { i++; return true; } + } + return false; + } + while (i < s.size() && !strchr(",}] \t\r\n", s[i])) i++; // (number, true, false, null) + return true; +} +// The releases list (GitHub's /releases: an array of objects): each one's tag_name and body +static inline std::vector json_releases(const std::string &s) { + std::vector out; + size_t i = s.find('['); + if (i == std::string::npos) return out; + for (i++;;) { + while (i < s.size() && strchr(" \t\r\n,", s[i])) i++; + if (i >= s.size() || s[i] != '{') break; + ReleaseInfo r; + for (i++;;) { // the object's keys (nested objects, e.g. author and assets, skipped whole) + while (i < s.size() && strchr(" \t\r\n,", s[i])) i++; + if (i >= s.size()) return out; + if (s[i] == '}') { i++; break; } + std::string key; + if (!json_string(s, i, &key)) return out; + while (i < s.size() && strchr(" \t\r\n:", s[i])) i++; + if ((key == "tag_name" || key == "body") && i < s.size() && s[i] == '"') { + if (!json_string(s, i, key == "tag_name" ? &r.tag : &r.body)) return out; + } else if (!json_skip(s, i)) return out; + } + if (!r.tag.empty()) out.push_back(r); + } + return out; +} + +// A release's notes as the dialog shows them: no warning banner (a leading "> [!WARNING]" quote), nothing from the +// "Built from" line / "---" before it or the sha256 table on, no HTML comments, CRs gone, no blank lines at the ends. +static inline std::string clean_release_notes(std::string b) { + std::string out; + std::vector lines; + for (size_t at = 0; at <= b.size();) { + size_t nl = b.find('\n', at); + if (nl == std::string::npos) nl = b.size(); + std::string l = b.substr(at, nl - at); + if (!l.empty() && l.back() == '\r') l.pop_back(); + lines.push_back(l); + at = nl + 1; + } + size_t i = 0; + while (i < lines.size() && lines[i].empty()) i++; + if (i < lines.size() && !lines[i].compare(0, 4, "> [!")) { // (the banner: the quote's lines) + while (i < lines.size() && !lines[i].compare(0, 1, ">")) i++; + } + size_t end = lines.size(); + for (size_t k = i; k < lines.size(); k++) { + if (!lines[k].compare(0, 11, "Built from ") || !lines[k].compare(0, 9, "| File | ")) { + end = k; + while (end > i && (lines[end - 1].empty() || lines[end - 1] == "---")) end--; + break; + } + } + bool comment = false; + for (size_t k = i; k < end; k++) { + std::string l = lines[k]; + bool had = !l.empty(); + for (size_t c; ;) { // (HTML comments, also across lines) + if (comment) { + size_t e = l.find("-->"); + if (e == std::string::npos) { l.clear(); break; } + l.erase(0, e + 3), comment = false; + } + if ((c = l.find("", c); + if (e == std::string::npos) { l.erase(c), comment = true; break; } + l.erase(c, e + 3 - c); + } + if (l.empty() && had) continue; // (a comment's line) + if (l.empty() && (out.empty() || (out.size() >= 2 && !out.compare(out.size() - 2, 2, "\n\n")))) continue; + out += l + "\n"; + } + while (!out.empty() && out.back() == '\n') out.pop_back(); + return out; +} + +// The changelog from this build (rank own) up to and including the offered release, newest first: each release's +// clean notes under its name ("## Beta 7"). Releases without notes still get their heading. +static inline std::string release_changelog(const std::vector &rel, long long own, const std::string &offered) { + long long top = tag_rank(offered); + std::vector pick; + for (const ReleaseInfo &r : rel) { + long long k = tag_rank(r.tag); + if (k > own && k <= top) pick.push_back(&r); + } + for (size_t a = 1; a < pick.size(); a++) // (newest first) + for (size_t b = a; b > 0 && tag_rank(pick[b]->tag) > tag_rank(pick[b - 1]->tag); b--) std::swap(pick[b], pick[b - 1]); + std::string out; + for (const ReleaseInfo *r : pick) { + std::string notes = clean_release_notes(r->body); + // (the notes' own first heading names the release already: "# LAS Recomp Beta 6 Changelog") + if (notes.compare(0, 2, "# ")) out += "# " + tag_label(r->tag, false) + "\n\n"; + if (!notes.empty()) out += notes + "\n\n"; + } + while (!out.empty() && out.back() == '\n') out.pop_back(); + return out; +} + +// The nightly page's rounds newer than this build's stamp (each "" to the next) +static inline std::string nightly_changelog(const std::vector &rel, long long own_stamp) { + std::string body; + for (const ReleaseInfo &r : rel) + if (r.tag == "nightly") body = r.body; + const std::string key = " X25519 /// +///////////////////////// +void crypto_eddsa_to_x25519(u8 x25519[32], const u8 eddsa[32]) +{ + // (u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x) + // Only converting y to u, the sign of x is ignored. + fe t1, t2; + fe_frombytes(t2, eddsa); + fe_add(t1, fe_one, t2); + fe_sub(t2, fe_one, t2); + fe_invert(t2, t2); + fe_mul(t1, t1, t2); + fe_tobytes(x25519, t1); + WIPE_BUFFER(t1); + WIPE_BUFFER(t2); +} + +void crypto_x25519_to_eddsa(u8 eddsa[32], const u8 x25519[32]) +{ + // (x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1)) + // Only converting u to y, x is assumed positive. + fe t1, t2; + fe_frombytes(t2, x25519); + fe_sub(t1, t2, fe_one); + fe_add(t2, t2, fe_one); + fe_invert(t2, t2); + fe_mul(t1, t1, t2); + fe_tobytes(eddsa, t1); + WIPE_BUFFER(t1); + WIPE_BUFFER(t2); +} + +///////////////////////////////////////////// +/// Dirty ephemeral public key generation /// +///////////////////////////////////////////// + +// Those functions generates a public key, *without* clearing the +// cofactor. Sending that key over the network leaks 3 bits of the +// private key. Use only to generate ephemeral keys that will be hidden +// with crypto_curve_to_hidden(). +// +// The public key is otherwise compatible with crypto_x25519(), which +// properly clears the cofactor. +// +// Note that the distribution of the resulting public keys is almost +// uniform. Flipping the sign of the v coordinate (not provided by this +// function), covers the entire key space almost perfectly, where +// "almost" means a 2^-128 bias (undetectable). This uniformity is +// needed to ensure the proper randomness of the resulting +// representatives (once we apply crypto_curve_to_hidden()). +// +// Recall that Curve25519 has order C = 2^255 + e, with e < 2^128 (not +// to be confused with the prime order of the main subgroup, L, which is +// 8 times less than that). +// +// Generating all points would require us to multiply a point of order C +// (the base point plus any point of order 8) by all scalars from 0 to +// C-1. Clamping limits us to scalars between 2^254 and 2^255 - 1. But +// by negating the resulting point at random, we also cover scalars from +// -2^255 + 1 to -2^254 (which modulo C is congruent to e+1 to 2^254 + e). +// +// In practice: +// - Scalars from 0 to e + 1 are never generated +// - Scalars from 2^255 to 2^255 + e are never generated +// - Scalars from 2^254 + 1 to 2^254 + e are generated twice +// +// Since e < 2^128, detecting this bias requires observing over 2^100 +// representatives from a given source (this will never happen), *and* +// recovering enough of the private key to determine that they do, or do +// not, belong to the biased set (this practically requires solving +// discrete logarithm, which is conjecturally intractable). +// +// In practice, this means the bias is impossible to detect. + +// s + (x*L) % 8*L +// Guaranteed to fit in 256 bits iff s fits in 255 bits. +// L < 2^253 +// x%8 < 2^3 +// L * (x%8) < 2^255 +// s < 2^255 +// s + L * (x%8) < 2^256 +static void add_xl(u8 s[32], u8 x) +{ + u64 mod8 = x & 7; + u64 carry = 0; + FOR (i , 0, 8) { + carry = carry + load32_le(s + 4*i) + L[i] * mod8; + store32_le(s + 4*i, (u32)carry); + carry >>= 32; + } +} + +// "Small" dirty ephemeral key. +// Use if you need to shrink the size of the binary, and can afford to +// slow down by a factor of two (compared to the fast version) +// +// This version works by decoupling the cofactor from the main factor. +// +// - The trimmed scalar determines the main factor +// - The clamped bits of the scalar determine the cofactor. +// +// Cofactor and main factor are combined into a single scalar, which is +// then multiplied by a point of order 8*L (unlike the base point, which +// has prime order). That "dirty" base point is the addition of the +// regular base point (9), and a point of order 8. +void crypto_x25519_dirty_small(u8 public_key[32], const u8 secret_key[32]) +{ + // Base point of order 8*L + // Raw scalar multiplication with it does not clear the cofactor, + // and the resulting public key will reveal 3 bits of the scalar. + // + // The low order component of this base point has been chosen + // to yield the same results as crypto_x25519_dirty_fast(). + static const u8 dirty_base_point[32] = { + 0xd8, 0x86, 0x1a, 0xa2, 0x78, 0x7a, 0xd9, 0x26, + 0x8b, 0x74, 0x74, 0xb6, 0x82, 0xe3, 0xbe, 0xc3, + 0xce, 0x36, 0x9a, 0x1e, 0x5e, 0x31, 0x47, 0xa2, + 0x6d, 0x37, 0x7c, 0xfd, 0x20, 0xb5, 0xdf, 0x75, + }; + // separate the main factor & the cofactor of the scalar + u8 scalar[32]; + crypto_eddsa_trim_scalar(scalar, secret_key); + + // Separate the main factor and the cofactor + // + // The scalar is trimmed, so its cofactor is cleared. The three + // least significant bits however still have a main factor. We must + // remove it for X25519 compatibility. + // + // cofactor = lsb * L (modulo 8*L) + // combined = scalar + cofactor (modulo 8*L) + add_xl(scalar, secret_key[0]); + scalarmult(public_key, scalar, dirty_base_point, 256); + WIPE_BUFFER(scalar); +} + +// Select low order point +// We're computing the [cofactor]lop scalar multiplication, where: +// +// cofactor = tweak & 7. +// lop = (lop_x, lop_y) +// lop_x = sqrt((sqrt(d + 1) + 1) / d) +// lop_y = -lop_x * sqrtm1 +// +// The low order point has order 8. There are 4 such points. We've +// chosen the one whose both coordinates are positive (below p/2). +// The 8 low order points are as follows: +// +// [0]lop = ( 0 , 1 ) +// [1]lop = ( lop_x , lop_y) +// [2]lop = ( sqrt(-1), -0 ) +// [3]lop = ( lop_x , -lop_y) +// [4]lop = (-0 , -1 ) +// [5]lop = (-lop_x , -lop_y) +// [6]lop = (-sqrt(-1), 0 ) +// [7]lop = (-lop_x , lop_y) +// +// The x coordinate is either 0, sqrt(-1), lop_x, or their opposite. +// The y coordinate is either 0, -1 , lop_y, or their opposite. +// The pattern for both is the same, except for a rotation of 2 (modulo 8) +// +// This helper function captures the pattern, and we can use it thus: +// +// select_lop(x, lop_x, sqrtm1, cofactor); +// select_lop(y, lop_y, fe_one, cofactor + 2); +// +// This is faster than an actual scalar multiplication, +// and requires less code than naive constant time look up. +static void select_lop(fe out, const fe x, const fe k, u8 cofactor) +{ + fe tmp; + fe_0(out); + fe_ccopy(out, k , (cofactor >> 1) & 1); // bit 1 + fe_ccopy(out, x , (cofactor >> 0) & 1); // bit 0 + fe_neg (tmp, out); + fe_ccopy(out, tmp, (cofactor >> 2) & 1); // bit 2 + WIPE_BUFFER(tmp); +} + +// "Fast" dirty ephemeral key +// We use this one by default. +// +// This version works by performing a regular scalar multiplication, +// then add a low order point. The scalar multiplication is done in +// Edwards space for more speed (*2 compared to the "small" version). +// The cost is a bigger binary for programs that don't also sign messages. +void crypto_x25519_dirty_fast(u8 public_key[32], const u8 secret_key[32]) +{ + // Compute clean scalar multiplication + u8 scalar[32]; + ge pk; + crypto_eddsa_trim_scalar(scalar, secret_key); + ge_scalarmult_base(&pk, scalar); + + // Compute low order point + fe t1, t2; + select_lop(t1, lop_x, sqrtm1, secret_key[0]); + select_lop(t2, lop_y, fe_one, secret_key[0] + 2); + ge_precomp low_order_point; + fe_add(low_order_point.Yp, t2, t1); + fe_sub(low_order_point.Ym, t2, t1); + fe_mul(low_order_point.T2, t2, t1); + fe_mul(low_order_point.T2, low_order_point.T2, D2); + + // Add low order point to the public key + ge_madd(&pk, &pk, &low_order_point, t1, t2); + + // Convert to Montgomery u coordinate (we ignore the sign) + fe_add(t1, pk.Z, pk.Y); + fe_sub(t2, pk.Z, pk.Y); + fe_invert(t2, t2); + fe_mul(t1, t1, t2); + + fe_tobytes(public_key, t1); + + WIPE_BUFFER(t1); WIPE_CTX(&pk); + WIPE_BUFFER(t2); WIPE_CTX(&low_order_point); + WIPE_BUFFER(scalar); +} + +/////////////////// +/// Elligator 2 /// +/////////////////// +static const fe A = {486662}; + +// Elligator direct map +// +// Computes the point corresponding to a representative, encoded in 32 +// bytes (little Endian). Since positive representatives fits in 254 +// bits, The two most significant bits are ignored. +// +// From the paper: +// w = -A / (fe(1) + non_square * r^2) +// e = chi(w^3 + A*w^2 + w) +// u = e*w - (fe(1)-e)*(A//2) +// v = -e * sqrt(u^3 + A*u^2 + u) +// +// We ignore v because we don't need it for X25519 (the Montgomery +// ladder only uses u). +// +// Note that e is either 0, 1 or -1 +// if e = 0 u = 0 and v = 0 +// if e = 1 u = w +// if e = -1 u = -w - A = w * non_square * r^2 +// +// Let r1 = non_square * r^2 +// Let r2 = 1 + r1 +// Note that r2 cannot be zero, -1/non_square is not a square. +// We can (tediously) verify that: +// w^3 + A*w^2 + w = (A^2*r1 - r2^2) * A / r2^3 +// Therefore: +// chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) +// chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) * 1 +// chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) * chi(r2^6) +// chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3) * r2^6) +// chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * A * r2^3) +// Corollary: +// e = 1 if (A^2*r1 - r2^2) * A * r2^3) is a non-zero square +// e = -1 if (A^2*r1 - r2^2) * A * r2^3) is not a square +// Note that w^3 + A*w^2 + w (and therefore e) can never be zero: +// w^3 + A*w^2 + w = w * (w^2 + A*w + 1) +// w^3 + A*w^2 + w = w * (w^2 + A*w + A^2/4 - A^2/4 + 1) +// w^3 + A*w^2 + w = w * (w + A/2)^2 - A^2/4 + 1) +// which is zero only if: +// w = 0 (impossible) +// (w + A/2)^2 = A^2/4 - 1 (impossible, because A^2/4-1 is not a square) +// +// Let isr = invsqrt((A^2*r1 - r2^2) * A * r2^3) +// isr = sqrt(1 / ((A^2*r1 - r2^2) * A * r2^3)) if e = 1 +// isr = sqrt(sqrt(-1) / ((A^2*r1 - r2^2) * A * r2^3)) if e = -1 +// +// if e = 1 +// let u1 = -A * (A^2*r1 - r2^2) * A * r2^2 * isr^2 +// u1 = w +// u1 = u +// +// if e = -1 +// let ufactor = -non_square * sqrt(-1) * r^2 +// let vfactor = sqrt(ufactor) +// let u2 = -A * (A^2*r1 - r2^2) * A * r2^2 * isr^2 * ufactor +// u2 = w * -1 * -non_square * r^2 +// u2 = w * non_square * r^2 +// u2 = u +void crypto_elligator_map(u8 curve[32], const u8 hidden[32]) +{ + fe r, u, t1, t2, t3; + fe_frombytes_mask(r, hidden, 2); // r is encoded in 254 bits. + fe_sq(r, r); + fe_add(t1, r, r); + fe_add(u, t1, fe_one); + fe_sq (t2, u); + fe_mul(t3, A2, t1); + fe_sub(t3, t3, t2); + fe_mul(t3, t3, A); + fe_mul(t1, t2, u); + fe_mul(t1, t3, t1); + int is_square = invsqrt(t1, t1); + fe_mul(u, r, ufactor); + fe_ccopy(u, fe_one, is_square); + fe_sq (t1, t1); + fe_mul(u, u, A); + fe_mul(u, u, t3); + fe_mul(u, u, t2); + fe_mul(u, u, t1); + fe_neg(u, u); + fe_tobytes(curve, u); + + WIPE_BUFFER(t1); WIPE_BUFFER(r); + WIPE_BUFFER(t2); WIPE_BUFFER(u); + WIPE_BUFFER(t3); +} + +// Elligator inverse map +// +// Computes the representative of a point, if possible. If not, it does +// nothing and returns -1. Note that the success of the operation +// depends only on the point (more precisely its u coordinate). The +// tweak parameter is used only upon success +// +// The tweak should be a random byte. Beyond that, its contents are an +// implementation detail. Currently, the tweak comprises: +// - Bit 1 : sign of the v coordinate (0 if positive, 1 if negative) +// - Bit 2-5: not used +// - Bits 6-7: random padding +// +// From the paper: +// Let sq = -non_square * u * (u+A) +// if sq is not a square, or u = -A, there is no mapping +// Assuming there is a mapping: +// if v is positive: r = sqrt(-u / (non_square * (u+A))) +// if v is negative: r = sqrt(-(u+A) / (non_square * u )) +// +// We compute isr = invsqrt(-non_square * u * (u+A)) +// if it wasn't a square, abort. +// else, isr = sqrt(-1 / (non_square * u * (u+A)) +// +// If v is positive, we return isr * u: +// isr * u = sqrt(-1 / (non_square * u * (u+A)) * u +// isr * u = sqrt(-u / (non_square * (u+A)) +// +// If v is negative, we return isr * (u+A): +// isr * (u+A) = sqrt(-1 / (non_square * u * (u+A)) * (u+A) +// isr * (u+A) = sqrt(-(u+A) / (non_square * u) +int crypto_elligator_rev(u8 hidden[32], const u8 public_key[32], u8 tweak) +{ + fe t1, t2, t3; + fe_frombytes(t1, public_key); // t1 = u + + fe_add(t2, t1, A); // t2 = u + A + fe_mul(t3, t1, t2); + fe_mul_small(t3, t3, -2); + int is_square = invsqrt(t3, t3); // t3 = sqrt(-1 / non_square * u * (u+A)) + if (is_square) { + // The only variable time bit. This ultimately reveals how many + // tries it took us to find a representable key. + // This does not affect security as long as we try keys at random. + + fe_ccopy (t1, t2, tweak & 1); // multiply by u if v is positive, + fe_mul (t3, t1, t3); // multiply by u+A otherwise + fe_mul_small(t1, t3, 2); + fe_neg (t2, t3); + fe_ccopy (t3, t2, fe_isodd(t1)); + fe_tobytes(hidden, t3); + + // Pad with two random bits + hidden[31] |= tweak & 0xc0; + } + + WIPE_BUFFER(t1); + WIPE_BUFFER(t2); + WIPE_BUFFER(t3); + return is_square - 1; +} + +void crypto_elligator_key_pair(u8 hidden[32], u8 secret_key[32], u8 seed[32]) +{ + u8 pk [32]; // public key + u8 buf[64]; // seed + representative + COPY(buf + 32, seed, 32); + do { + crypto_chacha20_djb(buf, 0, 64, buf+32, zero, 0); + crypto_x25519_dirty_fast(pk, buf); // or the "small" version + } while(crypto_elligator_rev(buf+32, pk, buf[32])); + // Note that the return value of crypto_elligator_rev() is + // independent from its tweak parameter. + // Therefore, buf[32] is not actually reused. Either we loop one + // more time and buf[32] is used for the new seed, or we succeeded, + // and buf[32] becomes the tweak parameter. + + crypto_wipe(seed, 32); + COPY(hidden , buf + 32, 32); + COPY(secret_key, buf , 32); + WIPE_BUFFER(buf); + WIPE_BUFFER(pk); +} + +/////////////////////// +/// Scalar division /// +/////////////////////// + +// Montgomery reduction. +// Divides x by (2^256), and reduces the result modulo L +// +// Precondition: +// x < L * 2^256 +// Constants: +// r = 2^256 (makes division by r trivial) +// k = (r * (1/r) - 1) // L (1/r is computed modulo L ) +// Algorithm: +// s = (x * k) % r +// t = x + s*L (t is always a multiple of r) +// u = (t/r) % L (u is always below 2*L, conditional subtraction is enough) +static void redc(u32 u[8], u32 x[16]) +{ + static const u32 k[8] = { + 0x12547e1b, 0xd2b51da3, 0xfdba84ff, 0xb1a206f2, + 0xffa36bea, 0x14e75438, 0x6fe91836, 0x9db6c6f2, + }; + + // s = x * k (modulo 2^256) + // This is cheaper than the full multiplication. + u32 s[8] = {0}; + FOR (i, 0, 8) { + u64 carry = 0; + FOR (j, 0, 8-i) { + carry += s[i+j] + (u64)x[i] * k[j]; + s[i+j] = (u32)carry; + carry >>= 32; + } + } + u32 t[16] = {0}; + multiply(t, s, L); + + // t = t + x + u64 carry = 0; + FOR (i, 0, 16) { + carry += (u64)t[i] + x[i]; + t[i] = (u32)carry; + carry >>= 32; + } + + // u = (t / 2^256) % L + // Note that t / 2^256 is always below 2*L, + // So a constant time conditional subtraction is enough + remove_l(u, t+8); + + WIPE_BUFFER(s); + WIPE_BUFFER(t); +} + +void crypto_x25519_inverse(u8 blind_salt [32], const u8 private_key[32], + const u8 curve_point[32]) +{ + static const u8 Lm2[32] = { // L - 2 + 0xeb, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, + }; + // 1 in Montgomery form + u32 m_inv [8] = { + 0x8d98951d, 0xd6ec3174, 0x737dcf70, 0xc6ef5bf4, + 0xfffffffe, 0xffffffff, 0xffffffff, 0x0fffffff, + }; + + u8 scalar[32]; + crypto_eddsa_trim_scalar(scalar, private_key); + + // Convert the scalar in Montgomery form + // m_scl = scalar * 2^256 (modulo L) + u32 m_scl[8]; + { + u32 tmp[16]; + ZERO(tmp, 8); + load32_le_buf(tmp+8, scalar, 8); + mod_l(scalar, tmp); + load32_le_buf(m_scl, scalar, 8); + WIPE_BUFFER(tmp); // Wipe ASAP to save stack space + } + + // Compute the inverse + u32 product[16]; + for (int i = 252; i >= 0; i--) { + ZERO(product, 16); + multiply(product, m_inv, m_inv); + redc(m_inv, product); + if (scalar_bit(Lm2, i)) { + ZERO(product, 16); + multiply(product, m_inv, m_scl); + redc(m_inv, product); + } + } + // Convert the inverse *out* of Montgomery form + // scalar = m_inv / 2^256 (modulo L) + COPY(product, m_inv, 8); + ZERO(product + 8, 8); + redc(m_inv, product); + store32_le_buf(scalar, m_inv, 8); // the *inverse* of the scalar + + // Clear the cofactor of scalar: + // cleared = scalar * (3*L + 1) (modulo 8*L) + // cleared = scalar + scalar * 3 * L (modulo 8*L) + // Note that (scalar * 3) is reduced modulo 8, so we only need the + // first byte. + add_xl(scalar, scalar[0] * 3); + + // Recall that 8*L < 2^256. However it is also very close to + // 2^255. If we spanned the ladder over 255 bits, random tests + // wouldn't catch the off-by-one error. + scalarmult(blind_salt, scalar, curve_point, 256); + + WIPE_BUFFER(scalar); WIPE_BUFFER(m_scl); + WIPE_BUFFER(product); WIPE_BUFFER(m_inv); +} + +//////////////////////////////// +/// Authenticated encryption /// +//////////////////////////////// +static void lock_auth(u8 mac[16], const u8 auth_key[32], + const u8 *ad , size_t ad_size, + const u8 *cipher_text, size_t text_size) +{ + u8 sizes[16]; // Not secret, not wiped + store64_le(sizes + 0, ad_size); + store64_le(sizes + 8, text_size); + crypto_poly1305_ctx poly_ctx; // auto wiped... + crypto_poly1305_init (&poly_ctx, auth_key); + crypto_poly1305_update(&poly_ctx, ad , ad_size); + crypto_poly1305_update(&poly_ctx, zero , gap(ad_size, 16)); + crypto_poly1305_update(&poly_ctx, cipher_text, text_size); + crypto_poly1305_update(&poly_ctx, zero , gap(text_size, 16)); + crypto_poly1305_update(&poly_ctx, sizes , 16); + crypto_poly1305_final (&poly_ctx, mac); // ...here +} + +void crypto_aead_init_x(crypto_aead_ctx *ctx, + u8 const key[32], const u8 nonce[24]) +{ + crypto_chacha20_h(ctx->key, key, nonce); + COPY(ctx->nonce, nonce + 16, 8); + ctx->counter = 0; +} + +void crypto_aead_init_djb(crypto_aead_ctx *ctx, + const u8 key[32], const u8 nonce[8]) +{ + COPY(ctx->key , key , 32); + COPY(ctx->nonce, nonce, 8); + ctx->counter = 0; +} + +void crypto_aead_init_ietf(crypto_aead_ctx *ctx, + const u8 key[32], const u8 nonce[12]) +{ + COPY(ctx->key , key , 32); + COPY(ctx->nonce, nonce + 4, 8); + ctx->counter = (u64)load32_le(nonce) << 32; +} + +void crypto_aead_write(crypto_aead_ctx *ctx, u8 *cipher_text, u8 mac[16], + const u8 *ad, size_t ad_size, + const u8 *plain_text, size_t text_size) +{ + u8 auth_key[64]; // the last 32 bytes are used for rekeying. + crypto_chacha20_djb(auth_key, 0, 64, ctx->key, ctx->nonce, ctx->counter); + crypto_chacha20_djb(cipher_text, plain_text, text_size, + ctx->key, ctx->nonce, ctx->counter + 1); + lock_auth(mac, auth_key, ad, ad_size, cipher_text, text_size); + COPY(ctx->key, auth_key + 32, 32); + WIPE_BUFFER(auth_key); +} + +int crypto_aead_read(crypto_aead_ctx *ctx, u8 *plain_text, const u8 mac[16], + const u8 *ad, size_t ad_size, + const u8 *cipher_text, size_t text_size) +{ + u8 auth_key[64]; // the last 32 bytes are used for rekeying. + u8 real_mac[16]; + crypto_chacha20_djb(auth_key, 0, 64, ctx->key, ctx->nonce, ctx->counter); + lock_auth(real_mac, auth_key, ad, ad_size, cipher_text, text_size); + int mismatch = crypto_verify16(mac, real_mac); + if (!mismatch) { + crypto_chacha20_djb(plain_text, cipher_text, text_size, + ctx->key, ctx->nonce, ctx->counter + 1); + COPY(ctx->key, auth_key + 32, 32); + } + WIPE_BUFFER(auth_key); + WIPE_BUFFER(real_mac); + return mismatch; +} + +void crypto_aead_lock(u8 *cipher_text, u8 mac[16], const u8 key[32], + const u8 nonce[24], const u8 *ad, size_t ad_size, + const u8 *plain_text, size_t text_size) +{ + crypto_aead_ctx ctx; + crypto_aead_init_x(&ctx, key, nonce); + crypto_aead_write(&ctx, cipher_text, mac, ad, ad_size, + plain_text, text_size); + crypto_wipe(&ctx, sizeof(ctx)); +} + +int crypto_aead_unlock(u8 *plain_text, const u8 mac[16], const u8 key[32], + const u8 nonce[24], const u8 *ad, size_t ad_size, + const u8 *cipher_text, size_t text_size) +{ + crypto_aead_ctx ctx; + crypto_aead_init_x(&ctx, key, nonce); + int mismatch = crypto_aead_read(&ctx, plain_text, mac, ad, ad_size, + cipher_text, text_size); + crypto_wipe(&ctx, sizeof(ctx)); + return mismatch; +} + +#ifdef MONOCYPHER_CPP_NAMESPACE +} +#endif diff --git a/third_party/monocypher/monocypher.h b/third_party/monocypher/monocypher.h new file mode 100644 index 000000000..765a07ff3 --- /dev/null +++ b/third_party/monocypher/monocypher.h @@ -0,0 +1,321 @@ +// Monocypher version 4.0.2 +// +// This file is dual-licensed. Choose whichever licence you want from +// the two licences listed below. +// +// The first licence is a regular 2-clause BSD licence. The second licence +// is the CC-0 from Creative Commons. It is intended to release Monocypher +// to the public domain. The BSD licence serves as a fallback option. +// +// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0 +// +// ------------------------------------------------------------------------ +// +// Copyright (c) 2017-2019, Loup Vaillant +// All rights reserved. +// +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// 1. Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the +// distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// ------------------------------------------------------------------------ +// +// Written in 2017-2019 by Loup Vaillant +// +// To the extent possible under law, the author(s) have dedicated all copyright +// and related neighboring rights to this software to the public domain +// worldwide. This software is distributed without any warranty. +// +// You should have received a copy of the CC0 Public Domain Dedication along +// with this software. If not, see +// + +#ifndef MONOCYPHER_H +#define MONOCYPHER_H + +#include +#include + +#ifdef MONOCYPHER_CPP_NAMESPACE +namespace MONOCYPHER_CPP_NAMESPACE { +#elif defined(__cplusplus) +extern "C" { +#endif + +// Constant time comparisons +// ------------------------- + +// Return 0 if a and b are equal, -1 otherwise +int crypto_verify16(const uint8_t a[16], const uint8_t b[16]); +int crypto_verify32(const uint8_t a[32], const uint8_t b[32]); +int crypto_verify64(const uint8_t a[64], const uint8_t b[64]); + + +// Erase sensitive data +// -------------------- +void crypto_wipe(void *secret, size_t size); + + +// Authenticated encryption +// ------------------------ +void crypto_aead_lock(uint8_t *cipher_text, + uint8_t mac [16], + const uint8_t key [32], + const uint8_t nonce[24], + const uint8_t *ad, size_t ad_size, + const uint8_t *plain_text, size_t text_size); +int crypto_aead_unlock(uint8_t *plain_text, + const uint8_t mac [16], + const uint8_t key [32], + const uint8_t nonce[24], + const uint8_t *ad, size_t ad_size, + const uint8_t *cipher_text, size_t text_size); + +// Authenticated stream +// -------------------- +typedef struct { + uint64_t counter; + uint8_t key[32]; + uint8_t nonce[8]; +} crypto_aead_ctx; + +void crypto_aead_init_x(crypto_aead_ctx *ctx, + const uint8_t key[32], const uint8_t nonce[24]); +void crypto_aead_init_djb(crypto_aead_ctx *ctx, + const uint8_t key[32], const uint8_t nonce[8]); +void crypto_aead_init_ietf(crypto_aead_ctx *ctx, + const uint8_t key[32], const uint8_t nonce[12]); + +void crypto_aead_write(crypto_aead_ctx *ctx, + uint8_t *cipher_text, + uint8_t mac[16], + const uint8_t *ad , size_t ad_size, + const uint8_t *plain_text, size_t text_size); +int crypto_aead_read(crypto_aead_ctx *ctx, + uint8_t *plain_text, + const uint8_t mac[16], + const uint8_t *ad , size_t ad_size, + const uint8_t *cipher_text, size_t text_size); + + +// General purpose hash (BLAKE2b) +// ------------------------------ + +// Direct interface +void crypto_blake2b(uint8_t *hash, size_t hash_size, + const uint8_t *message, size_t message_size); + +void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size, + const uint8_t *key, size_t key_size, + const uint8_t *message, size_t message_size); + +// Incremental interface +typedef struct { + // Do not rely on the size or contents of this type, + // for they may change without notice. + uint64_t hash[8]; + uint64_t input_offset[2]; + uint64_t input[16]; + size_t input_idx; + size_t hash_size; +} crypto_blake2b_ctx; + +void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size); +void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size, + const uint8_t *key, size_t key_size); +void crypto_blake2b_update(crypto_blake2b_ctx *ctx, + const uint8_t *message, size_t message_size); +void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash); + + +// Password key derivation (Argon2) +// -------------------------------- +#define CRYPTO_ARGON2_D 0 +#define CRYPTO_ARGON2_I 1 +#define CRYPTO_ARGON2_ID 2 + +typedef struct { + uint32_t algorithm; // Argon2d, Argon2i, Argon2id + uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes + uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i) + uint32_t nb_lanes; // parallelism level (single threaded anyway) +} crypto_argon2_config; + +typedef struct { + const uint8_t *pass; + const uint8_t *salt; + uint32_t pass_size; + uint32_t salt_size; // 16 bytes recommended +} crypto_argon2_inputs; + +typedef struct { + const uint8_t *key; // may be NULL if no key + const uint8_t *ad; // may be NULL if no additional data + uint32_t key_size; // 0 if no key (32 bytes recommended otherwise) + uint32_t ad_size; // 0 if no additional data +} crypto_argon2_extras; + +extern const crypto_argon2_extras crypto_argon2_no_extras; + +void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area, + crypto_argon2_config config, + crypto_argon2_inputs inputs, + crypto_argon2_extras extras); + + +// Key exchange (X-25519) +// ---------------------- + +// Shared secrets are not quite random. +// Hash them to derive an actual shared key. +void crypto_x25519_public_key(uint8_t public_key[32], + const uint8_t secret_key[32]); +void crypto_x25519(uint8_t raw_shared_secret[32], + const uint8_t your_secret_key [32], + const uint8_t their_public_key [32]); + +// Conversion to EdDSA +void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]); + +// scalar "division" +// Used for OPRF. Be aware that exponential blinding is less secure +// than Diffie-Hellman key exchange. +void crypto_x25519_inverse(uint8_t blind_salt [32], + const uint8_t private_key[32], + const uint8_t curve_point[32]); + +// "Dirty" versions of x25519_public_key(). +// Use with crypto_elligator_rev(). +// Leaks 3 bits of the private key. +void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]); +void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]); + + +// Signatures +// ---------- + +// EdDSA with curve25519 + BLAKE2b +void crypto_eddsa_key_pair(uint8_t secret_key[64], + uint8_t public_key[32], + uint8_t seed[32]); +void crypto_eddsa_sign(uint8_t signature [64], + const uint8_t secret_key[64], + const uint8_t *message, size_t message_size); +int crypto_eddsa_check(const uint8_t signature [64], + const uint8_t public_key[32], + const uint8_t *message, size_t message_size); + +// Conversion to X25519 +void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]); + +// EdDSA building blocks +void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]); +void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]); +void crypto_eddsa_mul_add(uint8_t r[32], + const uint8_t a[32], + const uint8_t b[32], + const uint8_t c[32]); +void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]); +int crypto_eddsa_check_equation(const uint8_t signature[64], + const uint8_t public_key[32], + const uint8_t h_ram[32]); + + +// Chacha20 +// -------- + +// Specialised hash. +// Used to hash X25519 shared secrets. +void crypto_chacha20_h(uint8_t out[32], + const uint8_t key[32], + const uint8_t in [16]); + +// Unauthenticated stream cipher. +// Don't forget to add authentication. +uint64_t crypto_chacha20_djb(uint8_t *cipher_text, + const uint8_t *plain_text, + size_t text_size, + const uint8_t key[32], + const uint8_t nonce[8], + uint64_t ctr); +uint32_t crypto_chacha20_ietf(uint8_t *cipher_text, + const uint8_t *plain_text, + size_t text_size, + const uint8_t key[32], + const uint8_t nonce[12], + uint32_t ctr); +uint64_t crypto_chacha20_x(uint8_t *cipher_text, + const uint8_t *plain_text, + size_t text_size, + const uint8_t key[32], + const uint8_t nonce[24], + uint64_t ctr); + + +// Poly 1305 +// --------- + +// This is a *one time* authenticator. +// Disclosing the mac reveals the key. +// See crypto_lock() on how to use it properly. + +// Direct interface +void crypto_poly1305(uint8_t mac[16], + const uint8_t *message, size_t message_size, + const uint8_t key[32]); + +// Incremental interface +typedef struct { + // Do not rely on the size or contents of this type, + // for they may change without notice. + uint8_t c[16]; // chunk of the message + size_t c_idx; // How many bytes are there in the chunk. + uint32_t r [4]; // constant multiplier (from the secret key) + uint32_t pad[4]; // random number added at the end (from the secret key) + uint32_t h [5]; // accumulated hash +} crypto_poly1305_ctx; + +void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]); +void crypto_poly1305_update(crypto_poly1305_ctx *ctx, + const uint8_t *message, size_t message_size); +void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]); + + +// Elligator 2 +// ----------- + +// Elligator mappings proper +void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]); +int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32], + uint8_t tweak); + +// Easy to use key pair generation +void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32], + uint8_t seed[32]); + +#ifdef __cplusplus +} +#endif + +#endif // MONOCYPHER_H diff --git a/tools/bundle_macos.sh b/tools/bundle_macos.sh index 9af8384a6..fdd1e5548 100755 --- a/tools/bundle_macos.sh +++ b/tools/bundle_macos.sh @@ -4,7 +4,8 @@ # paths rewritten to @rpath; Contents/Resources: the icon, the seed generator helper (build/helper/rando-gen, when # built: runtime/ui/rando_tab.mm generator_cmd) and what the start screen builds the game code with on the player's # machine (runtime/gamebuild.c): the generator helper (build/helper/lasgen), the compiler (build/zig, tools/fetch_zig.sh) -# and the headers the code is compiled against (gamelib-src/). Ad-hoc signed. No game files, game code or fonts: the +# and the headers the code is compiled against (gamelib-src/); Contents/Frameworks/Sparkle.framework: the updater +# (tools/fetch_sparkle.sh, runtime/ui/sparkle.mm), with the feed of this build's channel. Ad-hoc signed. No game files, game code or fonts: the # app keeps those in ~/Library/Application Support/ (runtime/paths.h). # MACOS_MIN=: LSMinimumSystemVersion, and every Mach-O inside must be built for it or older (else it fails and # lists them); without it the newest minimum found inside is used. @@ -38,6 +39,27 @@ mkdir -p "$C/MacOS" "$C/Frameworks" "$C/Resources" cp "$EXE" "$C/MacOS/lasrecomp" strip -S "$C/MacOS/lasrecomp" # (debug-map entries only: the symbols crash reports name functions by stay) if [ -x "$HELPER/rando-gen" ]; then cp -R "$HELPER" "$C/Resources/rando-gen"; else echo "note: no $HELPER (make rando-helper)"; fi +# Sparkle: the framework as released (its own ad-hoc signatures kept: ditto), the feed of this build's channel, and +# the update key's public half (runtime/update.cpp UPDATE_PUBLIC_KEY, hex there, base64 here). Until the key exists +# (all zeros) the app gets no feed and Sparkle stays off. +tools/fetch_sparkle.sh +ditto build/sparkle/Sparkle.framework "$C/Frameworks/Sparkle.framework" +cp build/sparkle/LICENSE "$C/Resources/Sparkle-LICENSE.txt" +UPDATE_KEY=$(sed -n 's/^#define UPDATE_PUBLIC_KEY "\([0-9a-f]\{64\}\)"/\1/p' runtime/update.cpp) +[ -n "$UPDATE_KEY" ] || { echo "no UPDATE_PUBLIC_KEY in runtime/update.cpp"; exit 1; } +SPARKLE_KEYS= +if [ "$UPDATE_KEY" != "$(printf '%064d' 0)" ]; then + case $TAG in + nightly-*) FEED=https://github.com/peiikai/LASRecomp/releases/download/nightly/appcast-macOS.xml ;; + *) FEED=https://github.com/peiikai/LASRecomp/releases/latest/download/appcast-macOS.xml ;; + esac + ED_KEY=$(printf '%s' "$UPDATE_KEY" | xxd -r -p | base64) + # (SUEnableAutomaticChecks false: Sparkle's own schedule off and no permission prompt; the app checks once at launch) + SPARKLE_KEYS="SUFeedURL$FEED + SUPublicEDKey$ED_KEY + SUEnableAutomaticChecks + SUVerifyUpdateBeforeExtraction" +fi cp -R "$GEN_HELPER" "$C/Resources/lasgen" cp -R "$ZIG" "$C/Resources/zig" mkdir "$C/Resources/gamelib-src" @@ -127,6 +149,7 @@ cat > "$C/Info.plist" <LSArchitecturePriorityarm64 LSApplicationCategoryTypepublic.app-category.games NSHighResolutionCapable + $SPARKLE_KEYS EOF @@ -134,9 +157,12 @@ printf 'APPL????' > "$C/PkgInfo" # ---- ad-hoc signature (Apple Silicon refuses to run unsigned code; the rewrites above invalidated the originals): # every Mach-O inside first, then the bundle -while read -r f; do [ "$f" = "$C/MacOS/lasrecomp" ] || codesign -f -s - "$f" 2>/dev/null; done < <(machos) +# (Sparkle.framework keeps its own signatures, made inside-out: re-signing its pieces here would leave its seal stale) +while read -r f; do + case $f in "$C/MacOS/lasrecomp"|"$C/Frameworks/Sparkle.framework/"*) ;; *) codesign -f -s - "$f" 2>/dev/null ;; esac +done < <(machos) codesign -f -s - "$APP" -codesign --verify --strict "$APP" +codesign --verify --deep --strict "$APP" if find "$APP" -name 'lasgame*' | grep . >/dev/null; then echo "game code inside $APP"; exit 1; fi # no path of this machine inside (configure lines, debug paths, frozen Python's file names). Not on CI: its paths # name no one, and prebuilt wheels hold the CI runner's own (/Users/runner/...) diff --git a/tools/bundle_version.sh b/tools/bundle_version.sh index 4b3dc2cda..3bc191aeb 100755 --- a/tools/bundle_version.sh +++ b/tools/bundle_version.sh @@ -4,8 +4,9 @@ # Short version (Finder, About): the semantic version under the tag - beta_ -> 0..0, beta_. -> 0.., # v0.6.0 -> 0.6.0, v1.2.3 -> 1.2.3. Build number: one number that always goes up, major*10000 + minor*100 + patch # (0.5.3 -> 503, 0.6.0 -> 600, 1.0.0 -> 10000). macOS compares build numbers part by part, so 503 is above Beta 5.2's -# 0.9.5.2. A test build (test-, previewing ) or a nightly (nightly--) is 0.0.1 to players and .1 inside: -# above the release before it, below the one it previews. +# 0.9.5.2. A test build (test-, previewing ) is 0.0.1 to players and .1 inside: +# above the release before it, below the one it previews. A nightly (nightly--) is the same with its +# stamp after it, .1., so each round is newer than the last (Sparkle compares these). set -eu tag=$1 semver() { @@ -22,7 +23,14 @@ build() { { print $1 * 10000 + $2 * 100 + $3 }' } case "$tag" in - test-*|nightly-*) + nightly-*) + [ $# -ge 2 ] || { echo "bundle_version.sh: a nightly needs the tag it previews" >&2; exit 1; } + stamp=${tag#nightly-}; stamp=${stamp%%-*} + case $stamp in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ;; + *) echo "bundle_version.sh: bad nightly stamp in $tag" >&2; exit 1 ;; esac + b=$(build "$(semver "$2")") + echo "0.0.1 $((b - 1)).1.$stamp" ;; + test-*) [ $# -ge 2 ] || { echo "bundle_version.sh: a test build needs the tag it previews" >&2; exit 1; } b=$(build "$(semver "$2")") echo "0.0.1 $((b - 1)).1" ;; diff --git a/tools/fetch_sparkle.sh b/tools/fetch_sparkle.sh new file mode 100755 index 000000000..ee5103e0f --- /dev/null +++ b/tools/fetch_sparkle.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Sparkle 2 (MIT: https://sparkle-project.org), the macOS app's updater (runtime/ui/sparkle.mm), into +# build/sparkle/ (Sparkle.framework, LICENSE). tools/bundle_macos.sh puts the framework in the app. Fetches the pinned +# release and checks its hash; needs network once, as tools/fetch_zig.sh does. +set -euo pipefail +cd "$(dirname "$0")/.." +VER=2.10.1 +SHA=1a856d2a9edb8e628de88121d7be04cf0e2167f8641650b78fca00b14244cf27 +DL=build/sparkle-dl +OUT=build/sparkle +[ -f "$OUT/.ok-$VER" ] && exit 0 +mkdir -p "$DL" +TAR="$DL/Sparkle-$VER.tar.xz" +if [ ! -f "$TAR" ]; then # (a .part file until it's whole) + curl -fsSL --proto '=https' -o "$TAR.part" "https://github.com/sparkle-project/Sparkle/releases/download/$VER/Sparkle-$VER.tar.xz" + mv "$TAR.part" "$TAR" +fi +GOT=$(shasum -a 256 < "$TAR" | cut -d' ' -f1) +[ "$GOT" = "$SHA" ] || { echo "Sparkle-$VER.tar.xz: sha256 $GOT, expected $SHA"; rm -f "$TAR"; exit 1; } +rm -rf "$OUT" "$DL/x" && mkdir -p "$DL/x" "$OUT" +tar -xJf "$TAR" -C "$DL/x" Sparkle.framework LICENSE +ditto "$DL/x/Sparkle.framework" "$OUT/Sparkle.framework" # (ditto: links and signatures as they are) +cp "$DL/x/LICENSE" "$OUT/LICENSE" +touch "$OUT/.ok-$VER" +echo "build/sparkle: Sparkle $VER" diff --git a/tools/nightly_publish.sh b/tools/nightly_publish.sh index 343ce3649..e52478244 100755 --- a/tools/nightly_publish.sh +++ b/tools/nightly_publish.sh @@ -2,6 +2,10 @@ # Puts one platform's nightly file on the rolling "nightly" pre-release (.github/workflows/nightly.yml), replacing that # platform's older nightly file. Each platform's job calls it as soon as its own build is done, so a platform that is # slow or fails never holds the others back. +# The platform's update manifest (build/dist/update-.txt, tools/update_manifest.sh) or Sparkle feed +# (appcast-macOS.xml, tools/sparkle_appcast.sh) goes up right after the file and before the older file goes, so it +# always names a file that is there; without one (no signing key) the old one is removed, so Update never fetches a +# file of an older round. # Usage: tools/nightly_publish.sh (GH_TOKEN, GH_REPO set) set -euo pipefail f=$1 @@ -10,6 +14,11 @@ suffix=$2 new=$(basename "$f" | tr ' ' .) # (GitHub stores "LAS Recomp-..." as "LAS.Recomp-...") case $new in LAS.Recomp-nightly-*-"$suffix") ;; *) echo "nightly_publish.sh: $new is not a nightly $suffix file" >&2; exit 1 ;; esac gh release upload nightly "$f" --clobber +platform=${suffix%.*} # (windows.zip -> windows, linux-x86_64.AppImage -> linux-x86_64) +for m in "update-$platform.txt" "appcast-$platform.xml"; do + if [ -f "$(dirname "$f")/$m" ]; then gh release upload nightly "$(dirname "$f")/$m" --clobber + else gh release delete-asset nightly "$m" -y 2>/dev/null || true; fi +done # (this platform's older nightly files out, after the new one is up: the page never lacks this platform) gh release view nightly --json assets -q '.assets[].name' | while read -r old; do case $old in LAS.Recomp-nightly-*-"$suffix") [ "$old" = "$new" ] || gh release delete-asset nightly "$old" -y ;; esac diff --git a/tools/sparkle_appcast.sh b/tools/sparkle_appcast.sh new file mode 100755 index 000000000..0c162c568 --- /dev/null +++ b/tools/sparkle_appcast.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# The macOS update feed for Sparkle (runtime/ui/sparkle.mm): build/dist/appcast-macOS.xml with one item, this build's +# .dmg, signed with the UPDATE_SIGNING_KEY secret (EdDSA, the same key as the Windows and Linux manifests: +# tools/update_sign.cpp). release.yml and nightly.yml put it next to the .dmg; the apps read it from +# releases/latest/download/ (releases) or releases/download/nightly/ (nightlies), tools/bundle_macos.sh. Without the +# key it only warns: no feed, and the app's Download still works. +# Usage: tools/sparkle_appcast.sh +set -euo pipefail +cd "$(dirname "$0")/.." +[ $# = 2 ] || { echo "usage: tools/sparkle_appcast.sh " >&2; exit 2; } +REL=$1 +DMG=$2 +OUT=build/dist/appcast-macOS.xml +rm -f "$OUT" +if [ -z "${UPDATE_SIGNING_KEY:-}" ]; then + echo "::warning::UPDATE_SIGNING_KEY is not set: no Sparkle feed for macOS (players there update with Download)" + exit 0 +fi +TAG=$(sed -n 's/^#define LARECOMP_VERSION "\(.*\)"/\1/p' runtime/ui/launcher.mm) +TEST_OF=$(sed -n 's/^#define LARECOMP_TEST_OF "\(.*\)"/\1/p' runtime/ui/launcher.mm) +read -r SHORT BUILD < "$OUT.part" < + + + LAS Recomp + + $TITLE + $(LC_ALL=C date -u '+%a, %d %b %Y %H:%M:%S +0000') + $BUILD + $SHORT + 15.0 + https://github.com/peiikai/LASRecomp/releases/tag/$REL + + + + +EOF +mv "$OUT.part" "$OUT" +cat "$OUT" diff --git a/tools/test_update.cpp b/tools/test_update.cpp new file mode 100644 index 000000000..5991f2a7f --- /dev/null +++ b/tools/test_update.cpp @@ -0,0 +1,235 @@ +// The self-updater's core (runtime/update_core.h): the signed manifest, the files it may write, the swap, its journal +// and the rollback, including a swap killed halfway and a folder that can't be written. No game, no network. +// Build + run (from the repo root): +// c++ -std=c++17 -O1 -Iruntime tools/test_update.cpp -o build/test-update && build/test-update +#include +#include + +#include "update_core.h" +// (Monocypher's C compiled into this file as C++, so one command builds it on every platform) +#include "../third_party/monocypher/monocypher.c" +#undef FOR // (each file defines its own) +#include "../third_party/monocypher/monocypher-ed25519.c" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint8_t g_sk[64], g_pk[32], g_other_pk[32]; +static std::string sign(const std::string &t) { + uint8_t sig[64]; + crypto_ed25519_sign(sig, g_sk, (const uint8_t *)t.data(), t.size()); + std::string s = t + "signature "; + char b[3]; + for (uint8_t c : sig) snprintf(b, 3, "%02x", c), s += b; + return s + "\n"; +} +static std::string sha_of(const std::string &data) { + CacheSha256 h; + h.add(data.data(), data.size()); + return h.finish(); +} +static void write(const std::string &p, const std::string &data) { + upd_mkdirs_for(p); + FILE *f = fopen(p.c_str(), "wb"); + fwrite(data.data(), 1, data.size(), f); + fclose(f); +} +static std::string read(const std::string &p) { + std::string s; + FILE *f = fopen(p.c_str(), "rb"); + if (!f) return ""; + char b[4096]; + size_t n; + while ((n = fread(b, 1, sizeof b, f)) > 0) s.append(b, n); + fclose(f); + return s; +} + +static void manifest_tests() { + UpdManifest m; + m.version = "nightly-202611161000-abc1234", m.platform = "windows"; + m.asset = "LAS.Recomp-nightly-202611161000-abc1234-windows.zip", m.asset_size = 5, m.asset_sha256 = sha_of("hello"); + m.files = {{"lasrecomp.exe", 3, sha_of("new")}, {"zig/lib/a b.h", 1, sha_of("x")}}; + std::string good = sign(upd_manifest_text(m)), why; + UpdManifest r; + CHECK(upd_parse_manifest(good, g_pk, &r, &why), "good manifest: %s", why.c_str()); + CHECK(r.version == m.version && r.platform == "windows" && r.asset == m.asset && r.asset_size == 5 && + r.files.size() == 2 && r.files[1].path == "zig/lib/a b.h", "fields read back"); + CHECK(!upd_parse_manifest(good, g_other_pk, &r, &why) && why == "signature does not match", "another key: %s", why.c_str()); + std::string t = good; + t[t.find("windows")] = 'W'; + CHECK(!upd_parse_manifest(t, g_pk, &r, &why), "a changed byte"); + t = good; + t[t.size() - 3] = t[t.size() - 3] == '0' ? '1' : '0'; + CHECK(!upd_parse_manifest(t, g_pk, &r, &why), "a changed signature"); + CHECK(!upd_parse_manifest(good.substr(0, good.size() / 2), g_pk, &r, &why), "truncated"); + { // written through a Windows text stream (CR LF lines): refused, and said so + std::string crlf; + for (char c : good) crlf += c == '\n' ? std::string("\r\n") : std::string(1, c); + CHECK(!upd_parse_manifest(crlf, g_pk, &r, &why) && why == "CR LF line ends", "CR LF: %s", why.c_str()); + } + CHECK(!upd_parse_manifest("", g_pk, &r, &why), "empty"); + CHECK(!upd_parse_manifest(good + "file 1 " + sha_of("x") + " extra\n", g_pk, &r, &why), "a line after the signature"); + // signed, but wrong inside: every one refused + const char *bad[] = { + "LAS Recomp update 2\nversion v\nplatform p\nasset 1 %s a\n", // a newer format + "LAS Recomp update 1\nversion v\nplatform p\n", // no asset + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a/b\n", // asset with a folder + "LAS Recomp update 1\nversion v\nplatform p\nasset x %s a\n", // size not a number + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nwhat 1\n", // unknown line + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s ../evil.dll\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s /etc/passwd\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s save/system.bin\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s Larecomp.ini\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s extracted/exefs/main\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s lasrecomp.log\n", + "LAS Recomp update 1\nversion v\nplatform p\nasset 1 %s a\nfile 1 %s a.exe\nfile 1 %s A.EXE\n", // twice + "LAS Recomp update 1\r\nversion v\nplatform p\nasset 1 %s a\n", + }; + std::string h = sha_of("x"); + for (const char *b : bad) { + char buf[1024]; + snprintf(buf, sizeof buf, b, h.c_str(), h.c_str(), h.c_str()); + CHECK(!upd_parse_manifest(sign(buf), g_pk, &r, &why), "refused: %s", buf); + } + // paths + const char *ok_paths[] = {"lasrecomp.exe", "zig/lib/std/a.zig", "rando-gen/_internal/x.pyd", "README.md", "saves.txt"}; + for (const char *p : ok_paths) CHECK(upd_safe_path(p), "app path %s", p); + const char *no_paths[] = {"", "/abs", "a//b", "a/./b", "a/../b", "C:/x", "a\\b", "dir/", "rando/x", "SAVE/x", + "cache/pipelines", "fonts/a.ttf", "data_root.txt", "lasrecomp-backup-1.log", ".lasrecomp-update/x", + "gamecontrollerdb.txt", "x.", "x "}; + for (const char *p : no_paths) CHECK(!upd_safe_path(p), "not an app path: '%s'", p); +} + +// An app folder with old files, data next to them (Windows: the data folder is the app's), new files staged +static std::string g_root; +static UpdPaths make_app(const char *name, std::vector *files) { + UpdPaths p{g_root + "/" + name}; + upd_remove_tree(p.app); + write(p.app + "/lasrecomp.exe", "old exe"); + write(p.app + "/SDL3.dll", "old sdl"); + write(p.app + "/zig/lib/a.h", "old a"); + write(p.app + "/save/system.bin", "SAVE"); // data: never touched + write(p.app + "/larecomp.ini", "settings"); + write(p.app + "/extracted/exefs/main", "game"); + write(p.app + "/old-only.dll", "a file the new version doesn't list"); + *files = {{"lasrecomp.exe", 7, sha_of("new exe")}, {"SDL3.dll", 7, sha_of("new sdl")}, + {"zig/lib/a.h", 5, sha_of("new a")}, {"zig/lib/added.h", 9, sha_of("new added")}}; + write(p.new_dir() + "/lasrecomp.exe", "new exe"); + write(p.new_dir() + "/SDL3.dll", "new sdl"); + write(p.new_dir() + "/zig/lib/a.h", "new a"); + write(p.new_dir() + "/zig/lib/added.h", "new added"); + return p; +} +static bool is_old(const UpdPaths &p) { + return read(p.app + "/lasrecomp.exe") == "old exe" && read(p.app + "/SDL3.dll") == "old sdl" && + read(p.app + "/zig/lib/a.h") == "old a" && !upd_exists(p.app + "/zig/lib/added.h"); +} +static bool is_new(const UpdPaths &p) { + return read(p.app + "/lasrecomp.exe") == "new exe" && read(p.app + "/SDL3.dll") == "new sdl" && + read(p.app + "/zig/lib/a.h") == "new a" && read(p.app + "/zig/lib/added.h") == "new added"; +} +static bool data_intact(const UpdPaths &p) { + return read(p.app + "/save/system.bin") == "SAVE" && read(p.app + "/larecomp.ini") == "settings" && + read(p.app + "/extracted/exefs/main") == "game" && upd_exists(p.app + "/old-only.dll"); +} + +static void swap_tests() { + std::vector files; + std::string why; + { // the normal way: swap, first start, start screen + UpdPaths p = make_app("normal", &files); + CHECK(upd_check_staged(p.new_dir(), files, &why), "staged: %s", why.c_str()); + CHECK(upd_swap(p, files, "v2", "v1", &why), "swap: %s", why.c_str()); + CHECK(is_new(p) && data_intact(p), "new files in place, data untouched"); + CHECK(read(p.old_dir() + "/lasrecomp.exe") == "old exe", "old kept"); + CHECK(upd_boot(p, "v2") == UPD_BOOT_STARTED, "first start"); + CHECK(is_new(p), "still new"); + upd_commit(p); + CHECK(is_new(p) && data_intact(p) && !upd_exists(p.work()), "committed: work folder gone"); + CHECK(upd_boot(p, "v2") == UPD_BOOT_NOTHING, "later starts: nothing"); + } + { // the new version starts but never reaches the start screen: the next start restores + UpdPaths p = make_app("crashy", &files); + CHECK(upd_swap(p, files, "v2", "v1", &why), "swap: %s", why.c_str()); + CHECK(upd_boot(p, "v2") == UPD_BOOT_STARTED, "first start"); + CHECK(upd_boot(p, "v2") == UPD_BOOT_RESTORED, "second start without the start screen: restored"); + CHECK(is_old(p) && data_intact(p) && !upd_exists(p.work()), "old files back, data untouched"); + } + for (int k = 0; k <= 4; k++) { // killed after k files (k = 4: before the journal said installed) + UpdPaths p = make_app(("killed" + std::to_string(k)).c_str(), &files); + CHECK(!upd_swap(p, files, "v2", "v1", &why, k), "stopped after %d", k); + UpdJournal j; + CHECK(upd_read_journal(p, &j) && j.state == "swapping", "journal says swapping"); + CHECK(upd_boot(p, "v1") == UPD_BOOT_RESTORED, "killed after %d: restored", k); + CHECK(is_old(p) && data_intact(p) && !upd_exists(p.work()), "killed after %d: old files back", k); + } + { // killed between "moved" written and the move (the journal names a file still in place) + UpdPaths p = make_app("journaled", &files); + write(p.state(), "state swapping \nversion v2\nfrom v1\nmoved lasrecomp.exe\n"); + CHECK(upd_boot(p, "v1") == UPD_BOOT_RESTORED && is_old(p) && data_intact(p), "nothing moved: unchanged"); + } + { // killed after the move aside, before the new file went in + UpdPaths p = make_app("halfmove", &files); + upd_mkdirs_for(p.old_dir() + "/lasrecomp.exe"); + upd_move(p.app + "/lasrecomp.exe", p.old_dir() + "/lasrecomp.exe"); + write(p.state(), "state swapping \nversion v2\nfrom v1\nmoved lasrecomp.exe\n"); + CHECK(upd_boot(p, "v1") == UPD_BOOT_RESTORED && is_old(p), "moved aside only: put back"); + } + { // a half-written journal line (killed while writing it) + UpdPaths p = make_app("halfline", &files); + CHECK(!upd_swap(p, files, "v2", "v1", &why, 2), "stopped"); + FILE *f = fopen(p.state().c_str(), "ab"); + fputs("moved zig/li", f); + fclose(f); + CHECK(upd_boot(p, "v1") == UPD_BOOT_RESTORED && is_old(p) && data_intact(p), "half line: restored"); + } + { // a staged file damaged or missing: refused before anything moves + UpdPaths p = make_app("damaged", &files); + write(p.new_dir() + "/SDL3.dll", "new sdX"); + CHECK(!upd_check_staged(p.new_dir(), files, &why), "damaged file refused"); + remove((p.new_dir() + "/zig/lib/a.h").c_str()); + CHECK(!upd_check_staged(p.new_dir(), files, &why), "missing file refused"); + CHECK(is_old(p), "nothing changed"); + } + { // a download left behind without a journal: cleaned at start + UpdPaths p = make_app("leftover", &files); + CHECK(upd_boot(p, "v1") == UPD_BOOT_NOTHING && !upd_exists(p.work()) && is_old(p), "leftover removed"); + } + { // installed, but another version runs (put back by hand): the old files go, nothing else changes + UpdPaths p = make_app("byhand", &files); + CHECK(upd_swap(p, files, "v2", "v1", &why), "swap"); + CHECK(upd_boot(p, "v1") == UPD_BOOT_NOTHING && !upd_exists(p.work()) && is_new(p), "other version: kept as is"); + } +#ifndef _WIN32 + if (geteuid() != 0) { // a folder that can't be written: Update is off (Download remains) + UpdPaths p = make_app("readonly", &files); + CHECK(upd_dir_writable(p.app), "writable before"); + chmod(p.app.c_str(), 0555); + CHECK(!upd_dir_writable(p.app), "read-only folder detected"); + CHECK(!upd_swap(p, files, "v2", "v1", &why), "swap into a read-only folder fails"); + chmod(p.app.c_str(), 0755); + UpdJournal j; + if (upd_read_journal(p, &j)) upd_restore(p, j); + CHECK(is_old(p) && data_intact(p), "read-only: unchanged"); + } +#endif +} + +int main() { + uint8_t seed[32], seed2[32], sk2[64]; + for (int i = 0; i < 32; i++) seed[i] = (uint8_t)(i * 7 + 1), seed2[i] = (uint8_t)(i * 13 + 5); + crypto_ed25519_key_pair(g_sk, g_pk, seed); + crypto_ed25519_key_pair(sk2, g_other_pk, seed2); + const char *tmp = getenv("TMPDIR"); + if (!tmp || !*tmp) tmp = getenv("TEMP"); + g_root = std::string(tmp && *tmp ? tmp : "/tmp") + "/lasrecomp-test-update"; + for (char &c : g_root) if (c == '\\') c = '/'; + upd_remove_tree(g_root); + upd_mkdir(g_root.c_str()); + manifest_tests(); + swap_tests(); + upd_remove_tree(g_root); + printf(fails ? "%d FAILED\n" : "all passed\n", fails); + return fails != 0; +} diff --git a/tools/test_update_e2e.cpp b/tools/test_update_e2e.cpp new file mode 100644 index 000000000..fda679742 --- /dev/null +++ b/tools/test_update_e2e.cpp @@ -0,0 +1,60 @@ +// The self-updater's AppImage path end to end (runtime/update.cpp, built with LARECOMP_SELF_UPDATE_TEST), driven by +// tools/test_update_e2e.sh against a local web server: fetch the manifest, check it, download, check, swap, restart, +// first start, keep or roll back. This file stands in for the platform layer (curl as a child process, the exe path). +// Usage: test-update-e2e start [restart] | boot | commit | available +// (TEST_VERSION: the version this "build" is; APPIMAGE: the file it updates) +#include +#include +#include +#include +#include +#include +#include + +#include "platform.h" +#include "update.h" + +extern char **environ; +extern "C" { +const char *larecomp_version(void) { return getenv("TEST_VERSION") ? getenv("TEST_VERSION") : "v1"; } +PlatProc plat_spawn(const char *const argv[], const char *log_path, bool append_log) { + (void)log_path, (void)append_log; + pid_t pid; + return posix_spawn(&pid, argv[0], NULL, NULL, (char *const *)argv, environ) == 0 ? (PlatProc)pid : 0; +} +int plat_proc_wait(PlatProc p, bool block) { + int st; + if (waitpid((pid_t)p, &st, block ? 0 : WNOHANG) <= 0) return -1; + return WIFEXITED(st) ? WEXITSTATUS(st) : PLAT_PROC_SIGNALED + WTERMSIG(st); +} +void plat_proc_kill(PlatProc p) { kill((pid_t)p, SIGTERM); } +bool plat_exe_path(char *buf, size_t size) { return snprintf(buf, size, "/nonexistent") > 0; } +#ifdef __linux__ +void plat_terminate(void) { _exit(0); } +#endif +} + +int main(int argc, char **argv) { + const char *cmd = argc > 1 ? argv[1] : ""; + if (!strcmp(cmd, "available")) { + const char *why = ""; + int a = update_available(&why); + printf("available %d %s\n", a, a ? "" : why); + return a ? 0 : 1; + } + if (!strcmp(cmd, "boot")) { update_boot(); printf("boot: continued\n"); return 0; } + if (!strcmp(cmd, "commit")) { update_reached_start_screen(); printf("committed\n"); return 0; } + if (!strcmp(cmd, "start") && argc >= 5) { + update_start(argv[2], argv[3], atoll(argv[4])); + char msg[256]; + double frac; + int st; + while ((st = update_status(&frac, msg, sizeof msg)) == UPDATE_RUNNING) usleep(20000); + printf("%s: %s\n", st == UPDATE_DONE ? "done" : "failed", msg); + fflush(stdout); + if (st == UPDATE_DONE && argc > 5 && !strcmp(argv[5], "restart")) update_restart(); + return st == UPDATE_DONE ? 0 : 1; + } + fprintf(stderr, "usage: see the top of tools/test_update_e2e.cpp\n"); + return 2; +} diff --git a/tools/test_update_e2e.sh b/tools/test_update_e2e.sh new file mode 100755 index 000000000..050e836cd --- /dev/null +++ b/tools/test_update_e2e.sh @@ -0,0 +1,107 @@ +#!/bin/bash +# The self-updater end to end on its AppImage path (runtime/update.cpp), on macOS or Linux, with no network beyond +# this machine: a throwaway signing key, a local web server (python3 -m http.server) holding a signed manifest and a +# fake AppImage (a shell script), and tools/test_update_e2e.cpp standing in for the app. Covers: an update and its +# restart, the first start and the start screen keeping it, a new version that never gets there (rolled back), and +# the failures that must leave the app as it was: bad signature, truncated or damaged download, a manifest for +# another platform or an older nightly, no manifest, a folder that can't be written. +# Usage: tools/test_update_e2e.sh (from anywhere; prints "all passed" and exits 0) +set -uo pipefail +cd "$(dirname "$0")/.." +ROOT=$(pwd) +T=$(mktemp -d "${TMPDIR:-/tmp}/lasrecomp-e2e.XXXXXX") +PORT=$((20000 + RANDOM % 20000)) +SERVER= +cleanup() { [ -n "$SERVER" ] && kill "$SERVER" 2>/dev/null; chmod -R u+w "$T" 2>/dev/null; rm -rf "$T"; } +trap cleanup EXIT +fails=0 +check() { if eval "$2"; then echo "ok $1"; else echo "FAIL $1"; fails=$((fails + 1)); fi; } + +CXX=${CXX:-clang++} +CC=${CC:-clang} +mkdir -p "$T/obj" +$CXX -std=c++17 -O1 -Iruntime -o "$T/update_sign" tools/update_sign.cpp || exit 1 +for f in update_crypto update_crypto_ed25519; do + $CC -O1 -DLARECOMP_SELF_UPDATE_TEST -Iruntime -c runtime/$f.c -o "$T/obj/$f.o" || exit 1 +done +$CXX -std=c++17 -O1 -DLARECOMP_SELF_UPDATE_TEST -Iruntime -Iruntime/nvn -o "$T/app" tools/test_update_e2e.cpp \ + runtime/update.cpp "$T/obj/update_crypto.o" "$T/obj/update_crypto_ed25519.o" -lpthread || exit 1 +"$T/update_sign" keygen "$T/key" > "$T/pub" || exit 1 +export UPDATE_SIGNING_KEY=$(cat "$T/key") +export LARECOMP_UPDATE_KEY=$(awk '/^public /{print $2}' "$T/pub") +export LARECOMP_UPDATE_URL=http://127.0.0.1:$PORT +export LARECOMP_UPDATE_NO_RESTART=1 +PLAT=linux-x86_64 +NEW=nightly-202611171000-abc1234 + +# the "AppImages": scripts that say which version they are (and leave a mark when started) +appimage() { printf '#!/bin/sh\necho %s > "%s/started"\n' "$1" "$T"; } +mkdir -p "$T/srv/nightly" "$T/app1" +appimage v2 > "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" +manifest() { # -> srv/nightly/update-.txt + "$T/update_sign" manifest "$1" "$2" "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" > "$T/srv/nightly/update-$2.txt" +} +manifest "$NEW" "$PLAT" +(cd "$T/srv" && exec python3 -m http.server "$PORT" --bind 127.0.0.1 >/dev/null 2>&1) & +SERVER=$! +for _ in $(seq 50); do curl -fs "http://127.0.0.1:$PORT/nightly/update-$PLAT.txt" >/dev/null && break; sleep 0.1; done + +fresh() { rm -rf "$T/app1"; mkdir -p "$T/app1"; appimage v1 > "$T/app1/LAS.AppImage"; chmod 755 "$T/app1/LAS.AppImage"; rm -f "$T/started"; } +export APPIMAGE=$T/app1/LAS.AppImage +is_v1() { grep -q 'echo v1' "$APPIMAGE"; } +is_v2() { grep -q 'echo v2' "$APPIMAGE"; } +app() { TEST_VERSION=${V:-nightly-202611151000-0000000} "$T/app" "$@" > "$T/out" 2>&1; } + +# an update, its restart into the new file, the first start, the start screen +fresh +check "update available" 'app available' +check "update installs" 'app start nightly $PLAT 202611151000 && grep -q "^done" "$T/out"' +check "new AppImage in place, executable" 'is_v2 && [ -x "$APPIMAGE" ]' +check "old AppImage kept" '[ -f "$T/app1/.lasrecomp-update/old/LAS.AppImage" ]' +check "first start of the new version" 'V=$NEW app boot && grep -q "first start" "$T/out"' +check "start screen keeps it, old gone" 'V=$NEW app commit && is_v2 && [ ! -e "$T/app1/.lasrecomp-update" ]' +fresh +unset LARECOMP_UPDATE_NO_RESTART +check "restart starts the new file" 'app start nightly $PLAT 202611151000 restart; sleep 0.5; [ "$(cat "$T/started" 2>/dev/null)" = v2 ]' +export LARECOMP_UPDATE_NO_RESTART=1 +# the new version starts once and never reaches the start screen: the next start puts the old one back +fresh +app start nightly $PLAT 202611151000 +V=$NEW app boot +check "never reached the start screen: rolled back" 'V=$NEW app boot; grep -q "previous version is back" "$T/out" && is_v1 && [ ! -e "$T/app1/.lasrecomp-update" ]' + +# failures: nothing changes, nothing left behind +fails_clean() { ! app "$@" && grep -q "^failed" "$T/out" && is_v1 && [ ! -e "$T/app1/.lasrecomp-update" ]; } +fresh +check "older nightly refused" 'fails_clean start nightly $PLAT 202611180000' +check "same nightly refused" 'fails_clean start nightly $PLAT 202611171000' +check "no manifest for the platform" 'fails_clean start nightly linux-aarch64 202611151000' +cp "$T/srv/nightly/update-$PLAT.txt" "$T/srv/nightly/update-linux-aarch64.txt" +check "manifest for another platform" 'fails_clean start nightly linux-aarch64 202611151000 && grep -q "another platform" "$T/out"' +check "a release tag gets no nightly" 'fails_clean start v0.7.0 $PLAT 0' +mkdir -p "$T/srv/v0.7.0" && cp "$T/srv/nightly/"* "$T/srv/v0.7.0/" +check "a release whose manifest names another version" 'fails_clean start v0.7.0 $PLAT 0 && grep -q "another version" "$T/out"' +cp "$T/srv/nightly/update-$PLAT.txt" "$T/good" +sed 's/^version .*/version nightly-202611171001-abc1234/' "$T/good" > "$T/srv/nightly/update-$PLAT.txt" +check "changed manifest (bad signature)" 'fails_clean start nightly $PLAT 202611151000 && grep -q "signature does not match" "$T/out"' +LARECOMP_UPDATE_KEY=$(printf '%064d' 1) check "another key" 'fails_clean start nightly $PLAT 202611151000' +cp "$T/good" "$T/srv/nightly/update-$PLAT.txt" +cp "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" "$T/full" +head -c 20 "$T/full" > "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" +check "truncated download" 'fails_clean start nightly $PLAT 202611151000 && grep -q "damaged" "$T/out"' +sed 's/v2/v3/' "$T/full" > "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" +check "damaged download (same size)" 'fails_clean start nightly $PLAT 202611151000 && grep -q "damaged" "$T/out"' +rm "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" +check "download missing" 'fails_clean start nightly $PLAT 202611151000' +cp "$T/full" "$T/srv/nightly/LAS.Recomp-$NEW-$PLAT.AppImage" +if [ "$(id -u)" != 0 ]; then + chmod 555 "$T/app1" + check "read-only folder: Update off" '! app available && grep -q "writable" "$T/out"' + check "read-only folder: refused, unchanged" '! app start nightly $PLAT 202611151000 && is_v1' + chmod 755 "$T/app1" +fi +unset APPIMAGE +check "not an AppImage: Update off" '! app available && grep -q "AppImage" "$T/out"' + +[ $fails = 0 ] && echo "all passed" || echo "$fails FAILED" +exit $((fails != 0)) diff --git a/tools/test_update_tags.cpp b/tools/test_update_tags.cpp index 9b0b97b7a..d20e5c2bb 100644 --- a/tools/test_update_tags.cpp +++ b/tools/test_update_tags.cpp @@ -1,12 +1,13 @@ // Update check tags (runtime/ui/update_tags.h): their order and labels; the macOS build numbers -// (tools/bundle_version.sh) go up in the same order. No game, no network. +// (tools/bundle_version.sh) go up in the same order. The update dialog's changelog (runtime/ui/update_notes.h). No game, +// no network. // Build + run (from the repo root): clang++ -std=c++17 -O1 tools/test_update_tags.cpp -o build/test-update-tags && // build/test-update-tags #include #include #include -#include "../runtime/ui/update_tags.h" +#include "../runtime/ui/update_notes.h" // (and update_tags.h) static int fails; #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) @@ -67,6 +68,12 @@ int main(void) { CHECK(bundle_cmp(t, bundle_build("beta_5.3")) > 0 && bundle_cmp(t, bundle_build("beta_5.4")) < 0, "test build of beta_5.4: %s between 5.3 and 5.4", t.c_str()); CHECK(bundle_build("test-foo").empty() && bundle_build("alpha_3").empty(), "no build number without a release"); + // nightlies: each round above the last (Sparkle's feed), all between the release before and the one previewed + std::string n1 = bundle_build("nightly-202611161000-abc1234", "v0.7.0"), n2 = bundle_build("nightly-202611171000-def5678", "v0.7.0"); + CHECK(n1 == "699.1.202611161000", "nightly build number: %s", n1.c_str()); + CHECK(bundle_cmp(n2, n1) > 0 && bundle_cmp(n1, bundle_build("v0.6.9")) > 0 && bundle_cmp(n2, bundle_build("v0.7.0")) < 0, + "nightlies in order: %s %s", n1.c_str(), n2.c_str()); + CHECK(bundle_build("nightly-2026-abc", "v0.7.0").empty(), "bad nightly stamp"); } const char *never[] = {"alpha_3", "test-388-grass", "beta_0", "beta_5.0", "v0.0.1", "v1.0", "v0.6.0-rc1", "beta_5.3x", "ci-test-1", "", "v", "beta_", "experimental", "experimental-202610302315-abc1234", "nightly", @@ -115,6 +122,60 @@ int main(void) { CHECK(newest_nightly(body, "windows") == 0, "nightly windows: none (windows-arm64 is another platform)"); CHECK(newest_experimental(body, "macOS") == 202612010000LL, "experimental macOS ignores nightlies"); } + { // the self-updater's manifests on the nightly page are never taken for a nightly build + std::string body = "\"name\":\"update-macOS.txt\"\n\"name\":\"update-windows.txt\"\n" + "\"browser_download_url\":\"https://github.com/x/releases/download/nightly/update-windows.txt\"\n"; + CHECK(newest_nightly(body, "windows") == 0 && newest_nightly(body, "macOS") == 0, "manifests aren't nightlies"); + } + { // the releases JSON: tag and notes of each, nested objects and escapes on the way + std::string json = "[{\"url\":\"x\",\"author\":{\"login\":\"a\",\"body\":\"not this\"},\"tag_name\":\"v0.7.0\"," + "\"assets\":[{\"name\":\"a\",\"size\":3}],\"prerelease\":false," + "\"body\":\"# LAS Recomp Beta 7\\r\\n\\r\\n- \\\"quoted\\\" caf\\u00e9 \\ud83d\\ude00\"}," + " {\"tag_name\":\"nightly\",\"body\":null}, {\"tag_name\":\"beta_6\",\"body\":\"B6\"}]"; + std::vector r = json_releases(json); + CHECK(r.size() == 3, "3 releases: %zu", r.size()); + if (r.size() == 3) { + CHECK(r[0].tag == "v0.7.0" && r[0].body == "# LAS Recomp Beta 7\r\n\r\n- \"quoted\" caf\xc3\xa9 \xf0\x9f\x98\x80", + "first: '%s' '%s'", r[0].tag.c_str(), r[0].body.c_str()); + CHECK(r[1].tag == "nightly" && r[1].body.empty() && r[2].tag == "beta_6" && r[2].body == "B6", "others"); + } + CHECK(json_releases("").empty() && json_releases("[{\"tag_name\":\"x").empty() && json_releases("{}").empty(), "broken JSON"); + } + { // a release's notes: banner, "Built from", sha256 table and comments out + std::string b = "> [!WARNING]\r\n> **Test build.** Not for general use.\r\n\r\n# LAS Recomp Beta 7 Changelog\r\n\r\n" + "## New\r\n\r\n- One thing\r\n- Two\r\n\r\n---\r\n\r\nBuilt from abc.\r\n\r\n" + "| File | sha256 |\r\n|---|---|\r\n| `a` | `b` |\r\n"; + std::string c = clean_release_notes(b); + CHECK(c == "# LAS Recomp Beta 7 Changelog\n\n## New\n\n- One thing\n- Two", "clean: [%s]", c.c_str()); + CHECK(clean_release_notes("Release notes: (to be written)\n\nBuilt from x.\n\n| File | sha256 |") == + "Release notes: (to be written)", "draft notes"); + CHECK(clean_release_notes("a\n\nb") == "a\nb", "comment over lines: [%s]", + clean_release_notes("a\n\nb").c_str()); + } + { // every release skipped, newest first; nothing older than this build, nothing newer than the offer + std::vector r = {{"v0.6.1", "- fix one"}, {"beta_6", "# LAS Recomp Beta 6 Changelog\n\n- six"}, + {"v0.7.0", "# LAS Recomp Beta 7 Changelog\n\n- seven"}, {"v0.6.2", ""}, + {"test-x", "no"}, {"nightly", "no"}, {"v0.8.0", "not offered"}}; + std::string c = release_changelog(r, tag_rank("v0.6.0"), "v0.7.0"); + CHECK(c == "# LAS Recomp Beta 7 Changelog\n\n- seven\n\n# Beta 6.2\n\n# Beta 6.1\n\n- fix one", "changelog: [%s]", c.c_str()); + CHECK(release_changelog(r, tag_rank("v0.7.0"), "v0.7.0").empty(), "nothing newer"); + } + { // nightly rounds newer than this build + std::string body = "> [!WARNING]\n> x\n\n## Changes\n\n\n**2026-11-17**\n\n" + "- Third (#3)\n\n\n**2026-11-16**\n\n- Second (#2)\n- First (#1)\n\n" + "To report a problem, open an issue.\n\n\n"; + std::vector r = {{"beta_6", "x"}, {"nightly", body}}; + std::string c = nightly_changelog(r, 202611161000LL); + CHECK(c == "**2026-11-17**\n\n- Third (#3)", "one round: [%s]", c.c_str()); + c = nightly_changelog(r, 202611151000LL); + CHECK(c == "**2026-11-17**\n\n- Third (#3)\n\n**2026-11-16**\n\n- Second (#2)\n- First (#1)", "two rounds: [%s]", c.c_str()); + CHECK(nightly_changelog(r, 202611171000LL).empty() && nightly_changelog({}, 0).empty(), "none newer"); + } + { // inline Markdown out + CHECK(md_inline("**Bold** and `code`, [a link](https://x.y/z) \\*star\\*") == + "Bold and code, a link https://a.b *star*", "inline: [%s]", + md_inline("**Bold** and `code`, [a link](https://x.y/z) \\*star\\*").c_str()); + } if (fails) { printf("%d failed\n", fails); return 1; } printf("update tags: all passed\n"); return 0; diff --git a/tools/update_manifest.sh b/tools/update_manifest.sh new file mode 100755 index 000000000..0a30c4e6c --- /dev/null +++ b/tools/update_manifest.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# The self-updater's signed manifest for one release file (runtime/update_core.h): build/dist/update-.txt. +# release.yml and nightly.yml run it after each platform's build, with the UPDATE_SIGNING_KEY secret in the +# environment. Without the key (a fork, or a run before the key existed) it only warns: the release still ships, and +# the start screen's Update needs the manifest, so players there use Download. +# Usage: tools/update_manifest.sh [] +set -euo pipefail +cd "$(dirname "$0")/.." +[ $# -ge 3 ] || { echo "usage: tools/update_manifest.sh []" >&2; exit 2; } +out=build/dist/update-$2.txt +rm -f "$out" +if [ -z "${UPDATE_SIGNING_KEY:-}" ]; then + echo "::warning::UPDATE_SIGNING_KEY is not set: no update manifest for $2 (players there update with Download)" + exit 0 +fi +mkdir -p build/dist +"${CXX_UPDATE:-clang++}" -std=c++17 -O2 -Iruntime tools/update_sign.cpp -o build/update_sign +build/update_sign manifest "$@" > "$out.part" +# (read back from the file as the app reads it: catches anything the way out changed, e.g. CR LF line ends) +build/update_sign verify "$out.part" "$(build/update_sign public | awk '/^public /{print $2}')" +mv "$out.part" "$out" +head -4 "$out" +echo "$(grep -c '^file ' "$out" || true) files; $out" diff --git a/tools/update_sign.cpp b/tools/update_sign.cpp new file mode 100644 index 000000000..40d97b4cb --- /dev/null +++ b/tools/update_sign.cpp @@ -0,0 +1,162 @@ +// Signs the self-updater's manifests (runtime/update_core.h) and the macOS update's Sparkle signature, with one +// Ed25519 key (Monocypher, CC0). Used by release.yml and nightly.yml; the private key is the UPDATE_SIGNING_KEY secret +// (base64 of the 32-byte seed, the format Sparkle's own tools use), never on disk in the repo. +// Build: c++ -std=c++17 -O2 -Iruntime tools/update_sign.cpp -o build/update_sign +// update_sign keygen a new key: the seed (base64) into the file, the public key printed +// update_sign public the public key of $UPDATE_SIGNING_KEY (hex, then base64) +// update_sign manifest [] > update-.txt +// the signed manifest for a release file; : what a zip holds +// (its top folder), every file in it listed +// update_sign sparkle the file's signature for a Sparkle appcast (base64) +// update_sign verify checks a manifest (exit 0: good) +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +#include +#include +#include + +#include "update_core.h" +// (Monocypher's C compiled into this file as C++, so one command builds it on every platform) +#include "../third_party/monocypher/monocypher.c" +#undef FOR // (each file defines its own) +#include "../third_party/monocypher/monocypher-ed25519.c" + +namespace fs = std::filesystem; + +static const char B64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static std::string b64(const uint8_t *p, size_t n) { + std::string o; + for (size_t i = 0; i < n; i += 3) { + uint32_t v = p[i] << 16 | (i + 1 < n ? p[i + 1] << 8 : 0) | (i + 2 < n ? p[i + 2] : 0); + o += B64[v >> 18 & 63], o += B64[v >> 12 & 63]; + o += i + 1 < n ? B64[v >> 6 & 63] : '='; + o += i + 2 < n ? B64[v & 63] : '='; + } + return o; +} +static bool unb64(const std::string &s, std::vector *out) { + uint32_t v = 0; + int bits = 0; + for (char c : s) { + if (c == '=' || c == '\n' || c == '\r' || c == ' ') continue; + const char *k = strchr(B64, c); + if (!k || !c) return false; + v = v << 6 | (uint32_t)(k - B64), bits += 6; + if (bits >= 8) bits -= 8, out->push_back((uint8_t)(v >> bits)); + } + return true; +} +static std::string hex(const uint8_t *p, size_t n) { + std::string o; + char b[3]; + for (size_t i = 0; i < n; i++) snprintf(b, 3, "%02x", p[i]), o += b; + return o; +} +static void die(const char *m) { + fprintf(stderr, "update_sign: %s\n", m); + exit(1); +} + +// $UPDATE_SIGNING_KEY -> the 64-byte Monocypher secret key and the public key +static void key(uint8_t sk[64], uint8_t pk[32]) { + const char *e = getenv("UPDATE_SIGNING_KEY"); + std::vector seed; + if (!e || !*e) die("UPDATE_SIGNING_KEY is not set"); + if (!unb64(e, &seed) || seed.size() != 32) die("UPDATE_SIGNING_KEY: not a base64 32-byte seed"); + crypto_ed25519_key_pair(sk, pk, seed.data()); // (wipes the seed) +} +static bool read_file(const char *path, std::string *out) { + FILE *f = fopen(path, "rb"); + if (!f) return false; + char b[1 << 16]; + size_t n; + while ((n = fread(b, 1, sizeof b, f)) > 0) out->append(b, n); + bool ok = !ferror(f); + fclose(f); + return ok; +} + +int main(int argc, char **argv) { +#ifdef _WIN32 + _setmode(_fileno(stdout), _O_BINARY); // (the manifest goes out as signed: LF lines, never CR LF) +#endif + std::string cmd = argc > 1 ? argv[1] : ""; + if (cmd == "keygen" && argc == 3) { + uint8_t seed[32], sk[64], pk[32]; + FILE *r = fopen("/dev/urandom", "rb"); + if (!r || fread(seed, 1, 32, r) != 32) die("no /dev/urandom"); + fclose(r); + if (upd_exists(argv[2])) die("the key file exists already: never replace a published key"); + std::string s = b64(seed, 32); + crypto_ed25519_key_pair(sk, pk, seed); + FILE *f = fopen(argv[2], "wx"); + if (!f || fprintf(f, "%s\n", s.c_str()) < 0 || fclose(f) != 0) die("can't write the key file"); + printf("public %s\npublic-base64 %s\n", hex(pk, 32).c_str(), b64(pk, 32).c_str()); + return 0; + } + if (cmd == "public" && argc == 2) { + uint8_t sk[64], pk[32]; + key(sk, pk); + printf("public %s\npublic-base64 %s\n", hex(pk, 32).c_str(), b64(pk, 32).c_str()); + return 0; + } + if (cmd == "sparkle" && argc == 3) { + uint8_t sk[64], pk[32], sig[64]; + key(sk, pk); + std::string data; + if (!read_file(argv[2], &data)) die("can't read the file"); + crypto_ed25519_sign(sig, sk, (const uint8_t *)data.data(), data.size()); + printf("%s\n", b64(sig, 64).c_str()); + return 0; + } + if (cmd == "manifest" && (argc == 5 || argc == 6)) { + uint8_t sk[64], pk[32], sig[64]; + key(sk, pk); + UpdManifest m; + m.version = argv[2], m.platform = argv[3]; + m.asset = fs::path(argv[4]).filename().string(); + std::replace(m.asset.begin(), m.asset.end(), ' ', '.'); // (GitHub stores "LAS Recomp-..." as "LAS.Recomp-...") + m.asset_sha256 = upd_file_sha256(argv[4], &m.asset_size); + if (m.asset_sha256.empty()) die("can't read the file"); + if (argc == 6) { + fs::path top(argv[5]); + for (auto &e : fs::recursive_directory_iterator(top)) { + if (e.is_symlink()) die("a link inside the folder (zips can't hold them)"); + if (!e.is_regular_file()) continue; + UpdFile f; + f.path = fs::relative(e.path(), top).generic_string(); + if (!upd_safe_path(f.path)) { fprintf(stderr, "%s\n", f.path.c_str()); die("a file the updater may not write"); } + f.sha256 = upd_file_sha256(e.path().string(), &f.size); + if (f.sha256.empty()) die("can't read a file in the folder"); + m.files.push_back(f); + } + std::sort(m.files.begin(), m.files.end(), [](const UpdFile &a, const UpdFile &b) { return a.path < b.path; }); + if (m.files.empty()) die("an empty folder"); + } + std::string t = upd_manifest_text(m); + crypto_ed25519_sign(sig, sk, (const uint8_t *)t.data(), t.size()); + t += "signature " + hex(sig, 64) + "\n"; + UpdManifest back; // (it must read back as the app will read it) + std::string why; + if (!upd_parse_manifest(t, pk, &back, &why)) die(why.c_str()); + fwrite(t.data(), 1, t.size(), stdout); + return 0; + } + if (cmd == "verify" && argc == 4) { + std::string t, why; + uint8_t pk[32]; + if (!read_file(argv[2], &t)) die("can't read the manifest"); + if (!upd_hex(argv[3], pk, 32)) die("not a public key (64 hex)"); + UpdManifest m; + if (!upd_parse_manifest(t, pk, &m, &why)) die(why.c_str()); + printf("%s %s: %s, %zu files\n", m.version.c_str(), m.platform.c_str(), m.asset.c_str(), m.files.size()); + return 0; + } + fprintf(stderr, "usage: see the top of tools/update_sign.cpp\n"); + return 2; +}