~/bend-docscommunity

Bcrypt.bend source

Bcrypt.bend on the hub · documented module

import Baseimport ./Pi.bend as Pi# Bcrypt: the password hash of Provos and Mazières (bcrypt's job in the# Haskell backend: Crypto.BCrypt, Openwall's crypt_blowfish underneath).## What it means. bcrypt(cost, salt, key) is a function of a cost (4..31), a# salt (16 bytes) and a key (the password's bytes, then a 0; only the first# 72 bytes of it, taken over and over, count): Blowfish's state, set up from# pi's digits, has the key and salt folded into it by ExpandKey once, then# 2^cost times the key and the salt alone, each by ExpandKey with no salt;# the 24 bytes "OrpheanBeholderScryDoubt", enciphered 64 times under the# state that results, are the hash (its first 23 bytes). Its text is# "$2y$", the cost in two digits, "$", the salt in 22 characters and the hash# in 31, in bcrypt's own base 64 ("./A-Za-z0-9", the bits big-endian, no# padding). A password is right for a text when the hash it gives with the# text's cost and salt is the text's hash.## What is checked how: that this is bcrypt is not a thing Bend can prove (as# a hash being SHA-256): probe/Bcrypt.bend checks it on the vectors of# OpenBSD's and Openwall's tests, and on hashes the Haskell backend's# library made (the same password, salt and cost give the same text). The# base 64 here is the inverse of itself on whole bytes (a law, PROPOSED.bend).## "$2b$" and "$2a$" texts are read as "$2y$" ones: the same function, but for# Openwall's "$2a$" on a password with bytes past 127 (its countermeasure for# an old bug), which this does not follow.## Every def is prefixed `bc.` (a program's defs share one namespace).# The state# ---------## Blowfish's state is one array of 2048 words (an Array's size is a power of# two): S-box k (0..3) at 256k .. 256k+255, the P-array at 1024 .. 1041.# the state beside a block (l, r): what an enciphering hands backtype bc.B is Type:  bc.B{a: Array<U32>, l: U32, r: U32}# where pi's i-th word goes: the first 18 are P's, the next 1024 the S-boxes'def bc.at(+i: U32) -> U32:  Bool.pick(U32, U32.is_lt(i, 18), (i + 1024 : U32), (i - 18 : U32))def bc.load(xs: List<&2, U32>, a: Array<U32>, +i: U32) -> Array<U32>:  match xs:    case Nil{}:      a    case Con{x, t}:      bc.load(t, Array.set(U32, a, bc.at(i), x), (i + 1 : U32))# Blowfish's initial state: pi's digitsdef bc.init() -> Array<U32>:  bc.load(Pi.bc.pi(), [0 : U32*2048n], 0)# Blowfish# --------def bc.f4(r: Array<U32> & U32, +s: U32) -> Array<U32> & U32:  match r:    case (a, s3):      (a, (s + s3 : U32))def bc.f3(r: Array<U32> & U32, +s: U32, +x: U32) -> Array<U32> & U32:  match r:    case (a, s2):      bc.f4(Array.get(U32, a, (768 + U32.and(x, 255) : U32)), U32.xor(s, s2))def bc.f2(r: Array<U32> & U32, +s0: U32, +x: U32) -> Array<U32> & U32:  match r:    case (a, s1):      bc.f3(Array.get(U32, a, (512 + U32.and(U32.shrn(x, 8n), 255) : U32)), (s0 + s1 : U32), x)def bc.f1(r: Array<U32> & U32, +x: U32) -> Array<U32> & U32:  match r:    case (a, s0):      bc.f2(Array.get(U32, a, (256 + U32.and(U32.shrn(x, 16n), 255) : U32)), s0, x)# Blowfish's F: ((S0[x >> 24] + S1[x >> 16 & 255]) ^ S2[x >> 8 & 255]) + S3[x & 255]def bc.f(a: Array<U32>, +x: U32) -> Array<U32> & U32:  bc.f1(Array.get(U32, a, U32.shrn(x, 24n)), x)def bc.half.p(r: Array<U32> & U32, +fx: U32, +y: U32) -> Array<U32> & U32:  match r:    case (a, p):      (a, U32.xor(U32.xor(y, fx), p))def bc.half.f(r: Array<U32> & U32, +i: U32, +y: U32) -> Array<U32> & U32:  match r:    case (a, fx):      bc.half.p(Array.get(U32, a, (1024 + i : U32)), fx, y)# a half round: y ^ F(x) ^ P[i]def bc.half(a: Array<U32>, +x: U32, +y: U32, +i: U32) -> Array<U32> & U32:  bc.half.f(bc.f(a, x), i, y)# n half rounds from P[i]: `cur` the half last made (beside the array),# `prev` the other; they end as (cur, prev)def bc.go(n: Nat, r: Array<U32> & U32, +prev: U32, +i: U32) -> bc.B:  match n r:    case 0n Tuple{a, +cur}:      bc.B{a, cur, prev}    case 1n+p Tuple{a, +cur}:      bc.go(p, bc.half(a, cur, prev, i), cur, (i + 1 : U32))def bc.enc.fin(r: Array<U32> & U32, +xl: U32, +xr: U32) -> bc.B:  match r:    case (a, p17):      bc.B{a, U32.xor(xr, p17), xl}def bc.enc.out(r: bc.B) -> bc.B:  match r:    case bc.B{a, +xl, +xr}:      bc.enc.fin(Array.get(U32, a, 1041), xl, xr)def bc.enc.in(r: Array<U32> & U32, +l: U32, +x: U32) -> bc.B:  match r:    case (a, p0):      bc.enc.out(bc.go(16n, (a, U32.xor(l, p0)), x, 1))# Blowfish's encryption of the block (l, r) under the state: l ^= P[0], 16# rounds, then (r ^ P[17], l)def bc.enc(a: Array<U32>, +l: U32, +r: U32) -> bc.B:  bc.enc.in(Array.get(U32, a, 1024), l, r)# The key schedule# ----------------def bc.xorp.at(r: Array<U32> & U32, +i: U32, +k: U32) -> Array<U32>:  match r:    case (a, p):      Array.set(U32, a, (1024 + i : U32), U32.xor(p, k))# the key's words xored into P, from P[i]def bc.xorp(ks: List<&2, U32>, a: Array<U32>, +i: U32) -> Array<U32>:  match ks:    case Nil{}:      a    case Con{+k, t}:      bc.xorp(t, bc.xorp.at(Array.get(U32, a, (1024 + i : U32)), i, k), (i + 1 : U32))def bc.store(r: bc.B, +at: U32) -> bc.B:  match r:    case bc.B{a, +l, +x}:      bc.B{Array.set(U32, Array.set(U32, a, at, l), (at + 1 : U32), x), l, x}# n blocks enciphered in a chain, each block first xored with the salt's# next two words (s0, s1: the four take turns), stored from `at` ondef bc.fill(n: Nat, r: bc.B, +at: U32, +s0: U32, +s1: U32, +s2: U32, +s3: U32) -> bc.B:  match n r:    case 0n x:      x    case 1n+p bc.B{a, l, x}:      bc.fill(p, bc.store(bc.enc(a, U32.xor(l, s0), U32.xor(x, s1)), at), (at + 2 : U32), s2, s3, s0, s1)def bc.arr(r: bc.B) -> Array<U32>:  match r:    case bc.B{a, l, x}:      a# (P's 9 blocks used the salt's words 0 to 17: the S-boxes' start at word 2)def bc.expand.s(r: bc.B, +s0: U32, +s1: U32, +s2: U32, +s3: U32) -> Array<U32>:  bc.arr(bc.fill(512n, r, 0, s2, s3, s0, s1))# ExpandKey(state, salt, key): the key's 18 words xored into P, then P and# the S-boxes made again in a chain of encipherings, each block xored first# with the salt's next words (s0..s3; zeros for no salt)def bc.expand(a: Array<U32>, +ks: List<&2, U32>, +s0: U32, +s1: U32, +s2: U32, +s3: U32) -> Array<U32>:  bc.expand.s(bc.fill(9n, bc.B{bc.xorp(ks, a, 0), 0, 0}, 1024, s0, s1, s2, s3), s0, s1, s2, s3)# n times ExpandKey(state, 0, key), then ExpandKey(state, 0, salt)def bc.rounds(n: Nat, a: Array<U32>, +ks: List<&2, U32>, +ss: List<&2, U32>) -> Array<U32>:  match n:    case 0n:      a    case 1n+p:      bc.rounds(p, bc.expand(bc.expand(a, ks, 0, 0, 0, 0), ss, 0, 0, 0, 0), ks, ss)# Bytes and words# ---------------# n bytes of `all`, taken over and over (from cur, the rest of this turn)def bc.cyc(n: Nat, cur: List<&2, U32>, +all: List<&2, U32>) -> List<&2, U32>:  match n cur all:    case 0n x y:      []    case 1n+p Con{b, t} y:      b <> bc.cyc(p, t, y)    case 1n+p Nil{} Con{+b, +t}:      b <> bc.cyc(p, t, b <> t)    case 1n+p Nil{} Nil{}:      []# bytes as big-endian words, 4 a worddef bc.words(bs: List<&2, U32>) -> List<&2, U32>:  match bs:    case Con{+a, Con{+b, Con{+c, Con{+d, t}}}}:      U32.or(U32.or(U32.shln(a, 24n), U32.shln(b, 16n)), U32.or(U32.shln(c, 8n), d)) <> bc.words(t)    case _:      []# words as big-endian bytesdef bc.bytes(ws: List<&2, U32>) -> List<&2, U32>:  match ws:    case Nil{}:      []    case Con{+x, t}:      U32.shrn(x, 24n) <> U32.and(U32.shrn(x, 16n), 255) <> U32.and(U32.shrn(x, 8n), 255) <> U32.and(x, 255) <> bc.bytes(t)# EksBlowfishSetup(cost, salt, key)def bc.setup(+ks: List<&2, U32>, sw: List<&2, U32>) -> Array<U32>:  match sw:    case Con{+s0, Con{+s1, Con{+s2, Con{+s3, t}}}}:      bc.expand(bc.init(), ks, s0, s1, s2, s3)    case _:      bc.expand(bc.init(), ks, 0, 0, 0, 0)def bc.eks(+cost: Nat, +salt: List<&2, U32>, +key: List<&2, U32>) -> Array<U32>:  bc.rounds(Nat.pow(2n, cost), bc.setup(bc.words(bc.cyc(72n, key, key)), bc.words(salt)), bc.words(bc.cyc(72n, key, key)), bc.words(bc.cyc(72n, salt, salt)))def bc.ecb(n: Nat, r: bc.B) -> bc.B:  match n r:    case 0n x:      x    case 1n+p bc.B{a, +l, +x}:      bc.ecb(p, bc.enc(a, l, x))def bc.out3(r: bc.B, +w0: U32, +w1: U32, +w2: U32, +w3: U32) -> List<&2, U32>:  match r:    case bc.B{a, +w4, +w5}:      [w0, w1, w2, w3, w4, w5]def bc.out2(r: bc.B, +w0: U32, +w1: U32, +w4: U32, +w5: U32) -> List<&2, U32>:  match r:    case bc.B{a, +w2, +w3}:      bc.out3(bc.ecb(64n, bc.B{a, w4, w5}), w0, w1, w2, w3)def bc.out1(r: bc.B, +w2: U32, +w3: U32, +w4: U32, +w5: U32) -> List<&2, U32>:  match r:    case bc.B{a, +w0, +w1}:      bc.out2(bc.ecb(64n, bc.B{a, w2, w3}), w0, w1, w4, w5)def bc.text.bytes(s: String) -> List<&2, U32>:  match s:    case SNil{}:      []    case SCon{Chr{c}, t}:      c <> bc.text.bytes(t)def bc.cipher.ws(a: Array<U32>, ws: List<&2, U32>) -> List<&2, U32>:  match ws:    case Con{+w0, Con{+w1, Con{+w2, Con{+w3, Con{+w4, Con{+w5, t}}}}}}:      bc.out1(bc.ecb(64n, bc.B{a, w0, w1}), w2, w3, w4, w5)    case _:      []# "OrpheanBeholderScryDoubt" (6 words) enciphered 64 times under the state# (each of its 3 blocks alone: ECB)def bc.cipher(a: Array<U32>) -> List<&2, U32>:  bc.cipher.ws(a, bc.words(bc.text.bytes("OrpheanBeholderScryDoubt")))# bcrypt's 23 bytes for the cost, the salt (16 bytes) and the password's bytesdef bc.raw(+cost: Nat, +salt: List<&2, U32>, +pw: List<&2, U32>) -> List<&2, U32>:  List.take(&2, U32, bc.bytes(bc.cipher(bc.eks(cost, salt, List.append(&2, U32, pw, [0])))), 23n)# Base 64, bcrypt's# -----------------# a 6-bit value's character: "./", "A".."Z", "a".."z", "0".."9"def bc.b64.c(+v: U32) -> Char:  Chr{Bool.pick(U32, U32.is_lt(v, 2), (v + 46 : U32), Bool.pick(U32, U32.is_lt(v, 28), (v + 63 : U32), Bool.pick(U32, U32.is_lt(v, 54), (v + 69 : U32), (v - 6 : U32))))}# a character's 6-bit value (64: none)def bc.b64.v(+c: U32) -> U32:  Bool.pick(U32, U32.is_lt(c, 46), 64,  Bool.pick(U32, U32.is_lt(c, 48), (c - 46 : U32),  Bool.pick(U32, U32.is_lt(c, 58), (c + 6 : U32),  Bool.pick(U32, U32.is_lt(c, 65), 64,  Bool.pick(U32, U32.is_lt(c, 91), (c - 63 : U32),  Bool.pick(U32, U32.is_lt(c, 97), 64,  Bool.pick(U32, U32.is_lt(c, 123), (c - 69 : U32), 64)))))))# bytes as text: 3 bytes to 4 characters; a last 1 or 2 to 2 or 3def bc.enc64(bs: List<&2, U32>) -> String:  match bs:    case Con{+a, Con{+b, Con{+c, t}}}:      SCon{bc.b64.c(U32.shrn(a, 2n)), SCon{bc.b64.c(U32.or(U32.shln(U32.and(a, 3), 4n), U32.shrn(b, 4n))),        SCon{bc.b64.c(U32.or(U32.shln(U32.and(b, 15), 2n), U32.shrn(c, 6n))), SCon{bc.b64.c(U32.and(c, 63)), bc.enc64(t)}}}}    case Con{+a, Con{+b, Nil{}}}:      SCon{bc.b64.c(U32.shrn(a, 2n)), SCon{bc.b64.c(U32.or(U32.shln(U32.and(a, 3), 4n), U32.shrn(b, 4n))), SCon{bc.b64.c(U32.shln(U32.and(b, 15), 2n)), ""}}}    case Con{+a, Nil{}}:      SCon{bc.b64.c(U32.shrn(a, 2n)), SCon{bc.b64.c(U32.shln(U32.and(a, 3), 4n)), ""}}    case Nil{}:      ""# text as bytes, the inverse (the bits past the last whole byte dropped)def bc.dec64(s: String) -> List<&2, U32>:  match s:    case SCon{Chr{+a}, SCon{Chr{+b}, SCon{Chr{+c}, SCon{Chr{+d}, t}}}}:      U32.and(U32.or(U32.shln(bc.b64.v(a), 2n), U32.shrn(bc.b64.v(b), 4n)), 255)        <> U32.and(U32.or(U32.shln(bc.b64.v(b), 4n), U32.shrn(bc.b64.v(c), 2n)), 255)        <> U32.and(U32.or(U32.shln(bc.b64.v(c), 6n), bc.b64.v(d)), 255) <> bc.dec64(t)    case SCon{Chr{+a}, SCon{Chr{+b}, SCon{Chr{+c}, SNil{}}}}:      [U32.and(U32.or(U32.shln(bc.b64.v(a), 2n), U32.shrn(bc.b64.v(b), 4n)), 255),       U32.and(U32.or(U32.shln(bc.b64.v(b), 4n), U32.shrn(bc.b64.v(c), 2n)), 255)]    case SCon{Chr{+a}, SCon{Chr{+b}, SNil{}}}:      [U32.and(U32.or(U32.shln(bc.b64.v(a), 2n), U32.shrn(bc.b64.v(b), 4n)), 255)]    case _:      []# every character one of bcrypt's 64def bc.b64.ok(s: String) -> Bool:  match s:    case SNil{}:      True{}    case SCon{Chr{+c}, t}:      U32.is_lt(bc.b64.v(c), 64) && bc.b64.ok(t)# The text# --------def bc.two(+n: Nat) -> String:  Bool.pick(String, Nat.is_lt(n, 10n), "0" ++ Nat.show(n), Nat.show(n))# The text of bcrypt for the cost (4..31), the salt (16 bytes) and the password's bytesdef bc.hash(+cost: Nat, +salt: List<&2, U32>, +pw: List<&2, U32>) -> String:  "$2y$" ++ bc.two(cost) ++ "$" ++ bc.enc64(salt) ++ bc.enc64(bc.raw(cost, salt, pw))# A text read: its cost, its salt's 22 characters, its hash's 31type bc.Set is Data:  bc.Set{cost: Nat, salt: String, hash: String}def bc.digit(+c: U32) -> Bool:  U32.is_ge(c, 48) && U32.is_le(c, 57)def bc.read.at(ok: Bool, +cost: U32, +rest: String) -> Maybe<&2, bc.Set>:  match ok:    case True{}:      Some{bc.Set{U32.to_nat(cost), String.take(rest, 22n), String.drop(rest, 22n)}}    case False{}:      None{}# "$2y$", "$2b$" or "$2a$", two digits (04..31), "$", 53 of bcrypt's charactersdef bc.read(s: String) -> Maybe<&2, bc.Set>:  match s:    case SCon{Chr{+d0}, SCon{Chr{+two}, SCon{Chr{+v}, SCon{Chr{+d1}, SCon{Chr{+c1}, SCon{Chr{+c2}, SCon{Chr{+d2}, +rest}}}}}}}:      bc.read.at(U32.is_eq(d0, 36) && U32.is_eq(two, 50) && (U32.is_eq(v, 121) || U32.is_eq(v, 98) || U32.is_eq(v, 97))          && U32.is_eq(d1, 36) && U32.is_eq(d2, 36) && bc.digit(c1) && bc.digit(c2)          && U32.is_ge(((c1 - 48) * 10 + (c2 - 48) : U32), 4) && U32.is_le(((c1 - 48) * 10 + (c2 - 48) : U32), 31)          && Nat.is_eq(String.length(rest), 53n) && bc.b64.ok(rest),        ((c1 - 48) * 10 + (c2 - 48) : U32), rest)    case _:      None{}def bc.check.set(m: Maybe<&2, bc.Set>, +pw: List<&2, U32>) -> Bool:  match m:    case None{}:      False{}    case Some{bc.Set{+cost, salt, hash}}:      String.eq(bc.enc64(bc.raw(cost, List.take(&2, U32, bc.dec64(salt), 16n), pw)), hash)# Whether the password's bytes are right for the text: the hash they give# with its cost and salt is its hash (a text that does not read: never)def bc.check(+pw: List<&2, U32>, text: String) -> Bool:  bc.check.set(bc.read(text), pw)