Hashing, encoding and dates

ImportWhat it gives you
abla/cryptoSHA-512, SHA-384, SHA-256, SHA-1, MD5, HMAC, PBKDF2, Base64, hexadecimal, constant-time comparison
abla/crypto/aes, abla/crypto/rsa, abla/crypto/pureAES (GCM, CBC, ECB), RSA signing, verifying and encryption, key parsing: in Abla, on every target
abla/crypto/p256NIST P-256: ECDH and ECDSA (SHA-256, RFC 6979 nonces), in Abla, on every target
abla/crypto/p384NIST P-384: ECDH, ECDSA signing (RFC 6979, SHA-384) and verification, in Abla
abla/crypto/sha3SHA3-256, SHA3-512, SHAKE128 and SHAKE256 (FIPS 202) with an incremental sponge, in Abla, on every target
abla/crypto/mlkemML-KEM-768 (FIPS 203): key generation, encapsulation and decapsulation with implicit rejection, in Abla, on every target
abla/crypto/x509X.509 certificates: parsing, chain building and validation (RFC 5280), host names (RFC 6125), a trust store, in Abla
abla/crypto/chacha20poly1305ChaCha20, Poly1305 and the RFC 8439 AEAD: in Abla, on every target
abla/crypto/rsapssRSASSA-PSS (SHA-256, -384, -512; MGF1 over the same hash) on abla/crypto/rsa keys: in Abla, on every target
abla/randoma cryptographically secure source (getrandom(2))
abla/dateUTC calendar dates, ISO 8601 and HTTP dates
abla/jwt, abla/jwt/remoteJSON Web Token verification (networking.md)

abla/crypto and abla/date are pure Abla: they work on every target and at compile time. abla/crypto/aes and abla/crypto/rsa are pure Abla and are available at run time.

abla/crypto

The hash functions return the raw digest bytes, not text. Wrap them in hexEncode (or base64Encode) to print or compare them as text:

import "abla/io"
import "abla/crypto"

fun main {
    println(hexEncode(sha256("abc")))
    println(hexEncode(hmacSha256("key", "message")))
    val encoded = base64Encode("hello, world")
    println("$encoded -> ${base64Decode(encoded)}")
    println("${sha256("abc").size} bytes; equal: ${constantTimeEquals("token", "token")}")
}
// output:
// ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
// 6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a
// aGVsbG8sIHdvcmxk -> hello, world
// 32 bytes; equal: true
FunctionResult
sha512(data), sha384(data), sha256(data), sha1(data), md5(data)raw digest bytes (64, 48, 32, 20 and 16 bytes)
hmacSha512(key, message), hmacSha384, hmacSha256, hmacSha1raw MAC of the hash's size
pbkdf2Sha256(password, salt, iterations)a raw 32-byte derived key
base64Encode(data), base64Decode(text)standard Base64 with padding; base64Decode gives "" for invalid input, which is also what valid empty input gives
base64DecodeOrNull(text), hexDecodeOrNull(text)the decoded bytes, or null for invalid input (valid empty input gives ""): prefer these unless the input is known to be valid
hexEncode(data), hexDecode(text)lowercase hexadecimal; decoding takes either case, and gives "" for anything invalid
constantTimeEquals(a, b)equality that takes the same time wherever the inputs differ; use it to compare secrets
binaryXor(a, b)bytewise XOR of two equal-length strings

SHA-1 and MD5 are there for protocols that require them (WebSocket handshakes, legacy checksums), not for security.

The hashes and HMACs have no secret-dependent branch or table index in their rounds, but nothing stops the code generator adding one, and the key's length shows. constantTimeEquals is abla/ct's ctEquals when compiled (what that promises), and an ordinary loop at compile time. Different lengths are unequal at once. hexEncode, hexDecode and the Base64 functions aren't constant-time at all. Nothing here wipes memory: a secret in a string stays there until the memory is reused.

abla/crypto/hkdf

HKDF (RFC 5869) over HMAC-SHA-1, -256, -384 and -512, and TLS 1.3's HKDF-Expand-Label and Derive-Secret (RFC 8446 section 7.1). The hash is HKDF_SHA1, HKDF_SHA256, HKDF_SHA384 or HKDF_SHA512.

FunctionResult
hkdfExtract(hash, salt, ikm)the pseudorandom key (an empty salt is hash-length zeros)
hkdfExpand(hash, prk, info, length)a CryptoResult of length bytes; ok is false past 255 hash lengths or below 0
hkdf(hash, salt, ikm, info, length)Extract then Expand
tls13ExpandLabel(hash, secret, label, context, length)HKDF-Expand-Label (label without its "tls13 " prefix); ok is false for a label of 0 or more than 249 bytes, a context over 255 or a length over 65535
tls13DeriveSecret(hash, secret, label, transcriptHash)Expand-Label with the transcript hash as context and the hash's length as output

