AtlatestRepositorysigil-crypto
sigil-crypto / treeREADME.md
1
# sigil-crypto3
Cryptographic primitives for [Sigil](https://codeberg.org/sigil/sigil).5
Hashing, message authentication, key derivation, Ed25519 signatures,6
minisign verification, base64 encoding, and7
cryptographically secure random bytes. Built on a vendored mbedTLS and8
usable independently of TLS.10
## Modules12
| Module | Purpose |13
|--------------------------|----------------------------------------------------------|14
| `(sigil crypto)` | SHA/HMAC/PBKDF2 hashes, Ed25519, BLAKE2b, base64, random |15
| `(sigil crypto minisign)`| Parse and verify minisign public keys and signatures |17
## API summary19
| Procedure | Purpose |20
|-------------------------|-------------------------------------------------|21
| `sha1` | SHA-1 hex digest of a string or bytevector |22
| `sha256` | SHA-256 hex digest of a string or bytevector |23
| `sha256-bytes` | SHA-256 digest as a bytevector |24
| `hmac-sha256` | HMAC-SHA256 hex digest (key + message) |25
| `hmac-sha1` | HMAC-SHA1 hex digest (key + message) |26
| `pbkdf2-sha1` | PBKDF2-SHA1 key derivation (hex digest) |27
| `base64-encode` | Base64 encoding of a string or bytevector |28
| `base64-decode` | Base64 decoding to a string |29
| `random-bytes` | Cryptographically secure random bytevector |30
| `timing-safe-equal?` | Constant-time string comparison |31
| `ed25519-verify` | Verify an Ed25519 (RFC 8032) signature |32
| `ed25519-public-key` | Ed25519 public key from a 32-byte seed |33
| `ed25519-sign` | Ed25519 signature with a 32-byte seed |34
| `blake2b-512` | BLAKE2b digest (64 bytes) as a bytevector |35
| `minisign-verify` | Verify a minisign signature; returns the trusted comment or #f |36
| `minisign-verify-prehashed` | As `minisign-verify`, refusing legacy signatures (`minisign -V -H`) |37
| `minisign-failure-reason` | #f when a signature verifies, else why it failed |39
## minisign without the minisign binary41
```scheme42
(import (sigil crypto minisign) (sigil fs))44
(define key (minisign-parse-public-key45
"RWRa9dPSUBFexBbLdzZIfuAmuCYL736UeHC7IbdAOIYpgGIyDmWRaQHY"))46
(define sig (minisign-parse-signature (read-file-bytes "registry.json.minisig")))48
(minisign-verify-prehashed key sig (read-file-bytes "registry.json"))49
; => "registry=pkg.usesigil.org path=/v1/meta/registry.json seq=1 ts=..."50
; or #f when the file, signature or trusted comment has been altered51
```53
Checks the key id, the signature over the file (`ED`, prehashed with54
BLAKE2b-512, or legacy `Ed`) and the global signature over the trusted55
comment, as `minisign -V` does. Verification is cofactored (RFC 803256
section 5.1.7) and rejects small-order keys and R values; it agrees with57
libsodium, which minisign uses, except when the key or R has a58
small-order component, which only the key holder can produce and59
minisign never does. Use `minisign-verify-prehashed` (`-V -H`) for new60
code: without it, a prehashed signature relabelled as legacy verifies61
over the file's BLAKE2b digest, as it does in `minisign -V`.63
## WebAssembly65
On wasm32-wasi only the Monocypher half of the package is built:66
`ed25519-verify`, `ed25519-public-key`, `ed25519-sign`, `blake2b-512` and67
`(sigil crypto minisign)`. Every Mbed TLS-backed procedure raises68
"not available on wasm" there, because Mbed TLS does not build for69
wasm32-wasi yet. The browser build uses this same compiled Ed25519 rather70
than WebCrypto, so the API stays synchronous.72
## System prerequisites74
None beyond a working C toolchain. mbedTLS is vendored under75
`vendor/mbedtls/` and compiled in-tree with a minimal76
`sigil_mbedtls_config.h`. TLS 1.2 and certificate-authenticated TLS 1.377
are enabled, including PSA, HKDF and RSA-PSS support. Session tickets,78
PSK handshakes and early data remain disabled. The vendored version is79
Mbed TLS 3.6.7; see `vendor/MBEDTLS.md` for upstream provenance.81
Ed25519 and BLAKE2b come from Monocypher 4.0.3, vendored unmodified under82
`vendor/monocypher/` (provenance and the reasons for choosing it in83
`vendor/MONOCYPHER.md`).85
## Dependencies87
- sigil-stdlib89
## Build91
```sh92
sigil deps install93
sigil build94
sigil test --report95
```97
Two further gates need tools the suite does not:99
```sh100
test/differential/run.sh # Ed25519 against libsodium (needs guix)101
node test/wasm/run-wasm-test.mjs # host, static musl and wasm32-wasi agree (needs guix, node)102
```104
The first build compiles ~108 mbedTLS translation units plus105
`native/crypto.c`. Subsequent builds hit the cache. The minisign106
fixtures in `test/fixtures/minisign` are regenerated with107
`test/fixtures/minisign/make-fixtures.sh`.109
## Usage111
```scheme112
(import (sigil crypto))114
(sha256 "hello") ; => hex string115
(sha256-bytes "hello") ; => 32-byte bytevector116
(hmac-sha256 "secret-key" "message") ; => hex string117
(pbkdf2-sha1 "password" "salt" 4096 20) ; => hex string118
(base64-encode "hello") ; => "aGVsbG8="119
(base64-decode "aGVsbG8=") ; => "hello"120
(random-bytes 16) ; => #u8(...)121
(timing-safe-equal? "abc" "abc") ; => #t122
```124
## License126
BSD-3-Clause.128
Vendored mbedTLS (under `vendor/mbedtls/`) is distributed under129
Apache-2.0 OR GPL-2.0-or-later. sigil-crypto's own sources are130
BSD-3-Clause. See `vendor/mbedtls/LICENSE` for the mbedTLS terms.132
Vendored Monocypher (under `vendor/monocypher/`) is dual-licensed133
BSD-2-Clause OR CC0-1.0; sigil-crypto uses it under BSD-2-Clause. See134
`vendor/monocypher/LICENCE.md`.