Skip to reference

std.x.crypto.scrypt

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: x

Module

std.x.crypto.scrypt — the scrypt password-based key derivation function (RFC 7914) and PBKDF2-HMAC-SHA-256 (RFC 8018 §5.2). Stability tier: x — the D53 crypto ladder's memory-hard rung (sprint sc80, the hash under PAX's passwords).

The nursery banner (D31). std.x.crypto.scrypt is not std.crypto: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it; its reason and graduation trigger live in std/x/README.md. HMAC-SHA-256 is std.x.crypto.sha2's through the module boundary, a resident-on-resident edge the graduation move must resolve (beside p256's, rsa's and curve25519's).

Implemented from the RFC 7914 DOCUMENT, not ported. Salsa20/8 Core is §3's word specification, scryptBlockMix §4, scryptROMix §5 and scrypt §6; PBKDF2 is RFC 8018 §5.2 with RFC 2104's HMAC (FIPS 180-4's SHA-256, sha2.hmac256). The texts are vendored at vendor/vectors/rfc/rfc7914.txt, rfc8018.txt and rfc2104.txt. Every vector RFC 7914 prints (§8–§13) is a row under tests/x/crypto/scrypt/; the document is the authority.

**The word currency is wrapping[u32], four octets an element.** Salsa's adds and rotations are the type's own (no masks), and a List[wrapping[u32]] strides by four octets, so V costs exactly RFC 7914's 128·r·N octets. Octet strings cross the API as List[byte], little-endian per word (§3: "without endianness conversion", and the vectors are little-endian words).

The inner loops allocate nothing. A callee's allocation lives in the caller's ambient region until that region dies, so a BlockMix that built a list per call would hold 2·N·128·r octets of garbage by the end of ROMix. BlockMix and ROMix therefore work in place over four buffers made once per scrypt call (X, Y, T and V), and every HMAC that PBKDF2 iterates runs inside its own region block, so the c = 80000 vector holds a constant footprint. ROMix's V lives in a region of its own and is gone before the final PBKDF2 runs.

Memory and the cap. memory(n_log2, r, p, dk_len) is the octets scrypt allocates at its peak, 128·r·(N + 2p + 2) plus the key's dk_len octets and a fixed 16 KiB for T and the HMAC states, and scrypt refuses over_cap before allocating anything when that exceeds the caller's cap (RFC 7914 §14: "systems should protect against a denial-of-service attack resulting from attackers presenting unreasonably large parameters"). The caller's own password and salt, and one transient copy of the salt, are not counted. romix alone is uncapped: it is the primitive, and scrypt is the guarded entry; PBKDF2 likewise allocates the dk_len octets its caller asks for.

Refusals, by name (lowercase marks, API-CONVENTIONS §12): bad_n (log2 N below 1, or N not below 2^(16·r), RFC 7914 §2; romix's N not a power of two above 1), bad_rp (r or p below 1, or r·p at least 2^30, which is §2's p ≤ ((2^32 − 1)·32)/(128·r) in integers), over_cap, bad_dk_len (below 1, or above (2^32 − 1)·32) and PBKDF2's bad_count (an iteration count below 1). A wrong-length block handed to salsa20_8, blockmix or romix traps assert: the caller could have checked (§2's trap rule).