Secrets only pass through hmacSha1/hmacSha256/hmacSha384/hmacSha512 (the paragraph above applies); the lengths, labels and contexts are public. Tested against all seven RFC 5869 vectors and the RFC 8448 early and derived secrets (tools/test-hkdf.sh).

abla/crypto/x25519

X25519 (RFC 7748 section 5): Diffie-Hellman on Curve25519, in Abla, no OpenSSL, on every target.

FunctionResult
x25519(scalar, point)a CryptoResult of the 32-byte product; ok is false for a wrong length and for an all-zero result (a point of small order; TLS 1.3 aborts then)
x25519PublicKey(scalar)the public key of a private key: x25519 of it and the base point 9

A private key is any 32 random bytes; the function clamps it as the RFC says. Bit 255 of a point is ignored and a point at or above 2^255 - 19 is taken mod p, as the RFC says.

It is constant-time: the Montgomery ladder runs its 255 steps and swaps by a mask on every bit, the field arithmetic (32 limbs of 8 bits in secretU32, so nothing wider than 32 bits is needed on any target) has no branch, memory index or division on a value, and the inverse is Fermat's. tools/test-ct.sh reads the core (BEGIN X25519 SECRET CORE in abla/crypto/x25519/core.ab) on x86-64, AArch64, Wasm and Xtensa, with deliberate leaks that it has to catch. The wrapper wipes its memory and the stack below it (2,048 bytes) after each call, and nothing else (the scalar a caller holds in a string stays there). Speed: about 1.4 ms per product on a current x86-64 core. Tested against RFC 7748 (section 5.2's two vectors, the iterated function after 1, 1,000 and, with X25519_MILLION=1, 1,000,000 rounds; section 6.1's Alice and Bob), the refusal of the small-order points, and random scalars and points against the openssl CLI (tools/test-x25519.sh).

abla/crypto/sha3

SHA-3 and SHAKE (FIPS 202): the Keccak-f[1600] permutation in Abla, on every target.

FunctionResult
sha3256(input), sha3512(input)the 32- and 64-byte digests
shake128(input, size), shake256(input, size)size bytes of output
keccakSha3(rate), keccakShake128(), keccakShake256()a Keccak sponge: absorb(text), then squeeze(count) as often as needed (absorbAt and squeezeTo, which take an address, are the library's own, in abla/crypto/sha3/raw.ab), and close() (wipes the state)

The permutation is exclusive-or, and, not and rotation by constant amounts on 64-bit lanes: no table, branch or division on a lane, so it is constant time by construction; tools/test-ct.sh reads it (BEGIN SHA3 SECRET CORE), a leaky twin must be found. Tested against the FIPS 202 answers and openssl dgst for 31 message lengths around every rate boundary, one call against pieces (tools/test-crypto-pure.sh).

abla/crypto/mlkem

ML-KEM-768 (FIPS 203), the KEM of the TLS 1.3 hybrid X25519MLKEM768, in Abla, on every target. An encapsulation key is 1184 bytes, a decapsulation key 2400 (dkPKE || ek || H(ek) || z), a ciphertext 1088, a shared secret 32.

FunctionResult
mlkem768GenerateKey()a decapsulation key from the random source (cryptoSetRandom)
mlkem768KeyGenFromSeeds(d, z)the key from the 32-byte seeds (algorithm 16)
mlkem768EncapsulationKey(dk)the encapsulation key inside a decapsulation key
mlkem768Encapsulate(ek), mlkem768EncapsulateWithMessage(ek, m)a MlkemEncapsulation: ciphertext, sharedSecret; ok is false for a key that fails the check of section 7.2
mlkem768Decapsulate(dk, ciphertext)the shared secret. A ciphertext that was not made for the key gives a pseudorandom secret (implicit rejection), not an error; a wrong length or a key whose hash does not match is refused (section 7.3)
mlkem768CheckEncapsulationKey(ek), mlkem768CheckDecapsulationKey(dk)the checks of sections 7.2 and 7.3

Every coefficient of a secret is a secretU32 in memory, the arithmetic mod 3329 is Montgomery's (multiplications, shifts, masks: no division, no branch or table lookup on a coefficient; the NTT's zeta tables are read at public positions), compression is a multiplication by a reciprocal checked for every input, and decapsulation compares the whole re-encrypted ciphertext in constant time and selects the secret or J(z || c) by a mask. The matrix is sampled from the public rho by rejection, which branches on public bytes. tools/test-ct.sh reads the kernels (BEGIN MLKEM SECRET CORE) on x86-64, AArch64, Wasm and Xtensa, with leaky twins. Strings (seeds, shared secrets) cannot be wiped; the polynomials, the working memory and the sponge are. Tested against the NIST ACVP vectors for ML-KEM-768 (25 key generations, 25 encapsulations, 10 decapsulations with altered ciphertexts, 20 key checks: tests/vectors/crypto/mlkem768), refusals, and round trips (tools/test-crypto-pure.sh), and by interoperating with openssl s_server and s_client in TLS (tools/test-tls-pure-interop.sh).

