Skip to content

Commit 103d848

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 658a571 commit 103d848

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
@@ -544,6 +552,7 @@ mod test {
544552
};
545553

546554
let task_id = compute_task_id(
555+
version,
547556
&taskprov_advertisemnt
548557
.get_encoded_with_param(&version)
549558
.unwrap(),
@@ -603,6 +612,75 @@ mod test {
603612

604613
test_versions! { check_vdaf_key_computation }
605614

615+
#[test]
616+
fn check_task_id_draft09() {
617+
let taskprov_advertisemnt_bytes = messages::taskprov::TaskprovAdvertisement {
618+
task_info: "cool task".as_bytes().to_vec(),
619+
leader_url: messages::taskprov::UrlBytes {
620+
bytes: b"https://leader.com/".to_vec(),
621+
},
622+
helper_url: messages::taskprov::UrlBytes {
623+
bytes: b"http://helper.org:8788/".to_vec(),
624+
},
625+
time_precision: 3600,
626+
min_batch_size: 1,
627+
query_config: messages::taskprov::QueryConfig::LeaderSelected {
628+
draft09_max_batch_size: Some(NonZeroU32::new(2).unwrap()),
629+
},
630+
lifetime: messages::taskprov::TaskLifetime::Draft09 { expiration: 23 },
631+
vdaf_config: messages::taskprov::VdafConfig::Prio2 { dimension: 10 },
632+
extensions: Vec::new(),
633+
draft09_max_batch_query_count: Some(23),
634+
draft09_dp_config: Some(messages::taskprov::DpConfig::None),
635+
}
636+
.get_encoded_with_param(&DapVersion::Draft09)
637+
.unwrap();
638+
639+
let expected_task_id = TaskId([
640+
142, 26, 248, 229, 126, 249, 222, 59, 10, 221, 34, 151, 27, 60, 28, 0, 134, 194, 142,
641+
84, 167, 128, 139, 140, 98, 35, 119, 117, 109, 108, 125, 211,
642+
]);
643+
let task_id = compute_task_id(DapVersion::Latest, &taskprov_advertisemnt_bytes);
644+
println!("{:?}", task_id.0);
645+
assert_eq!(task_id, expected_task_id);
646+
}
647+
648+
#[test]
649+
fn check_task_id() {
650+
let taskprov_advertisemnt_bytes = messages::taskprov::TaskprovAdvertisement {
651+
task_info: "cool task".as_bytes().to_vec(),
652+
leader_url: messages::taskprov::UrlBytes {
653+
bytes: b"https://leader.com/".to_vec(),
654+
},
655+
helper_url: messages::taskprov::UrlBytes {
656+
bytes: b"http://helper.org:8788/".to_vec(),
657+
},
658+
time_precision: 3600,
659+
min_batch_size: 1,
660+
query_config: messages::taskprov::QueryConfig::LeaderSelected {
661+
draft09_max_batch_size: None,
662+
},
663+
lifetime: messages::taskprov::TaskLifetime::Latest {
664+
start: 23,
665+
duration: 23,
666+
},
667+
vdaf_config: messages::taskprov::VdafConfig::Prio2 { dimension: 10 },
668+
extensions: Vec::new(),
669+
draft09_max_batch_query_count: None,
670+
draft09_dp_config: None,
671+
}
672+
.get_encoded_with_param(&DapVersion::Latest)
673+
.unwrap();
674+
675+
let expected_task_id = TaskId([
676+
29, 66, 37, 142, 99, 73, 46, 14, 193, 147, 230, 204, 154, 75, 129, 177, 55, 2, 228, 62,
677+
227, 204, 248, 200, 120, 251, 5, 161, 203, 149, 72, 55,
678+
]);
679+
let task_id = compute_task_id(DapVersion::Latest, &taskprov_advertisemnt_bytes);
680+
println!("{:?}", task_id.0);
681+
assert_eq!(task_id, expected_task_id);
682+
}
683+
606684
fn resolve_advertised_task_config_expect_abort_unrecognized_vdaf(version: DapVersion) {
607685
// Create a request for a taskprov task with an unrecognized VDAF.
608686
let (req, task_id) = {
@@ -640,6 +718,7 @@ mod test {
640718
};
641719
let task_id = {
642720
compute_task_id(
721+
version,
643722
&taskprov_advertisement
644723
.get_encoded_with_param(&version)
645724
.unwrap(),
@@ -712,6 +791,7 @@ mod test {
712791
};
713792
let task_id = {
714793
compute_task_id(
794+
version,
715795
&taskprov_advertisement
716796
.get_encoded_with_param(&version)
717797
.unwrap(),

0 commit comments

Comments
 (0)