feat: zero-knowledge encryption Phase 1 — backend opaque + crypto module (WIP)

Backend: server can no longer read user data. All data blobs (medication,
appointment, profile name) are now opaque client-encrypted ciphertext — the
server stores and returns them verbatim, never deserializing the contents.

- Medication: removed MedicationData + flat MedicationResponse; new
  MedicationResponse echoes metadata + encrypted_data blob. Create/update
  accept opaque blobs (whole-blob replace). Update is no longer load-mutate-
  reserialize (server can't read the data).
- Appointment: same opaque treatment; status moved to a top-level document
  field so it remains filterable without decryption.
- Profile: name is now an opaque encrypted blob (name_data/name_iv). Auto-
  created profile on register starts with an empty name; client sets it.
- EncryptedFieldWire type shared across medication/appointment.

Frontend (partial): crypto module using Web Crypto API —
- crypto/keys.ts: double-PBKDF2 derivation (auth secret sent to server +
  encryption key kept in memory); in-memory key store (set/get/clear).
- crypto/cipher.ts: AES-GCM encrypt/decrypt + JSON convenience wrappers.
- crypto/index.ts: re-exports.

NOT YET DONE (frontend integration): auth store key derivation on login/register,
stores decrypt-on-load/encrypt-on-write, types update, UI components wired,
crypto round-trip tests, ADR. This commit is a verified checkpoint — backend
builds clean (21 tests, 0 warnings); frontend crypto module exists but is not
yet wired into the data flow.
This commit is contained in:
goose 2026-06-28 20:08:38 -03:00
parent 057303a8d0
commit 149ce37654
9 changed files with 331 additions and 566 deletions

View file

@ -31,28 +31,13 @@ pub async fn create_medication(
let now = SystemTime::now();
let medication_id = uuid::Uuid::new_v4().to_string();
// Build medication data JSON
let medication_data_value = serde_json::json!({
"name": req.name,
"dosage": req.dosage,
"frequency": req.frequency,
"route": req.route,
"reason": req.reason,
"instructions": req.instructions,
"sideEffects": req.side_effects.unwrap_or_default(),
"prescribedBy": req.prescribed_by,
"prescribedDate": req.prescribed_date,
"startDate": req.start_date,
"endDate": req.end_date,
"notes": req.notes,
"tags": req.tags.unwrap_or_default(),
});
// Zero-knowledge: the client sends an already-encrypted blob; the server
// stores it verbatim and never inspects the contents.
let medication_data = crate::models::health_data::EncryptedField {
data: medication_data_value.to_string(),
encrypted: false,
iv: String::new(),
auth_tag: String::new(),
data: req.encrypted_data.data,
encrypted: true,
iv: req.encrypted_data.iv,
auth_tag: req.encrypted_data.auth_tag,
};
let medication = Medication {
@ -61,19 +46,11 @@ pub async fn create_medication(
user_id: claims.sub,
profile_id: req.profile_id.clone(),
medication_data,
reminders: req
.reminder_times
.unwrap_or_default()
.into_iter()
.map(|time| crate::models::medication::MedicationReminder {
reminder_id: uuid::Uuid::new_v4().to_string(),
scheduled_time: time,
})
.collect(),
reminders: vec![],
created_at: now.into(),
updated_at: now.into(),
active: req.active,
pill_identification: req.pill_identification, // Phase 2.8
pill_identification: None,
};
match repo.create(medication).await {