Module ztcl::crypto
ztcl::crypto gives the SHA-256, SHA-384, and SHA-512 hashes and HMACs of bytes, checks the signatures of bytes, and signs bytes with an Ed25519 seed. Each primitive is from std.crypto of the Zig standard library.
package require ztcl::crypto
set seed [binary decode hex 9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60]
set sig [ztcl::crypto sign ed25519 $seed hello]
puts [ztcl::crypto verify ed25519 [ztcl::crypto pubkey ed25519 $seed] hello $sig] ;# 1
The module needs no capability. It is in each native build, and not in the WebAssembly builds: a page has the Web Crypto API. Each command is a pure function of its arguments and reads no entropy, so a script with the module stays deterministic. The module has no command that makes a new key: that needs entropy.
Commands
ztcl::crypto verify ?-encoding der|raw? ALGORITHM PUBLICKEY MESSAGE SIGNATURE
ztcl::crypto sign ed25519 SEED MESSAGE
ztcl::crypto pubkey ed25519 SEED
ztcl::crypto hash sha256|sha384|sha512 DATA
ztcl::crypto hmac sha256|sha384|sha512 KEY DATA
verifygives 1 when SIGNATURE is a valid signature of MESSAGE by PUBLICKEY, and 0 otherwise. A signature of the wrong length or form gives 0.signgives the 64-byte Ed25519 signature of MESSAGE by the key of the 32-byte SEED. It uses no noise, so the same seed and message give the same signature.pubkeygives the 32-byte Ed25519 public key of SEED.hashgives the digest of DATA (FIPS 180-4): 32, 48, or 64 bytes.binary encode hexgives its hex form.hmacgives the HMAC of DATA with KEY (RFC 2104), with the length of the digest.
Each argument and each result is a byte string, as binary gives it.
| ALGORITHM | Public key | Signature | Source |
|---|---|---|---|
ed25519 |
32 bytes | 64 bytes | std.crypto.sign.Ed25519 (RFC 8032) |
ecdsa-p256-sha256 |
a SEC 1 point: 65 bytes, or 33 compressed | DER (the default), or the 64 bytes of r and s with -encoding raw |
std.crypto.sign.ecdsa.EcdsaP256Sha256 |
ECDSA hashes MESSAGE with SHA-256. A WebAuthn assertion with the algorithm ES256 is such a signature in DER: its message is the authenticator data, then the SHA-256 of the client data.
Cost
Each command charges one compute unit for each 4,096 bytes of the message, the data, and the key. The signature commands charge 64 more units for the work on the curve.
Errors
errorCode |
When |
|---|---|
ZTCL CRYPTO ALG |
An unknown algorithm, or an algorithm that the subcommand does not have. |
ZTCL CRYPTO KEY |
A public key or a seed of the wrong length or form. |
ZTCL CRYPTO ENCODING |
-encoding with ed25519. |
Tests
src/crypto/tests.zig has the vectors of FIPS 180-4 ("abc", the empty string, and the two-block message), the 256 byte values, the vectors of RFC 4231 (tests 1, 2, and 6), the vectors of RFC 8032 (Ed25519, tests 1 and 2) and of RFC 6979 (P-256 with SHA-256, the message "sample"), changed messages, changed signatures, and the errors.