crypto.bend
bend-kit-crypto@0.2.1.0 · 103 lines · 0 definitions
1# Hashes, HMAC, HKDF, RSA and ECDSA signatures, and secure random bytes through OpenSSL 3 libcrypto. Source: https://github.com/paymog/bend-kit/tree/main/crypto
2import Base
3
4# Bytes travel as (len, words): len octets, four to a U32 word, low byte first, as Wire.recv.words gives them.
5# The libcrypto effects fail with ENOENT when libcrypto.3 does not load; set BEND_LIBCRYPTO to its path.
6
7# alg is an OpenSSL digest name, such as "SHA256" or "SHA3-256". Fails with EINVAL for an unknown name.
8def digest.words(alg: String, len: U32, words: Array<U32>) ->
9 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
10 import "./effs/crypto.c"
11 import "./effs/crypto.js"
12
13def sha256.words(len: U32, words: Array<U32>) -> IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
14 digest.words("SHA256", len, words)
15
16def sha512.words(len: U32, words: Array<U32>) -> IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
17 digest.words("SHA512", len, words)
18
19# Legacy: SHA-1 is broken for collisions. Use it only where a protocol needs it, such as the WebSocket handshake.
20def sha1.words(len: U32, words: Array<U32>) -> IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
21 digest.words("SHA1", len, words)
22
23# HMAC (RFC 2104) of data under key, with the digest alg.
24def hmac.words(alg: String, klen: U32, key: Array<U32>, dlen: U32, data: Array<U32>) ->
25 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
26 import "./effs/crypto.c"
27 import "./effs/crypto.js"
28
29# HKDF (RFC 5869): n octets from ikm, salt, and info. Fails with EINVAL when n is 0 or more than 255 digest lengths.
30def hkdf.words(alg: String, slen: U32, salt: Array<U32>, klen: U32, ikm: Array<U32>, ilen: U32, info: Array<U32>, n: U32) ->
31 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
32 import "./effs/crypto.c"
33 import "./effs/crypto.js"
34
35# PBKDF2 (RFC 8018 §5.2) with HMAC over alg: n octets from pass and salt. Fails with EINVAL when iters or n is 0.
36def pbkdf2.words(alg: String, plen: U32, pass: Array<U32>, slen: U32, salt: Array<U32>, iters: U32, n: U32) ->
37 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
38 import "./effs/crypto.c"
39 import "./effs/crypto.js"
40
41# n octets from the OS secure random source (getentropy, crypto.getRandomValues). Needs no libcrypto.
42def random.words(n: U32) -> IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
43 import "./effs/crypto.c"
44 import "./effs/crypto.js"
45
46# Equal octets, in time that depends only on the lengths. Use it to compare MACs.
47def eq.ct.words(alen: U32, a: Array<U32>, blen: U32, b: Array<U32>) -> IO(Bool):
48 import "./effs/crypto.c"
49 import "./effs/crypto.js"
50
51# Signatures. Sign keys are unencrypted PEM private keys (PKCS#8 "PRIVATE KEY", or "RSA PRIVATE KEY" / "EC PRIVATE KEY");
52# verify keys are PEM SubjectPublicKeyInfo ("PUBLIC KEY"), or JWK parts as big-endian octets (RFC 7518 §6).
53# Each key type is fixed per def, so a key of the other type fails: RSA needs 2048 bits or more, ECDSA needs P-256.
54# Verify gives Done False for any wrong signature, whatever its length; Fail EINVAL is for a bad alg or key.
55
56# RSASSA-PKCS1-v1_5 (RS256, RS384, RS512): alg is "SHA256", "SHA384", or "SHA512". Gives a modulus-length signature.
57def rsa.sign.words(alg: String, klen: U32, key: Array<U32>, dlen: U32, data: Array<U32>) ->
58 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
59 import "./effs/crypto.c"
60 import "./effs/crypto.js"
61
62def rsa.verify.words(alg: String, klen: U32, key: Array<U32>, dlen: U32, data: Array<U32>, slen: U32, sig: Array<U32>) ->
63 IO(Result<&1, &1, U32 & String, Bool>):
64 import "./effs/crypto.c"
65 import "./effs/crypto.js"
66
67# n and e of a JWK "RSA" key. e must be odd and at least 3.
68def rsa.verify.jwk.words(alg: String, nlen: U32, n: Array<U32>, elen: U32, e: Array<U32>, dlen: U32, data: Array<U32>, slen: U32, sig: Array<U32>) ->
69 IO(Result<&1, &1, U32 & String, Bool>):
70 import "./effs/crypto.c"
71 import "./effs/crypto.js"
72
73# ECDSA on P-256 (ES256): alg is "SHA256". Signatures are JOSE raw r || s, 64 octets (RFC 7518 §3.4), not DER.
74def ecdsa.sign.words(alg: String, klen: U32, key: Array<U32>, dlen: U32, data: Array<U32>) ->
75 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
76 import "./effs/crypto.c"
77 import "./effs/crypto.js"
78
79def ecdsa.verify.words(alg: String, klen: U32, key: Array<U32>, dlen: U32, data: Array<U32>, slen: U32, sig: Array<U32>) ->
80 IO(Result<&1, &1, U32 & String, Bool>):
81 import "./effs/crypto.c"
82 import "./effs/crypto.js"
83
84# x and y of a JWK "EC" key on "P-256", 32 octets each.
85def ecdsa.verify.jwk.words(alg: String, xlen: U32, x: Array<U32>, ylen: U32, y: Array<U32>, dlen: U32, data: Array<U32>, slen: U32, sig: Array<U32>) ->
86 IO(Result<&1, &1, U32 & String, Bool>):
87 import "./effs/crypto.c"
88 import "./effs/crypto.js"
89
90# AEAD: alg is "AES-256-GCM" or "CHACHA20-POLY1305". Key: 32 octets; nonce: 12 octets.
91# Seal returns ciphertext followed by a 16-octet tag; open verifies the tag before returning plaintext.
92# Never reuse a (key, nonce) pair. Bad lengths, algorithm or authentication fail with EINVAL.
93def aead.seal.words(alg: String, klen: U32, key: Array<U32>, nlen: U32, nonce: Array<U32>,
94 alen: U32, aad: Array<U32>, dlen: U32, data: Array<U32>) ->
95 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
96 import "./effs/crypto.c"
97 import "./effs/crypto.js"
98
99def aead.open.words(alg: String, klen: U32, key: Array<U32>, nlen: U32, nonce: Array<U32>,
100 alen: U32, aad: Array<U32>, dlen: U32, data: Array<U32>) ->
101 IO(Result<&1, &1, U32 & String, U32 & Array<U32>>):
102 import "./effs/crypto.c"
103 import "./effs/crypto.js"