abla/crypto/bignum: numbers modulo a number, with no address

Big-number arithmetic for code that signs or verifies on its own curve or field. A BigModulus is an odd modulus of 4 to 512 bytes; a BigNumber is a value modulo one, of that width. Both own their memory, wipe it when dropped, and borrow in every parameter, so a.add(b) leaves both alive and returns a number the caller owns. The arithmetic is in Montgomery form inside and constant time in the values: the same code runs for any values of one width, and none of it branches or indexes on one. The raw engine under it (the bn* functions that take the address of a work area) is internal to the standard library since v0.2.73 (bignum/core.ab, // public: none): a program uses BigModulus and BigNumber.

CallAnswers
bigModulus(bytes, bigEndian = true)the modulus, or null: the size must be a multiple of 4 from 4 to 512 bytes and the value odd
m.width(), m.zero(), m.one()the width in bytes, 0 and 1
m.number(bytes, bigEndian = true)the number, or null unless the bytes are exactly the width and below the modulus (the range is checked in constant time; only its answer is told)
m.reduce(bytes, bigEndian = true)the bytes (1 to twice the width) reduced modulo the modulus, in constant time, or null for another size
a.add(b), a.sub(b), a.mul(b), a.square(), a.copy()the sum, difference and product modulo the modulus; numbers of two moduli in one operation stop the program
a.pow(exponent)a to the power of exponent (big-endian bytes, public: the work depends on it, never on a)
a.select(b, mask)a where mask (a secretU32) is all ones, b where it is zero
a.isEqual(b), a.bitMask(index)all ones or zero, as a secret: equality, and bit index (0 is the least significant) of the value
a.toBytes(bigEndian = true)the value as width bytes: the place where a secret leaves the type

The raw bn* functions of the same module take the address of a work area as an integer and trust it: they are the engine of the library's RSA and curves, not a safe API. Tests: tools/test-bignum-checked.sh (JavaScript's BigInt as the oracle at 4, 8, 32, 64 and 512 bytes), and tools/test-ct.sh reads the code of the steps that touch a value. The timing of mul, add, sub, select, isEqual, pow and toBytes at 256 and 384 bits is measured by tools/ct-timing (the big-* targets: a zero against a random secret operand, with a control that must be caught), and that a dropped number's memory is wiped by tests/cases/crypto-pure/heap-residue.ab (bignum).

There is no inverse: a number's inverse modulo a prime is a.pow(p - 2), whose exponent is public. Nothing on a secret path takes a variable-time inverse (bnPublicInverse of the raw engine works on public values only).

abla/crypto/ed25519

Ed25519 (RFC 8032 section 5.1) in Abla, no OpenSSL, on every target, over the field of abla/crypto/x25519.

FunctionResult
ed25519PublicKey(seed)the 32-byte public key of a 32-byte private seed
ed25519Sign(seed, message)the 64-byte signature (R, then S), deterministic
ed25519Verify(publicKey, message, signature)whether it verifies; false for any wrong length

Verification is the strict one: S is below L, the public key is a point with a canonical encoding, and the encoding of [S]B - [k]A is R's bytes (so a non-canonical R, or S + L, is refused), and the public key is not of small order (order 1, 2, 4 or 8: the identity key would verify a signature of any message with no secret; RFC 8032 does not require the refusal, Wycheproof accepts either answer). The key, the nonce and S are secret; the message, the public key and R are public.

Constant-time: the scalar multiplications that take a secret run 256 steps of a doubling, an addition (the unified, complete Edwards formula) and a masked select of the whole point, and the arithmetic modulo L is bit by bit with masked selects, so no branch, memory index or division depends on a secret. tools/test-ct.sh reads the core (BEGIN ED25519 SECRET CORE in abla/crypto/ed25519/core.ab) on x86-64, AArch64, Wasm and Xtensa, with deliberate leaks it has to catch. What it does not cover: the SHA-512 over strings (the seed's hash and the nonce's pass through ordinary strings, which are not wiped) and, as for X25519, a seed the caller holds in a string. Verification uses the same constant-time code for both products, so it is not faster than signing. Speed: about 8 ms to sign and 8 ms to verify on a current x86-64 core (a whole process run). Tested against RFC 8032 section 7.1's vectors, Wycheproof's 210 verification cases (tests/vectors/crypto/wycheproof-ed25519.json), and random seeds and messages against the openssl CLI both ways (tools/test-ed25519.sh).

abla/crypto/chacha20poly1305

RFC 8439 in Abla, with no OpenSSL and no table: chaCha20Poly1305Seal(key, nonce, plaintext, associatedData = "") returns the ciphertext and then the 16-byte tag in a CryptoResult; chaCha20Poly1305Open(key, nonce, sealed, associatedData = "") checks the tag in constant time before any plaintext exists and fails with one message whichever part was wrong. The key is 32 bytes and the nonce 12 (never reuse a nonce under a key). chaCha20Poly1305SealRandom(key, plaintext, associatedData = "") draws a fresh random nonce and returns nonce, ciphertext and tag in one box that chaCha20Poly1305OpenRandom(key, box, associatedData = "") opens; it fails without a random source, and a key may seal at most 2^32 messages this way (the chance of a repeated random 96-bit nonce stays below 2^-32): count nonces yourself, or change the key, beyond that. chaCha20Xor(key, counter, nonce, data) is the bare cipher and poly1305(key, message) the bare one-time authenticator, for protocols that build on them (TLS 1.3 uses the AEAD). The secret-handling cores are read in the generated code of x86-64, AArch64, Wasm and Xtensa by tools/test-ct.sh, with a leaking twin of each kind that the check must catch. Tests: RFC 8439's vectors on every target, and every message length from 0 to 200 against Node.

abla/crypto/rsapss

RSASSA-PSS (RFC 8017) over abla/crypto/rsa's keys, for TLS 1.3's rsa_pss_rsae_* and X.509: key.signPssSha256(message) (also signPssSha384, signPssSha512) signs with a random salt of the hash's length; publicKey.verifyPssSha256(message, signature) (and the others) checks one. rsaPssSign(key, hashBits, message, saltLength) and rsaPssVerify(key, hashBits, message, signature, saltLength) take another salt length (0 is common); rsaPssSignWithSalt signs with a salt you give, for known-answer tests. Keys under 2048 bits are refused. The private operation is abla/crypto/rsa's (blinded, CRT, constant time); the encoding and the check hold nothing secret and take variable time. Tests: Wycheproof's RSA-PSS vectors (2048, 3072, 4096 bits, SHA-256 and -512, salt lengths 32 and 0), and signing and verifying against Node, including a given salt byte for byte.

abla/crypto/aes and abla/crypto/rsa

abla/crypto/aes and abla/crypto/rsa implement AES and RSA in Abla. Import abla/crypto/pure to use both. See the migration guide for former hosted names and format changes.

import "abla/io"
import "abla/crypto"
import "abla/crypto/aes"

fun main {
    // The key and nonce here are an RFC test vector's: never copy them. Draw a key with secureRandomBytes and use aesGcmSealRandom, which picks a fresh nonce.
    val key = hexDecode("000102030405060708090a0b0c0d0e0f")
    val nonce = hexDecode("202122232425262728292a2b")
    val sealed = aesGcmEncrypt(key, nonce, "a message", "its context")
    println(aesGcmDecrypt(key, nonce, sealed.bytes, "its context").bytes)
    println(aesGcmDecrypt(key, nonce, sealed.bytes, "another").error)
    println(aesEcbDecryptPkcs5(key, aesEcbEncryptPkcs5(key, "hello").bytes).bytes)
}
// output:
// a message
// authentication failed: the data, key, nonce or associated data is wrong
// hello
aesGcmSealRandom(key, plaintext, associatedData = ""), aesGcmOpenRandom(key, box, associatedData = "")AES-GCM with a fresh random 12-byte nonce, returned first in the box (nonce, ciphertext, tag); fails without a random source. At most 2^32 messages per key this way (NIST SP 800-38D 8.3). Never reuse a nonce under a key with aesGcmEncrypt: one repeat gives away the authentication key and the XOR of two plaintexts
aesGcmEncrypt, aesGcmDecrypt, aesCbcEncryptPkcs7, aesCbcDecryptPkcs7, aesEcbEncryptPkcs5, aesEcbDecryptPkcs5AES-GCM with a 12-byte nonce and optional associated data, or ECB/CBC with PKCS#7 padding. Use ECB/CBC only for legacy interoperability.
rsaPublicKeyFromPem, rsaPublicKeyFromDer, rsaPublicKeyFromCertificateDerRead a public key from PEM, DER, or a DER certificate. Reading a key from a certificate does not verify the certificate.
rsaPrivateKeyFromPem(pem), rsaPrivateKeyFromDer(der)PKCS#8 and PKCS#1, unencrypted. Password-resource overloads read PBES2/PBKDF2/AES-CBC encrypted PKCS#8; rsaPrivateKeyFromPkcs12(der, password, limits) reads modern MAC-verified DER key bags (see the same limits and restrictions)
private.signPkcs1Sha256, public.verifyPkcs1Sha256, private.signPkcs1Sha1Legacy, public.verifyPkcs1Sha1LegacyPKCS#1 v1.5 signing and verification. Prefer SHA-256.
RSA encryption and decryptionUses empty-label OAEP. PKCS#1 v1.5 uses deterministic implicit rejection: invalid padding returns synthetic plaintext. Prefer OAEP, and make subsequent protocol checks indistinguishable to remote peers
rsaGeneratePrivateKey(bits = 2048)Generate a key with 2048, 3072 or 4096 bits and exponent 65537. Check ok() or error; generation fails without randomness.
private.toPkcs8Der(), private.toPkcs8Pem()Export unencrypted PKCS#8. A number-only key without its private exponent returns an error.
cryptoSetRandom(source)where the randomness for encryption, blinding and key generation comes from. Needed on WebAssembly (bare and WASI: the module does not ask the host for random bytes on its own) and on an ESP32, whose hardware RNG it takes; Linux (x86-64, AArch64) uses getrandom, macOS getentropy and Windows ProcessPrng. A Wasm host's crypto.getRandomValues is the usual source. Its quality is yours to answer for. Without one, what needs it fails, and signing never goes on unblinded

