Skip to content

hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator, adds Base32#225

Merged
ZeWaka merged 18 commits intotgstation:masterfrom
itsmeowForks:csprng
Nov 13, 2025
Merged

hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator, adds Base32#225
ZeWaka merged 18 commits intotgstation:masterfrom
itsmeowForks:csprng

Conversation

@itsmeow
Copy link
Copy Markdown
Contributor

@itsmeow itsmeow commented Jul 1, 2025

Adds to functions to hash:

  • rustg_csprng_chacha20(format, n_bytes): Cryptographically-secure pseudo-random number generator seeded by the OS/hardware.
  • rustg_prng_chacha20_seeded(format, n_bytes, seed): High-quality known-seed deterministic pseudo-random number generator.
  • rustg_encode_base32(string, padding)
  • rustg_decode_base32(string, padding)

Both functions support output into any of the following formats:

  • Hex [0-9a-z]
  • Alphanumeric [A-Za-z0-9]
  • Base32 [A-Z2-7=]
  • Base64 [A-Za-z0-9+/=]

Both functions take a parameter, n_bytes which is the number of bytes sampled by the RNG. The relation of n_bytes to string output length is not 1:1 and varies by format.

  • Hex: n_bytes * 2
  • Alphanumeric: n_bytes
  • Base32: ceil(n_bytes / 5) * 8
  • Base64: 4 * ceil(n_bytes/3)

CSPRNG seeds are provided by SeedableRng::from_os_rng which uses getrandom

On Windows 10, getrandom calls ProcessPrng
On Linux, getrandom performs a getrandom system call if available, otherwise /dev/urandom after successfully polling /dev/random

Updates TOTP generator from #76 to support SHA256 and SHA512 HMACs, as well as implements tests from the RFC rather than a private edu paper. Also allows more than 10 bytes worth of entropy for the secret key because ????????????? why?????? and stop implementing HMAC from scratch and just use a crate

Breaking Changes

  • Breaking change: rustg_generate_totp and rustg_hash_generate_totp_tolerance have been updated from rustg_generate_totpXXX(seed, ...) to rustg_generate_totpXXX(algorithm, seed, ...)
  • Breaking change: rustg_generate_totp now accepts seeds in Base32, not hex, as this is standard for OTP apps & QR codes

@ZeWaka
Copy link
Copy Markdown
Collaborator

ZeWaka commented Jul 1, 2025

Does this belong under hash?

@itsmeow itsmeow changed the title hash: Adds ChaCha20 CSPRNG functions hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator Jul 1, 2025
@itsmeow
Copy link
Copy Markdown
Contributor Author

itsmeow commented Jul 1, 2025

imo yes, it's using almost all the same packages and the TOTP generator is already there. The only reason I added a CSPRNG is because the TOTP generator needs a cryptographically secure random seed

@itsmeow itsmeow changed the title hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator, adds Base32 Jul 1, 2025
@itsmeow
Copy link
Copy Markdown
Contributor Author

itsmeow commented Jul 1, 2025

Why base32?

Yog is the only current user of this function indexed on GitHub, and they already convert it to Base32

Google Authenticator requires that keys be encoded as Base32 without padding: https://github.com/google/google-authenticator/wiki/Key-Uri-Format

@optimumtact
Copy link
Copy Markdown
Member

do the calls block if there is no hardware entropy available?

@itsmeow
Copy link
Copy Markdown
Contributor Author

itsmeow commented Jul 1, 2025

do the calls block if there is no hardware entropy available?

Yes, early in the boot process. The OS will eventually give the necessary data though, from what I understand.
https://docs.rs/getrandom/latest/getrandom/#early-boot

@ZeWaka ZeWaka merged commit 1777f70 into tgstation:master Nov 13, 2025
4 checks passed
Absolucy pushed a commit to Monkestation/rust-g that referenced this pull request Nov 16, 2025
@ZeWaka ZeWaka mentioned this pull request Dec 21, 2025
itsmeow added a commit to BeeStation/rust-g that referenced this pull request Feb 26, 2026
itsmeow added a commit to BeeStation/rust-g that referenced this pull request Feb 26, 2026
* fixes the clippy lints on nightly (tgstation#215)

* Adds a new time function, `formatted_timestamp` (tgstation#214)

* dmi_create_png is rgba instead of rgb (tgstation#217)

* add useragent to byond installer

* crate internal updates (tgstation#218)

* v3.9.0 (tgstation#219)

* Enable pathfinder by default (tgstation#220)

* Fix clippy URL lifetime lint (tgstation#223)

* Improve DM test functionality on Windows, fix regressions (tgstation#221)

* Adds `roll_dice`, an advanced xdy dice roller. (tgstation#216)

Co-authored-by: Kapu1178 <75460809+Kapu1178@users.noreply.github.com>

* v3.11.0 (tgstation#224)

* Fix TOML dme test failing (tgstation#227)

* hash: optimize file hashing (tgstation#228)

* IconForge: DMI Generation, Code Reorganization, Improved Caching, Cleaner I/O (tgstation#213)

* Adds a new `uuid` module (tgstation#229)

* IconForge: BYOND Parity + Tests, Optimizations, New Transforms (tgstation#230)

* `clippy` lints for tgstation#230 (tgstation#233)

* Adds `http_request_fire_and_forget` (tgstation#232)

* DMI metadata reading and injection (tgstation#234)

* 4.0.0 (tgstation#235)

* dmi: Add QR code generation (tgstation#226)

* Bump `dmi` to 0.5.0, optimize `dmi_read_metadata` (tgstation#238)

* Update CI to target 516.1666 (tgstation#237)

* 4.1.0 (tgstation#239)

* makes `rustg_noise_poisson_map` around 8x faster (tgstation#240)

* IconForge: Headless Icon Generation (tgstation#236)

* `cargo update` & `cargo upgrade` & `png` fix (tgstation#241)

* assorted optimizations to `cellularnoise`, `dbpnoise`, and `worleynoise` (tgstation#243)

* v4.2.0 (tgstation#242)

* Fix attempt for CI

* hash: Adds ChaCha20 CSPRNG functions, updates TOTP generator, adds Base32 (tgstation#225)

* Handle errors in panic hook explicitly (tgstation#245)

* do not store Git information static (tgstation#247)

* Prevent `decode_base64` and `decode_base32` from panicking if given invalid base64 (tgstation#244)

* 4.3.0 (tgstation#249)

* fix iconforge generate_headless on win for bad paths not panicing (tgstation#248)

* 5.0.0 (tgstation#250)

* redo iconforge errors - 5.0.1 (tgstation#252)

* 6.0.0 - drop windows 7 support (tgstation#251)

* fix spritesheet gen on linux (tgstation#255)

* cargo update and clippy (tgstation#253)

* 6.0.1 - and `cargo update` (tgstation#256)

---------

Co-authored-by: Lucy <lucy@absolucy.moe>
Co-authored-by: TiviPlus <57223640+TiviPlus@users.noreply.github.com>
Co-authored-by: ZeWaka <zewakagamer@gmail.com>
Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: Comrade Niobe <126028983+ComradeNiobe@users.noreply.github.com>
Co-authored-by: Kapu1178 <75460809+Kapu1178@users.noreply.github.com>
Co-authored-by: Y0SH1M4S73R <y0sh1m4s73r@gmail.com>
Co-authored-by: Ivy <distributivgesetz93@gmail.com>
Co-authored-by: Zephyr <12817816+ZephyrTFA@users.noreply.github.com>
Co-authored-by: Kashargul <144968721+Kashargul@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants