8000 Stdlib: password hashing, HMAC and constant-time comparison (`Crypto`) · Issue #459 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Stdlib: password hashing, HMAC and constant-time comparison (Crypto) #459

Open

Description

@akoskovacs

The stdlib has Digest.sha256 and secure random bytes (#404). Authentication needs three more things, all of which are one call into OTP's crypto but are untyped Any through BEAM.* today:

Need What an application writes now
A password KDF BEAM.crypto.pbkdf2_hmac(:sha256, password, salt, 600000, 32)
Constant-time comparison BEAM.crypto.hash_equals(a, b) == true
HMAC, for signed cookies and tokens BEAM.crypto.mac(:hmac, :sha256, key, data) (not used yet; server-stored sessions avoided it)
Hex encoding of the results "${BEAM.binary.encode_hex(bytes)}", BEAM.binary.decode_hex(text)

Getting any of these subtly wrong is a security bug, which is the argument for one reviewed implementation over one per application.

A possible surface:

Crypto.Password.hash(password)            # -> String, self-describing: scheme$rounds$salt$key
Crypto.Password.verify(password, stored)  # -> Bool, constant time
Crypto.hmac(:sha256, key, data)           # -> Binary
Crypto.equal?(a, b)                       # -> Bool, constant time

docs/web-stack-plan.md lists Crypto among the wrappers to promote into the stdlib once a framework has proved the API; a working userland version of the first two rows now exists in an application (PBKDF2-HMAC-SHA256, per-password salt, parameters stored with the hash) and can be contributed as a starting point.

Environment: Kex 0.4.0-rc.1 (30c7979), Tey 0.2.1, Erlang/OTP 29.0.5 (Homebrew), macOS arm64. Found while building an application on Rodolfo 0.2 and Harmat 0.1.

Activity

  1. akoskovacs commented on Oct 15, 2026

    @akoskovacs
    CollaboratorAuthor

    Another use case: an S3-compatible storage backend for Metro (Metro.Storage). Signing a request for S3 (AWS Signature Version 4) chains HMAC-SHA256, each key being the previous step's binary digest:

    kDate    = HMAC("AWS4" + secret, date)
    kRegion  = HMAC(kDate, region)
    kService = HMAC(kRegion, "s3")
    kSigning = HMAC(kService, "aws4_request")
    signature = hex(HMAC(kSigning, stringToSign))
    

    So beyond Crypto.hmacSha256(key, message), it needs to:

    • take a Binary key and return Binary, with .hex for the last step;
    • not only work on text.

    Digest.sha256(Binary) -> Binary already exists, which covers the payload hash. Until HMAC exists, Metro can only keep uploads in a folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0