For parsed PEM and DER keys, the synthetic PKCS#1 v1.5 result matches OpenSSL's implicit rejection for the same private exponent and ciphertext. rsaPrivateFromNumbers accepts an optional private exponent as its last argument; without it, rejection is derived from the CRT secrets and is deterministic but does not match OpenSSL. Do not mix these representations for the same key in an attacker-observable service. The derived hash and intermediate strings, like PEM text, cannot be wiped.

Key generation uses the probable-prime bounds, separation and Miller–Rabin rounds in FIPS 186-5; this is a choice of parameters, not a FIPS validation claim. Prime candidate search, sieving, primality decisions, and its duration are variable-time. Key derivation uses fixed-width secret arithmetic; DER/PEM encoding is variable-time. The random source must be cryptographically secure, and its quality is the caller's responsibility when using cryptoSetRandom. Prime candidates, intermediate key strings, and exported PEM/DER are ordinary strings and cannot be wiped. The private key's raw allocation is wiped when dropped. Protect the generated unencrypted export as private key material.

abla/crypto/p256

NIST P-256 (secp256r1) with no OpenSSL, on the same constant-time big-number arithmetic as RSA. Keys are strings: a private key is its 32 big-endian bytes, a public key the 65-byte uncompressed point 0x04 || X || Y (compressed points are refused). A failure comes back as a CryptoResult with error set.

p256GeneratePrivateKey()a random private key in 1..n-1; needs a random source like RSA's (cryptoSetRandom)
p256PublicKey(privateKey)the public point of a private key
p256IsValidPublicKey(point)whether it is an uncompressed point on the curve
p256Ecdh(privateKey, peerPublicKey)ECDH: the shared x coordinate, 32 bytes. The peer's point is checked to be on the curve (P-256 has cofactor 1)
p256EcdsaSign(privateKey, message), p256EcdsaSignRawECDSA over SHA-256 with the RFC 6979 nonce, so one message always gets one signature: as DER, or as r || s (64 bytes)
p256EcdsaVerify(publicKey, message, signature), p256EcdsaVerifyRaw, p256EcdsaVerifySha384, p256EcdsaVerifyDigestRawverification of a strict DER signature (no BER, no padding, no negative or oversized integers) or of r || s; the digest forms take any hash, cut to its leftmost 256 bits
p256SignatureToDer(raw), p256SignatureFromDer(der)between the two forms

Point addition and doubling are the complete formulas of Renes, Costello and Batina, so no input (the point at infinity included) takes another path. A scalar multiplication always does 64 windows of four doublings and one addition, reading the whole table of sixteen multiples each time; inversions are Fermat's, by public exponents. A private key out of range is refused, and so is an RFC 6979 candidate nonce out of range (it happens about once in 2^32 signatures): both outcomes are public. tools/test-ct.sh reads the field, point and scalar-multiplication routines (the P256 SECRET CORES of the source) for a secret reaching a branch or an address on each target, with a planted table read and a planted branch that it must find. Verification works on public values and is variable-time. A private key, a nonce, its HMAC chain and the exported bytes are strings and cannot be wiped; the limbs and table made inside a call are, and so is the stack it used.

