Skip to content

Commit 7691577

Browse files
committed
Fix double rekey when V7 stream contains BEEFCODE
Codebreaker checked is_beefcode after every encrypt/decrypt and called Cb7::beefcode, but Cb7::*_code_mut already self-handles the rekey on BEEFCODE inputs. In the V7 scheme path this double-fired and rotated seeds/key twice. Restrict the Codebreaker-side rekey to the non-V7 branch so each scheme has exactly one owner. auto_decrypt_code_mut gets the same treatment; its V7 branch now only adjusts code_lines for BEEFCODF's two-line shape. Add a Codebreaker::new_v7() regression case that feeds a BEEFCODE through the V7 path.
1 parent 1cbda75 commit 7691577

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

src/lib.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ impl Codebreaker {
131131
self.cb7.encrypt_code_mut(addr, val);
132132
} else {
133133
cb1::encrypt_code_mut(addr, val);
134-
}
135134

136-
if is_beefcode(oldaddr) {
137-
self.cb7.beefcode(oldaddr, oldval);
138-
self.scheme = Scheme::V7;
135+
if is_beefcode(oldaddr) {
136+
self.cb7.beefcode(oldaddr, oldval);
137+
self.scheme = Scheme::V7;
138+
}
139139
}
140140
}
141141

@@ -196,11 +196,11 @@ impl Codebreaker {
196196
self.cb7.decrypt_code_mut(addr, val);
197197
} else {
198198
cb1::decrypt_code_mut(addr, val);
199-
}
200199

201-
if is_beefcode(*addr) {
202-
self.cb7.beefcode(*addr, *val);
203-
self.scheme = Scheme::V7;
200+
if is_beefcode(*addr) {
201+
self.cb7.beefcode(*addr, *val);
202+
self.scheme = Scheme::V7;
203+
}
204204
}
205205
}
206206

@@ -258,6 +258,12 @@ impl Codebreaker {
258258
}
259259
cb1::decrypt_code_mut(addr, val);
260260
}
261+
262+
if is_beefcode(*addr) {
263+
self.cb7.beefcode(*addr, *val);
264+
self.scheme = Scheme::V7;
265+
self.code_lines = 1;
266+
}
261267
} else {
262268
self.cb7.decrypt_code_mut(addr, val);
263269
if self.code_lines == 0 {
@@ -269,12 +275,10 @@ impl Codebreaker {
269275
}
270276
}
271277
self.code_lines -= 1;
272-
}
273278

274-
if is_beefcode(*addr) {
275-
self.cb7.beefcode(*addr, *val);
276-
self.scheme = Scheme::V7;
277-
self.code_lines = 1;
279+
if is_beefcode(*addr) {
280+
self.code_lines = 1;
281+
}
278282
}
279283
}
280284
}
@@ -333,6 +337,21 @@ mod tests {
333337
"973E0B2A A7D4AF10".into(),
334338
],
335339
},
340+
Test {
341+
cb: Codebreaker::new_v7(),
342+
decrypted: vec![
343+
"BEEFC0DE 00000000".into(),
344+
"9029BEAC 0C0A9225".into(),
345+
"201F6024 00000000".into(),
346+
"2096F5B8 000000BE".into(),
347+
],
348+
encrypted: vec![
349+
"8787C575 1AC4C1B4".into(),
350+
"E8C99F89 14B426D6".into(),
351+
"CA64CBFA CD67C364".into(),
352+
"4FE0642D 3EBDE7D4".into(),
353+
],
354+
},
336355
Test {
337356
cb: Codebreaker::default(),
338357
decrypted: vec![

0 commit comments

Comments
 (0)