unkocrypto_rs/
lib.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
// unkocrypto
// author: Leonardone @ NEETSDKASU
// MIT License

use std::convert::TryFrom;
use std::error;
use std::fmt;
use std::io;
use std::mem::size_of;
use std::result;
use std::slice;

pub const MIN_BLOCK_SIZE: usize = 32;
pub const MAX_BLOCK_SIZE: usize = 1 << 20;
pub const META_SIZE: usize = size_of::<i32>() + size_of::<i64>();

const BYTE: i32 = u8::MAX as i32 + 1;

pub trait IntRng {
    fn next_int(&mut self) -> i32;
}

pub trait Checksum {
    fn reset(&mut self);
    fn update(&mut self, v: u8);
    fn get_value(&self) -> i64;
}

pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    IoError(io::Error),
    UnkocryptoError(Cause),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cause {
    InvalidBlockSize,
    InvalidSourceSize,
    InvalidDataCount,
    InvalidData,
    InvalidChecksum,
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Error::IoError(error)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Error::*;
        match self {
            IoError(ref e) => fmt::Display::fmt(e, f),
            UnkocryptoError(ref c) => write!(f, "UnkocryptoError({})", c),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        use Error::*;
        match self {
            IoError(ref e) => Some(e),
            UnkocryptoError(_) => None,
        }
    }
}