It is checked against Wycheproof's ECDSA (484 vectors) and ECDH (355 point-encoded vectors, tests/cases/p256/vectors), RFC 6979 appendix A.2.5, itself, and the openssl command line in both directions (tools/test-p256.sh). Wycheproof's compressed-point cases, which it marks acceptable, are refused.

What timing protection each has, and hasn't

Read constant-time.md first: it's what "read" below means, and its limits (a CPU's own variable-time instructions, Wasm engines, speculation, memory left behind) apply to every line here.

ProtectsRead on the 4 targetsDoesn't protect
AES software (bitsliced)no table, no branch on key or dataS-box, rounds, in both directions. The key schedule and the block's loading aren't read: they use the same secret typesa CPU whose word operations vary with data
AES hardware (x86-64 AES-NI, ARMv8)no table, per the vendorno: one instruction a round, and vendors state the timing doesn't depend on data, though ARM's DIT isn't set
Which of the twonot hidden: a program on one machine always takes the same one
GHASH softwaremasks and shifts, 128 steps for each block, no tableyes
GHASH hardwarecarry-less multiply, per the vendorno
GCM's tag checkevery byte compared, before any plaintext is madeyesa tag length is public
CBC and ECB paddingchecked with masksyesthe answer: "bad padding" is returned. That is a padding oracle if a remote party sees it. Never let one
RSA verify, encryptpublic values, so nothing to protect but the message before encryption: its bytes aren't branched on in the arithmeticMontgomery multiplicationpadding is built from strings with the usual operations; the plaintext's length is public
RSA signingblinded base, CRT with a 4-bit fixed window read whole, squarings and multiplications independent of the exponent, result checked with the public exponentMontgomery multiplication, the table lookup, the conditional subtraction (on x86-64 and AArch64 the 64-bit-limb forms the exponentiation uses there: a quarter of the products, read too). Not read: the steps around them in rsaCrt, which use the same secret types and branch only on lengthssizes of p, q and their numbers are public. No exponent blinding, which the fixed window makes unnecessary for timing but would help against power analysis. On Xtensa mull's timing is unpublished: see below
RSA decryptionthe same blinded, checked CRT operation; OAEP and PKCS#1 padding are scanned with masks. PKCS#1 v1.5 selects a deterministic synthetic message on bad paddingpadding and synthetic-message selectionmalformed ciphertext and operational failures are reported. Decrypted blocks and plaintext are strings and cannot be wiped
Key loadingthe base 64 of a PEM isn't branched on or looked upthe character stepDER tags and lengths are branched on, which are public, as is the position of line breaks. An integer's length shows through parsing. The `string`s holding the DER and base 64 aren't wiped
Keys in memorya private key's numbers are in raw memory, wiped on drop; AES's schedule toowhat was copied before: the PEM, its decoded string
hexDecode, base64Decodenothingnot constant-time: don't feed them secrets that matter

On ESP32-S3 all of this is software (the S3's AES peripheral isn't used), and it's the least examined: read in the assembly, compiled, and never run here. Its multiplier has no published timing. For a key that matters, don't use it there without measuring it on the board.

Nothing here has been through what OpenSSL's has: years of attack, and timing measured on real hardware. No statistical test (dudect) has been run: the assembly check shows no branch or index on a secret, and no more than that.

How this is tested

tools/test-crypto-pure.sh: NIST CAVP and Wycheproof vectors on both AES paths, on this CPU and on qemu-x86_64 -cpu qemu64 with neither instruction; refusals; random inputs against the openssl CLI, both ways. Known answers on AArch64 under qemu (with AES and PMULL) and on Wasm under Node. On Xtensa it's compiled, and read by tools/test-ct.sh, not run. RSA decryption vectors run on the hosted native paths.

Dates: abla/date

Times are int milliseconds since 1970-01-01T00:00:00Z. A UtcDate is that time split into calendar fields, in the proleptic Gregorian calendar, without leap seconds:

import "abla/io"
import "abla/date"

