Clé de synchro, dérivations HKDF et chiffrement des événements
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c29e7c6390
commit
ff2b22f5fd
@@ -0,0 +1,77 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { hkdfSync } from "node:crypto";
|
||||
import {
|
||||
generateSyncKey, normalizeSyncKey, isValidSyncKey, deriveKeys, encryptEvent, decryptEvent,
|
||||
SyncCryptoError, fromBase64url,
|
||||
} from "../../app/domain/sync-crypto.js";
|
||||
|
||||
// Clé dont les 16 octets valent 0, 1, …, 15.
|
||||
const KEY = "000G-40R4-0M30-E209-185G-R38E-1W";
|
||||
const BYTES = Uint8Array.from({ length: 16 }, (_, i) => i);
|
||||
|
||||
test("une clé tirée au hasard : 26 caractères Crockford par groupes de 4, et toujours différente", () => {
|
||||
const a = generateSyncKey();
|
||||
assert.match(a, /^([0-9A-HJKMNP-TV-Z]{4}-){6}[0-9A-HJKMNP-TV-Z]{2}$/);
|
||||
assert.equal(normalizeSyncKey(a), a);
|
||||
assert.notEqual(generateSyncKey(), a);
|
||||
});
|
||||
|
||||
test("normalisation : espaces, tirets, minuscules et lettres ambiguës", () => {
|
||||
assert.equal(normalizeSyncKey("000g 40r4 0m30 e209 185g r38e 1w"), KEY);
|
||||
assert.equal(normalizeSyncKey("OOOG40R4OM3OE2O9185GR38EIW"), KEY);
|
||||
assert.equal(normalizeSyncKey(" ooog-40r4-om3o-e2o9-l85g-r38e-lw "), KEY);
|
||||
assert.equal(normalizeSyncKey(KEY.replace(/-/g, "")), KEY);
|
||||
});
|
||||
|
||||
test("validation : longueur, alphabet et bits de complément", () => {
|
||||
assert.equal(normalizeSyncKey(KEY.slice(0, -1)), null, "trop courte");
|
||||
assert.equal(normalizeSyncKey(KEY + "0"), null, "trop longue");
|
||||
assert.equal(normalizeSyncKey(KEY.replace("G", "U")), null, "U hors de l'alphabet");
|
||||
assert.equal(normalizeSyncKey(KEY.slice(0, -1) + "X"), null, "bits de complément non nuls");
|
||||
assert.equal(normalizeSyncKey(/** @type {any} */ (42)), null);
|
||||
assert.equal(isValidSyncKey(KEY), true);
|
||||
assert.equal(isValidSyncKey("bonjour"), false);
|
||||
});
|
||||
|
||||
test("dérivations : vecteurs fixes, conformes à HKDF-SHA-256 (sel ia-fit)", async () => {
|
||||
const k = await deriveKeys(KEY);
|
||||
assert.equal(k.journalId, "ZgJsF_SV8W3E27y5p0AhMg");
|
||||
assert.equal(k.authToken, "_NaCNrXbQlORkPj77TOhUThrbXoF740wNVYw9Djg8Z0");
|
||||
// Recalcul indépendant avec l'implémentation de Node.
|
||||
const hkdf = (/** @type {string} */ info, /** @type {number} */ n) =>
|
||||
Buffer.from(hkdfSync("sha256", BYTES, "ia-fit", info, n)).toString("base64url");
|
||||
assert.equal(k.journalId, hkdf("ia-fit/id", 16));
|
||||
assert.equal(k.authToken, hkdf("ia-fit/auth", 32));
|
||||
assert.equal(k.encKey.algorithm.name, "AES-GCM");
|
||||
assert.equal(k.encKey.extractable, false);
|
||||
// Même clé, saisie autrement : mêmes dérivations.
|
||||
const again = await deriveKeys(KEY.toLowerCase().replace(/-/g, " "));
|
||||
assert.equal(again.journalId, k.journalId);
|
||||
assert.equal(again.authToken, k.authToken);
|
||||
});
|
||||
|
||||
test("une clé invalide est refusée sans être recopiée dans le message", async () => {
|
||||
await assert.rejects(deriveKeys("ABCD-EFGH-SECRET"), (err) => {
|
||||
assert.ok(err instanceof SyncCryptoError);
|
||||
assert.doesNotMatch(String(err.message), /SECRET|ABCD/);
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
test("chiffrement : aller-retour, IV aléatoire, autre clé refusée, bloc altéré refusé", async () => {
|
||||
const { encKey } = await deriveKeys(KEY);
|
||||
const event = { id: "e1", at: "2026-11-18T10:00:00.000Z", device: "pc", v: 1, type: "WeightRecorded", data: { date: "2026-11-18", kg: 80.5 } };
|
||||
const blob = await encryptEvent(encKey, event);
|
||||
assert.match(blob, /^[A-Za-z0-9_-]+$/);
|
||||
assert.equal(fromBase64url(blob)?.length, 12 + JSON.stringify(event).length + 16);
|
||||
assert.deepEqual(await decryptEvent(encKey, blob), event);
|
||||
assert.notEqual(await encryptEvent(encKey, event), blob, "deux chiffrements diffèrent (IV aléatoire)");
|
||||
|
||||
const other = await deriveKeys(generateSyncKey());
|
||||
await assert.rejects(decryptEvent(other.encKey, blob), /autre clé de synchro/);
|
||||
const flipped = blob.slice(0, 20) + (blob[20] === "A" ? "B" : "A") + blob.slice(21);
|
||||
await assert.rejects(decryptEvent(encKey, flipped), SyncCryptoError);
|
||||
await assert.rejects(decryptEvent(encKey, "court"), /mal formé/);
|
||||
await assert.rejects(decryptEvent(encKey, "pas du base64 !"), /mal formé/);
|
||||
});
|
||||
Reference in New Issue
Block a user