gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3e07b1cf3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre3e07b1cf/plugins/crypto/server.hl

9.3 KB

  1. \* hl:crypto — passwords first. Native realm wrapper (see server.js for the JS twin).
  2. The whole surface is eight calls for passwords and hashing, and then the Web Push
  3. primitives (ticket #173, RFC 8030/8291/8292) further down: ES256, P-256 ECDH,
  4. HKDF-SHA256, AES-128-GCM and base64url, all on Bytes (see the README).
  5. The first eight: six of them exist to serve the first two.
  6. What this plugin is FOR is that an application never stores a password: it
  7. stores the answer to "could this password have produced that", and the answer
  8. carries its own algorithm and cost so it stays readable when both change.
  9. hash(password, options) the stored value — a PHC string
  10. verify(password, stored) true / false, in constant time
  11. parsePhc(stored) what a stored value says about itself
  12. kdf() which algorithm THIS host hashes with
  13. sha256(data) content hashing (fast on purpose — not for passwords)
  14. randomBytes(n, encoding) n bytes from the kernel CSPRNG
  15. toBase64(data) a String's or a Bytes' bytes as base64 text
  16. fromBase64(text) base64 text back to a Bytes (null if it is not base64)
  17. The realm is SERVER (plugin.json). A password never crosses to the client, so
  18. importing this file is also a declaration about where the importing code runs. *\
  19. \* Hash a password for storage. Returns a self-describing PHC string:
  20. $argon2id$v=19$m=32768,t=2,p=1$<salt>$<hash>
  21. $scrypt$ln=15,r=8,p=1$<salt>$<hash>
  22. Every call salts freshly, so hashing the same password twice gives two
  23. different strings and both verify.
  24. `options` is optional: { cost = 15; kdf = "argon2id" }
  25. cost base-2 log of the working memory in KiB — 15 is 32 MiB, the default.
  26. CAPPED to 10..17 (1 MiB .. 128 MiB); the string records what was
  27. actually used, so asking for 999 and reading the result back is how
  28. you see the cap rather than being told about it.
  29. kdf force an algorithm instead of taking the host's best one. Normally
  30. unnecessary: `hash` picks argon2id when the system libcrypto has it
  31. (OpenSSL >= 3.2) and scrypt otherwise, and `verify` reads both. *\
  32. hash(password, options) {
  33. return __native("crypto.hash", password, options)
  34. }
  35. \* Check a password against a stored PHC string. The comparison is constant-time
  36. and the answer is a plain boolean: a wrong password, a truncated string, a
  37. flipped character and a string that is not PHC at all are all `false`. A
  38. stored string whose ALGORITHM this host cannot compute is a loud error
  39. instead, because answering `false` to that would read as "wrong password". *\
  40. verify(password, stored) {
  41. return __native("crypto.verify", password, stored)
  42. }
  43. \* Compare String UTF-8 bytes or Bytes without depending on the mismatch position.
  44. Lengths are public: unequal byte lengths return false immediately. *\
  45. timingSafeEqual(a, b) {
  46. if (hlTypeName(a) != 'String' && hlTypeName(a) != 'Bytes') { return false }
  47. if (hlTypeName(b) != 'String' && hlTypeName(b) != 'Bytes') { return false }
  48. return __native("crypto.timing_safe_equal", toBytes(a).hex(), toBytes(b).hex())
  49. }
  50. \* Read a stored string without the password:
  51. { kdf = "argon2id"; version = 19; params = { m; t; p }; saltLen; hashLen }
  52. { kdf = "scrypt"; version = null; params = { ln; r; p }; saltLen; hashLen }
  53. `null` when the string is not a PHC string this plugin understands — which is
  54. also the cheapest way to spot a store that was never migrated. *\
  55. parsePhc(stored) {
  56. return __native("crypto.parse", stored)
  57. }
  58. \* Which algorithm `hash()` writes with on this host. Reporting only — nothing
  59. needs to branch on it, because every stored string names its own. *\
  60. kdf() {
  61. return __native("crypto.kdf")
  62. }
  63. \* SHA-256 of a string, as 64 lowercase hex characters. Content hashing: fast by
  64. design, and therefore exactly the wrong tool for a password. *\
  65. sha256(data) {
  66. return __native("crypto.sha256", data)
  67. }
  68. \* n random bytes from the kernel CSPRNG, rendered as "hex" (the default) or
  69. "base64". n is 1..1024. Suitable for session ids, one-time tokens and nonces. *\
  70. randomBytes(n, encoding) {
  71. return __native("crypto.random_bytes", n, encoding)
  72. }
  73. \* Base64 (the standard alphabet, padded) of a String's bytes — its UTF-8 — or
  74. of a Bytes, byte for byte:
  75. toBase64('user:pa55') \\ "dXNlcjpwYTU1"
  76. toBase64(req.bytes) \\ any bytes at all, NUL and 0xff included *\
  77. toBase64(
  78. data \\ a String or a Bytes
  79. ) {
  80. if (hlTypeName(data) == 'Bytes') {
  81. return __native("crypto.base64_encode_hex", data.hex())
  82. }
  83. return __native("crypto.base64_encode", data)
  84. }
  85. \* Base64 text back to its bytes, as a Bytes; `.toString()` of it is the text
  86. when the bytes are UTF-8. Padded and unpadded text both decode; anything
  87. that is not base64 in the standard alphabet is null, not an error — a
  88. malformed `Authorization: Basic` header is an answer, not a crash:
  89. let b = fromBase64(req.headers.authorization.slice(6))
  90. if (b != null) { let pair = b.toString() } \\ "user:pa55" *\
  91. fromBase64(String text) {
  92. let raw = __native("crypto.base64_decode", text)
  93. if (raw == null) {
  94. return null
  95. }
  96. return toBytes(raw)
  97. }
  98. \* ── Web Push primitives (ticket #173) ──────────────────────────────────────
  99. std.crypto only, no OpenSSL; node:crypto twins in server.js. Everything takes and
  100. returns Bytes; a function that can be handed bad input answers null instead of
  101. throwing, so "this subscription's key is garbage" is an answer, not a crash. *\
  102. \* base64url (RFC 4648 §5) WITHOUT padding: the form VAPID and Web Push use.
  103. base64urlEncode(toBytes("hi")) \\ "aGk" (a String encodes as its UTF-8) *\
  104. base64urlEncode(
  105. data \\ a String or a Bytes
  106. ) {
  107. if (hlTypeName(data) == 'Bytes') {
  108. return __native("crypto.base64url_encode_hex", data.hex())
  109. }
  110. return __native("crypto.base64url_encode", data)
  111. }
  112. \* base64url text back to a Bytes; null when it is not base64url (the + and / of the
  113. standard alphabet are refused). Padded and unpadded text both decode. *\
  114. base64urlDecode(String text) {
  115. let raw = __native("crypto.base64url_decode", text)
  116. if (raw == null) {
  117. return null
  118. }
  119. return toBytes(raw)
  120. }
  121. \* A fresh P-256 key pair from the kernel CSPRNG, for ES256 (VAPID) AND for ECDH (the
  122. Web Push message key): { privateKey = Bytes 32; publicKey = Bytes 65 } where the
  123. public key is the uncompressed point 0x04 || X || Y. *\
  124. p256Generate() {
  125. let both = toBytes(__native("crypto.p256_generate"))
  126. return { privateKey = both.slice(0, 32); publicKey = both.slice(32, 97) }
  127. }
  128. \* The uncompressed 65-byte public point of a 32-byte private scalar; null when the
  129. scalar is not a valid key (zero, or not below the group order). *\
  130. p256PublicKey(Bytes privateKey) {
  131. let raw = __native("crypto.p256_public", privateKey.hex())
  132. if (raw == null) {
  133. return null
  134. }
  135. return toBytes(raw)
  136. }
  137. \* ES256 (RFC 7518 §3.4): ECDSA P-256 over SHA-256 of `message`, signature as the JWS
  138. wants it — raw r || s, 64 bytes, NOT DER. Hedged with fresh noise, so two
  139. signatures of one message differ and both verify. *\
  140. es256Sign(Bytes privateKey, Bytes message) {
  141. return toBytes(__native("crypto.es256_sign", privateKey.hex(), message.hex()))
  142. }
  143. \* true when `signature` (64 bytes r || s) is `publicKey`'s (65 bytes) ES256 signature
  144. of `message`; false for every other case, malformed lengths and off-curve keys
  145. included. *\
  146. es256Verify(Bytes publicKey, Bytes message, Bytes signature) {
  147. return __native("crypto.es256_verify", publicKey.hex(), message.hex(), signature.hex())
  148. }
  149. \* ECDH on P-256: the shared secret, 32 bytes (the X coordinate of the product — what
  150. RFC 8291 §3.4 calls ecdh_secret). `peerPublicKey` is a 65-byte uncompressed point;
  151. null when it is not a point on the curve. *\
  152. ecdhSharedSecret(Bytes privateKey, Bytes peerPublicKey) {
  153. let raw = __native("crypto.ecdh_shared", privateKey.hex(), peerPublicKey.hex())
  154. if (raw == null) {
  155. return null
  156. }
  157. return toBytes(raw)
  158. }
  159. \* HKDF-SHA256 extract (RFC 5869 §2.2): PRK (32 bytes) = HMAC(salt, ikm). *\
  160. hkdfExtract(Bytes salt, Bytes ikm) {
  161. return toBytes(__native("crypto.hkdf_extract", salt.hex(), ikm.hex()))
  162. }
  163. \* HKDF-SHA256 expand (RFC 5869 §2.3): `length` (1..8160) bytes from a 32-byte PRK. *\
  164. hkdfExpand(Bytes prk, Bytes info, Number length) {
  165. return toBytes(__native("crypto.hkdf_expand", prk.hex(), info.hex(), length))
  166. }
  167. \* extract then expand in one call. *\
  168. hkdf(Bytes salt, Bytes ikm, Bytes info, Number length) {
  169. return hkdfExpand(hkdfExtract(salt, ikm), info, length)
  170. }
  171. \* AES-128-GCM seal: key 16 bytes, nonce 12 bytes; answers ciphertext || 16-byte tag
  172. (plaintext length + 16). `aad` is optional (null = none; Web Push uses none). *\
  173. aes128gcmEncrypt(Bytes key, Bytes nonce, Bytes plaintext, aad) {
  174. let ad = aad
  175. if (ad == null) {
  176. ad = toBytes(0)
  177. }
  178. return toBytes(__native("crypto.aes128gcm_encrypt", key.hex(), nonce.hex(), plaintext.hex(), ad.hex()))
  179. }
  180. \* AES-128-GCM open: the plaintext, or null when the tag does not verify (tampered,
  181. wrong key, wrong nonce, wrong aad) or the input is shorter than a tag. *\
  182. aes128gcmDecrypt(Bytes key, Bytes nonce, Bytes ciphertext, aad) {
  183. let ad = aad
  184. if (ad == null) {
  185. ad = toBytes(0)
  186. }
  187. let raw = __native("crypto.aes128gcm_decrypt", key.hex(), nonce.hex(), ciphertext.hex(), ad.hex())
  188. if (raw == null) {
  189. return null
  190. }
  191. return toBytes(raw)
  192. }

Branches

Latest commits

  • 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
  • d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre
  • 16bc8fd8Re-vendor Hybriel day38b with the mpackdb store-open race fixmre
  • d860dc2dnotes#12: the markup names its own rules instead of hand-written class strings: the selected row is `aria-current="page"` (the list-first theme rule styles it, no class), the delete question is `Style.pAsk` and follows its own `open` attribute (bound to `asking`), the error line is `Style.pMessage` (body once in lib/looks.hl, rule in notes.hl, main.hl and loginfailed.hl). The CSS declarations are the same, only selectors changed (.ask -> p.ask, .ask.open -> p.ask[open], .message -> p.message); the pages look the same (AE 0 at 4 sizes). main.hl's selectorClass in/out stays (login.js reads it).mre
  • 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
  • 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
  • e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
  • 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
  • 71a9538bnotes: B27W report, log linemre
  • 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
  • 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
  • 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
  • 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
  • dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
  • 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
  • c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
  • 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
  • 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
  • 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
  • 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre