Skip to content

Commit 35820e5

Browse files
committed
Replacing outer/inner states for method_specific entry
We avoid repetition of shared-values among methods This affects ProcessingM2 and ProcessingM3
1 parent 69792a9 commit 35820e5

4 files changed

Lines changed: 124 additions & 66 deletions

File tree

lakers-c/src/lib.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,48 +95,75 @@ impl EadItemsC {
9595

9696
#[derive(Debug)]
9797
#[repr(C)]
98-
pub struct ProcessingM2C {
98+
pub enum ProcessingM2MethodSpecificsKindC {
99+
Pm2StatStat,
100+
}
101+
102+
#[derive(Debug)]
103+
#[repr(C)]
104+
pub struct ProcessingM2MethodSpecificsC {
105+
pub kind: ProcessingM2MethodSpecificsKindC,
99106
pub mac_2: BytesMac2,
107+
pub id_cred_r: IdCred,
108+
}
109+
110+
#[derive(Debug)]
111+
#[repr(C)]
112+
pub struct ProcessingM2C {
113+
pub method_specifics: ProcessingM2MethodSpecificsC,
114+
pub method: EDHOCMethod,
100115
pub prk_2e: BytesHashLen,
101116
pub th_2: BytesHashLen,
102117
pub x: BytesP256ElemLen,
103118
pub g_y: BytesP256ElemLen,
104119
pub plaintext_2: EdhocMessageBuffer,
105120
pub c_r: u8,
106-
pub id_cred_r: IdCred,
107121
pub ead_2: *mut EadItemsC,
108122
}
109123

110124
impl Default for ProcessingM2C {
111125
fn default() -> Self {
112126
ProcessingM2C {
113-
mac_2: Default::default(),
127+
method_specifics: ProcessingM2MethodSpecificsC {
128+
kind: ProcessingM2MethodSpecificsKindC::Pm2StatStat,
129+
mac_2: Default::default(),
130+
id_cred_r: Default::default(),
131+
},
132+
method: EDHOCMethod::StatStat,
114133
prk_2e: Default::default(),
115134
th_2: Default::default(),
116135
x: Default::default(),
117136
g_y: Default::default(),
118137
plaintext_2: Default::default(),
119138
c_r: Default::default(),
120-
id_cred_r: Default::default(),
121139
ead_2: core::ptr::null_mut(),
122140
}
123141
}
124142
}
125143

126144
impl ProcessingM2C {
127145
pub fn to_rust(&self) -> ProcessingM2 {
128-
ProcessingM2::StatStat(ProcessingM2StatStat {
129-
mac_2: self.mac_2,
146+
let method_specifics = match self.method_specifics.kind {
147+
ProcessingM2MethodSpecificsKindC::Pm2StatStat => {
148+
ProcessingM2MethodSpecifics::StatStat {
149+
mac_2: self.method_specifics.mac_2,
150+
id_cred_r: self.method_specifics.id_cred_r.clone(),
151+
}
152+
}
153+
};
154+
155+
ProcessingM2 {
156+
method_specifics,
157+
method: self.method,
130158
prk_2e: self.prk_2e,
131159
th_2: self.th_2,
132160
x: self.x,
133161
g_y: self.g_y,
134162
plaintext_2: self.plaintext_2.clone(),
135163
#[allow(deprecated)]
136164
c_r: ConnId::from_int_raw(self.c_r),
137-
id_cred_r: self.id_cred_r.clone(),
138165
ead_2: unsafe { (*self.ead_2).to_rust() },
139-
})
166+
}
140167
}
141168

142169
/// note that it is a shallow copy (ead_2 is handled separately by the caller)
@@ -145,18 +172,23 @@ impl ProcessingM2C {
145172
panic!("processing_m2_c is null");
146173
}
147174

148-
match processing_m2 {
149-
ProcessingM2::StatStat(s) => {
150-
(*processing_m2_c).mac_2 = s.mac_2;
151-
(*processing_m2_c).prk_2e = s.prk_2e;
152-
(*processing_m2_c).th_2 = s.th_2;
153-
(*processing_m2_c).x = s.x;
154-
(*processing_m2_c).g_y = s.g_y;
155-
(*processing_m2_c).plaintext_2 = s.plaintext_2;
156-
let c_r = s.c_r.as_slice();
157-
assert_eq!(c_r.len(), 1, "C API only supports short C_R");
158-
(*processing_m2_c).c_r = c_r[0];
159-
(*processing_m2_c).id_cred_r = s.id_cred_r;
175+
(*processing_m2_c).prk_2e = processing_m2.prk_2e;
176+
(*processing_m2_c).th_2 = processing_m2.th_2;
177+
(*processing_m2_c).x = processing_m2.x;
178+
(*processing_m2_c).g_y = processing_m2.g_y;
179+
(*processing_m2_c).plaintext_2 = processing_m2.plaintext_2;
180+
(*processing_m2_c).method = processing_m2.method;
181+
let c_r = processing_m2.c_r.as_slice();
182+
assert_eq!(c_r.len(), 1, "C API only supports short C_R");
183+
(*processing_m2_c).c_r = c_r[0];
184+
185+
match processing_m2.method_specifics {
186+
ProcessingM2MethodSpecifics::StatStat { mac_2, id_cred_r } => {
187+
(*processing_m2_c).method_specifics = ProcessingM2MethodSpecificsC {
188+
kind: ProcessingM2MethodSpecificsKindC::Pm2StatStat,
189+
mac_2,
190+
id_cred_r,
191+
};
160192
}
161193
}
162194
}

lib/src/edhoc.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ pub fn r_verify_message_3(
103103
crypto: &mut impl CryptoTrait,
104104
valid_cred_i: Credential,
105105
) -> Result<(ProcessedM3, BytesHashLen), EDHOCError> {
106-
match state {
107-
ProcessingM3::StatStat(inner) => r_verify_message_3_statstat(inner, crypto, valid_cred_i), // ProcessingM3::Psk(inner) => r_verify_message_3_psk(inner, crypto, valid_cred_i),
106+
match state.method {
107+
EDHOCMethod::StatStat => r_verify_message_3_statstat(state, crypto, valid_cred_i),
108+
_ => Err(EDHOCError::UnsupportedMethod),
108109
}
109110
}
110111

@@ -174,10 +175,10 @@ pub fn i_verify_message_2(
174175
valid_cred_r: Credential,
175176
i: &BytesP256ElemLen, // I's static private DH key
176177
) -> Result<ProcessedM2, EDHOCError> {
177-
match state {
178-
ProcessingM2::StatStat(inner_state) => {
179-
i_verify_message_2_statstat(inner_state, crypto, valid_cred_r, i)
180-
} // ProcessingM2::Psk(inner_state) => i_verify_message_2_psk();
178+
match state.method {
179+
EDHOCMethod::StatStat => i_verify_message_2_statstat(state, crypto, valid_cred_r, i),
180+
// EDHOCMethod::PSK => i_verify_message_2_psk()
181+
_ => Err(EDHOCError::UnsupportedMethod),
181182
}
182183
}
183184

@@ -192,7 +193,7 @@ pub fn i_prepare_message_3(
192193
EDHOCMethod::StatStat => {
193194
i_prepare_message_3_statstat(state, crypto, cred_i, cred_transfer, ead_3)
194195
}
195-
// EDHOCMethod::PSK => r_parse_message_3_psk()
196+
// EDHOCMethod::PSK => i_prepare_message_3_psk()
196197
_ => Err(EDHOCError::UnsupportedMethod),
197198
}
198199
}

lib/src/edhoc/statstat.rs

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@ pub fn r_parse_message_3_statstat(
7272

7373
if let Ok((id_cred_i, mac_3, ead_3)) = decoded_p3_res {
7474
Ok((
75-
ProcessingM3::StatStat(ProcessingM3StatStat {
76-
mac_3,
75+
ProcessingM3 {
76+
method_specifics: ProcessingM3MethodSpecifics::StatStat {
77+
mac_3,
78+
id_cred_i: id_cred_i.clone(), // needed for compute_mac_3
79+
},
80+
method: state.method,
7781
y: state.y,
7882
prk_3e2m: state.prk_3e2m,
7983
th_3: state.th_3,
80-
id_cred_i: id_cred_i.clone(), // needed for compute_mac_3
8184
plaintext_3, // NOTE: this is needed for th_4, which needs valid_cred_i, which is only available at the 'verify' step
8285
ead_3: ead_3.clone(), // NOTE: this clone could be avoided by using a reference or an index to the ead_3 item in plaintext_3
83-
}),
86+
},
8487
id_cred_i,
8588
ead_3,
8689
))
@@ -94,36 +97,44 @@ pub fn r_parse_message_3_statstat(
9497
}
9598

9699
pub fn r_verify_message_3_statstat(
97-
inner_state: &ProcessingM3StatStat,
100+
state: &ProcessingM3,
98101
crypto: &mut impl CryptoTrait,
99102
valid_cred_i: Credential,
100103
) -> Result<(ProcessedM3, BytesHashLen), EDHOCError> {
101104
// compute salt_4e3m
102-
let salt_4e3m = compute_salt_4e3m(crypto, &inner_state.prk_3e2m, &inner_state.th_3);
105+
let salt_4e3m = compute_salt_4e3m(crypto, &state.prk_3e2m, &state.th_3);
103106

104107
let prk_4e3m = match valid_cred_i.key {
105108
CredentialKey::EC2Compact(public_key) => {
106-
compute_prk_4e3m(crypto, &salt_4e3m, &inner_state.y, &public_key)
109+
compute_prk_4e3m(crypto, &salt_4e3m, &state.y, &public_key)
107110
}
108111
CredentialKey::Symmetric(_psk) => todo!("PSK not implemented"),
109112
};
110113

114+
let id_cred_i = match &state.method_specifics {
115+
ProcessingM3MethodSpecifics::StatStat { id_cred_i, .. } => id_cred_i,
116+
};
117+
111118
// compute mac_3
112119
let expected_mac_3 = compute_mac_3(
113120
crypto,
114121
&prk_4e3m,
115-
&inner_state.th_3,
116-
inner_state.id_cred_i.as_full_value(),
122+
&state.th_3,
123+
id_cred_i.as_full_value(),
117124
valid_cred_i.bytes.as_slice(),
118-
&inner_state.ead_3,
125+
&state.ead_3,
119126
);
120127

128+
let mac_3 = match state.method_specifics {
129+
ProcessingM3MethodSpecifics::StatStat { mac_3, .. } => mac_3,
130+
};
131+
121132
// verify mac_3
122-
if inner_state.mac_3 == expected_mac_3 {
133+
if mac_3 == expected_mac_3 {
123134
let th_4 = compute_th_4(
124135
crypto,
125-
&inner_state.th_3,
126-
&inner_state.plaintext_3,
136+
&state.th_3,
137+
&state.plaintext_3,
127138
valid_cred_i.bytes.as_slice(),
128139
);
129140

@@ -169,17 +180,20 @@ pub fn i_parse_message_2_statstat<'a>(
169180
let plaintext_2_decoded = decode_plaintext_2(&plaintext_2);
170181

171182
if let Ok((c_r_2, id_cred_r, mac_2, ead_2)) = plaintext_2_decoded {
172-
let state = ProcessingM2::StatStat(ProcessingM2StatStat {
173-
mac_2,
183+
let state = ProcessingM2 {
184+
method_specifics: ProcessingM2MethodSpecifics::StatStat {
185+
mac_2,
186+
id_cred_r: id_cred_r.clone(), // needed for compute_mac_2
187+
},
188+
method: state.method,
174189
prk_2e,
175190
th_2,
176191
x: state.x,
177192
g_y,
178193
plaintext_2: plaintext_2,
179194
c_r: c_r_2,
180-
id_cred_r: id_cred_r.clone(), // needed for compute_mac_2
181-
ead_2: ead_2.clone(), // needed for compute_mac_2
182-
});
195+
ead_2: ead_2.clone(), // needed for compute_mac_2
196+
};
183197

184198
Ok((state, c_r_2, id_cred_r, ead_2))
185199
} else {
@@ -191,45 +205,53 @@ pub fn i_parse_message_2_statstat<'a>(
191205
}
192206

193207
pub fn i_verify_message_2_statstat(
194-
inner_state: &ProcessingM2StatStat,
208+
state: &ProcessingM2,
195209
crypto: &mut impl CryptoTrait,
196210
valid_cred_r: Credential,
197211
i: &BytesP256ElemLen, // I's static private DH key
198212
) -> Result<ProcessedM2, EDHOCError> {
199213
// verify mac_2
200-
let salt_3e2m = compute_salt_3e2m(crypto, &inner_state.prk_2e, &inner_state.th_2);
214+
let salt_3e2m = compute_salt_3e2m(crypto, &state.prk_2e, &state.th_2);
201215

202216
let prk_3e2m = match valid_cred_r.key {
203217
CredentialKey::EC2Compact(public_key) => {
204-
compute_prk_3e2m(crypto, &salt_3e2m, &inner_state.x, &public_key)
218+
compute_prk_3e2m(crypto, &salt_3e2m, &state.x, &public_key)
205219
}
206220
CredentialKey::Symmetric(_psk) => todo!("PSK not implemented"),
207221
};
208222

223+
let id_cred_r = match &state.method_specifics {
224+
ProcessingM2MethodSpecifics::StatStat { id_cred_r, .. } => id_cred_r,
225+
};
226+
209227
let expected_mac_2 = compute_mac_2(
210228
crypto,
211229
&prk_3e2m,
212-
inner_state.c_r,
213-
inner_state.id_cred_r.as_full_value(),
230+
state.c_r,
231+
id_cred_r.as_full_value(),
214232
valid_cred_r.bytes.as_slice(),
215-
&inner_state.th_2,
216-
&inner_state.ead_2,
233+
&state.th_2,
234+
&state.ead_2,
217235
);
218236

219-
if inner_state.mac_2 == expected_mac_2 {
237+
let mac_2 = match state.method_specifics {
238+
ProcessingM2MethodSpecifics::StatStat { mac_2, .. } => mac_2,
239+
};
240+
241+
if mac_2 == expected_mac_2 {
220242
// step is actually from processing of message_3
221243
// but we do it here to avoid storing plaintext_2 in State
222244
let th_3 = compute_th_3(
223245
crypto,
224-
&inner_state.th_2,
225-
&inner_state.plaintext_2,
246+
&state.th_2,
247+
&state.plaintext_2,
226248
valid_cred_r.bytes.as_slice(),
227249
);
228250
// message 3 processing
229251

230252
let salt_4e3m = compute_salt_4e3m(crypto, &prk_3e2m, &th_3);
231253

232-
let prk_4e3m = compute_prk_4e3m(crypto, &salt_4e3m, i, &inner_state.g_y);
254+
let prk_4e3m = compute_prk_4e3m(crypto, &salt_4e3m, i, &state.g_y);
233255

234256
let state = ProcessedM2 {
235257
// We need the method for next step. Since we are in the branch of StatStat,

shared/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,24 @@ pub struct WaitM3 {
515515
pub prk_3e2m: BytesHashLen,
516516
pub th_3: BytesHashLen,
517517
}
518+
518519
#[derive(Debug)]
519-
pub enum ProcessingM2 {
520-
StatStat(ProcessingM2StatStat), // PSK(PProcessingM2PSK) // To be added later
520+
#[repr(C)]
521+
pub enum ProcessingM2MethodSpecifics {
522+
StatStat { mac_2: BytesMac2, id_cred_r: IdCred },
523+
// PSK, -- is empty, but in other stages it might have fields that StatStat has not.
521524
}
522525
#[derive(Debug)]
523526
#[repr(C)]
524-
pub struct ProcessingM2StatStat {
525-
pub mac_2: BytesMac2,
527+
pub struct ProcessingM2 {
528+
pub method_specifics: ProcessingM2MethodSpecifics,
529+
pub method: EDHOCMethod,
526530
pub prk_2e: BytesHashLen,
527531
pub th_2: BytesHashLen,
528532
pub x: BytesP256ElemLen,
529533
pub g_y: BytesP256ElemLen,
530534
pub plaintext_2: BufferPlaintext2,
531535
pub c_r: ConnId,
532-
pub id_cred_r: IdCred,
533536
pub ead_2: EadItems,
534537
}
535538

@@ -542,16 +545,16 @@ pub struct ProcessedM2 {
542545
pub th_3: BytesHashLen,
543546
}
544547
#[derive(Debug)]
545-
pub enum ProcessingM3 {
546-
StatStat(ProcessingM3StatStat), // PSK(ProcessingM3PSK) // To be added later
548+
pub enum ProcessingM3MethodSpecifics {
549+
StatStat { mac_3: BytesMac3, id_cred_i: IdCred },
547550
}
548551
#[derive(Debug)]
549-
pub struct ProcessingM3StatStat {
550-
pub mac_3: BytesMac3,
552+
pub struct ProcessingM3 {
553+
pub method_specifics: ProcessingM3MethodSpecifics,
554+
pub method: EDHOCMethod,
551555
pub y: BytesP256ElemLen, // ephemeral private key of the responder
552556
pub prk_3e2m: BytesHashLen,
553557
pub th_3: BytesHashLen,
554-
pub id_cred_i: IdCred,
555558
pub plaintext_3: BufferPlaintext3,
556559
pub ead_3: EadItems,
557560
}

0 commit comments

Comments
 (0)