Skip to content

Commit ad4efa6

Browse files
committed
taskprov: Align task ID computation with latest draft
draft-ietf-ppm-dap-taskprov-01 adds a salt to the input of the hash.
1 parent 4cbbdb1 commit ad4efa6

File tree

2 files changed

+92
-12
lines changed

2 files changed

+92
-12
lines changed

crates/daphne/src/messages/taskprov.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl TaskprovAdvertisement {
556556
)
557557
})?;
558558

559-
if compute_task_id(taskprov_data.as_ref()) != *task_id {
559+
if compute_task_id(version, taskprov_data.as_ref()) != *task_id {
560560
// Return unrecognizedTask following section 5.1 of the taskprov draft.
561561
return Err(DapAbort::UnrecognizedTask { task_id: *task_id });
562562
}
@@ -578,7 +578,7 @@ impl TaskprovAdvertisement {
578578

579579
#[cfg(any(test, feature = "test-utils"))]
580580
pub fn compute_task_id(&self, version: DapVersion) -> TaskId {
581-
compute_task_id(&self.get_encoded_with_param(&version).unwrap())
581+
compute_task_id(version, &self.get_encoded_with_param(&version).unwrap())
582582
}
583583
}
584584

crates/daphne/src/taskprov.rs

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,27 @@ use ring::{
3030
use serde::{Deserialize, Serialize};
3131
use url::Url;
3232

33-
/// SHA-256 of "dap-taskprov"
34-
pub(crate) const TASKPROV_SALT: [u8; 32] = [
35-
0x28, 0xb9, 0xbb, 0x4f, 0x62, 0x4f, 0x67, 0x9a, 0xc1, 0x98, 0xd9, 0x68, 0xf4, 0xb0, 0x9e, 0xec,
36-
0x74, 0x01, 0x7a, 0x52, 0xcb, 0x4c, 0xf6, 0x39, 0xfb, 0x83, 0xe0, 0x47, 0x72, 0x3a, 0x0f, 0xfe,
33+
/// SHA-256 of `b"dap-taskprov"`.
34+
const TASKPROV_SALT: [u8; 32] = [
35+
40, 185, 187, 79, 98, 79, 103, 154, 193, 152, 217, 104, 244, 176, 158, 236, 116, 1, 122, 82,
36+
203, 76, 246, 57, 251, 131, 224, 71, 114, 58, 15, 254,
37+
];
38+
39+
/// SHA-256 of `b"dap-takprov task id"`.
40+
const TASKPROV_TASK_ID_SALT: [u8; 32] = [
41+
70, 13, 237, 116, 40, 100, 135, 190, 152, 104, 104, 209, 157, 184, 219, 27, 5, 132, 88, 56,
42+
228, 214, 41, 30, 241, 91, 110, 32, 82, 11, 220, 130,
3743
];
3844

3945
/// Compute the task id of a serialized task config.
40-
pub(crate) fn compute_task_id(serialized: &[u8]) -> TaskId {
41-
let d = digest::digest(&digest::SHA256, serialized);
42-
let dref = d.as_ref();
43-
let mut b: [u8; 32] = [0; 32];
44-
b[..32].copy_from_slice(&dref[..32]);
45-
TaskId(b)
46+
pub(crate) fn compute_task_id(version: DapVersion, taskprov_advertisemnt_bytes: &[u8]) -> TaskId {
47+
let mut hash = ring::digest::Context::new(&digest::SHA256);
48+
if version == DapVersion::Latest {
49+
hash.update(&TASKPROV_TASK_ID_SALT);
50+
}
51+
hash.update(taskprov_advertisemnt_bytes);
52+
let digest = hash.finish();
53+
TaskId(digest.as_ref().try_into().unwrap())
4654
}
4755

4856
// The documentation for ring::hkdf says computing the Salt is expensive, and we use the same PRK all the
@@ -537,6 +545,7 @@ mod test {
537545
};
538546

539547
let task_id = compute_task_id(
548+
version,
540549
&taskprov_advertisemnt
541550
.get_encoded_with_param(&version)
542551
.unwrap(),
@@ -589,6 +598,75 @@ mod test {
589598

590599
test_versions! { check_vdaf_key_computation }
591600

601+
#[test]
602+
fn check_task_id_draft09() {
603+
let taskprov_advertisemnt_bytes = messages::taskprov::TaskprovAdvertisement {
604+
task_info: "cool task".as_bytes().to_vec(),
605+
leader_url: messages::taskprov::UrlBytes {
606+
bytes: b"https://leader.com/".to_vec(),
607+
},
608+
helper_url: messages::taskprov::UrlBytes {
609+
bytes: b"http://helper.org:8788/".to_vec(),
610+
},
611+
time_precision: 3600,
612+
min_batch_size: 1,
613+
query_config: messages::taskprov::QueryConfig::LeaderSelected {
614+
draft09_max_batch_size: Some(NonZeroU32::new(2).unwrap()),
615+
},
616+
lifetime: messages::taskprov::TaskLifetime::Draft09 { expiration: 23 },
617+
vdaf_config: messages::taskprov::VdafConfig::Prio2 { dimension: 10 },
618+
extensions: Vec::new(),
619+
draft09_max_batch_query_count: Some(23),
620+
draft09_dp_config: Some(messages::taskprov::DpConfig::None),
621+
}
622+
.get_encoded_with_param(&DapVersion::Draft09)
623+
.unwrap();
624+
625+
let expected_task_id = TaskId([
626+
142, 26, 248, 229, 126, 249, 222, 59, 10, 221, 34, 151, 27, 60, 28, 0, 134, 194, 142,
627+
84, 167, 128, 139, 140, 98, 35, 119, 117, 109, 108, 125, 211,
628+
]);
629+
let task_id = compute_task_id(DapVersion::Latest, &taskprov_advertisemnt_bytes);
630+
println!("{:?}", task_id.0);
631+
assert_eq!(task_id, expected_task_id);
632+
}
633+
634+
#[test]
635+
fn check_task_id() {
636+
let taskprov_advertisemnt_bytes = messages::taskprov::TaskprovAdvertisement {
637+
task_info: "cool task".as_bytes().to_vec(),
638+
leader_url: messages::taskprov::UrlBytes {
639+
bytes: b"https://leader.com/".to_vec(),
640+
},
641+
helper_url: messages::taskprov::UrlBytes {
642+
bytes: b"http://helper.org:8788/".to_vec(),
643+
},
644+
time_precision: 3600,
645+
min_batch_size: 1,
646+
query_config: messages::taskprov::QueryConfig::LeaderSelected {
647+
draft09_max_batch_size: None,
648+
},
649+
lifetime: messages::taskprov::TaskLifetime::Latest {
650+
start: 23,
651+
duration: 23,
652+
},
653+
vdaf_config: messages::taskprov::VdafConfig::Prio2 { dimension: 10 },
654+
extensions: Vec::new(),
655+
draft09_max_batch_query_count: None,
656+
draft09_dp_config: None,
657+
}
658+
.get_encoded_with_param(&DapVersion::Latest)
659+
.unwrap();
660+
661+
let expected_task_id = TaskId([
662+
29, 66, 37, 142, 99, 73, 46, 14, 193, 147, 230, 204, 154, 75, 129, 177, 55, 2, 228, 62,
663+
227, 204, 248, 200, 120, 251, 5, 161, 203, 149, 72, 55,
664+
]);
665+
let task_id = compute_task_id(DapVersion::Latest, &taskprov_advertisemnt_bytes);
666+
println!("{:?}", task_id.0);
667+
assert_eq!(task_id, expected_task_id);
668+
}
669+
592670
fn resolve_advertised_task_config_expect_abort_unrecognized_vdaf(version: DapVersion) {
593671
// Create a request for a taskprov task with an unrecognized VDAF.
594672
let (req, task_id) = {
@@ -626,6 +704,7 @@ mod test {
626704
};
627705
let task_id = {
628706
compute_task_id(
707+
version,
629708
&taskprov_advertisement
630709
.get_encoded_with_param(&version)
631710
.unwrap(),
@@ -698,6 +777,7 @@ mod test {
698777
};
699778
let task_id = {
700779
compute_task_id(
780+
version,
701781
&taskprov_advertisement
702782
.get_encoded_with_param(&version)
703783
.unwrap(),

0 commit comments

Comments
 (0)