/
cb7.rs
685 lines (623 loc) · 26.3 KB
/
cb7.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Encrypt and decrypt cheat codes for CodeBreaker PS2 v7+.
use crate::rc4::Rc4;
use crate::std_alloc::Vec;
use core::fmt;
use core::mem::size_of;
use core::slice;
/// A processor for CB v7+ codes.
#[derive(Clone, Copy)]
pub struct Cb7 {
seeds: [[u8; 256]; 5],
key: [u32; 5],
beefcodf: bool,
initialized: bool,
}
/// Implements the default CB v7 encryption used by former CMGSCCC.com.
///
/// Lets you omit `B4336FA9 4DFEFB79` as the first code in the list.
impl Default for Cb7 {
fn default() -> Self {
let mut cb7 = Self::new();
cb7.beefcode(BEEFCODE, 0);
cb7
}
}
impl fmt::Debug for Cb7 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Cb7")
.field("seeds[0][0..16]", &self.seeds[0][0..16].to_vec())
.field("key", &self.key)
.field("beefcodf", &self.beefcodf)
.field("initialized", &self.initialized)
.finish()
}
}
impl Cb7 {
/// Returns a new processor for encrypting and decrypting a list of CB v7+
/// codes.
pub const fn new() -> Self {
Self {
seeds: ZERO_SEEDS,
key: [0; 5],
beefcodf: false,
initialized: false,
}
}
/// Generates or changes the encryption key and seeds.
///
/// Needs to be called for every "beefcode", which comes in two flavors:
///
/// ```text
/// BEEFC0DE vvvvvvvv
///
/// or:
///
/// BEEFC0DF vvvvvvvv
/// wwwwwwww wwwwwwww
///
/// v = seed value
/// w = extra seed value
/// ```
///
/// # Example
/// ```
/// use codebreaker::cb7::Cb7;
///
/// let mut cb7 = Cb7::new();
/// cb7.beefcode(0xBEEFC0DE, 0x00000000);
/// ```
pub fn beefcode(&mut self, addr: u32, val: u32) {
assert!(is_beefcode(addr));
// Easy access to all bytes of val
let v: Vec<usize> = val.to_le_bytes().iter().map(|&i| i as usize).collect();
// Set up key and seeds
if !self.initialized {
self.key.copy_from_slice(&RC4_KEY);
if val != 0 {
self.seeds.copy_from_slice(&SEEDS);
for i in 0..4 {
self.key[i] = u32::from(self.seeds[(i + 3) % 4][v[3]]) << 24
| u32::from(self.seeds[(i + 2) % 4][v[2]]) << 16
| u32::from(self.seeds[(i + 1) % 4][v[1]]) << 8
| u32::from(self.seeds[i % 4][v[0]]);
}
} else {
self.seeds.copy_from_slice(&ZERO_SEEDS);
}
self.initialized = true;
} else if val != 0 {
for i in 0..4 {
self.key[i] = u32::from(self.seeds[(i + 3) % 4][v[3]]) << 24
| u32::from(self.seeds[(i + 2) % 4][v[2]]) << 16
| u32::from(self.seeds[(i + 1) % 4][v[1]]) << 8
| u32::from(self.seeds[i % 4][v[0]]);
}
} else {
// Special case for 2x BEEFC0DE 00000000 in a row
self.seeds.copy_from_slice(&ZERO_SEEDS);
self.key[0] = 0;
self.key[1] = 0;
self.key[2] = 0;
self.key[3] = 0;
}
// Use key to encrypt seeds with RC4
let k = unsafe { slice_to_u8_mut(&mut self.key) };
for i in 0..5 {
let mut rc4 = Rc4::new(k);
// Encrypt seeds
rc4.crypt(&mut self.seeds[i]);
// Encrypt original key for next round
rc4.crypt(k);
}
// Since we don't know the extra seed value of BEEFC0DF yet,
// all we can do is set a flag.
self.beefcodf = addr & 1 != 0;
}
/// Encrypts a code and returns the result.
///
/// # Example
/// ```
/// use codebreaker::cb7::Cb7;
///
/// let mut cb7 = Cb7::default();
/// let code = cb7.encrypt_code(0x2043AFCC, 0x2411FFFF);
/// assert_eq!((0x397951B0, 0x41569FE0), code);
/// ```
pub fn encrypt_code(&mut self, addr: u32, val: u32) -> (u32, u32) {
let mut code = (addr, val);
self.encrypt_code_mut(&mut code.0, &mut code.1);
code
}
/// Encrypts a code directly.
///
/// # Example
/// ```
/// use codebreaker::cb7::Cb7;
///
/// let mut cb7 = Cb7::default();
/// let mut code = (0x2043AFCC, 0x2411FFFF);
/// cb7.encrypt_code_mut(&mut code.0, &mut code.1);
/// assert_eq!((0x397951B0, 0x41569FE0), code);
/// ```
pub fn encrypt_code_mut(&mut self, addr: &mut u32, val: &mut u32) {
let oldaddr = *addr;
let oldval = *val;
// Step 1: Multiplication, modulo (2^32)
*addr = mul_encrypt(*addr, self.key[0].wrapping_sub(self.key[1]));
*val = mul_encrypt(*val, self.key[2].wrapping_add(self.key[3]));
// Step 2: RC4
let mut code = [*addr, *val];
unsafe {
let mut rc4 = Rc4::new(slice_to_u8(&self.key));
rc4.crypt(slice_to_u8_mut(&mut code));
}
*addr = code[0];
*val = code[1];
// Step 3: RSA
rsa_crypt(addr, val, RSA_ENC_KEY, RSA_MODULUS);
// Step 4: Encryption loop of 64 cycles, using the generated seeds
let s = unsafe { slice_to_u32(&self.seeds) };
for i in 0..64 {
*addr = (addr.wrapping_add(s[2 * 64 + i]) ^ s[i]).wrapping_sub(*val ^ s[4 * 64 + i]);
*val = (val.wrapping_sub(s[3 * 64 + i]) ^ s[64 + i]).wrapping_add(*addr ^ s[4 * 64 + i]);
}
// BEEFC0DE
if is_beefcode(oldaddr) {
self.beefcode(oldaddr, oldval);
return;
}
// BEEFC0DF uses two codes. If the previous code was the first of the
// two, use the current one to encrypt the seeds.
if self.beefcodf {
unsafe {
let mut rc4 = Rc4::new(slice_to_u8(&[oldaddr, oldval]));
rc4.crypt(slice_to_u8_mut(&mut self.seeds));
}
self.beefcodf = false;
}
}
/// Decrypts a code and returns the result.
///
/// # Example
/// ```
/// use codebreaker::cb7::Cb7;
///
/// let mut cb7 = Cb7::default();
/// let code = cb7.decrypt_code(0x397951B0, 0x41569FE0);
/// assert_eq!((0x2043AFCC, 0x2411FFFF), code);
/// ```
pub fn decrypt_code(&mut self, addr: u32, val: u32) -> (u32, u32) {
let mut code = (addr, val);
self.decrypt_code_mut(&mut code.0, &mut code.1);
code
}
/// Decrypts a code directly.
///
/// # Example
/// ```
/// use codebreaker::cb7::Cb7;
///
/// let mut cb7 = Cb7::default();
/// let mut code = (0x397951B0, 0x41569FE0);
/// cb7.decrypt_code_mut(&mut code.0, &mut code.1);
/// assert_eq!((0x2043AFCC, 0x2411FFFF), code);
/// ```
pub fn decrypt_code_mut(&mut self, addr: &mut u32, val: &mut u32) {
// Step 1: Decryption loop of 64 cycles, using the generated seeds
let s = unsafe { slice_to_u32(&self.seeds) };
for i in (0..64).rev() {
*val = (val.wrapping_sub(*addr ^ s[4 * 64 + i]) ^ s[64 + i]).wrapping_add(s[3 * 64 + i]);
*addr = (addr.wrapping_add(*val ^ s[4 * 64 + i]) ^ s[i]).wrapping_sub(s[2 * 64 + i]);
}
// Step 2: RSA
rsa_crypt(addr, val, RSA_DEC_KEY, RSA_MODULUS);
// Step 3: RC4
let mut code = [*addr, *val];
unsafe {
let mut rc4 = Rc4::new(slice_to_u8(&self.key));
rc4.crypt(slice_to_u8_mut(&mut code));
}
*addr = code[0];
*val = code[1];
// Step 4: Multiplication with multiplicative inverse, modulo (2^32)
*addr = mul_decrypt(*addr, self.key[0].wrapping_sub(self.key[1]));
*val = mul_decrypt(*val, self.key[2].wrapping_add(self.key[3]));
// BEEFC0DF uses two codes. If the previous code was the first of the
// two, use the current one to decrypt the seeds.
if self.beefcodf {
unsafe {
let mut rc4 = Rc4::new(slice_to_u8(&[*addr, *val]));
rc4.crypt(slice_to_u8_mut(&mut self.seeds));
}
self.beefcodf = false;
return;
}
// BEEFC0DE
if is_beefcode(*addr) {
self.beefcode(*addr, *val);
}
}
}
/// Returns true if the code address indicates a "beefcode". In that case, the
/// [`beefcode`](struct.Cb7.html#method.beefcode) method should be invoked.
///
/// # Example
/// ```
/// use codebreaker::cb7::is_beefcode;
///
/// assert_eq!(true, is_beefcode(0xBEEFC0DE));
/// assert_eq!(true, is_beefcode(0xBEEFC0DF));
/// assert_eq!(false, is_beefcode(0x12345678));
/// ```
#[inline(always)]
pub const fn is_beefcode(addr: u32) -> bool {
addr & 0xffff_fffe == BEEFCODE
}
// Multiplication, modulo (2^32)
#[inline(always)]
const fn mul_encrypt(a: u32, b: u32) -> u32 {
a.wrapping_mul(b | 1)
}
// Multiplication with multiplicative inverse, modulo (2^32)
#[inline(always)]
const fn mul_decrypt(a: u32, b: u32) -> u32 {
a.wrapping_mul(mod_inverse(b | 1))
}
// Computes the multiplicative inverse of x modulo (2^32). x must be odd!
// The code is based on Newton's method as explained in this blog post:
// https://lemire.me/blog/2017/09/18/computing-the-inverse-of-odd-integers/
const fn mod_inverse(x: u32) -> u32 {
let mut y = x;
// Call this recurrence formula 4 times for 32-bit values:
// f(y) = y * (2 - y * x) modulo 2^32
y = y.wrapping_mul(2u32.wrapping_sub(y.wrapping_mul(x)));
y = y.wrapping_mul(2u32.wrapping_sub(y.wrapping_mul(x)));
y = y.wrapping_mul(2u32.wrapping_sub(y.wrapping_mul(x)));
y = y.wrapping_mul(2u32.wrapping_sub(y.wrapping_mul(x)));
y
}
// RSA encryption/decryption
fn rsa_crypt(addr: &mut u32, val: &mut u32, rsakey: u64, modulus: u64) {
use num_bigint::BigUint;
let code = BigUint::from_slice(&[*val, *addr]);
let m = BigUint::from(modulus);
// Exponentiation is only invertible if code < modulus
if code < m {
let digits = code.modpow(&BigUint::from(rsakey), &m).to_u32_digits();
*addr = digits[1];
*val = digits[0];
}
}
// Source: https://github.com/BurntSushi/byteorder/blob/master/src/io.rs
unsafe fn slice_to_u8_mut<T: Copy>(slice: &mut [T]) -> &mut [u8] {
let len = size_of::<T>() * slice.len();
slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, len)
}
unsafe fn slice_to_u8<T: Copy>(slice: &[T]) -> &[u8] {
let len = size_of::<T>() * slice.len();
slice::from_raw_parts(slice.as_ptr() as *const u8, len)
}
unsafe fn slice_to_u32<T: Copy>(slice: &[T]) -> &[u32] {
let len = size_of::<T>() * slice.len();
slice::from_raw_parts(slice.as_ptr() as *const u32, len)
}
const BEEFCODE: u32 = 0xbeef_c0de;
const RC4_KEY: [u32; 5] = [0xd0db_a9d7, 0x13a0_a96c, 0x8041_0df0, 0x2ccd_be1f, 0xe570_a86b];
const RSA_DEC_KEY: u64 = 11;
// This is how I calculated the encryption key e from d (some number theory):
//
// d = 11
// n = 18446744073709551605
// e = d^(-1) mod phi(n)
//
// n factored:
// n = 5 * 2551 * 1446236305269271
// = p*q*r, only single prime factors
//
// phi(n) = phi(p*q*r)
// = phi(p) * phi(q) * phi(r), phi(p) = p - 1
// = (p-1)*(q-1)*(r-1)
// = (5-1) * (2551-1) * (1446236305269271-1)
// = 4 * 2550 * 1446236305269270
// = 14751610313746554000
//
// e = 11^(-1) mod 14751610313746554000
// e = 2682110966135737091
const RSA_ENC_KEY: u64 = 2_682_110_966_135_737_091;
const RSA_MODULUS: u64 = 18_446_744_073_709_551_605; // 0xffff_ffff_ffff_fff5
const ZERO_SEEDS: [[u8; 256]; 5] = [[0; 256]; 5];
#[rustfmt::skip]
const SEEDS: [[u8; 256]; 5] = [
[
0x84, 0x01, 0x21, 0xa4, 0xfa, 0x4d, 0x50, 0x8d, 0x75, 0x33, 0xc5, 0xf7, 0x4a, 0x6d, 0x7c, 0xa6,
0x1c, 0xf8, 0x40, 0x18, 0xa1, 0xb3, 0xa2, 0xf9, 0x6a, 0x19, 0x63, 0x66, 0x29, 0xae, 0x10, 0x75,
0x84, 0x7d, 0xec, 0x6a, 0xf9, 0x2d, 0x8e, 0x33, 0x44, 0x5c, 0x33, 0x6d, 0x78, 0x3e, 0x1b, 0x6c,
0x02, 0xe0, 0x7d, 0x77, 0x1d, 0xb1, 0x61, 0x2a, 0xcd, 0x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 0xdf, 0x60, 0xb9, 0xaf,
0xaa, 0x70, 0xb4, 0xa8, 0x17, 0x99, 0x72, 0xb9, 0x88, 0x9d, 0x3d, 0x2a, 0x11, 0x87, 0x1e, 0xf3,
0x9d, 0x33, 0x8d, 0xed, 0x52, 0x60, 0x36, 0x71, 0xff, 0x7b, 0x37, 0x84, 0x3d, 0x27, 0x9e, 0xd9,
0xdf, 0x58, 0xf7, 0xc2, 0x58, 0x0c, 0x9d, 0x5e, 0xee, 0x23, 0x83, 0x70, 0x3f, 0x95, 0xbc, 0xf5,
0x42, 0x86, 0x91, 0x5b, 0x3f, 0x77, 0x31, 0xd2, 0xb7, 0x09, 0x59, 0x53, 0xf5, 0xf2, 0xe5, 0xf1,
0xdc, 0x92, 0x83, 0x14, 0xc1, 0xa2, 0x25, 0x62, 0x13, 0xfd, 0xd4, 0xc5, 0x54, 0x9d, 0x9c, 0x27,
0x6c, 0xc2, 0x75, 0x8b, 0xbc, 0xc7, 0x4e, 0x0a, 0xf6, 0x5c, 0x2f, 0x12, 0x8e, 0x25, 0xbb, 0xf2,
0x5f, 0x89, 0xaa, 0xea, 0xd9, 0xcd, 0x05, 0x74, 0x20, 0xd6, 0x17, 0xed, 0xf0, 0x66, 0x6c, 0x7b,
],
];
#[cfg(test)]
mod tests {
use super::*;
use crate::code;
use crate::std_alloc::Vec;
fn mul_tests() -> Vec<(u32, u32, u32)> {
vec![
(0x0000_0000, 0xa686_d3b6, 0x0000_0000),
(0x000e_0000, 0xa686_d3b6, 0xac62_0000),
(0x0067_bd20, 0x4fd9_31ff, 0x2008_02e0),
(0x2ba0_a76e, 0xa686_d3b6, 0x2405_0002),
(0x4adf_d954, 0x4fd9_31ff, 0x9029_beac),
(0x7c01_6806, 0x2912_dedd, 0x0000_00be),
(0xa942_2f21, 0xa686_d3b6, 0x03d2_03e7),
(0xfff5_76e0, 0xa686_d3b6, 0x27bd_0020),
]
}
#[test]
fn test_mul_encrypt() {
for t in mul_tests().iter() {
assert_eq!(t.0, mul_encrypt(t.2, t.1));
}
}
#[test]
fn test_mul_decrypt() {
for t in mul_tests().iter() {
assert_eq!(t.2, mul_decrypt(t.0, t.1));
}
}
#[test]
fn test_mod_inverse() {
let tests = vec![
(0x0d31_3243, 0x6c7b_2a6b),
(0x0efd_8231, 0xd4c0_96d1),
(0x2912_dedd, 0xe09d_e975),
(0x4fd9_31ff, 0x9a62_cdff),
(0x5a53_abb5, 0x58f4_2a9d),
(0x9ab2_af6d, 0x1043_b265),
(0xa686_d3b7, 0x57ed_7a07),
(0xec35_a92f, 0xd274_3dcf),
(0x0000_0000, 0x0000_0000), // Technically, 0 has no inverse
(0x0000_0001, 0x0000_0001),
(0xffff_ffff, 0xffff_ffff),
];
for t in tests.iter() {
assert_eq!(t.1, mod_inverse(t.0));
}
}
struct Test {
beefcode: &'static str,
decrypted: Vec<&'static str>,
encrypted: Vec<&'static str>,
}
#[rustfmt::skip]
fn tests() -> Vec<Test> {
vec![
Test {
// default BEEFC0DE
beefcode: "BEEFC0DE 00000000",
decrypted: vec![
"9029BEAC 0C0A9225",
"201F6024 00000000",
"2096F5B8 000000BE",
],
encrypted: vec![
"D08F3A49 00078A53",
"3818DDE5 E72B2B16",
"973E0B2A A7D4AF10",
],
},
Test {
// non-default BEEFC0DE
beefcode: "BEEFC0DE DEADFACE",
decrypted: vec![
"9029BEAC 0C0A9225",
"201F6024 00000000",
"2096F5B8 000000BE",
],
encrypted: vec![
"E65B5422 B12543CF",
"D14F5E52 FE26C9ED",
"DD9BB6F0 F5DF87F7",
],
},
Test {
// BEEFC0DF
beefcode: "BEEFC0DF B16B00B5",
decrypted: vec![
"01234567 89ABCDEF",
"9029BEAC 0C0A9225",
"201F6024 00000000",
"2096F5B8 000000BE",
],
encrypted: vec![
"862316AB C59C5FB1",
"06133B66 95444FF1",
"565FD08D 9154AFF4",
"4EF412FE D03E4E13",
],
},
Test {
// BEEFC0DE & BEEFC0DF
beefcode: "BEEFC0DE 00000000",
decrypted: vec![
"BEEFC0DF B16B00B5",
"01234567 89ABCDEF",
"9029BEAC 0C0A9225",
"201F6024 00000000",
"2096F5B8 000000BE",
],
encrypted: vec![
"FE8B8601 C7C6F6CE",
"2195D855 63FA11A7",
"0CA31760 A6F7E88A",
"679DC392 FA43E30B",
"1CD9CCC3 6AF74E36",
],
},
Test {
// 2x default BEEFC0DE
beefcode: "BEEFC0DE 00000000",
decrypted: vec![
"BEEFC0DE 00000000",
"9029BEAC 0C0A9225",
"201F6024 00000000",
"2096F5B8 000000BE",
],
encrypted: vec![
"8787C575 1AC4C1B4",
"02210430 184C16E8",
"32E2A916 7E6017BA",
"CBB720FD D61505E0",
],
},
]
}
#[test]
fn test_encrypt_code() {
for t in tests().iter() {
let code = code::parse(t.beefcode);
let mut cb7 = Cb7::new();
cb7.beefcode(code.0, code.1);
for (i, line) in t.decrypted.iter().enumerate() {
let code = code::parse(line);
let result = cb7.encrypt_code(code.0, code.1);
assert_eq!(t.encrypted[i], code::format(result));
if is_beefcode(code.0) {
cb7.beefcode(code.0, code.1)
}
}
}
}
#[test]
fn test_encrypt_code_mut() {
for t in tests().iter() {
let code = code::parse(t.beefcode);
let mut cb7 = Cb7::new();
cb7.beefcode(code.0, code.1);
for (i, line) in t.decrypted.iter().enumerate() {
let mut code = code::parse(line);
let oldcode = code;
cb7.encrypt_code_mut(&mut code.0, &mut code.1);
assert_eq!(t.encrypted[i], code::format(code));
if is_beefcode(oldcode.0) {
cb7.beefcode(oldcode.0, oldcode.1)
}
}
}
}
#[test]
fn test_decrypt_code() {
for t in tests().iter() {
let code = code::parse(t.beefcode);
let mut cb7 = Cb7::new();
cb7.beefcode(code.0, code.1);
for (i, line) in t.encrypted.iter().enumerate() {
let code = code::parse(line);
let result = cb7.decrypt_code(code.0, code.1);
assert_eq!(t.decrypted[i], code::format(result));
if is_beefcode(result.0) {
cb7.beefcode(result.0, result.1)
}
}
}
}
#[test]
fn test_decrypt_code_mut() {
for t in tests().iter() {
let code = code::parse(t.beefcode);
let mut cb7 = Cb7::new();
cb7.beefcode(code.0, code.1);
for (i, line) in t.encrypted.iter().enumerate() {
let mut code = code::parse(line);
cb7.decrypt_code_mut(&mut code.0, &mut code.1);
assert_eq!(t.decrypted[i], code::format(code));
if is_beefcode(code.0) {
cb7.beefcode(code.0, code.1)
}
}
}
}
}