-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbse.c
83 lines (67 loc) · 1.52 KB
/
bse.c
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
#include "bse.h"
#include "decrypt.h"
#include <stdio.h>
#include <string.h>
static int32_t bse_rand(int32_t * seed);
int bse_is_valid(uint8_t * data, uint32_t size)
{
if(size < 80) return 0;
return (memcmp((char*)data, "BSE 1.0", 7) == 0);
}
int bse_decrypt(uint8_t * crypted)
{
int32_t hash = 0;
uint8_t sum_check = 0;
uint8_t xor_check = 0;
uint8_t sum_data = 0;
uint8_t xor_data = 0;
int flags[64] = {0};
int counter = 0;
crypted += 8;
/* 0x100 =*/ read16(&crypted);
sum_check = read8(&crypted);
xor_check = read8(&crypted);
hash = read32(&crypted);
for(counter = 0;counter < 64;counter++)
{
int target = NULL;
int s, k;
int r = bse_rand(&hash);
int i = r & 0x3F;
while(flags[i])
{
i = (i + 1) & 0x3F;
}
r = bse_rand(&hash);
s = r & 0x07;
target = i;
k = bse_rand(&hash);
r = bse_rand(&hash);
r = ((crypted[target] & 255) - r) & 255;
if(k & 1)
{
crypted[target] = r << s | r >> (8 - s);
}
else
{
crypted[target] = r >> s | r << (8 - s);
}
flags[i] = 1;
}
for(counter = 0;counter < 64;counter++)
{
sum_data = sum_data + (crypted[counter] & 255);
xor_data = xor_data ^ (crypted[counter] & 255);
}
if(sum_data == sum_check && xor_data == xor_check)
{
return 1;
}
return 0;
}
int32_t bse_rand(int32_t * seed)
{
int32_t tmp = (((*seed * 257 >> 8) + *seed * 97) + 23) ^ -1496474763;
*seed = ((tmp >> 16) & 65535) | (tmp << 16);
return *seed & 32767;
}