Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-crypto

Source of release 0.6.1

0.6.1 / src / native.lucb

## Public native facade; arithmetic/test helpers are not exported here.
import sha2
import hmac as mac
import hkdf as kdf
import secret
import secure
import argon2id as password_kdf
import aead
import gcm
import sha1
import shake
import mldsa
import x25519 as curve
import p256
import p384
import rsa

pub type Hash = sha2.Hash
pub type Hmac = mac.Hmac
pub type Secret = secret.Buffer
pub let invalid = sha2.invalid
pub let closed = sha2.closed
pub let length_exceeded = sha2.length_exceeded
pub let cancelled = password_kdf.cancelled
pub type Argon2Parameters = password_kdf.Parameters
pub type Argon2Limits = password_kdf.Limits
pub type Cancellation = password_kdf.Cancellation

pub func argon2id(password: const u8[], salt: const u8[], output: u8[], parameters: Argon2Parameters,
    key: const u8[] = b"", associated: const u8[] = b"", workers: usize = 1,
    limits: Argon2Limits = Argon2Limits(), cancellation: const Cancellation*? = none) -> !:
    try password_kdf.derive(password, salt, output, parameters, key, associated, workers, limits, cancellation)

pub func make_hash(algorithm: str = "sha256") -> Hash!:
    return try sha2.make_hash(algorithm)

pub func digest(input: const u8[], output: u8[], algorithm: str = "sha256") -> usize!:
    return try sha2.digest(input, output, algorithm)

pub func make_hmac(key: const u8[], algorithm: str = "sha256") -> Hmac!:
    return try mac.make_hmac(key, algorithm)

pub func authenticate(key: const u8[], input: const u8[], output: u8[], algorithm: str = "sha256") -> usize!:
    return try mac.authenticate(key, input, output, algorithm)

pub func hkdf_extract(input: const u8[], salt: const u8[], output: u8[], algorithm: str = "sha256") -> usize!:
    return try kdf.extract(input, salt, output, algorithm)

pub func hkdf_expand(key: const u8[], info: const u8[], output: u8[], algorithm: str = "sha256") -> !:
    try kdf.expand(key, info, output, algorithm)

pub func hkdf(input: const u8[], salt: const u8[], info: const u8[], output: u8[], algorithm: str = "sha256") -> !:
    try kdf.derive(input, salt, info, output, algorithm)

pub func secret_from_bytes(input: const u8[]) -> Secret!:
    return try secret.from_bytes(input)

pub func random_secret(count: usize) -> Secret!:
    return try secret.random(count)

pub let forged = aead.forged

pub func aead_seal(key: const u8[], nonce: const u8[], aad: const u8[], plaintext: const u8[], ciphertext: u8[], tag: u8[]) -> !:
    try aead.seal(key, nonce, aad, plaintext, ciphertext, tag)

pub func aead_open(key: const u8[], nonce: const u8[], aad: const u8[], ciphertext: const u8[], tag: const u8[], plaintext: u8[]) -> !:
    try aead.open(key, nonce, aad, ciphertext, tag, plaintext)

## AES-GCM (SP 800-38D) with a 16-, 24- or 32-byte key, a nonce of any length
## (12 bytes is the norm) and a tag of `tag.length` bytes (16, or 4, 8, 12-15).
## Output may be the same bytes as input. Constant-time bitsliced AES.
pub func aes_gcm_seal(key: const u8[], nonce: const u8[], aad: const u8[], plaintext: const u8[], ciphertext: u8[], tag: u8[]) -> !:
    try gcm.seal(key, nonce, aad, plaintext, ciphertext, tag)

## Verify the `tag.length`-byte tag first; a forged message fails with `forged`
## and is not decrypted.
pub func aes_gcm_open(key: const u8[], nonce: const u8[], aad: const u8[], ciphertext: const u8[], tag: const u8[], plaintext: u8[]) -> !:
    try gcm.open(key, nonce, aad, ciphertext, tag, plaintext)

