Why one signature is not enough
Chapter 11 signed a token: one small piece of data, finished before it was signed. A log is never finished, and that breaks the obvious ideas.
- Sign the whole file. That works only once the file is closed, and checking it means reading all of it. A log is read while it is being written, and usually only its latest part.
- Sign every record. Then a changed record is caught. A deleted one is not: the records that remain are all correctly signed. Neither are two records that changed places.
What has to be signed is the stream itself: these bytes, in this order, with nothing missing between them. And it has to be done as the log grows.
Records about the records
The answer is to put, between the events, records of another kind that say something about the bytes before them. Dagr calls them meta records. A sink declares them in a list of their own:
AuditLog = DataSink("AuditLog",
node_types=[
Node("Event", fields=[
"ts" >> t.u64 >> required,
"actor" >> t.utf8 >> required,
"action" >> t.ref("Action") >> required,
"target" >> t.utf8,
"details" >> t.utf8,
]),
Enum("Action", ["login", "logout", "read", "write", "delete", "grant", "revoke"]),
],
meta_types=[
Node("Checkpoint", fields=[...]),
Node("Seal", fields=[...]),
],
doubly_linked=True,
)
This is all Dagr itself contributes, and it is little on purpose.
- Meta records are ordinary records. They have a kind and a length like every record in chapter 12. Their kinds are numbered from a range of their own, far from the others, so adding one never renumbers your events.
- A reader that does not care steps over them, by their length, as it would over any kind it does not know. A log with checkpoints in it is still a plain log to an old program.
- The writer and the reader hand your code the exact bytes of each record, and where in the stream they are. A signature is over bytes, so this is the part that matters.
- Dagr brings no cryptography and decides nothing. What a checkpoint contains, how it is signed and what a verifier accepts is your code. What follows is a recipe that Dagr's specification works out in detail. It is advice, and not part of the format.
Checkpoints
Every few events the writer adds a checkpoint. A checkpoint says: "the stretch of the stream that ends right here, and began so many bytes back, had this hash", and signs that statement.
Build a log below. Then choose a checkpoint to see which records it vouches for.
- A checkpoint covers what is before it, never itself. Its stretch ends where its own bytes begin. So everything it signs exists when it is written, and nothing has to be filled in afterwards.
- The stretches overlap. With "the last 2 periods", each checkpoint reaches back past the checkpoint before it, to the one before that. Every event is then vouched for twice, and each checkpoint vouches for the one before it, bytes and all.
- Each checkpoint carries the signature of the one before. That makes them a chain. A checkpoint cannot be dropped or replaced without the next one noticing.
- It counts as well as hashes. A checkpoint says how many bytes back its stretch begins and how many records are in it. A verifier checks that the two agree with the stream before it looks at any signature.
- What is signed is a short statement, not the stretch. The stretch is hashed, and the signature is over the hash together with every other field of the checkpoint. The writer keeps a few running hashes and never has to hold on to the bytes.
The seal
Tick "close the log with a seal". A seal is one last record: how many records and bytes came before it, a hash of all of them, the last checkpoint's signature, and a signature over those. It says something no checkpoint can say: this is the end.
Try to change the past
Here is a finished log and a verifier. The verifier knows two things that it did not get from the log: the key, and which log this is supposed to be. Change the log and see what it says.
- A changed byte, a deleted record, two records swapped, a record added: each is caught at the next checkpoint. Its hash is over the bytes in their order, so any of these changes it. When a record was added or removed, the count of bytes is wrong as well, and the verifier sees that before it hashes anything.
- Bytes from another log are refused although every signature in them is genuine. The name of the log is part of what each checkpoint signs, and the verifier uses the name it was told, not one it reads from the file.
- The verifier reads from the front and never goes back. It keeps a few running hashes and can check a log as it arrives, without holding it.
What no log can prove about itself
Now untick both boxes, so that the log is still open, and press "cut the log off after its first checkpoint". The verifier accepts what is left. It is right to: what is left is a correct log that happens to be shorter. Every signature in it points backwards, at bytes that existed when it was made. Nothing in a log can vouch for what was written after it.
- No log, in any format, can detect that its own end was cut off. This is not a gap in the recipe. A count of records inside a checkpoint does not help either: "a thousand so far" is still true of the shorter file.
- A seal helps only if someone insists on it. An attacker cuts off the seal along with the tail, and the file looks like a log that is still open. Tick "the log was sealed", then "cut off only the seal": accepted. Now tick "the verifier was told this log is finished" as well: refused. The seal forges nothing and proves nothing by being there. It lets a verifier who knows the log is complete demand it. That knowledge comes from outside: a file that was rotated out, an archive of last year, a later file that continues this one.
- The newest records have no signature yet. Events after the last checkpoint can be changed or dropped freely, until the next checkpoint is written. A careful reader shows them as unconfirmed. A careful writer adds a checkpoint every few seconds as well as every few events.
- For more, something outside the log must remember. Publish the latest signature somewhere the attacker cannot reach: another machine, a newspaper, a public ledger. This is why the public logs that browsers rely on for certificates publish signed summaries and have others countersign them.
It also does not help against the one who holds the key. A log signed by its writer shows that nobody else changed it. And nothing here hides anything: the events are as readable as before.
What it costs
- Bytes. Choose a checkpoint in the first panel: it is about ninety bytes, most of them its own signature and the previous one, 32 bytes each. One for every three events, as in the panel, is far too many for real use. One for every few hundred makes it a rounding error.
- Hashing. Every byte is hashed once for each stretch that covers it, and once more for the seal. With "the last 2 periods" that is three times. Hashing is fast, and it is still the main cost of writing such a log.
- One writer. The hashes depend on the exact order of the bytes, so the records have to pass through one place that sees them all.
- Care. The details of what exactly is signed are where such schemes go wrong, quietly. Dagr's specification spends most of its length on them, and there is a complete example, an audit log, in six languages that all write the same bytes.
Next to the usual ways
- A hash chain. Each record contains the hash of the one before it. Git's history works like this, and so do blockchains. Changing anything changes every hash after it. It needs a signature somewhere to mean anything, and it puts a hash in every record. Checkpoints are the same idea with the hashes gathered into occasional records, so that the events themselves stay as they were.
- A tree of hashes. The public certificate logs arrange their hashes as a tree. That gives something this recipe does not have: a short proof that one particular record is in the log, without the rest of it. It is the right tool when many strangers each care about one entry.
- Signing the file when it is closed. Simple, and enough for archives. It says nothing while the log is being written.
What these panels are.
- The bytes and the signatures are real. The schema is that of the audit-log example in Dagr's repository, and the checkpoints and seals follow its recipe exactly. The page's writer is checked against that example's reference implementation: the streams are identical. Its verifier is checked too, against the reference verifier, on logs with records deleted, swapped, repeated, cut off at every byte, and with single bytes changed.
- The signatures are HMAC-SHA256, as in chapter 11, with a key that is in the page and therefore no secret. A real log would more likely use a signature that can be checked without the key that made it.
- The settings are chosen to be seen: a checkpoint every three events is for the picture.
Try it
- In the first panel, set "each covering the last 1 periods". Choose the second checkpoint. Which records does it vouch for, and what now connects it to the first?
- Add events one at a time and watch for the moment a checkpoint appears. How many events can be unconfirmed at most?
- In the second panel, change a byte of event 2. Which checkpoint notices, and what would have happened had the log ended before that checkpoint was written?
- Delete event 4. The verifier complains about a count and never gets to a signature. Why is it good that the cheap check comes first?
- With the log sealed and the verifier told nothing, find every attack that still succeeds. What do they have in common?
- You archive last month's log. Which box describes the verifier you should use for it, and how does that verifier know?
What comes next
Chapter 14 goes back to a single value and asks where exactly its bytes sit in memory. Some code, the kind that processes many numbers at once on a processor or a graphics card, insists on data at particular addresses. It is about alignment.
