gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2e22618e2e22618etests: cdp-blank/md-bg/md-keys-run Chrome range and base port from env (MDK_CHROME, MDK_PORT), defaults unchangedmre2e22618e/plugins/crypto/server.hl

8.9 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. \* Read a stored string without the password:
  44. { kdf = "argon2id"; version = 19; params = { m; t; p }; saltLen; hashLen }
  45. { kdf = "scrypt"; version = null; params = { ln; r; p }; saltLen; hashLen }
  46. `null` when the string is not a PHC string this plugin understands — which is
  47. also the cheapest way to spot a store that was never migrated. *\
  48. parsePhc(stored) {
  49. return __native("crypto.parse", stored)
  50. }
  51. \* Which algorithm `hash()` writes with on this host. Reporting only — nothing
  52. needs to branch on it, because every stored string names its own. *\
  53. kdf() {
  54. return __native("crypto.kdf")
  55. }
  56. \* SHA-256 of a string, as 64 lowercase hex characters. Content hashing: fast by
  57. design, and therefore exactly the wrong tool for a password. *\
  58. sha256(data) {
  59. return __native("crypto.sha256", data)
  60. }
  61. \* n random bytes from the kernel CSPRNG, rendered as "hex" (the default) or
  62. "base64". n is 1..1024. Suitable for session ids, one-time tokens and nonces. *\
  63. randomBytes(n, encoding) {
  64. return __native("crypto.random_bytes", n, encoding)
  65. }
  66. \* Base64 (the standard alphabet, padded) of a String's bytes — its UTF-8 — or
  67. of a Bytes, byte for byte:
  68. toBase64('user:pa55') \\ "dXNlcjpwYTU1"
  69. toBase64(req.bytes) \\ any bytes at all, NUL and 0xff included *\
  70. toBase64(
  71. data \\ a String or a Bytes
  72. ) {
  73. if (hlTypeName(data) == 'Bytes') {
  74. return __native("crypto.base64_encode_hex", data.hex())
  75. }
  76. return __native("crypto.base64_encode", data)
  77. }
  78. \* Base64 text back to its bytes, as a Bytes; `.toString()` of it is the text
  79. when the bytes are UTF-8. Padded and unpadded text both decode; anything
  80. that is not base64 in the standard alphabet is null, not an error — a
  81. malformed `Authorization: Basic` header is an answer, not a crash:
  82. let b = fromBase64(req.headers.authorization.slice(6))
  83. if (b != null) { let pair = b.toString() } \\ "user:pa55" *\
  84. fromBase64(String text) {
  85. let raw = __native("crypto.base64_decode", text)
  86. if (raw == null) {
  87. return null
  88. }
  89. return toBytes(raw)
  90. }
  91. \* ── Web Push primitives (ticket #173) ──────────────────────────────────────
  92. std.crypto only, no OpenSSL; node:crypto twins in server.js. Everything takes and
  93. returns Bytes; a function that can be handed bad input answers null instead of
  94. throwing, so "this subscription's key is garbage" is an answer, not a crash. *\
  95. \* base64url (RFC 4648 §5) WITHOUT padding: the form VAPID and Web Push use.
  96. base64urlEncode(toBytes("hi")) \\ "aGk" (a String encodes as its UTF-8) *\
  97. base64urlEncode(
  98. data \\ a String or a Bytes
  99. ) {
  100. if (hlTypeName(data) == 'Bytes') {
  101. return __native("crypto.base64url_encode_hex", data.hex())
  102. }
  103. return __native("crypto.base64url_encode", data)
  104. }
  105. \* base64url text back to a Bytes; null when it is not base64url (the + and / of the
  106. standard alphabet are refused). Padded and unpadded text both decode. *\
  107. base64urlDecode(String text) {
  108. let raw = __native("crypto.base64url_decode", text)
  109. if (raw == null) {
  110. return null
  111. }
  112. return toBytes(raw)
  113. }
  114. \* A fresh P-256 key pair from the kernel CSPRNG, for ES256 (VAPID) AND for ECDH (the
  115. Web Push message key): { privateKey = Bytes 32; publicKey = Bytes 65 } where the
  116. public key is the uncompressed point 0x04 || X || Y. *\
  117. p256Generate() {
  118. let both = toBytes(__native("crypto.p256_generate"))
  119. return { privateKey = both.slice(0, 32); publicKey = both.slice(32, 97) }
  120. }
  121. \* The uncompressed 65-byte public point of a 32-byte private scalar; null when the
  122. scalar is not a valid key (zero, or not below the group order). *\
  123. p256PublicKey(Bytes privateKey) {
  124. let raw = __native("crypto.p256_public", privateKey.hex())
  125. if (raw == null) {
  126. return null
  127. }
  128. return toBytes(raw)
  129. }
  130. \* ES256 (RFC 7518 §3.4): ECDSA P-256 over SHA-256 of `message`, signature as the JWS
  131. wants it — raw r || s, 64 bytes, NOT DER. Hedged with fresh noise, so two
  132. signatures of one message differ and both verify. *\
  133. es256Sign(Bytes privateKey, Bytes message) {
  134. return toBytes(__native("crypto.es256_sign", privateKey.hex(), message.hex()))
  135. }
  136. \* true when `signature` (64 bytes r || s) is `publicKey`'s (65 bytes) ES256 signature
  137. of `message`; false for every other case, malformed lengths and off-curve keys
  138. included. *\
  139. es256Verify(Bytes publicKey, Bytes message, Bytes signature) {
  140. return __native("crypto.es256_verify", publicKey.hex(), message.hex(), signature.hex())
  141. }
  142. \* ECDH on P-256: the shared secret, 32 bytes (the X coordinate of the product — what
  143. RFC 8291 §3.4 calls ecdh_secret). `peerPublicKey` is a 65-byte uncompressed point;
  144. null when it is not a point on the curve. *\
  145. ecdhSharedSecret(Bytes privateKey, Bytes peerPublicKey) {
  146. let raw = __native("crypto.ecdh_shared", privateKey.hex(), peerPublicKey.hex())
  147. if (raw == null) {
  148. return null
  149. }
  150. return toBytes(raw)
  151. }
  152. \* HKDF-SHA256 extract (RFC 5869 §2.2): PRK (32 bytes) = HMAC(salt, ikm). *\
  153. hkdfExtract(Bytes salt, Bytes ikm) {
  154. return toBytes(__native("crypto.hkdf_extract", salt.hex(), ikm.hex()))
  155. }
  156. \* HKDF-SHA256 expand (RFC 5869 §2.3): `length` (1..8160) bytes from a 32-byte PRK. *\
  157. hkdfExpand(Bytes prk, Bytes info, Number length) {
  158. return toBytes(__native("crypto.hkdf_expand", prk.hex(), info.hex(), length))
  159. }
  160. \* extract then expand in one call. *\
  161. hkdf(Bytes salt, Bytes ikm, Bytes info, Number length) {
  162. return hkdfExpand(hkdfExtract(salt, ikm), info, length)
  163. }
  164. \* AES-128-GCM seal: key 16 bytes, nonce 12 bytes; answers ciphertext || 16-byte tag
  165. (plaintext length + 16). `aad` is optional (null = none; Web Push uses none). *\
  166. aes128gcmEncrypt(Bytes key, Bytes nonce, Bytes plaintext, aad) {
  167. let ad = aad
  168. if (ad == null) {
  169. ad = toBytes(0)
  170. }
  171. return toBytes(__native("crypto.aes128gcm_encrypt", key.hex(), nonce.hex(), plaintext.hex(), ad.hex()))
  172. }
  173. \* AES-128-GCM open: the plaintext, or null when the tag does not verify (tampered,
  174. wrong key, wrong nonce, wrong aad) or the input is shorter than a tag. *\
  175. aes128gcmDecrypt(Bytes key, Bytes nonce, Bytes ciphertext, aad) {
  176. let ad = aad
  177. if (ad == null) {
  178. ad = toBytes(0)
  179. }
  180. let raw = __native("crypto.aes128gcm_decrypt", key.hex(), nonce.hex(), ciphertext.hex(), ad.hex())
  181. if (raw == null) {
  182. return null
  183. }
  184. return toBytes(raw)
  185. }

Branches

Latest commits

  • 2e22618etests: cdp-blank/md-bg/md-keys-run Chrome range and base port from env (MDK_CHROME, MDK_PORT), defaults unchangedmre
  • a5d2384bre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 52902d23docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 022b943fdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • d17f8543mission 012: re-vendor Hybriel master f9614007 (plugins), fixes comments push redraw; gate 480/0mre
  • 5f95de05mission 012: re-vendor Hybriel ce6f2e0a (plugins), report: gate 478/2 on comments push redrawmre
  • 3794c591mission 011: report, LOG, STATUS, md-keys-run widths, cdp-blank helpermre
  • 77a93c16mission 011: gate hook for md-keys (480/0), flatten() runs one at a time (removeChild race), surface also built on first focus / pointer-down (background tab), md-bg test, README keysmre
  • 3c906718mission 011: md-editor key tests (real keys) and fixes: nested p/ul flatten, bare br, link change, Ctrl+E, code block exit (WIP)mre
  • 1442e583mission 010: report, LOG, STATUSmre
  • 6f27ad31md-editor: phone toolbar row contained (inline-size) so it never widens a grid parentmre
  • d702d76dmd-editor look: root class md-empty (a host .empty rule hit it), rows = surface height, one phone toolbar row with 44 px targets; comment form rows 3; gate 360/0mre
  • 15be2ac0comments: attach (FilesUpload pick), onPost(text, files), files per comment; md-editor files=off; README, gate fixmre
  • 1f8d79d6comments (mission 009 WIP, tickets#27): attachments — attach="true" puts the form in FilesUpload pick mode, onPost(text, files), files per comment (thumbnails open full size, file rows, no url = no link); MdEditor files="off"; demo with attachments; gate m009 checks (WIP: 356/1)mre
  • 13389d67mission 008: report, LOG, STATUSmre
  • ab47e4a2files-upload (mission 008, #18 for hycon#46): pick-first-send-later mode — host-owned list (files + onChange), slot for the host's input area, Ctrl+V paste (both modes), thumbnails, name+size rows, ✕ remove, maxSize per file, Camera on touch screens, 44 px targets; demo /files-upload/pick; gate 343/0 (19 new, 18 fail on the old code)mre
  • 172e66bemission brief 008mre
  • 9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • 00a95329components (mission 007): re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ git-ignored), gate 324/0 old and new, real-data copy unchanged, report 007, same-output.mjs ports overridablemre
  • fe50766bmission brief 007mre