//! Cryptographic hashes for `std/crypto` (`louise_rt_prims::crypto` does //! the work; PostgreSQL's SCRAM-SHA-256 needs them, issue #145), Ed25519 //! signatures (ADR 0067), and ChaCha20-Poly1305 with HKDF-SHA-256 (ADR //! 0071). //! //! `tl_crypto_pbkdf2_sha256` is `blocking`: the iteration count is the //! caller's, so the call runs on the blocking pool and never stalls a //! scheduler thread. //! //! Without the `crypto` feature (`ring`'s build script needs a C //! cross-compiler the no-qemu aarch64 check does not have, scripts/ci.sh) //! the externs exist and panic with "crypto support not built in", the //! `tls` feature's precedent: a program that hashes, linked against such //! a runtime, fails with that message instead of a link error. use crate::abi::{Ctx, Value}; #[cfg(feature = "crypto")] pub use enabled::*; #[cfg(not(feature = "crypto"))] pub use disabled::*; #[cfg(not(feature = "crypto"))] mod disabled { use super::*; use crate::prims::panic_with; const NOT_BUILT: &str = "crypto support not built in (louise_rt's `crypto` feature)"; macro_rules! not_built { ($($name:ident($($arg:ident),*)),*) => {$( /// # Safety /// Never reached: panics before touching the arguments. #[unsafe(no_mangle)] pub unsafe extern "C" fn $name(ctx: *mut Ctx, $($arg: Value),*) -> Value { $(let _ = $arg;)* // SAFETY: caller guarantees the running actor's context. unsafe { panic_with(ctx, NOT_BUILT) } } )*}; } not_built!( tl_crypto_sha256(data), tl_crypto_hmac_sha256(key, message), tl_crypto_pbkdf2_sha256(password, salt, iterations, length), tl_crypto_ed25519_verify(public_key, message, signature), tl_crypto_ed25519_public_key(seed), tl_crypto_ed25519_sign(seed, message), tl_crypto_chacha20_poly1305_seal(key, nonce, plaintext, aad), tl_crypto_chacha20_poly1305_open(key, nonce, sealed, aad), tl_crypto_hkdf_sha256(secret, salt, info, length) ); } #[cfg(feature = "crypto")] mod enabled { use super::*; use crate::int::saturating; use crate::prims::{boolean, panic_with}; use crate::values::{bytes_from, bytes_of, error, ok}; /// SHA-256 of `data` as `Bytes`. /// /// # Safety /// `data` must be live `Bytes`. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_sha256(ctx: *mut Ctx, data: Value) -> Value { // SAFETY: caller guarantees live Bytes. let hash = louise_rt_prims::crypto::sha256(unsafe { bytes_of(data) }); bytes_from(ctx, &hash) } /// HMAC-SHA-256 of `message` under `key`, as `Bytes`. /// /// # Safety /// Both arguments must be live `Bytes`. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_hmac_sha256( ctx: *mut Ctx, key: Value, message: Value, ) -> Value { // SAFETY: caller guarantees live Bytes. let tag = louise_rt_prims::crypto::hmac_sha256(unsafe { bytes_of(key) }, unsafe { bytes_of(message) }); bytes_from(ctx, &tag) } /// PBKDF2-HMAC-SHA-256 of `password` with `salt` and `iterations`, /// `length` bytes of output. /// /// # Safety /// `password` and `salt` must be live `Bytes`, `iterations` and `length` /// live `Int`s. Runs on the blocking pool: the result is built with the /// allocation functions of `louise_rt.h`, which work on any thread. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_pbkdf2_sha256( ctx: *mut Ctx, password: Value, salt: Value, iterations: Value, length: Value, ) -> Value { // SAFETY: caller guarantees live Bytes and Ints. let (password, salt, iterations, length) = unsafe { ( bytes_of(password), bytes_of(salt), saturating(iterations), saturating(length).clamp(0, 1 << 20), ) }; let derived = louise_rt_prims::crypto::pbkdf2_hmac_sha256( password, salt, iterations, length as usize, ); bytes_from(ctx, &derived) } /// Whether `signature` is the Ed25519 signature of `message` under /// `public_key`, as a `Bool`; wrong lengths are `False`. /// /// # Safety /// All three arguments must be live `Bytes`. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_ed25519_verify( _ctx: *mut Ctx, public_key: Value, message: Value, signature: Value, ) -> Value { // SAFETY: caller guarantees live Bytes. let valid = unsafe { louise_rt_prims::crypto::ed25519_verify( bytes_of(public_key), bytes_of(message), bytes_of(signature), ) }; boolean(valid) } /// The Ed25519 public key of a 32-byte `seed`, as `Bytes`; panics on a /// seed of another length. /// /// # Safety /// `seed` must be live `Bytes`, and `ctx` the running actor's context. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_ed25519_public_key(ctx: *mut Ctx, seed: Value) -> Value { // SAFETY: caller guarantees live Bytes. match louise_rt_prims::crypto::ed25519_public_key(unsafe { bytes_of(seed) }) { Ok(key) => bytes_from(ctx, &key), // SAFETY: caller guarantees the running actor's context. Err(message) => unsafe { panic_with(ctx, &message) }, } } /// The Ed25519 signature of `message` under the key pair of a 32-byte /// `seed`, as `Bytes`; panics on a seed of another length. /// /// # Safety /// Both arguments must be live `Bytes`, and `ctx` the running actor's /// context. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_ed25519_sign( ctx: *mut Ctx, seed: Value, message: Value, ) -> Value { // SAFETY: caller guarantees live Bytes. let signature = louise_rt_prims::crypto::ed25519_sign(unsafe { bytes_of(seed) }, unsafe { bytes_of(message) }); match signature { Ok(signature) => bytes_from(ctx, &signature), // SAFETY: caller guarantees the running actor's context. Err(message) => unsafe { panic_with(ctx, &message) }, } } /// The ChaCha20-Poly1305 sealing of `plaintext`, as `Bytes` (the /// ciphertext, then the 16-byte tag); panics on a key that is not 32 /// bytes or a nonce that is not 12. /// /// # Safety /// All four arguments must be live `Bytes`, and `ctx` the running /// actor's context. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_chacha20_poly1305_seal( ctx: *mut Ctx, key: Value, nonce: Value, plaintext: Value, aad: Value, ) -> Value { // SAFETY: caller guarantees live Bytes. let sealed = unsafe { louise_rt_prims::crypto::chacha20_poly1305_seal( bytes_of(key), bytes_of(nonce), bytes_of(plaintext), bytes_of(aad), ) }; match sealed { Ok(sealed) => bytes_from(ctx, &sealed), // SAFETY: caller guarantees the running actor's context. Err(message) => unsafe { panic_with(ctx, &message) }, } } /// `Ok(plaintext)` if `sealed` authenticates under `key`, `nonce` and /// `aad`, else `Error`; panics on a key or nonce of the wrong length. /// /// # Safety /// All four arguments must be live `Bytes`, and `ctx` the running /// actor's context. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_chacha20_poly1305_open( ctx: *mut Ctx, key: Value, nonce: Value, sealed: Value, aad: Value, ) -> Value { // SAFETY: caller guarantees live Bytes. let opened = unsafe { louise_rt_prims::crypto::chacha20_poly1305_open( bytes_of(key), bytes_of(nonce), bytes_of(sealed), bytes_of(aad), ) }; match opened { Ok(Some(plaintext)) => { let plaintext = bytes_from(ctx, &plaintext); ok(ctx, plaintext) } Ok(None) => error(ctx, louise_rt_prims::crypto::NOT_AUTHENTIC), // SAFETY: caller guarantees the running actor's context. Err(message) => unsafe { panic_with(ctx, &message) }, } } /// HKDF-SHA-256 of `secret` with `salt` and `info`, `length` bytes, as /// `Bytes`; panics on a length outside 0 to 8160. /// /// # Safety /// `secret`, `salt` and `info` must be live `Bytes`, `length` a live /// `Int`, and `ctx` the running actor's context. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_crypto_hkdf_sha256( ctx: *mut Ctx, secret: Value, salt: Value, info: Value, length: Value, ) -> Value { // SAFETY: caller guarantees live Bytes and an Int. let derived = unsafe { louise_rt_prims::crypto::hkdf_sha256( bytes_of(secret), bytes_of(salt), bytes_of(info), saturating(length), ) }; match derived { Ok(derived) => bytes_from(ctx, &derived), // SAFETY: caller guarantees the running actor's context. Err(message) => unsafe { panic_with(ctx, &message) }, } } }