Encrypt Incidents
In this guide, we will create a monitorable incident, add an event, and close it with encrypted requests.Why Encrypt Incidents?
Incident events describe what is happening at a monitored premises, person, or vehicle. Encrypted incident requests keep those events unreadable between your device and Hyperion.
Your device generates an incident key for each incident. The create request delivers it to Hyperion in two halves, and every event and update for the incident is encrypted with it. When the incident closes, Hyperion deletes the key.
For the full construction, including the incident key lifecycle, see the Encrypted Requests concept page.
Getting Started
Before encrypting incidents, you need:
- An application with the
key:readandmonitorable-incident:writescopes. - An active monitorable and an event type to report. See Create an Incident.
- The helpers and encryption key from the Encrypt Monitorable Data guide.
- If the monitorable has a signing key, the device's
SigningKey.
Step 1: Add the Incident Helpers
Incident payloads use AES-256-GCM under the incident key. Add these helpers next to the ones from Encrypt Monitorable Data:
import { hkdfSync, webcrypto } from "node:crypto";
const INCIDENT_KEY_LENGTH = 32;
const BUCKET_MINUTES = 10;
export function randomBytes(length: number): Uint8Array {
return webcrypto.getRandomValues(new Uint8Array(length));
}
export function currentBucketTimestamp(date: Date = new Date()): string {
const bucket = new Date(date);
bucket.setUTCMinutes(
bucket.getUTCMinutes() - (bucket.getUTCMinutes() % BUCKET_MINUTES),
0,
0,
);
return canonicalTimestamp(bucket);
}
export function incidentBinding(
monitorableId: string,
bucketTimestamp: string,
): Uint8Array {
return concat(lp(monitorableId), lp(bucketTimestamp));
}
export async function sealIncidentPayload(
incidentKey: Uint8Array,
payload: object,
aad: Uint8Array,
): Promise<Uint8Array> {
const iv = randomBytes(12);
const key = await webcrypto.subtle.importKey(
"raw",
incidentKey,
{ name: "AES-GCM" },
false,
["encrypt"],
);
const ciphertext = await webcrypto.subtle.encrypt(
{ name: "AES-GCM", iv, additionalData: aad },
key,
jsonBytes(payload),
);
return concat(iv, new Uint8Array(ciphertext));
}
export function payloadCommitment(
bucketTimestamp: string,
incidentKeyConfirm: Uint8Array,
encryptedPayload: Uint8Array,
incidentId?: string,
): Uint8Array {
return sha256(
concat(
lp(bucketTimestamp),
lp(incidentKeyConfirm),
incidentId ? lp(incidentId) : EMPTY,
lp(sha256(encryptedPayload)),
),
);
}
Step 2: Generate and Split the Incident Key
Generate a new incident key and pick the bucket it belongs to. The bucket is the start of the current 10-minute window, and it must be no more than 30 minutes before the time you escalate the incident.
Split the key into two shares. keyShare1 is sent as is. keyShare2 is encrypted to your encryption key with HPKE, and the same HPKE context exports keyShare2Confirm:
const incidentKey = randomBytes(INCIDENT_KEY_LENGTH);
const bucketTimestamp = currentBucketTimestamp();
const binding = incidentBinding(monitorableId, bucketTimestamp);
const pkSigningKeyHash = signingKey && sha256(signingKey.publicKeySpki);
const keyShare1 = randomBytes(INCIDENT_KEY_LENGTH);
const keyShare2 = keyShare1.map((byte, index) => byte ^ incidentKey[index]);
const { encapsulatedSecret, ctx } = await suite.SetupSender(
await importEncryptionKey(encryptionKey),
{
info: concat(
pkSigningKeyHash ? lp(pkSigningKeyHash) : EMPTY,
lp(monitorableId),
),
},
);
const encryptedKeyShare2 = concat(
encapsulatedSecret,
await ctx.Seal(keyShare2, binding),
);
const keyShare2Confirm = await ctx.Export(EMPTY, INCIDENT_KEY_LENGTH);
const incidentKeyConfirm = new Uint8Array(
hkdfSync(
"sha256",
incidentKey,
EMPTY,
concat(Uint8Array.of(0, INCIDENT_KEY_LENGTH), binding),
INCIDENT_KEY_LENGTH,
),
);
const hashEncryptedKeyShare1 = sha256(encryptedKeyShare1);
encryptedKeyShare1 is the copy of keyShare1 that you keep encrypted for your own systems. Hyperion only receives its hash.
Keep incidentKey and bucketTimestamp on the device until the incident closes. Every later request for the incident needs them.
Step 3: Create the Incident
Encrypt each event with the incident key and the binding as aad. Each event is the same JSON as an event in a plaintext create incident request. The first event must also include responseKey, a Base64 32-byte key:
const payloads = [
{ ...firstEvent, responseKey: toBase64(randomBytes(32)) },
...laterEvents,
];
const events = await Promise.all(
payloads.map(async (payload) => {
const encryptedPayload = await sealIncidentPayload(
incidentKey,
payload,
binding,
);
return {
timestamp: canonicalTimestamp(),
encryptedPayload: toBase64(encryptedPayload),
...(signingKey && {
signaturePayload: toBase64(
await signingKey.sign(
payloadCommitment(bucketTimestamp, incidentKeyConfirm, encryptedPayload),
),
),
}),
};
}),
);
Build the envelope. With a signing key, also sign the incident key commitment and send pkSigningKeyHash:
const incidentKeyCommitment = sha256(
concat(
lp(bucketTimestamp),
lp(hashEncryptedKeyShare1),
lp(sha256(encryptedKeyShare2)),
lp(keyShare2Confirm),
lp(incidentKeyConfirm),
),
);
const envelope = {
escalationTimestamp: canonicalTimestamp(),
bucketTimestamp,
...(pkSigningKeyHash && { pkSigningKeyHash: toBase64(pkSigningKeyHash) }),
reconstruction: {
encryptionKeyId: encryptionKey.id,
encryptedKeyShare2: toBase64(encryptedKeyShare2),
keyShare1: toBase64(keyShare1),
hashEncryptedKeyShare1: toBase64(hashEncryptedKeyShare1),
keyShare2Confirm: toBase64(keyShare2Confirm),
incidentKeyConfirm: toBase64(incidentKeyConfirm),
...(signingKey && {
signatureIncidentKey: toBase64(await signingKey.sign(incidentKeyCommitment)),
}),
},
events,
};
Send it to the Create Monitorable Incident endpoint:
The response is the same incident response a plaintext request returns. Keep its id for the next steps.
Step 4: Add an Event
Encrypt the event with the same incident key and binding, and send the incident's bucketTimestamp:
const encryptedPayload = await sealIncidentPayload(incidentKey, event, binding);
const envelope = {
bucketTimestamp,
...(pkSigningKeyHash && { pkSigningKeyHash: toBase64(pkSigningKeyHash) }),
timestamp: canonicalTimestamp(),
encryptedPayload: toBase64(encryptedPayload),
...(signingKey && {
signaturePayload: toBase64(
await signingKey.sign(
payloadCommitment(bucketTimestamp, incidentKeyConfirm, encryptedPayload),
),
),
}),
};
Send it to the Add Monitorable Incident Event endpoint:
Step 5: Update the Incident
An update closes a resolved incident, records a reason, or merges media. It is also bound to the incident: add lp(incidentId) to the binding, and pass incidentId to the commitment:
const update = { status: "CLOSED", reason: "Resolved by the customer" };
const encryptedPayload = await sealIncidentPayload(
incidentKey,
update,
concat(binding, lp(incidentId)),
);
const envelope = {
bucketTimestamp,
...(pkSigningKeyHash && { pkSigningKeyHash: toBase64(pkSigningKeyHash) }),
encryptedPayload: toBase64(encryptedPayload),
...(signingKey && {
signaturePayload: toBase64(
await signingKey.sign(
payloadCommitment(
bucketTimestamp,
incidentKeyConfirm,
encryptedPayload,
incidentId,
),
),
),
}),
};
Send it to the Update Monitorable Incident endpoint:
Closing the incident deletes its incident key. You can delete it from the device too.
Once an incident is closed, encrypted add event and update requests for it return 409.
Try It: Encrypt an Incident
Use the builder below to run Steps 2 through 5 in your browser:
- Choose Create an incident, enter the monitorable's ID, and fill in the first event. Use an event type your monitorable supports. The builder generates the incident key, splits it, and adds the
responseKey. - Send the request and copy the incident's
idfrom the response. - Choose Add an event or Update the incident and enter the incident's ID. These steps reuse the incident key and bucket from step 1.
The builder uses a random placeholder for encryptedKeyShare1. In your integration, it is your own encrypted copy of keyShare1.
Encrypted incident request builder
Create an incident with a new incident key, then add events to it and close it.
Encryption key
Paste the id, algorithm, and publicKey from GET /v1/encryption-keys/current, or generate a demo key. Hyperion can't decrypt requests built with a demo key.
Monitorable signing key
Generated in this browser tab and kept only until you reload. Other builders on these pages sign with the same key.
Request
Event type identifier
Date and time when the event was observed
Media entries to merge onto the monitorable incident
{}
Troubleshooting
| Status | Message | Cause |
|---|---|---|
| 400 | Validation error on bucketTimestamp | bucketTimestamp isn't on the 10-minute grid, is after escalationTimestamp, or is more than 30 minutes before it |
| 400 | bucketTimestamp must match the bucketTimestamp the incident was created with | An add event or update sent a different bucket |
| 400 | incidentKeyConfirm does not match the reconstructed incident key | The shares, the binding, or the HKDF input are wrong |
| 400 | Failed to decrypt ... | The wrong key, info, or aad, such as an add event payload sent as an update |
| 400 | Encrypted request has already been processed | The same ciphertext was accepted in the last 24 hours |
| 409 | The encrypted update window for this incident has closed | The incident is closed, or was created from a plaintext request |
Conclusion
You've created a monitorable incident, added an event, and closed it, all with encrypted requests under one incident key.
Best Practices
- Use a new incident key for every incident: Never reuse a key or a key share
- Keep the key until the incident closes: Store it with its
bucketTimestampin protected storage on the device - Retry failed requests as they are: Hyperion accepts each ciphertext once, and only remembers requests that succeed
- Use the current bucket: Compute
bucketTimestampwhen you escalate the incident, not ahead of time
Learn More
Related Guides
- Encrypt Monitorable Data - The shared helpers and encryption key
- Sign Requests - Add a monitorable signing key and sign requests
- Look Up Records - Read incidents and actions with encrypted lookups
Related Concepts
- Encrypted Requests - Every envelope, binding, and signed commitment
- Monitorable Incidents - Incidents and their lifecycle
API Reference
- Create Monitorable Incident - Create an incident
- Add Monitorable Incident Event - Add an event to an incident
- Update Monitorable Incident - Close or update an incident