impl fmt::Display for Cause {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

// bound [1, 2^31)
fn next_int<T: IntRng>(rng: &mut T, bound: i32) -> i32 {
    if (bound & -bound) == bound {
        return ((bound as u64 * (rng.next_int() as u32 >> 1) as u64) >> 31) as i32;
    }
    let mut val: i32;
    loop {
        let bits = ((rng.next_int() as u32) >> 1) as i32;
        val = bits % bound;
        if (bits - val).wrapping_add(bound - 1) >= 0 {
            break;
        }
    }
    val
}

fn read<R: io::Read>(src: &mut R, one_byte: &mut u8) -> Result<usize> {
    let size = src.read(slice::from_mut(one_byte))?;
    Ok(size)
}

fn read_int(src: &[u8], pos: usize) -> i32 {
    let src = &src[pos..pos + size_of::<i32>()];
    i32::from_be_bytes(TryFrom::try_from(src).unwrap())
}

fn read_long(src: &[u8], pos: usize) -> i64 {
    let src = &src[pos..pos + size_of::<i64>()];
    i64::from_be_bytes(TryFrom::try_from(src).unwrap())
}

fn write_int(dst: &mut [u8], pos: usize, v: i32) {
    dst[pos..pos + size_of::<i32>()].copy_from_slice(&v.to_be_bytes());
}

fn write_long(dst: &mut [u8], pos: usize, v: i64) {
    dst[pos..pos + size_of::<i64>()].copy_from_slice(&v.to_be_bytes());
}

pub fn decrypt<C, T, R, W>(
    block_size: usize,
    mut checksum: C,
    rng: &mut T,
    src: &mut R,
    dst: &mut W,
) -> Result<(u64, u64)>
where
    C: Checksum,
    T: IntRng,
    R: io::Read,
    W: io::Write,
{
    if !(MIN_BLOCK_SIZE..=MAX_BLOCK_SIZE).contains(&block_size) {
        return Err(Error::UnkocryptoError(Cause::InvalidBlockSize));
    }
    let data_size: usize = block_size - META_SIZE;
    let mut mask: Vec<u8> = vec![0; block_size];
    let mut indexes: Vec<usize> = vec![0; block_size];
    let mut data: Vec<u8> = vec![0; block_size];
    let mut src_size: u64 = 0;
    let mut dst_size: u64 = 0;
    let mut one_byte: u8 = 0;
    let mut read_size: usize = read(src, &mut one_byte)?;
    if read_size == 0 {
        return Err(Error::UnkocryptoError(Cause::InvalidSourceSize));
    }
    while read_size > 0 {
        for i in 0..block_size {
            mask[i] = next_int(rng, BYTE) as u8;
            indexes[i] = i;
        }
        for i in 0..block_size {
            let j = next_int(rng, (block_size - i) as i32) as usize + i;
            indexes.swap(i, j);
        }
        for j in indexes.iter() {
            if read_size == 0 {
                return Err(Error::UnkocryptoError(Cause::InvalidSourceSize));
            }
            data[*j] = one_byte ^ mask[*j];
            read_size = read(src, &mut one_byte)?;
        }
        src_size += block_size as u64;
        let count = read_int(&data, data_size);
        let code = read_long(&data, data_size + size_of::<i32>());
        if count < 0 || (count == 0 && read_size > 0) || data_size < count as usize {
            return Err(Error::UnkocryptoError(Cause::InvalidDataCount));
        }
        let count = count as usize;
        dst_size += count as u64;
        checksum.reset();
        for (i, d) in data.iter().enumerate().take(data_size) {
            if i < count {
                dst.write_all(slice::from_ref(d))?;
                checksum.update(*d);
            } else if *d != 0 {
                return Err(Error::UnkocryptoError(Cause::InvalidData));
            }
        }
        if code != checksum.get_value() {
            return Err(Error::UnkocryptoError(Cause::InvalidChecksum));
        }
    }
    Ok((src_size, dst_size))
}

pub fn encrypt<C, T, R, W>(
    block_size: usize,
    mut checksum: C,
    rng: &mut T,
    src: &mut R,
    dst: &mut W,
) -> Result<(u64, u64)>
where
    C: Checksum,
    T: IntRng,
    R: io::Read,
    W: io::Write,
{
    if !(MIN_BLOCK_SIZE..=MAX_BLOCK_SIZE).contains(&block_size) {
        return Err(Error::UnkocryptoError(Cause::InvalidBlockSize));
    }
    let data_size: usize = block_size - META_SIZE;
    let mut data: Vec<u8> = vec![0; block_size];
    let mut src_size: u64 = 0;
    let mut dst_size: u64 = 0;
    let mut one_byte: u8 = 0;
    let mut read_size: usize = read(src, &mut one_byte)?;
    loop {
        dst_size += block_size as u64;
        checksum.reset();
        let mut count: usize = 0;
        while count < data_size {
            if read_size == 0 {
                break;
            }
            checksum.update(one_byte);
            data[count] = one_byte ^ next_int(rng, BYTE) as u8;
            count += 1;
            read_size = read(src, &mut one_byte)?;
        }
        src_size += count as u64;
        for d in data.iter_mut().take(data_size).skip(count) {
            *d = next_int(rng, BYTE) as u8;
        }
        write_int(&mut data, data_size, count as i32);
        write_long(
            &mut data,
            data_size + size_of::<i32>(),
            checksum.get_value(),
        );
        for d in data.iter_mut().skip(data_size) {
            *d ^= next_int(rng, BYTE) as u8;
        }
        for i in 0..data.len() {
            let j = next_int(rng, (data.len() - i) as i32) as usize + i;
            data.swap(i, j);
        }
        dst.write_all(&data)?;
        if read_size == 0 {
            break;
        }
    }
    Ok((src_size, dst_size))
}

pub fn calc_block_count(block_size: usize, src_len: u64) -> Result<u64> {
    if !(MIN_BLOCK_SIZE..=MAX_BLOCK_SIZE).contains(&block_size) {
        return Err(Error::UnkocryptoError(Cause::InvalidBlockSize));
    }
    let data_size: u64 = (block_size - META_SIZE) as u64;
    let block_count: u64 = (src_len + data_size - 1) / data_size;
    Ok(block_count)
}

pub fn calc_encrypted_size(block_size: usize, src_len: u64) -> Result<u64> {
    calc_block_count(block_size, src_len)?
        .checked_mul(block_size as u64)
        .ok_or_else(|| Error::UnkocryptoError(Cause::InvalidSourceSize))
}

pub fn consume<T: IntRng>(rng: &mut T, block_size: usize, block_count: u64) -> Result<()> {
    if !(MIN_BLOCK_SIZE..=MAX_BLOCK_SIZE).contains(&block_size) {
        return Err(Error::UnkocryptoError(Cause::InvalidBlockSize));
    }
    let _ = block_count
        .checked_mul(block_size as u64)
        .ok_or_else(|| Error::UnkocryptoError(Cause::InvalidSourceSize))?;
    for _ in 0..block_count {
        for _ in 0..block_size {
            let _ = next_int(rng, BYTE);
        }
        for i in 0..block_size {
            let _ = next_int(rng, (block_size - i) as i32);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    struct JavaRandom {
        seed: i64,
    }

    impl JavaRandom {
        fn new(seed: i64) -> JavaRandom {
            let mut jr = JavaRandom { seed: 0 };
            jr.set_seed(seed);
            jr
        }

        fn set_seed(&mut self, seed: i64) {
            self.seed = (seed ^ 0x5DEECE66D) & ((1 << 48) - 1);
        }

        fn next(&mut self, bits: i32) -> i32 {
            self.seed = self.seed.wrapping_mul(0x5DEECE66D).wrapping_add(0xB) & ((1 << 48) - 1);
            (self.seed as u64 >> (48 - bits)) as i32
        }
    }

    impl crate::IntRng for JavaRandom {
        fn next_int(&mut self) -> i32 {
            self.next(32)
        }
    }

    static CRC32_TABLE: [i64; 256] = {
        const fn f(c: i64) -> i64 {
            (c >> 1) ^ (0xedb88320 * (c & 1))
        }
        const fn calc(v: i64) -> i64 {
            f(f(f(f(f(f(f(f(v))))))))
        }
        let mut table = [0_i64; 256];
        macro_rules! m {
            ($e:expr) => {
                table[$e] = calc($e);
            };
            ($e:expr, $a:expr $(,$b:expr)*) => {
                m!(($e<<1) $(,$b)*);
                m!((($e<<1)|1) $(,$b)*);
            };
        }
        m!(0, 1, 2, 3, 4, 5, 6, 7, 8);
        table
    };

    struct Crc32 {
        value: i64,
    }

    impl Crc32 {
        fn new() -> Self {
            Self { value: 0xffffffff }
        }
    }

    impl crate::Checksum for Crc32 {
        fn reset(&mut self) {
            self.value = 0xffffffff;
        }

        fn update(&mut self, v: u8) {
            let b = CRC32_TABLE[(self.value & 0xff) as usize ^ v as usize];
            let c = self.value >> 8;
            self.value = b ^ c;
        }

        fn get_value(&self) -> i64 {
            self.value ^ 0xffffffff
        }
    }

    #[test]
    fn it_works() {
        let block_size: usize = crate::MIN_BLOCK_SIZE;

        let seed: i64 = 123456789;

        let data_src: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        let secret_src: [u8; 32] = [
            0xa2, 0xfc, 0x45, 0x90, 0x64, 0x80, 0x77, 0x46, 0x3f, 0x7e, 0x1d, 0x7c, 0x64, 0xfe,
            0x5c, 0x98, 0x7a, 0x00, 0x79, 0xa8, 0x64, 0xf2, 0x7d, 0xc1, 0xe3, 0x66, 0x31, 0x31,
            0x1e, 0x62, 0xb6, 0x04,
        ];

        // decrypt
        {
            let crc = Crc32::new();

            let mut rng = JavaRandom::new(seed);

            let mut cur = secret_src.as_slice();

            let mut dst = Vec::new();

            let (src_len, dst_len) =
                crate::decrypt(block_size, crc, &mut rng, &mut cur, &mut dst).unwrap();

            assert_eq!(secret_src.len() as u64, src_len);
            assert_eq!(data_src.len() as u64, dst_len);
            assert_eq!(data_src, dst.as_ref());
        }

        // encrypt
        {
            let crc = Crc32::new();

            let mut rng = JavaRandom::new(seed);

            let mut cur = data_src.as_slice();

            let mut dst = Vec::new();

            let (src_len, dst_len) =
                crate::encrypt(block_size, crc, &mut rng, &mut cur, &mut dst).unwrap();

            assert_eq!(data_src.len() as u64, src_len);
            assert_eq!(secret_src.len() as u64, dst_len);
            assert_eq!(secret_src, dst.as_ref());
        }

        // calc_encrypted_size
        {
            const LARGE_SRC_LEN: u64 = 12345;

            let crc = Crc32::new();

            let mut rng = JavaRandom::new(seed);

            let mut cur = std::io::Read::take(std::io::repeat(123), LARGE_SRC_LEN);

            let mut dst = std::io::sink();

            let (_, len) = crate::encrypt(block_size, crc, &mut rng, &mut cur, &mut dst).unwrap();

            let guess_size = crate::calc_encrypted_size(block_size, LARGE_SRC_LEN).unwrap();

            assert_eq!(len, guess_size);
        }

        // consume
        {
            let multi_src: Vec<Vec<u8>> = vec![data_src.as_slice(); 5]
                .iter()
                .enumerate()
                .map(|(i, a)| {
                    a.iter()
                        .map(|e| i as u8 + *e)
                        .cycle()
                        .take(10 * (i + 10))
                        .collect()
                })
                .collect();
            let encrypted = {
                let mut rng = JavaRandom::new(seed);
                let mut dst = Vec::new();
                for mut src in multi_src.iter().map(|a| a.as_slice()) {
                    let block_count =
                        crate::calc_block_count(block_size, src.len() as u64).unwrap();
                    dst.push(block_count as u8);
                    let crc = Crc32::new();
                    crate::encrypt(block_size, crc, &mut rng, &mut src, &mut dst).unwrap();
                }
                dst
            };
            {
                let mut rng = JavaRandom::new(seed);
                let mut cur = encrypted.as_slice();
                for (i, src) in multi_src.iter().enumerate() {
                    let block_count = cur[0] as u64;
                    let size = block_size * block_count as usize;
                    cur = &cur[1..];
                    if i % 2 == 1 {
                        crate::consume(&mut rng, block_size, block_count).unwrap();
                    } else {
                        let crc = Crc32::new();
                        let mut chunk = &cur[..size];
                        let mut dst = Vec::new();
                        crate::decrypt(block_size, crc, &mut rng, &mut chunk, &mut dst).unwrap();
                        assert_eq!(src, &dst);
                    }
                    cur = &cur[size..];
                }
            }
        }
    }
}