fun main {
    val ms = utcMilliseconds(2024, 2, 29, 13, 5, 9)
    val date = utcDate(ms)
    println("${date.iso8601()} weekday ${date.weekday}, day ${date.dayOfYear()} of the year")
    println(httpDate(ms))
    val parsed = utcIso8601Parse("2024-02-29T13:05:09.000Z")
    if (parsed != null) println("round trip: ${parsed == ms}")
    println("${utcIso8601Parse("not a date") == null} ${dateLeapYear(2100)} ${dateDaysInMonth(2024, 2)}")
}
// output:
// 2024-02-29T13:05:09.000Z weekday 4, day 60 of the year
// Thu, 29 Feb 2024 13:05:09 GMT
// round trip: true
// true false 29
FunctionResult
utcMilliseconds(year, month, day, hour = 0, minute = 0, second = 0, millisecond = 0)the time of a civil date
utcDate(milliseconds)a UtcDate(year, month, day, hour, minute, second, millisecond, weekday): month 1-12, day 1-31, weekday 0 (Sunday) to 6
date.milliseconds(), date.dayOfYear()back to a time; the day number (1-366)
date.isoDate(), date.isoTime(), date.iso8601(), utcIso8601(ms)2024-02-29, 13:05:09.000, 2024-02-29T13:05:09.000Z; a year outside 0000 to 9999 is written as JavaScript does, with a sign and six digits (-000001-12-31T23:59:59.999Z, +010000-01-01T00:00:00.000Z)
date.httpDate(), httpDate(ms)Thu, 29 Feb 2024 13:05:09 GMT
utcIso8601Parse(text), httpDateParse(text)int?: the time, or null for invalid text; utcIso8601Parse also reads the signed six-digit years iso8601() writes (-000000 is not a year), httpDateParse only four-digit years
dateLeapYear(year), dateDaysInMonth(year, month), dateWeekdayName(weekday), dateMonthName(month)calendar facts

The current time comes from utcNow() in abla/process/time (system.md).

abla/tls/pure: upgrading streams

tlsUpgrade(connection, host, caPath = "", timeoutMilliseconds = 5000, readWriteTimeoutMilliseconds = 30000) consumes an established TcpConnection or UnixConnection and returns a verified TlsConnection. host is the certificate hostname and SNI name, rather than the Unix socket pathname. caPath selects a CA file; the empty default uses system trust. Failure consumes and closes the raw connection too.

For mutual TLS, use tlsUpgradeWithIdentity(connection, host, identity, caPath = "", timeoutMilliseconds = 5000, readWriteTimeoutMilliseconds = 30000, deadline = 0) on either TCP or Unix. The identity comes from tlsClientIdentityFromPem or tlsClientIdentityFromPkcs12, as with tlsConnectWithIdentity; the caller may drop it once upgrade returns. The nonzero deadline is an absolute monotonic millisecond time bounding the handshake. On the client, already-configured socket timeouts survive upgrade; otherwise the arguments supply handshake and subsequent read/write limits.

tlsServerUpgrade(connection, certificatePath, privateKeyPath, handshakeTimeoutMilliseconds = 5000, readWriteTimeoutMilliseconds = 30000, deadline = 0) consumes an accepted TCP or Unix stream and negotiates server TLS. It loads the certificate and private key on every call. Use tlsListen when serving many TCP connections: it reuses its context. There is no separate public context handle API. A nonzero deadline bounds the entire server handshake, including peers that trickle a partial record. A failed upgrade closes the connection; a successful TlsConnection owns it until close/drop.

Unix upgrades support pathname sockets on Linux and Darwin, and Linux abstract sockets. A Unix peer has no IP peerAddress (socketPeerAddress returns null). Closing a TLS connection or listener does not unlink a Unix pathname: the caller still owns that cleanup. Existing TCP upgrade signatures and behavior are unchanged; overload selection uses the connection's type, including named calls.

abla/crypto/x509

Certificate parsing and server-chain validation with no OpenSSL, for the TLS that is written in Abla. All of it is public input: the parser reads a certificate once by a fixed schema (strict DER: minimal lengths, no trailing bytes, one-byte tags, UTCTime and GeneralizedTime only in their RFC 5280 forms), with no recursion, and answers ok = false with an error for anything else; tests feed it every truncation and byte change of a certificate.

val store = x509SystemTrustStore()                      // or x509TrustStoreFromPem(text), x509TrustStoreFromFile(path)
val result = x509Verify([leafDer, intermediateDer], store, X509Options("example.com", nowSeconds))
if (!result.ok) println(result.error)                   // result.path is the validated chain, leaf first, anchor last
x509ParseDer(der), x509ParsePem(text)an X509Certificate: subject, issuer (X509Name: DER, relative distinguished names, a canonical form to compare them), notBefore and notAfter in seconds since 1970, serial, spki with keyOid, keyParameters and keyBits, and the extensions basicConstraints (isCa, pathLength), keyUsage (a bit mask), extendedKeyUsage, subjectAltName (altNames: DNS, IP address, e-mail, URI, directory names), nameConstraints (permitted, excluded), the key identifiers, and the first critical extension not known (unknownCritical)
x509Verify(chain, store, options)builds a path from the leaf (the first DER of chain; the others are candidate intermediates in any order, extras and unparsable ones are ignored) to a certificate of the trust store, depth-first, with at most 64 signature checks, and validates it
X509Options(hostname, now, maximumDepth = 8, usage = 0)the host name or IP address to match ("" skips the check), the time in seconds since 1970 (required: this module reads no clock, so a TLS layer passes its own), the longest path, and 0 for server authentication (1 for client)
x509MatchesHost(cert, host), x509DnsMatches(pattern, host), x509IpBytes(text)the RFC 6125 rules on their own
x509CheckSignature(cert, issuer), x509ValidatePath(path, options)the pieces: one signature, and the checks of a built path
x509TrustStoreFromPem, x509TrustStoreFromFile, x509SystemTrustStore()the store; the system one is the first of the usual bundle files (x509SystemBundlePaths(): Debian, Red Hat, SUSE, Alpine, FreeBSD, macOS) that holds a certificate; abla/tls/pure on Windows uses the user's ROOT certificate store instead (x509TrustStoreFromDerRecords parses what the runtime's system call 4006 returns)

