inst_pbkdf2.bend

bend-osdev-crypto@0.1.0.0 · 21 lines · 2 definitions

1# The named PBKDF2 instances (pbkdf2.bend): PBKDF2-HMAC-SHA-256 (over
2# bend-collections' SHA-256, inst_sha2.bend) and PBKDF2-HMAC-SHA-1 (over the
3# hashes package's SHA-1), each taking the password, salt, iteration count
4# and output length. As KDFs (kdf.bend), with c iterations:
5#   ~Pb.CrPbkdf2.derive(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~SH.CrSha2.sha256), ~Hm.CrHmac.mac(~SH.CrSha2.sha256), ~32n, ~c)
6#   ~Pb.CrPbkdf2.derive(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~S1.HsSha1.digest), ~Hm.CrHmac.mac(~S1.HsSha1.digest), ~20n, ~c)
7# PBKDF2 over HMAC over any other hash is the same tuple with that hash's
8# block size, one-shot digest and digest size.
9import Base
10import bend-osdev-bytes@0.1.0.0/bytes.bend as B
11import bend-osdev-hashes@0.1.0.0/sha1.bend as S1
12import bend-osdev-crypto@0.1.0.0/crypto.bend as Cr
13import bend-osdev-crypto@0.1.0.0/hmac.bend as Hm
14import bend-osdev-crypto@0.1.0.0/pbkdf2.bend as Pb
15import bend-osdev-crypto@0.1.0.0/inst_sha2.bend as SH
16
17def CrPbkdf2.hmac_sha256(password: B.Octets, salt: B.Octets, c: U32, n: Nat) -> Result<&2, &2, Cr.CrError, B.Octets>:
18  Pb.CrPbkdf2.pbkdf2(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~SH.CrSha2.sha256), ~Hm.CrHmac.mac(~SH.CrSha2.sha256), ~32n, password, salt, c, n)
19
20def CrPbkdf2.hmac_sha1(password: B.Octets, salt: B.Octets, c: U32, n: Nat) -> Result<&2, &2, Cr.CrError, B.Octets>:
21  Pb.CrPbkdf2.pbkdf2(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~S1.HsSha1.digest), ~Hm.CrHmac.mac(~S1.HsSha1.digest), ~20n, password, salt, c, n)