1use ed25519_dalek::{Signature, VerifyingKey};
30
31use crate::checkpoint::{Checkpoint, MAX_NOTE_BYTES, SignedNote, valid_name};
32use crate::{Hash, LogError};
33
34pub const C2SP_ED25519_ALGORITHM: u8 = 0x01;
38
39pub const MAX_WITNESS_PROOF_LINES: usize = 63;
41
42const COSIGNATURE_LABEL: &[u8] = b"cosignature/v1";
44pub fn empty_tree_root() -> Hash {
46 use sha2::Digest;
47 let digest = sha2::Sha256::digest([]);
48 let mut root = [0_u8; 32];
49 root.copy_from_slice(&digest);
50 root
51}
52
53pub fn c2sp_key_id(name: &str, ed25519_public: &[u8; 32]) -> [u8; 4] {
59 use sha2::Digest;
60 let mut hasher = sha2::Sha256::new();
61 hasher.update(name.as_bytes());
62 hasher.update([0x0a, C2SP_ED25519_ALGORITHM]);
63 hasher.update(ed25519_public);
64 let digest = hasher.finalize();
65 [digest[0], digest[1], digest[2], digest[3]]
66}
67
68pub fn cosignature_input(timestamp: u64, checkpoint_text: &str) -> Vec<u8> {
75 let mut out = Vec::with_capacity(32 + checkpoint_text.len());
76 out.extend_from_slice(COSIGNATURE_LABEL);
77 out.push(b'\n');
78 out.extend_from_slice(format!("time {timestamp}\n").as_bytes());
79 out.extend_from_slice(checkpoint_text.as_bytes());
80 out
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct Witness {
86 pub name: String,
87 pub ed25519_public: [u8; 32],
88}
89
90impl Witness {
91 pub fn new(name: impl Into<String>, ed25519_public: [u8; 32]) -> Result<Self, LogError> {
94 let name = name.into();
95 if !valid_name(&name) {
96 return Err(LogError::Malformed);
97 }
98 Ok(Self {
99 name,
100 ed25519_public,
101 })
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub struct Cosignature {
109 pub timestamp: u64,
110}
111
112pub fn verify_ed25519_cosignature(
123 note: &[u8],
124 origin: &str,
125 log: &hide_sign::VerifyingIdentity,
126 witness: &Witness,
127 timestamp: u64,
128) -> Result<(Checkpoint, Cosignature), LogError> {
129 let checkpoint = Checkpoint::verify(note, origin, log)?;
132
133 if note.len() > MAX_NOTE_BYTES {
134 return Err(LogError::Malformed);
135 }
136 let parsed = SignedNote::decode(note)?;
137 let id = c2sp_key_id(&witness.name, &witness.ed25519_public);
138 let line = parsed
139 .signatures
140 .iter()
141 .find(|line| line.name == witness.name && line.key_id == id)
142 .ok_or(LogError::Unsigned)?;
143
144 let key =
145 VerifyingKey::from_bytes(&witness.ed25519_public).map_err(|_| LogError::BadSignature)?;
146 let signature = Signature::from_slice(&line.signature).map_err(|_| LogError::BadSignature)?;
147 let message = cosignature_input(timestamp, &parsed.text);
148 key.verify_strict(&message, &signature)
151 .map_err(|_| LogError::BadSignature)?;
152
153 Ok((checkpoint, Cosignature { timestamp }))
154}
155
156pub fn add_checkpoint_body(
163 old_size: u64,
164 proof: &[Hash],
165 note: &[u8],
166) -> Result<Vec<u8>, LogError> {
167 use base64::Engine;
168 if proof.len() > MAX_WITNESS_PROOF_LINES {
169 return Err(LogError::ProofTooLong(proof.len()));
170 }
171 if old_size == 0 && !proof.is_empty() {
172 return Err(LogError::Malformed);
173 }
174 if note.len() > MAX_NOTE_BYTES {
175 return Err(LogError::Malformed);
176 }
177 let mut out = Vec::with_capacity(32 + 45 * proof.len() + note.len());
178 out.extend_from_slice(format!("old {old_size}\n").as_bytes());
179 for hash in proof {
180 out.extend_from_slice(
181 base64::engine::general_purpose::STANDARD
182 .encode(hash)
183 .as_bytes(),
184 );
185 out.push(b'\n');
186 }
187 out.push(b'\n');
188 out.extend_from_slice(note);
189 Ok(out)
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
196pub enum AddCheckpointOutcome {
197 Cosigned(Vec<u8>),
199 UnknownOrigin,
201 NotSigned,
203 BadOldSize,
205 Conflict { newest_size: u64 },
208 ProofRejected,
211}
212
213pub fn classify_add_checkpoint(status: u16, body: &[u8]) -> Result<AddCheckpointOutcome, LogError> {
216 Ok(match status {
217 200 => AddCheckpointOutcome::Cosigned(body.to_vec()),
218 403 => AddCheckpointOutcome::NotSigned,
219 404 => AddCheckpointOutcome::UnknownOrigin,
220 400 => AddCheckpointOutcome::BadOldSize,
221 409 => {
222 let text = std::str::from_utf8(body).map_err(|_| LogError::Malformed)?;
223 let line = text.strip_suffix('\n').ok_or(LogError::Malformed)?;
224 if line.is_empty() || !line.bytes().all(|b| b.is_ascii_digit()) {
227 return Err(LogError::Malformed);
228 }
229 if line.len() > 1 && line.starts_with('0') {
230 return Err(LogError::Malformed);
231 }
232 let newest_size: u64 = line.parse().map_err(|_| LogError::Malformed)?;
233 AddCheckpointOutcome::Conflict { newest_size }
234 }
235 422 => AddCheckpointOutcome::ProofRejected,
236 _ => return Err(LogError::Malformed),
237 })
238}
239
240pub fn is_empty_tree_checkpoint(checkpoint: &Checkpoint) -> bool {
244 checkpoint.size == 0 && checkpoint.root == empty_tree_root()
245}
246
247#[cfg(test)]
248mod tests {
249 use super::*;
250 use ed25519_dalek::{Signer, SigningKey};
251
252 fn checkpoint(size: u64) -> Checkpoint {
253 Checkpoint {
254 origin: "hide.example/log".into(),
255 size,
256 root: [7_u8; 32],
257 }
258 }
259
260 fn witness_key() -> SigningKey {
261 SigningKey::from_bytes(&[0x51; 32])
262 }
263
264 fn cosigned_note(timestamp: u64) -> (Vec<u8>, Witness) {
265 let log = hide_sign::SigningIdentity::from_bytes(&[0x42; 32]).unwrap();
266 let text = checkpoint(9).text().unwrap();
267 let mut note = checkpoint(9).sign(&log).unwrap();
268 let witness =
269 Witness::new("witness.example", witness_key().verifying_key().to_bytes()).unwrap();
270 let signature = witness_key().sign(&cosignature_input(timestamp, &text));
271 let mut raw = Vec::new();
272 raw.extend_from_slice(&c2sp_key_id(&witness.name, &witness.ed25519_public));
273 raw.extend_from_slice(&signature.to_bytes());
274 note.signatures.push(crate::checkpoint::NoteSignature {
275 name: witness.name.clone(),
276 key_id: c2sp_key_id(&witness.name, &witness.ed25519_public),
277 signature: raw[4..].to_vec(),
278 });
279 (note.encode().into_bytes(), witness)
280 }
281
282 #[test]
285 fn a_c2sp_ed25519_cosignature_verifies_over_a_hide_checkpoint() {
286 let log = hide_sign::SigningIdentity::from_bytes(&[0x42; 32]).unwrap();
287 let (note, witness) = cosigned_note(1_700_000_000);
288 let (checkpoint, cosignature) = verify_ed25519_cosignature(
289 ¬e,
290 "hide.example/log",
291 &log.verifying_key(),
292 &witness,
293 1_700_000_000,
294 )
295 .expect("a genuine cosignature verifies");
296 assert_eq!(checkpoint.size, 9);
297 assert_eq!(cosignature.timestamp, 1_700_000_000);
298 }
299
300 #[test]
302 fn a_cosignature_is_bound_to_its_timestamp() {
303 let log = hide_sign::SigningIdentity::from_bytes(&[0x42; 32]).unwrap();
304 let (note, witness) = cosigned_note(1_700_000_000);
305 assert_eq!(
306 verify_ed25519_cosignature(
307 ¬e,
308 "hide.example/log",
309 &log.verifying_key(),
310 &witness,
311 1_700_000_001
312 ),
313 Err(LogError::BadSignature)
314 );
315 }
316
317 #[test]
319 fn a_cosignature_over_an_unsigned_checkpoint_is_refused() {
320 let other = hide_sign::SigningIdentity::from_bytes(&[0x43; 32]).unwrap();
321 let (note, witness) = cosigned_note(1_700_000_000);
322 assert_eq!(
326 verify_ed25519_cosignature(
327 ¬e,
328 "hide.example/log",
329 &other.verifying_key(),
330 &witness,
331 1_700_000_000
332 ),
333 Err(LogError::Unsigned)
334 );
335 }
336
337 #[test]
340 fn a_cosignature_from_another_witness_is_refused() {
341 let log = hide_sign::SigningIdentity::from_bytes(&[0x42; 32]).unwrap();
342 let (note, _) = cosigned_note(1_700_000_000);
343 let other = SigningKey::from_bytes(&[0x52; 32]);
344 let stranger = Witness::new("witness.example", other.verifying_key().to_bytes()).unwrap();
345 assert_eq!(
346 verify_ed25519_cosignature(
347 ¬e,
348 "hide.example/log",
349 &log.verifying_key(),
350 &stranger,
351 1_700_000_000
352 ),
353 Err(LogError::Unsigned)
354 );
355 }
356
357 #[test]
360 fn the_request_body_has_the_specified_framing() {
361 let note = b"origin\n1\nAAAA\n\xe2\x80\x94 sig AAAA\n";
362 let proof = [[1_u8; 32], [2_u8; 32]];
363 let body = add_checkpoint_body(5, &proof, note).unwrap();
364 let text = String::from_utf8(body.clone()).unwrap();
365 let mut lines = text.split_inclusive('\n');
366 assert_eq!(lines.next().unwrap(), "old 5\n");
367 assert_eq!(
368 lines.next().unwrap(),
369 "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=\n"
370 );
371 assert_eq!(
372 lines.next().unwrap(),
373 "AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI=\n"
374 );
375 assert_eq!(lines.next().unwrap(), "\n");
376 assert_eq!(text.split("\n\n").nth(1).unwrap().as_bytes(), note);
377 }
378
379 #[test]
382 fn a_non_empty_proof_at_size_zero_is_refused() {
383 let note = b"origin\n0\nAAAA\n";
384 assert_eq!(
385 add_checkpoint_body(0, &[[1_u8; 32]], note),
386 Err(LogError::Malformed)
387 );
388 assert!(add_checkpoint_body(0, &[], note).is_ok());
389 }
390
391 #[test]
394 fn too_many_proof_lines_are_refused() {
395 let note = b"origin\n1\nAAAA\n";
396 let proof = vec![[1_u8; 32]; MAX_WITNESS_PROOF_LINES + 1];
397 assert!(matches!(
398 add_checkpoint_body(1, &proof, note),
399 Err(LogError::ProofTooLong(_))
400 ));
401 assert!(add_checkpoint_body(1, &vec![[1_u8; 32]; MAX_WITNESS_PROOF_LINES], note).is_ok());
402 }
403
404 #[test]
405 fn every_named_status_is_classified() {
406 assert_eq!(
407 classify_add_checkpoint(200, b"\xe2\x80\x94 w AAAA\n").unwrap(),
408 AddCheckpointOutcome::Cosigned(b"\xe2\x80\x94 w AAAA\n".to_vec())
409 );
410 assert_eq!(
411 classify_add_checkpoint(403, b"").unwrap(),
412 AddCheckpointOutcome::NotSigned
413 );
414 assert_eq!(
415 classify_add_checkpoint(404, b"").unwrap(),
416 AddCheckpointOutcome::UnknownOrigin
417 );
418 assert_eq!(
419 classify_add_checkpoint(400, b"").unwrap(),
420 AddCheckpointOutcome::BadOldSize
421 );
422 assert_eq!(
423 classify_add_checkpoint(409, b"20852014\n").unwrap(),
424 AddCheckpointOutcome::Conflict {
425 newest_size: 20_852_014
426 }
427 );
428 assert_eq!(
429 classify_add_checkpoint(422, b"").unwrap(),
430 AddCheckpointOutcome::ProofRejected
431 );
432 }
433
434 #[test]
437 fn a_malformed_conflict_body_is_refused() {
438 for body in [
439 &b"05\n"[..],
440 &b"+5\n"[..],
441 &b"5"[..],
442 &b"\n"[..],
443 &b"5 5\n"[..],
444 &b"18446744073709551616\n"[..],
445 ] {
446 assert_eq!(
447 classify_add_checkpoint(409, body),
448 Err(LogError::Malformed),
449 "accepted {body:?}"
450 );
451 }
452 }
453
454 #[test]
456 fn an_unnamed_status_is_refused() {
457 for status in [201, 301, 500, 0] {
458 assert_eq!(
459 classify_add_checkpoint(status, b""),
460 Err(LogError::Malformed)
461 );
462 }
463 }
464
465 #[test]
467 fn the_cosignature_input_cannot_be_shifted() {
468 assert_ne!(cosignature_input(1, "a\n"), cosignature_input(11, "\n"));
469 assert_eq!(
470 cosignature_input(1, "origin\n1\nAAAA\n"),
471 b"cosignature/v1\ntime 1\norigin\n1\nAAAA\n".to_vec()
472 );
473 }
474
475 #[test]
478 fn an_unusable_witness_name_is_refused() {
479 assert!(Witness::new("", [0_u8; 32]).is_err());
480 assert!(Witness::new("has space", [0_u8; 32]).is_err());
481 assert!(Witness::new("has+plus", [0_u8; 32]).is_err());
482 assert!(Witness::new("witness.example", [0_u8; 32]).is_ok());
483 }
484
485 #[test]
488 fn the_empty_root_matches_rfc_6962() {
489 use base64::Engine;
490 assert_eq!(
491 base64::engine::general_purpose::STANDARD.encode(empty_tree_root()),
492 "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="
493 );
494 assert!(is_empty_tree_checkpoint(&Checkpoint {
495 origin: "o".into(),
496 size: 0,
497 root: empty_tree_root(),
498 }));
499 assert!(!is_empty_tree_checkpoint(&Checkpoint {
500 origin: "o".into(),
501 size: 0,
502 root: [1_u8; 32],
503 }));
504 assert!(!is_empty_tree_checkpoint(&checkpoint(1)));
505 }
506
507 #[test]
510 fn the_two_key_id_derivations_differ() {
511 let log = hide_sign::SigningIdentity::from_bytes(&[0x42; 32]).unwrap();
512 let ed = log.verifying_key().ed25519_bytes();
513 let name = "witness.example";
514 assert_ne!(
515 c2sp_key_id(name, &ed),
516 crate::key_id(name, &log.verifying_key())
517 );
518 }
519}