Skip to content

Commit dd223be

Browse files
committed
Refactor method-specific identity/state
- Made static keys I and R method dependednt (not needed ofr PSK) - Added high level identity enums - Made i_verify_message_2 take optional I - Added ProcessedM2MethodSpecifics and ParsedMessage2Detials - Updated i_parse_message_2 and high level initiaotr API - Adapted lakers-python and lakers-c
1 parent d767859 commit dd223be

File tree

12 files changed

+209
-52
lines changed

12 files changed

+209
-52
lines changed

examples/coap/src/bin/coapclient.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ fn client_handshake() -> Result<(), EDHOCError> {
5454
println!("message_2 len = {}", response.message.payload.len());
5555

5656
let message_2 = EdhocBuffer::new_from_slice(&response.message.payload[..]).unwrap();
57-
let (mut initiator, c_r, id_cred_r, ead_2) = initiator.parse_message_2(&message_2)?;
57+
let (mut initiator, c_r, details) = initiator.parse_message_2(&message_2)?;
58+
let ParsedMessage2Details::StatStat { id_cred_r, ead_2 } = details;
5859
ead_2.processed_critical_items().unwrap();
5960
let valid_cred_r = credential_check_or_fetch(Some(cred_r), id_cred_r).unwrap();
6061
initiator.set_identity(I.try_into().unwrap(), cred_i)?;

examples/coap/src/bin/coapserver-coaphandler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ impl coap_handler::Handler for EdhocHandler {
116116

117117
let (responder, _c_i, mut ead_1) = EdhocResponder::new(
118118
lakers_crypto::default_crypto(),
119-
R.try_into().expect("Wrong length of responder private key"),
119+
ResponderIdentity::StatStat {
120+
r: R.try_into().expect("Wrong length of responder private key"),
121+
},
120122
cred_r,
121123
)
122124
.process_message_1(message_1)

examples/coap/src/bin/coapserver.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ fn main() {
4747
let cred_r: Credential = Credential::parse_ccs(CRED_R.try_into().unwrap()).unwrap();
4848
let responder = EdhocResponder::new(
4949
lakers_crypto::default_crypto(),
50-
R.try_into().unwrap(),
50+
ResponderIdentity::StatStat {
51+
r: R.try_into().unwrap(),
52+
},
5153
cred_r,
5254
);
5355

examples/lakers-no_std/src/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ fn main() -> ! {
108108
);
109109
let responder = EdhocResponder::new(
110110
lakers_crypto::default_crypto(),
111-
R.try_into().expect("Wrong length of responder private key"),
111+
ResponderIdentity::StatStat {
112+
r: R.try_into().expect("Wrong length of responder private key"),
113+
},
112114
cred_r.clone(),
113115
);
114116

@@ -119,8 +121,14 @@ fn main() -> ! {
119121
.prepare_message_2(CredentialTransfer::ByReference, None, &EadItems::new())
120122
.unwrap();
121123

122-
let (mut initiator, _c_r, id_cred_r, _ead_2) =
123-
initiator.parse_message_2(&message_2).unwrap();
124+
let (mut initiator, _c_r, details) = initiator.parse_message_2(&message_2).unwrap();
125+
let ParsedMessage2Details::StatStat {
126+
id_cred_r,
127+
ead_2: _ead_2,
128+
} = details
129+
else {
130+
panic!("Expected stat-stat details");
131+
};
124132
let valid_cred_r = credential_check_or_fetch(Some(cred_r), id_cred_r).unwrap();
125133
initiator
126134
.set_identity(

lakers-c/src/initiator.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct EdhocInitiator {
1414
pub start: InitiatorStart,
1515
pub wait_m2: WaitM2,
1616
pub processing_m2: ProcessingM2C,
17-
pub processed_m2: ProcessedM2,
17+
pub processed_m2: ProcessedM2C,
1818
pub wait_m4: WaitM4,
1919
pub cred_i: *mut CredentialC,
2020
pub completed: Completed,
@@ -106,11 +106,14 @@ pub unsafe extern "C" fn initiator_parse_message_2(
106106
let state = core::ptr::read(&(*initiator_c).wait_m2);
107107

108108
let result = match i_parse_message_2(&state, crypto, &(*message_2)) {
109-
Ok((state, c_r, id_cred_r, ead_2)) => {
109+
Ok((state, c_r, details)) => {
110110
ProcessingM2C::copy_into_c(state, &mut (*initiator_c).processing_m2);
111111
let c_r = c_r.as_slice();
112112
assert_eq!(c_r.len(), 1, "C API only supports short C_R");
113113
*c_r_out = c_r[0];
114+
let (id_cred_r, ead_2) = match details {
115+
ParsedMessage2Details::StatStat { id_cred_r, ead_2 } => (id_cred_r, ead_2),
116+
};
114117
*id_cred_r_out = id_cred_r;
115118

116119
EadItemsC::copy_into_c(ead_2, ead_2_c_out);
@@ -140,9 +143,9 @@ pub unsafe extern "C" fn initiator_verify_message_2(
140143

141144
let state = core::ptr::read(&(*initiator_c).processing_m2).to_rust();
142145

143-
match i_verify_message_2(&state, crypto, (*valid_cred_r).to_rust(), &(*i)) {
146+
match i_verify_message_2(&state, crypto, (*valid_cred_r).to_rust(), Some(&(*i))) {
144147
Ok(state) => {
145-
(*initiator_c).processed_m2 = state;
148+
ProcessedM2C::copy_into_c(state, &mut (*initiator_c).processed_m2);
146149
(*initiator_c).cred_i = cred_i;
147150
0
148151
}
@@ -165,7 +168,7 @@ pub unsafe extern "C" fn initiator_prepare_message_3(
165168
}
166169
let crypto = &mut default_crypto();
167170

168-
let state = core::ptr::read(&(*initiator_c).processed_m2);
171+
let state = core::ptr::read(&(*initiator_c).processed_m2).to_rust();
169172

170173
let ead_3 = if ead_3_c.is_null() {
171174
EadItems::new()

lakers-c/src/lib.rs

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ pub struct ProcessingM2MethodSpecificsC {
111111
#[repr(C)]
112112
pub struct ProcessingM2C {
113113
pub method_specifics: ProcessingM2MethodSpecificsC,
114-
pub method: EDHOCMethod,
115114
pub prk_2e: BytesHashLen,
116115
pub th_2: BytesHashLen,
117116
pub x: BytesP256ElemLen,
@@ -129,7 +128,6 @@ impl Default for ProcessingM2C {
129128
mac_2: Default::default(),
130129
id_cred_r: Default::default(),
131130
},
132-
method: EDHOCMethod::StatStat,
133131
prk_2e: Default::default(),
134132
th_2: Default::default(),
135133
x: Default::default(),
@@ -154,7 +152,6 @@ impl ProcessingM2C {
154152

155153
ProcessingM2 {
156154
method_specifics,
157-
method: self.method,
158155
prk_2e: self.prk_2e,
159156
th_2: self.th_2,
160157
x: self.x,
@@ -177,7 +174,6 @@ impl ProcessingM2C {
177174
(*processing_m2_c).x = processing_m2.x;
178175
(*processing_m2_c).g_y = processing_m2.g_y;
179176
(*processing_m2_c).plaintext_2 = processing_m2.plaintext_2;
180-
(*processing_m2_c).method = processing_m2.method;
181177
let c_r = processing_m2.c_r.as_slice();
182178
assert_eq!(c_r.len(), 1, "C API only supports short C_R");
183179
(*processing_m2_c).c_r = c_r[0];
@@ -224,6 +220,79 @@ impl CredentialC {
224220
}
225221
}
226222

223+
#[derive(Debug)]
224+
#[repr(C)]
225+
pub enum ProcessedM2MethodSpecificsKindC {
226+
Prm2StatStat,
227+
}
228+
229+
#[derive(Debug)]
230+
#[repr(C)]
231+
pub struct ProcessedM2MethodSpecificsC {
232+
pub kind: ProcessedM2MethodSpecificsKindC,
233+
}
234+
235+
#[derive(Debug)]
236+
#[repr(C)]
237+
pub struct ProcessedM2C {
238+
pub method: EDHOCMethod,
239+
pub method_specifics: ProcessedM2MethodSpecificsC,
240+
pub prk_3e2m: BytesHashLen,
241+
pub prk_4e3m: BytesHashLen,
242+
pub th_3: BytesHashLen,
243+
}
244+
245+
impl Default for ProcessedM2C {
246+
fn default() -> Self {
247+
Self {
248+
method: EDHOCMethod::StatStat,
249+
method_specifics: ProcessedM2MethodSpecificsC {
250+
kind: ProcessedM2MethodSpecificsKindC::Prm2StatStat,
251+
},
252+
prk_3e2m: Default::default(),
253+
prk_4e3m: Default::default(),
254+
th_3: Default::default(),
255+
}
256+
}
257+
}
258+
259+
impl ProcessedM2C {
260+
pub fn to_rust(&self) -> ProcessedM2 {
261+
let method_specifics = match self.method_specifics.kind {
262+
ProcessedM2MethodSpecificsKindC::Prm2StatStat => {
263+
ProcessedM2MethodSpecifics::StatStat {}
264+
}
265+
};
266+
267+
ProcessedM2 {
268+
method: self.method,
269+
method_specifics,
270+
prk_3e2m: self.prk_3e2m,
271+
prk_4e3m: self.prk_4e3m,
272+
th_3: self.th_3,
273+
}
274+
}
275+
276+
pub unsafe fn copy_into_c(processed_m2: ProcessedM2, processed_m2_c: *mut ProcessedM2C) {
277+
if processed_m2_c.is_null() {
278+
panic!("processed_m2_c is null");
279+
}
280+
281+
(*processed_m2_c).method = processed_m2.method;
282+
(*processed_m2_c).prk_3e2m = processed_m2.prk_3e2m;
283+
(*processed_m2_c).prk_4e3m = processed_m2.prk_4e3m;
284+
(*processed_m2_c).th_3 = processed_m2.th_3;
285+
286+
match processed_m2.method_specifics {
287+
ProcessedM2MethodSpecifics::StatStat {} => {
288+
(*processed_m2_c).method_specifics = ProcessedM2MethodSpecificsC {
289+
kind: ProcessedM2MethodSpecificsKindC::Prm2StatStat,
290+
};
291+
}
292+
}
293+
}
294+
}
295+
227296
#[no_mangle]
228297
pub unsafe extern "C" fn credential_new(
229298
cred: *mut CredentialC,

lakers-python/src/initiator.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ impl PyEdhocInitiator {
110110
let message_2 = EdhocMessageBuffer::new_from_slice(message_2.as_slice())
111111
.with_cause(py, "Message 2 too long")?;
112112

113-
let (state, c_r, id_cred_r, ead_2) =
113+
let (state, c_r, details) =
114114
i_parse_message_2(&self.take_wait_m2()?, &mut default_crypto(), &message_2)?;
115+
let (id_cred_r, ead_2) = match details {
116+
ParsedMessage2Details::StatStat { id_cred_r, ead_2 } => (id_cred_r, ead_2),
117+
// ParsedMessage2Details::Psk { ead_2 } => (IdCred::new(), ead_2),
118+
};
115119
self.processing_m2 = Some(state);
116120
Ok((
117121
PyBytes::new(py, c_r.as_slice()),
@@ -143,9 +147,11 @@ impl PyEdhocInitiator {
143147
&self.take_processing_m2()?,
144148
&mut default_crypto(),
145149
valid_cred_r,
146-
i.as_slice()
147-
.try_into()
148-
.expect("Wrong length of initiator private key"),
150+
Some(
151+
i.as_slice()
152+
.try_into()
153+
.expect("Wrong length of initiator private key"),
154+
),
149155
)?;
150156
self.processed_m2 = Some(state);
151157
self.cred_i = Some(cred_i);

lib/src/edhoc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ pub fn i_parse_message_2<'a>(
160160
state: &WaitM2,
161161
crypto: &mut impl CryptoTrait,
162162
message_2: &BufferMessage2,
163-
) -> Result<(ProcessingM2, ConnId, IdCred, EadItems), EDHOCError> {
163+
) -> Result<(ProcessingM2, ConnId, ParsedMessage2Details), EDHOCError> {
164164
match state.method {
165165
EDHOCMethod::StatStat => i_parse_message_2_statstat(state, crypto, message_2),
166-
// EDHOCMethod::PSK => r_parse_message_3_psk()
166+
// EDHOCMethod::PSK => i_parse_message_2_psk()
167167
_ => Err(EDHOCError::UnsupportedMethod),
168168
}
169169
}
@@ -173,10 +173,11 @@ pub fn i_verify_message_2(
173173
state: &ProcessingM2,
174174
crypto: &mut impl CryptoTrait,
175175
valid_cred_r: Credential,
176-
i: &BytesP256ElemLen, // I's static private DH key
176+
i: Option<&BytesP256ElemLen>, // I's static private DH key when required by method
177177
) -> Result<ProcessedM2, EDHOCError> {
178178
match state.method_specifics {
179179
ProcessingM2MethodSpecifics::StatStat { .. } => {
180+
let i = i.ok_or(EDHOCError::MissingIdentity)?;
180181
i_verify_message_2_statstat(state, crypto, valid_cred_r, i)
181182
} // ProcessingM2MethodSpecifics::Psk { .. } =>
182183
}

lib/src/edhoc/statstat.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ pub fn r_verify_message_3_statstat(
124124
&state.ead_3,
125125
);
126126

127-
let mac_3 = match state.method_specifics {
128-
ProcessingM3MethodSpecifics::StatStat { mac_3, .. } => mac_3,
127+
let mac_3 = match &state.method_specifics {
128+
ProcessingM3MethodSpecifics::StatStat { mac_3, .. } => *mac_3,
129129
};
130130

131131
// verify mac_3
@@ -165,7 +165,7 @@ pub fn i_parse_message_2_statstat<'a>(
165165
state: &WaitM2,
166166
crypto: &mut impl CryptoTrait,
167167
message_2: &BufferMessage2,
168-
) -> Result<(ProcessingM2, ConnId, IdCred, EadItems), EDHOCError> {
168+
) -> Result<(ProcessingM2, ConnId, ParsedMessage2Details), EDHOCError> {
169169
let res = parse_message_2(message_2);
170170
if let Ok((g_y, ciphertext_2)) = res {
171171
let th_2 = compute_th_2(crypto, &g_y, &state.h_message_1);
@@ -193,7 +193,11 @@ pub fn i_parse_message_2_statstat<'a>(
193193
ead_2: ead_2.clone(), // needed for compute_mac_2
194194
};
195195

196-
Ok((state, c_r_2, id_cred_r, ead_2))
196+
Ok((
197+
state,
198+
c_r_2,
199+
ParsedMessage2Details::StatStat { id_cred_r, ead_2 },
200+
))
197201
} else {
198202
Err(EDHOCError::ParsingError)
199203
}
@@ -255,6 +259,7 @@ pub fn i_verify_message_2_statstat(
255259
// We need the method for next step. Since we are in the branch of StatStat,
256260
// we can add EDHOCMethod::StatStat
257261
method: EDHOCMethod::StatStat,
262+
method_specifics: ProcessedM2MethodSpecifics::StatStat {},
258263
prk_3e2m: prk_3e2m,
259264
prk_4e3m: prk_4e3m,
260265
th_3: th_3,

0 commit comments

Comments
 (0)