Constant-time obligations (D53's retrofit clause; c28 does not exist yet, so these are recorded, not verified). Salsa20/8 and BlockMix are add-rotate-xor over words with indices that depend on r and the loop counters only: no secret-dependent branch or index. ROMix is NOT constant-time, by design: its second loop reads V[Integerify(X) mod N], a memory index computed from secret state. That access pattern is scrypt's memory-hardness (RFC 7914 §5), and a cache-timing observer can learn it; Argon2id's data-independent first pass is the answer to that, and is a named follow-on. PBKDF2 inherits sha2.hmac256's obligations (control flow depends on the password's LENGTH only). The parameter checks branch on public parameters. to_hex indexes an alphabet by value and is for public data.

Types and traits

No public declarations in this section.

Functions and methods

salsa20_8

fn · Source line 245

pub fn salsa20_8(input: List[byte]) -> List[byte]

Salsa20/8 Core (RFC 7914 §3) of one 64-octet block: sixteen little-endian words in, sixteen out. Traps assert unless input.len == 64 (the caller could have checked). ct: as the core — no secret-dependent branch or index.

let out = scrypt.salsa20_8("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".bytes())
out.len == 64

blockmix

fn · Source line 323

pub fn blockmix(b: List[byte], r: int) -> List[byte]

scryptBlockMix (RFC 7914 §4) of b, 2·r 64-octet blocks, with Salsa20/8 as H. Traps assert unless r >= 1 and b.len == 128 * r. ct: indices depend on r only; see salsa20_8.

scrypt.blockmix("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".bytes(), 1).len == 128

romix

fn · Source line 432

pub fn romix(b: List[byte], r: int, n: int) -> List[byte] ! {bad_n}

scryptROMix (RFC 7914 §5) of one 128·r-octet block with cost n. Rows: bad_n when n is not a power of two above 1, or not below 2^(16·r) (§5's "less than 2^(128 * r / 8)"). Traps assert unless r >= 1 and b.len == 128 * r. Uncapped: it allocates 128·r·n octets for V; scrypt is the entry that refuses a cap. ct: NOT constant-time — V is indexed by Integerify(X) (the module header).

let out = scrypt.romix("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".bytes(), 1, 16)?
out.len == 128

pbkdf2_hmac_sha256

fn · Source line 536

pub fn pbkdf2_hmac_sha256( password: List[byte], salt: List[byte], c: int, dk_len: int, ) -> List[byte] ! {bad_count, bad_dk_len}

PBKDF2-HMAC-SHA-256 (RFC 8018 §5.2): dk_len octets derived from password and salt with c iterations. Rows: bad_count when c < 1; bad_dk_len when dk_len < 1 or dk_len > (2^32 − 1)·32 (step 1's "derived key too long"). Never traps. ct: sha2.hmac256's obligations — control flow depends on the password's length, c and dk_len (public), never on a secret octet.

let dk = scrypt.pbkdf2_hmac_sha256("passwd".bytes(), "salt".bytes(), 1, 64)?
scrypt.to_hex(dk)[0..16] == "55ac046e56e3089f"

memory

fn · Source line 592

pub fn memory(n_log2: int, r: int, p: int, dk_len: int) -> int

The octets scrypt allocates at its peak for these parameters: 128·r·(N + 2p + 2) — V, B, the final PBKDF2's copy of B and the two scratch blocks X and Y — plus the dk_len octets of the key it returns and a fixed 16 KiB for Salsa's T and the HMAC states. The caller's own password and salt are not counted. Saturates at the int ceiling (so log2 N of 63 and up answer the ceiling). Traps assert unless n_log2 >= 0, r >= 1, p >= 1 and dk_len >= 0: the domain is the caller's to check (scrypt checks before it calls this). ct: public parameters only.

scrypt.memory(14, 8, 1, 32) == 128 * 8 * (16384 + 4) + 32 + 16384
scrypt.memory(70, 8, 1, 32) == 9223372036854775807

scrypt

fn · Source line 630

pub fn scrypt( password: List[byte], salt: List[byte], n_log2: int, r: int, p: int, dk_len: int, cap: int, ) -> List[byte] ! {bad_n, bad_rp, over_cap, bad_dk_len}

scrypt (RFC 7914 §6): dk_len octets derived from password and salt with N = 2^n_log2, block size r and parallelization p, refusing before it allocates anything when memory(n_log2, r, p, dk_len) exceeds cap octets. Step 1: B = PBKDF2-HMAC-SHA256(P, S, 1, p·128·r); step 2: each B[i] = scryptROMix(r, B[i], N), in order; step 3: DK = PBKDF2-HMAC-SHA256(P, B, 1, dkLen).

Rows, checked in this order: bad_rp (r or p below 1, or r·p at least 2^30), bad_n (log2 N below 1, or N not below 2^(16·r)), bad_dk_len (below 1, or above (2^32 − 1)·32), over_cap (the working memory over cap). Never traps. ct: NOT constant-time — ROMix indexes V by secret state (the module header); the parameter checks branch on public parameters only.

let dk = scrypt.scrypt(List[byte](), List[byte](), 4, 1, 1, 64, scrypt.memory(4, 1, 1, 64))?
scrypt.to_hex(dk)[0..16] == "77d6576238657b20"

to_hex

fn · Source line 692

pub fn to_hex(b: List[byte]) -> str

Lowercase hex of b, this module's own so that its tests and doc examples import exactly one module (sha2's precedent). ct: NOT constant-time — indexes an alphabet by each octet's value. Render public values with it.

scrypt.to_hex("ab".bytes()) == "6162"

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: bad_count, bad_dk_len, bad_n, bad_rp, over_cap. Their conditions are documented on the functions that return them.

No public declarations in this section.