Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

itsmeow
Copy link
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
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
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
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
Member

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

@itsmeow
Copy link
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

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