pub func sha1_digest(input: const u8[], output: u8[]) -> usize!:
    return try sha1.digest(input, output)

pub func shake256(input: const u8[], output: u8[]) -> !:
    try shake.shake256(input, output)

pub func shake128(input: const u8[], output: u8[]) -> !:
    try shake.shake128(input, output)

pub func mldsa_keygen(seed: const u8[], pk: u8[], sk: u8[]) -> !:
    try mldsa.keygen(seed, pk, sk)

pub func mldsa_sign(sk: const u8[], msg: const u8[], rnd: const u8[], sig: u8[]) -> !:
    try mldsa.sign(sk, msg, rnd, sig)

pub func mldsa_verify(pk: const u8[], msg: const u8[], sig: const u8[]) -> bool!:
    return try mldsa.verify(pk, msg, sig)

## X25519 (RFC 7748): `out` = `scalar` * `point`, and against the base point.
pub func x25519(out: u8[], scalar: const u8[], point: const u8[]) -> !:
    try curve.scalarmult(out, scalar, point)

pub func x25519_base(out: u8[], scalar: const u8[]) -> !:
    try curve.scalarmult_base(out, scalar)

## Verify an ECDSA P-256 signature: the key coordinates and r, s are 32
## big-endian bytes; the digest's leftmost 256 bits are used.
pub func ecdsa_p256_verify(qx: const u8[], qy: const u8[], message: const u8[], r: const u8[], s: const u8[]) -> bool!:
    return try p256.verify(qx, qy, message, r, s)

pub func ecdsa_p256_public(d: const u8[], qx: u8[], qy: u8[]) -> !:
    try p256.public_from_private(d, qx, qy)

pub func ecdsa_p256_keygen(d: u8[], qx: u8[], qy: u8[]) -> !:
    try p256.keygen(d, qx, qy)

## P-256 ECDH: the 32-byte x-coordinate of d·Q for a validated peer point.
pub func ecdh_p256(d: const u8[], qx: const u8[], qy: const u8[], shared: u8[]) -> !:
    try p256.ecdh(d, qx, qy, shared)

## Sign a SHA-224/256/384/512 digest with deterministic RFC 6979 nonces and
## constant-time scalar multiplication; r and s are 32 big-endian bytes.
pub func ecdsa_p256_sign(d: const u8[], message: const u8[], r: u8[], s: u8[]) -> !:
    try p256.sign(d, message, r, s)

## Verify ECDSA P-384 over a digest (SHA-256/384 as is, longer ones truncated
## to 384 bits). Coordinates and r, s are exact 48-byte big-endian values.
pub func ecdsa_p384_verify(qx: const u8[], qy: const u8[], message: const u8[], r: const u8[], s: const u8[]) -> bool!:
    return try p384.verify(qx, qy, message, r, s)

## RSASSA-PKCS1-v1_5 verification of a sha256/sha384/sha512 digest `hashed` under the
## big-endian `modulus` (2048–8192 bits) and `exponent`.
pub func rsa_verify_pkcs1(modulus: const u8[], exponent: const u8[], algorithm: str, hashed: const u8[], signature: const u8[]) -> bool!:
    let key = try rsa.public_key(modulus, exponent)
    return try rsa.verify_pkcs1(&key, algorithm, hashed, signature)

## RSASSA-PSS verification with MGF1 over the same hash and a salt of exactly
## `salt_size` bytes (TLS 1.3 uses the digest length).
pub func rsa_verify_pss(modulus: const u8[], exponent: const u8[], algorithm: str, hashed: const u8[], signature: const u8[], salt_size: usize) -> bool!:
    let key = try rsa.public_key(modulus, exponent)
    return try rsa.verify_pss(&key, algorithm, hashed, signature, salt_size)

pub func wipe(data: u8[]):
    secure.wipe(data)

pub func equal(left: const u8[], right: const u8[]) -> bool:
    return secure.equal(left, right)