What a path must satisfy: every certificate is within its validity and has no critical extension this code does not know; every issuer is a CA (basicConstraints cA, and keyCertSign in its keyUsage if it has one), within its path length constraint (self-issued intermediates not counted), and, unless it is the anchor, allows server authentication in its extended key usage; the leaf allows the purpose and has the host name among its subjectAltName entries (the common name is never used; a wildcard is a lone * as the whole left label of a name with at least three labels and matches exactly one label; IP addresses match iPAddress entries byte for byte); name constraints (DNS, IP, directory name) of each CA apply to the names below it, a wildcard that covers an excluded name is refused, and e-mail or URI names and other name forms under constraints are refused as unchecked. Signatures: RSA PKCS#1 v1.5 and ECDSA with SHA-256, -384 or -512 on RSA, P-256 and P-384 keys; RSASSA-PSS (SHA-2, MGF1 of the same hash, an explicit salt length; the issuer's key rsaEncryption) and Ed25519; SHA-1 and MD5 signatures are refused. RSA keys under 2048 bits and unusual EC curves in a leaf or an intermediate are refused. Not done: revocation, certificate policies, the common name, the public suffix list, id-RSASSA-PSS public keys.

abla/crypto/p384 has ECDH (p384Ecdh, p384PublicKey, p384EcdsaSign and p384EcdsaSignRaw over SHA-384 with the RFC 6979 nonce, checked against RFC 6979 A.2.6 and openssl: 48-byte private keys, constant time, tools/test-ct.sh reads the cores; checked against Wycheproof's 790 ECDH vectors and openssl) and ECDSA verification (variable time, on public values), checked against Wycheproof's 504 secp384r1/SHA-384 vectors. Tests (tools/test-x509.sh): the chains committed in tests/cases/x509/chains (made with openssl by tools/gen-x509-fixtures.sh: good RSA, P-256 and P-384 chains, expired, not yet valid, wrong name, wildcard and IP names, wrong usage, missing intermediate, unknown root, self-signed, tampered signature, name constraints, path length, a CA that is not one, an unknown critical extension), the system bundle's certificates, and x509-limbo's 270 server cases and every 25th of BetterTLS's 9,491 name-constraint cases (tests/cases/x509/limbo): the cases this code answers differently from the suite are listed in known.tsv with the reason (CA/Browser Forum profile rules, key identifiers, policies, the trust anchor's own fields, the public suffix list), and the test fails when one is missing from it or no longer differs.

abla/tls/pure: a listener that asks for client certificates

abla/tls/pure offers a listener that verifies client certificates:

FunctionResult
tlsListenWithClientCa(certificatePath, privateKeyPath, clientCaPath, host = "::", port = 443, backlog = 128, dualStack = true, required = true)a TlsListener that asks every client for a certificate, which must chain to a CA certificate in the PEM file clientCaPath, be valid for client authentication (extended key usage clientAuth, or none) and prove it holds the key. A client that sends none is refused unless required is false; one that sends a certificate that does not verify is always refused. A missing CA file, or one with no certificate, gives handle == -1 (tlsLastError() 11)
connection.peerCertificate()the DER of the peer's end-entity certificate, or "" when it sent none
connection.peerChain()the peer's certificates (DER, the end entity first); on a connection a listener accepted, the client's
connection.peerCertificateVerified()whether the peer presented a certificate and it verified (false for a client that sent none, and for a failed connection)

After a failed handshake peerChain() is the chain that was refused. The subject comes from abla/crypto/x509: x509ParseDer(connection.peerCertificate()).subject.commonName. Tested against openssl s_client with a good certificate, none, a certificate from another CA and one without the client usage, under TLS 1.3 and 1.2 (tools/test-tls-client-ca.sh).

Built from compiler source ba5d6b7d. Examples and generated signatures follow that source revision.