Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-auth

Source of release 0.6.1

0.6.1 / src / password.lucb

## Versioned native password records with process-wide bounded KDF admission.
from luce_crypto import native as crypto_native

pub let invalid = crypto_native.invalid
pub let record_size: usize = 64
pub let busy: ErrorCode = ErrorCode.package(1)

## Four simultaneous KDFs; no waiting queue. Zero initialization is ready to use.
## Do not copy while in use; release exactly once per successful acquisition.
pub struct Admission:
    var active: @u32

    pub func try_acquire() -> bool:
        var seen = self.active.load(.acquire)
        while seen < 4:
            let (taken, current) = self.active.cas(seen, seen + 1, .acq_rel, .acquire)
            if taken: return true
            seen = current
        return false

    pub func release():
        _ = self.active.sub(1, .release)

var admission: Admission

pub enum Profile as u8:
    interactive = 1
    test_only = 2

func parameters(profile: Profile) -> crypto_native.Argon2Parameters!:
    if profile == Profile.interactive:
        return crypto_native.Argon2Parameters(memory_kib = 65536, passes = 3, lanes = 4)
    if profile == Profile.test_only:
        return crypto_native.Argon2Parameters(memory_kib = 32, passes = 1, lanes = 1)
    error(invalid, "unknown password profile")

func put32(wire: u8[], at: usize, value: u32):
    for i in 0..<(usize)4: wire[at + i] = (u8)((value >> (i * 8)) & 255)

func get32(wire: const u8[], at: usize) -> u32:
    var value: u32 = 0
    for i in 0..<(usize)4: value |= (u32)wire[at + i] << (i * 8)
    return value

func check_password(password: const u8[]) -> !:
    if password.length == 0 or password.length > 1024: error(invalid, "password byte length outside bounds")

## Fixed LAP1 record: magic, little-endian memory/passes/lanes, salt16, hash32.
## Only explicitly supported profiles are accepted, before expensive work.
pub func profile_of(wire: const u8[], allow_test: bool = false) -> Profile!:
    if wire.length != record_size: error(invalid, "password record size invalid")
    if (str)wire[..<4] != "LAP1": error(invalid, "password record version invalid")
    let memory = get32(wire, 4)
    let passes = get32(wire, 8)
    let lanes = get32(wire, 12)
    if memory == 65536 and passes == 3 and lanes == 4: return Profile.interactive
    if allow_test and memory == 32 and passes == 1 and lanes == 1: return Profile.test_only
    error(invalid, "unsupported or disallowed password cost profile")

## Output unchanged on failure. Random16-byte salt; no secret serialization.
pub func create(password: const u8[], output: u8[], profile: Profile = Profile.interactive) -> !:
    try check_password(password)
    if output.length != record_size: error(invalid, "password record output size invalid")
    let costs = try parameters(profile)
    if not admission.try_acquire(): error(busy, "password hashing capacity exhausted")
    defer admission.release()
    var record: u8[64]
    defer crypto_native.wipe(record)
    for i in 0..<(usize)4: record[i] = b"LAP1"[i]
    put32(record, 4, costs.memory_kib)
    put32(record, 8, costs.passes)
    put32(record, 12, costs.lanes)
    var salt = try crypto_native.random_secret(16)
    defer salt.close()
    let bytes = try salt.bytes()
    for i in 0..<(usize)16: record[16 + i] = bytes[i]
    try crypto_native.argon2id(password, record[16..<32], record[32..], costs, workers = 4 if profile == Profile.interactive else 1)
    for i in 0..<record_size: output[i] = record[i]

pub func verify(password: const u8[], wire: const u8[], allow_test: bool = false) -> bool!:
    try check_password(password)
    let profile = try profile_of(wire, allow_test)
    let costs = try parameters(profile)
    if not admission.try_acquire(): error(busy, "password hashing capacity exhausted")
    defer admission.release()
    var actual: u8[32]
    defer crypto_native.wipe(actual)
    try crypto_native.argon2id(password, wire[16..<32], actual, costs, workers = 4 if profile == Profile.interactive else 1)
    return crypto_native.equal(actual, wire[32..])