pbkdf2.bend checks
raw source on the hub · import bend-osdev-crypto@0.1.0.0/pbkdf2.bend as Pbkdf2
PBKDF2 over any MAC (RFC 8018, PKCS #5 v2.1, section 5.2), on the MAC interface (mac.bend) as its pseudorandom function PRF: DK = T_1 || T_2 || ... , cut to dkLen bytes, T_i = F(P, S, c, i) = U_1 xor U_2 xor ... xor U_c, U_1 = PRF(P, S || INT(i)), U_j = PRF(P, U_(j-1)), INT(i) the four-byte big-endian block index, from 1. The PRF's key is the password P; a MAC that refuses P's length refuses the derivation (its own CrError). In practice the PRF is HMAC over a hash (hmac.bend); any MAC with hLen-byte tags will do.
Refused (RFC 8018 step 1, and the iteration count's lower bound): an iteration count c of 0 (CrTooShort{0}: too few iterations), an output longer than (2^32 - 1) hLen bytes, or any output when hLen is 0 (CrBadOutputLength{dkLen}).
The output is generated a byte at a time, each T_i computed when the bytes before it are used up, so its length is dkLen for every MAC and input (LAWS.bend: pbkdf2_length), and a shorter output is a prefix of a longer one (pbkdf2_stream_prefix). As a KDF (kdf.bend), PBKDF2 over a MAC (~K, ~key, ~compute, and its tag length ~hlen) with c iterations is ~CrPbkdf2.derive(~K, ~key, ~compute, ~hlen, ~c) whose context argument (info) is unused: PBKDF2 has none. The named instances (PBKDF2-HMAC-SHA-256 and PBKDF2-HMAC-SHA-1) are in inst_pbkdf2.bend.
3 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./crypto.bend as Cr
Definitions
def cr.pbkdf2.xor source · line 30 · raw
@xs:List<&2, U32> -> @ys:List<&2, U32> -> List<&2, U32>
bytewise XOR of two tags (as long as the shorter)
def cr.pbkdf2.head source · line 51 · raw
@xs:List<&2, U32> -> U32
def cr.pbkdf2.tail source · line 58 · raw
@xs:List<&2, U32> -> List<&2, U32>
def cr.pbkdf2.fits source · line 103 · raw
@pos:Bool -> @+n:Nat -> @+hlen:Nat -> Bool
Templates
template cr.pbkdf2.chain source · line 38 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @c:Nat -> @+k:K -> @u:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @acc:List<&2, U32> -> List<&2, U32>
acc xor U_(j+1) xor ... for c more iterations, u = U_j
template cr.pbkdf2.block source · line 47 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @+k:K -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:Nat -> @i:U32 -> List<&2, U32>
T_i, with c the iteration count less one
template cr.pbkdf2.out source · line 67 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @n:Nat -> @cur:List<&2, U32> -> @+k:K -> @+salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+c:Nat -> @+i:U32 -> List<&2, U32>
n more bytes: the rest of the current block cur, then T_(i+1), T_(i+2), ... (a MAC whose tags are empty gives zeros)
template CrPbkdf2.stream source · line 82 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @k:K -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:U32 -> @n:Nat -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
The first n bytes of the derived key, under a prepared key k (the MAC's, from the password) and an iteration count c of at least 1, without the checks: T_1 || T_2 || ... cut to n bytes
template cr.pbkdf2.key source · line 85 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @kr:Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K> -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:U32 -> @n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
template cr.pbkdf2.checked source · line 92 · raw
@-K:Data -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @zero:Bool -> @fits:Bool -> @kr:Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K> -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @c:U32 -> @+n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
template CrPbkdf2.pbkdf2 source · line 112 · raw
@-K:Data -> @-key:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K>) -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-hlen:Nat -> @password:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @+c:U32 -> @+n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
DK = PBKDF2(P, S, c, dkLen) over the MAC (~K, ~key, ~compute) with hLen-byte tags (~hlen)
template CrPbkdf2.derive source · line 117 · raw
@-K:Data -> @-key:(@_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, K>) -> @-compute:(@_:K -> @_:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets) -> @-hlen:Nat -> @-c:U32 -> @secret:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @salt:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @info:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @n:Nat -> Result<&2, &2, 0x01313a4f070f7ffd2bd276fcd2143eda/crypto.CrError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
The KDF interface's ~derive (secret, salt, info, length) for c iterations; info is unused