openvibers/openvibe.finance servercrypto.js
crypto.js
| 1 | ; |
| 2 | /** |
| 3 | * Per-person encryption of the words in a person's money data (plan §L.2, "hosted private: encrypted per person"). |
| 4 | * |
| 5 | * const vault = createVault(process.env.FINANCE_DATA_KEY); // 32 bytes, base64 |
| 6 | * const sealed = vault.seal(subject, 'Whole Foods'); // 'v1.<iv>.<tag>.<ciphertext>' (base64url) |
| 7 | * vault.open(subject, sealed) // 'Whole Foods' |
| 8 | * vault.mark(subject, 'some text') // an HMAC: equal inputs, equal marks (dedupe) |
| 9 | * |
| 10 | * Each person's key is HKDF-SHA256 of the service key with their subject as the info, so one person's rows cannot be |
| 11 | * opened with another's key, and a database copy without the service key reads as noise. This is encryption at rest: |
| 12 | * the service holds the key and opens a row only to show it to its owner. |
| 13 | */ |
| 14 | const crypto = require('node:crypto'); |
| 15 | |
| 16 | const SALT = Buffer.from('openvibe-finance/v1'); |
| 17 | |
| 18 | function createVault(keyBase64) { |
| 19 | const master = Buffer.from(String(keyBase64 || ''), 'base64'); |
| 20 | if (master.length !== 32) throw new Error('FINANCE_DATA_KEY must be 32 bytes, base64'); |
| 21 | const keys = new Map(); |
| 22 | const keyOf = (subject, use) => { |
| 23 | const id = `${use}\u0000${subject}`; |
| 24 | let k = keys.get(id); |
| 25 | if (!k) { |
| 26 | k = Buffer.from(crypto.hkdfSync('sha256', master, SALT, Buffer.from(id), 32)); |
| 27 | if (keys.size > 5000) keys.clear(); |
| 28 | keys.set(id, k); |
| 29 | } |
| 30 | return k; |
| 31 | }; |
| 32 | const subjectOk = (subject) => { |
| 33 | if (!/^usr_[0-9A-Za-z]{10,40}$/.test(String(subject || ''))) throw new Error('a person (usr_ subject) is required'); |
| 34 | }; |
| 35 | function seal(subject, text) { |
| 36 | subjectOk(subject); |
| 37 | const plain = String(text == null ? '' : text); |
| 38 | if (!plain) return ''; |
| 39 | const iv = crypto.randomBytes(12); |
| 40 | const c = crypto.createCipheriv('aes-256-gcm', keyOf(subject, 'seal'), iv); |
| 41 | const body = Buffer.concat([c.update(plain, 'utf8'), c.final()]); |
| 42 | return ['v1', iv.toString('base64url'), c.getAuthTag().toString('base64url'), body.toString('base64url')].join('.'); |
| 43 | } |
| 44 | function open(subject, sealed) { |
| 45 | subjectOk(subject); |
| 46 | if (!sealed) return ''; |
| 47 | const [v, iv, tag, body] = String(sealed).split('.'); |
| 48 | if (v !== 'v1' || !iv || !tag || body == null) throw new Error('not a sealed value'); |
| 49 | const d = crypto.createDecipheriv('aes-256-gcm', keyOf(subject, 'seal'), Buffer.from(iv, 'base64url')); |
| 50 | d.setAuthTag(Buffer.from(tag, 'base64url')); |
| 51 | return Buffer.concat([d.update(Buffer.from(body, 'base64url')), d.final()]).toString('utf8'); |
| 52 | } |
| 53 | function mark(subject, text) { |
| 54 | subjectOk(subject); |
| 55 | return crypto.createHmac('sha256', keyOf(subject, 'mark')).update(String(text)).digest('base64url'); |
| 56 | } |
| 57 | return { seal, open, mark }; |
| 58 | } |
| 59 | |
| 60 | module.exports = { createVault }; |
| 61 |