Module
std.x.crypto.passwd — password hashing in crypt(5)'s $7$ form: scrypt (RFC 7914) behind the string a shadow(5) line stores. The module name is passwd (sprint sc80 chose it: the layer a passwd, login, su or sudo calls). Stability tier: x — the D53 crypto ladder's memory-hard rung, the hash under PAX's passwords.
The nursery banner (D31). std.x.crypto.passwd 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. scrypt is std.x.crypto.scrypt's through the module boundary, a resident-on-resident edge the graduation move must resolve.
The surface. gen_salt(random) makes a salt string from octets the caller read from the OS random source (getrandom(2), or /dev/urandom): never a fixed salt, and a short or all-zero buffer is refused. cost(n_log2, r, p) checks scrypt's three parameters. hash(password, salt, cost) returns the exact $7$ string. verify(password, stored) answers whether password hashes to stored, comparing the derived key in constant time, and refuses by name a string it cannot check. parse(stored) is the parser both use, and encode is its inverse.
The string. crypt(5) gives $7$'s shape as \$7\$[./A-Za-z0-9]{11,97}\$[./A-Za-z0-9]{43} and nothing of its inner encoding. That encoding was learned from Ubuntu 24.04's crypt (libxcrypt 4.4.36) run as a black box, and is held to it by sc80's interop rows. After $7$ come: one character, log2 N; five, r; five, p; then the salt characters; then $ and 43 characters of the 32-octet derived key. The alphabet is crypt's ./0-9A-Za-z (value 0 to 63, in that order). r and p are 30-bit numbers, six bits a character, least significant first. The key is written three octets to four characters, the first octet in the low bits and the low six bits first; its last two octets take three characters, so the 43rd carries four bits and is canonical only below 16. The salt characters ARE scrypt's salt octets (they are not decoded first): scrypt(password, salt-string, N, r, p, 32).
What is refused, by name (lowercase marks): empty (an empty field, which shadow(5) reads as "no password"), locked (a field starting ! or *), unsupported (another crypt(5) method: $y$, $gy$, $2a$/$2b$/$2x$/$2y$, $6$, $5$, $sha1$, $md5, $1$, $3$, any other $id$ or $id, prefix, BSDi's _ form and the 13-to-178-character DES forms), malformed (any other shape, and a $7$ setting with no $ before a hash), bad_n (log2 N not a character of the alphabet, below 1, or not below 16·r), bad_rp (r or p not five characters of the alphabet, below 1, or r·p at least 2^30), bad_salt (a character outside the alphabet, or more than 86), bad_hash (not 43 characters of the alphabet, or a non-canonical last character), over_cap (scrypt's working memory over the cap) and bad_random (gen_salt's input). None of them traps.
The cap. verify holds a stored string's cost to default_cap(): scrypt.memory(18, 32, 1, 32), a little over 1 GiB, the largest cost Ubuntu's crypt_gensalt writes (mkpasswd -R 11). verify_capped takes a caller's own. hash uses default_cap() too, so every string it makes verifies under the default.
A named difference. crypt(3) reads the password as a C string and stops at a zero octet; hash and verify hash every octet of the List[byte] they are given. A password containing a zero octet therefore hashes differently here than under crypt(3).
What is and is not constant-time (D53's retrofit clause; c28 does not exist yet, so these are recorded, not verified): - verify's comparison of the derived key with the stored one is constant-time over the 32 octets: it ORs every octet's XOR into one accumulator and tests it once, with no early exit. - scrypt itself is NOT: ROMix indexes V by secret state, by design (std.x.crypto.scrypt's header). - The parser, the base-64 alphabet and encode branch and index on the stored string's characters. The string is what shadow(5) stores, and the salt and cost are public parts of it; the stored key is decoded with value-indexed lookups, which a local observer with a cache side channel could in principle see. - gen_salt indexes the alphabet by the random octets' values. A salt is public once stored. - Every path's control flow depends on the password's length (HMAC's key path, std.x.crypto.sha2), never on its octets.
Functions and methods
fn · Source line 115
pub fn cost(n_log2: int, r: int, p: int) -> Cost ! {bad_n, bad_rp}
The cost N = 2^n_log2, r, p, checked as a $7$ string must carry it. Rows: bad_rp when r or p is below 1 or r·p is at least 2^30 (five characters hold 30 bits, and RFC 7914 §2's bound on p); bad_n when n_log2 is below 1, above 63 (one character), or not below 16·r (RFC 7914 §2: N < 2^(128·r/8)). Never traps. ct: public parameters only.
let c = passwd.cost(14, 8, 1)?
c.n_log2 == 14 && c.r == 8 && c.p == 1
fn · Source line 145
pub fn default_cap() -> int
The working-memory ceiling verify and hash hold a cost to: scrypt.memory(18, 32, 1, 32) octets (N = 2^18, r = 32, p = 1, the largest cost Ubuntu's crypt_gensalt writes). Total. ct: a constant.
passwd.default_cap() > 1073741824
fn · Source line 317
pub fn gen_salt(random: List[byte]) -> str ! {bad_random}
A salt string from random, octets the caller read from the OS random source (getrandom(2) or /dev/urandom; std draws no entropy of its own): the octets in crypt's base-64, so 16 octets make the 22 characters Ubuntu's crypt_gensalt writes and 64 make the 86 a $7$ string holds at most. Rows: bad_random when random is shorter than 16 octets (crypt(5)'s 128 recommended bits), longer than 64, or all zero (an unfilled buffer, never a draw). Never traps. ct: NOT constant-time — the alphabet is indexed by the octets' values. A salt is public once it is stored.
passwd.gen_salt("0123456789abcdef".bytes())? == "k2XAnEHBqQ1Ct2aMXFKNa/"
fn · Source line 351
pub fn encode(h: Hashed) -> str
The $7$ string for h: $7$, log2 N, r and p, the salt, $, the 43-character key. The inverse of parse on every string parse accepts. Traps assert when h was not made by parse or hash (a digest that is not 32 octets, or a cost outside one character and thirty bits): the caller could have checked. ct: NOT constant-time — the alphabet is indexed by the key's octets.
let line = "$7$C6..../....yb/XZj1NCg1rb/5RytWq5/$N8T0E6rYyvGsPmqhviAsRS2f9A6wYzomUd6ouVMA/S2"
passwd.encode(passwd.parse(line)?) == line
fn · Source line 399
pub fn parse(stored: str) -> Hashed ! { empty, locked, unsupported, malformed, bad_n, bad_rp, bad_salt, bad_hash, }
Parses a stored crypt(5) field as a $7$ string. Rows, in this order: empty (no characters), locked (a leading ! or *, shadow(5)'s marks), unsupported (another method's form: a $id$ or $id, prefix other than $7$, BSDi's 20-character _ form, or the 13-to-178-character DES forms), malformed (any other shape, a $7$ string too short to hold its eleven parameter characters, or one with no $ before a hash), then bad_n, bad_rp, bad_salt and bad_hash as the module header defines them. Never traps. ct: branches on the string's characters (see the module header).
let h = passwd.parse("$7$C6..../....yb/XZj1NCg1rb/5RytWq5/$N8T0E6rYyvGsPmqhviAsRS2f9A6wYzomUd6ouVMA/S2")?
h.cost.n_log2 == 14 && h.cost.r == 8 && h.cost.p == 1
h.salt == "yb/XZj1NCg1rb/5RytWq5/" && h.digest.len == 32
fn · Source line 515
pub fn hash(password: List[byte], salt: str, c: Cost) -> str ! {bad_n, bad_rp, bad_salt, over_cap}
The $7$ string for password under salt and c: scrypt(password, salt, 2^n_log2, r, p, 32) behind the parameters, exactly as Ubuntu's crypt writes it for the same setting. salt is a string of 0 to 86 alphabet characters, normally gen_salt's. Rows: bad_salt (a character outside the alphabet, or more than 86), bad_n and bad_rp (a Cost edited past cost's checks), over_cap (the cost needs more than default_cap()). Never traps. ct: see the module header — the derived key's encoding indexes the alphabet by value; scrypt indexes V by secret state.
let c = passwd.cost(4, 1, 1)?
let line = passwd.hash("pleaseletmein".bytes(), "SodiumChloride", c)?
line == "$7$2/..../....SodiumChloride$vqI1uzLxlCqAVSFu53sXGjHrKfJFlaQ0Qh2wdmJ4QP/"
fn · Source line 555
pub fn verify_capped(password: List[byte], stored: str, cap: int) -> bool ! { empty, locked, unsupported, malformed, bad_n, bad_rp, bad_salt, bad_hash, over_cap, }
verify with the caller's own memory ceiling: whether password hashes to stored when scrypt's working memory for the stored cost is at most cap octets. Rows: every parse row, then over_cap. A wrong password is false, never a row. Never traps. ct: the key comparison is constant-time (the module header).
let line = passwd.hash("pleaseletmein".bytes(), "SodiumChloride", passwd.cost(4, 1, 1)?)?
passwd.verify_capped("pleaseletmein".bytes(), line, 65536)? == true
fn · Source line 590
pub fn verify(password: List[byte], stored: str) -> bool ! { empty, locked, unsupported, malformed, bad_n, bad_rp, bad_salt, bad_hash, over_cap, }
Whether password hashes to stored, a crypt(5) field: the stored $7$ string's own cost and salt are used, and the derived key is compared with the stored one in constant time. The cost is held to default_cap(). Rows: empty, locked, unsupported, malformed, bad_n, bad_rp, bad_salt, bad_hash (parse's), and over_cap (a stored cost over the cap). A wrong password is false, never a row. Never traps. ct: the key comparison is constant-time (the module header).
let line = passwd.hash("pleaseletmein".bytes(), "SodiumChloride", passwd.cost(4, 1, 1)?)?
passwd.verify("pleaseletmein".bytes(), line)? == true
var wrong = "pleaseletmein".bytes()
wrong[0] = 0 as byte
passwd.verify(wrong, line)? == false