7 #define UPPER_LIMIT 0x400000
9 #define BIT(x, n) (((x) >> (n)) & 1)
11 #define BITSWAP8(val, B7, B6, B5, B4, B3, B2, B1, B0) \
12 ((BIT(val, B7) << 7) | (BIT(val, B6) << 6) | (BIT(val, B5) << 5) | (BIT(val, B4) << 4) | \
13 (BIT(val, B3) << 3) | (BIT(val, B2) << 2) | (BIT(val, B1) << 1) | (BIT(val, B0) << 0))
134 static const int fn1_groupA[8] = { 10, 4, 6, 7, 2, 13, 15, 14 };
135 static const int fn1_groupB[8] = { 0, 1, 3, 5, 8, 9, 11, 12 };
137 static const int fn2_groupA[8] = { 6, 0, 2, 13, 1, 4, 14, 7 };
138 static const int fn2_groupB[8] = { 3, 5, 9, 10, 8, 15, 12, 11 };
228 { 3, 4, 5, 6, -1, -1 },
297 { 0, 1, 2, 4, 7, -1 },
366 { 0, 1, 2, 3, 6, 7 },
435 { 0, 1, 3, 5, 6, 7 },
507 { 0, 1, 2, 3, 6, -1 },
576 { 2, 4, 5, 6, 7, -1 },
645 { 1, 2, 3, 4, 5, 7 },
714 { 0, 1, 3, 4, 6, 7 },
786 { 0, 1, 5, 6, 7, -1 },
855 { 2, 3, 4, 6, 7, -1 },
924 { 0, 2, 3, 4, 5, 6 },
993 { 0, 1, 2, 3, 5, 7 },
1065 { 1, 2, 3, 4, 5, 7 },
1134 { 0, 1, 2, 3, 5, 6 },
1203 { 0, 2, 4, 5, 6, 7 },
1272 { 0, 1, 3, 4, 6, 7 },
1346 { 0, 3, 4, 5, 7, -1 },
1415 { 1, 2, 3, 4, 6, -1 },
1484 { 1, 2, 4, 5, 6, 7 },
1553 { 0, 2, 3, 5, 6, 7 },
1625 { 0, 2, 4, 6, -1, -1 },
1694 { 1, 3, 4, 5, 6, 7 },
1763 { 0, 1, 3, 4, 5, 7 },
1832 { 1, 2, 3, 5, 6, 7 },
1904 { 2, 3, 4, 6, -1, -1 },
1973 { 0, 1, 3, 5, 7, -1 },
2042 { 0, 1, 2, 3, 5, 7 },
2111 { 0, 2, 4, 5, 6, 7 },
2183 { 0, 1, 3, 6, 7, -1 },
2252 { 0, 1, 2, 4, 5, 6 },
2321 { 0, 2, 3, 4, 5, 7 },
2390 { 2, 3, 4, 5, 6, 7 },
2411 static const int bits[96] = {
2516 for (
i = 0;
i < 96;
i++) {
2517 dstkey[
i / 24] |=
BIT(srckey[
bits[
i] / 32],
bits[
i] % 32) << (
i % 24);
2524 static const int bits[96] = {
2629 for (
i = 0;
i < 96;
i++) {
2630 dstkey[
i / 24] |=
BIT(srckey[
bits[
i] / 32],
bits[
i] % 32) << (
i % 24);
2639 static const int bits[64] = {
2710 for (
i = 0;
i < 64;
i++) {
2711 subkey[
i / 32] |=
BIT(seed,
bits[
i]) << (
i % 32);
2719 ut8 l =
BITSWAP8(
val, bitsB[7], bitsB[6], bitsB[5], bitsB[4], bitsB[3], bitsB[2], bitsB[1], bitsB[0]);
2720 ut8 r =
BITSWAP8(
val, bitsA[7], bitsA[6], bitsA[5], bitsA[4], bitsA[3], bitsA[2], bitsA[1], bitsA[0]);
2722 l ^=
fn(
r, boxes1, key1);
2723 r ^=
fn(l, boxes2, key2);
2724 l ^=
fn(
r, boxes3, key3);
2725 r ^=
fn(l, boxes4, key4);
2727 return (
BIT(l, 0) << bitsA[0]) |
2728 (
BIT(l, 1) << bitsA[1]) |
2729 (
BIT(l, 2) << bitsA[2]) |
2730 (
BIT(l, 3) << bitsA[3]) |
2731 (
BIT(l, 4) << bitsA[4]) |
2732 (
BIT(l, 5) << bitsA[5]) |
2733 (
BIT(l, 6) << bitsA[6]) |
2734 (
BIT(l, 7) << bitsA[7]) |
2735 (
BIT(
r, 0) << bitsB[0]) |
2736 (
BIT(
r, 1) << bitsB[1]) |
2737 (
BIT(
r, 2) << bitsB[2]) |
2738 (
BIT(
r, 3) << bitsB[3]) |
2739 (
BIT(
r, 4) << bitsB[4]) |
2740 (
BIT(
r, 5) << bitsB[5]) |
2741 (
BIT(
r, 6) << bitsB[6]) |
2742 (
BIT(
r, 7) << bitsB[7]);
2747 for (
i = 0;
i < 6;
i++) {
2748 if (inputs[
i] != -1) {
2758 for (box = 0; box < 4; box++) {
2760 for (
i = 0;
i < 256;
i++) {
2764 for (
i = 0;
i < 64;
i++) {
2765 int o =
in[box].table[
i];
2766 out[box].output[
i] = 0;
2768 out[box].output[
i] |= 1 <<
in[box].outputs[0];
2771 out[box].output[
i] |= 1 <<
in[box].outputs[1];
2796 key1[0] ^=
BIT(key1[0], 1) << 4;
2797 key1[0] ^=
BIT(key1[0], 2) << 5;
2798 key1[0] ^=
BIT(key1[0], 8) << 11;
2799 key1[1] ^=
BIT(key1[1], 0) << 5;
2800 key1[1] ^=
BIT(key1[1], 8) << 11;
2801 key1[2] ^=
BIT(key1[2], 1) << 5;
2802 key1[2] ^=
BIT(key1[2], 8) << 11;
2804 for (
i = 0;
i < 0x10000;
i++) {
2810 if ((
i & 0xff) == 0) {
2811 eprintf(
"Crypting %d%%\r",
i * 100 / 0x10000);
2816 &sboxes1[0 * 4], &sboxes1[1 * 4], &sboxes1[2 * 4], &sboxes1[3 * 4],
2817 key1[0], key1[1], key1[2], key1[3]);
2823 subkey[0] ^= master_key[0];
2824 subkey[1] ^= master_key[1];
2830 key2[0] ^=
BIT(key2[0], 0) << 5;
2831 key2[0] ^=
BIT(key2[0], 6) << 11;
2832 key2[1] ^=
BIT(key2[1], 0) << 5;
2833 key2[1] ^=
BIT(key2[1], 1) << 4;
2834 key2[2] ^=
BIT(key2[2], 2) << 5;
2835 key2[2] ^=
BIT(key2[2], 3) << 4;
2836 key2[2] ^=
BIT(key2[2], 7) << 11;
2837 key2[3] ^=
BIT(key2[3], 1) << 5;
2840 for (
a =
i;
a <
length &&
a < upper_limit / 2;
a += 0x10000) {
2844 &sboxes2[0 * 4], &sboxes2[1 * 4], &sboxes2[2 * 4], &sboxes2[3 * 4],
2845 key2[0], key2[1], key2[2], key2[3]);
2846 dec[
a] = (dec[
a] << 8) | (dec[
a] >> 8);
2849 dec[
a] = (rom[
a] << 8) | (rom[
a] >> 8);
2851 &sboxes2[3 * 4], &sboxes2[2 * 4], &sboxes2[1 * 4], &sboxes2[0 * 4],
2852 key2[3], key2[2], key2[1], key2[0]);
2857 dec[
a] = (rom[
a] << 8) | (rom[
a] >> 8);
2864 main(cps_state,cps2crypt) {
2869 std::string skey1 = parameter(
"cryptkey1");;
2870 key[0] = strtoll(skey1.c_str(),
nullptr, 16);
2872 std::string skey2 = parameter(
"cryptkey2");
2873 key[1] = strtoll(skey2.c_str(),
nullptr, 16);
2875 std::string slower = parameter(
"cryptlower");
2876 lower = strtoll(slower.c_str(),
nullptr, 16);
2878 std::string supper = parameter(
"cryptupper");
2879 upper = strtoll(supper.c_str(),
nullptr, 16);
2882 if (lower != 0xff0000) {
2883 cps2_decrypt( (
ut16 *)memregion(
"maincpu")->base(), m_decrypted_opcodes, memregion(
"maincpu")->
bytes(),
key, lower,upper);
2892 cry->
dir = direction;
2909 return !strcmp(algo,
"cps2");
2916 size_t slen =
len / 2;
2926 for (
size_t i = 0;
i < slen;
i++) {
2931 for (
size_t i = 0;
i < slen;
i++) {
2956 .author =
"pancake,esanfelix,pof",
2957 .license =
"LGPL-3",
2966 #ifndef RZ_PLUGIN_INCORE
int bits(struct state *s, int need)
const lzma_allocator const uint8_t * in
const lzma_allocator const uint8_t size_t uint8_t * out
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
static const struct sbox fn1_r4_boxes[4]
#define BITSWAP8(val, B7, B6, B5, B4, B3, B2, B1, B0)
static const struct sbox fn1_rz_boxes[4]
static ut8 fn(ut8 in, const struct optimised_sbox *sboxes, ut32 key)
static bool set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
static const struct sbox fn2_r1_boxes[4]
static const int fn1_groupB[8]
static const int fn2_groupB[8]
static ut16 feistel(ut16 val, const int *bitsA, const int *bitsB, const struct optimised_sbox *boxes1, const struct optimised_sbox *boxes2, const struct optimised_sbox *boxes3, const struct optimised_sbox *boxes4, ut32 key1, ut32 key2, ut32 key3, ut32 key4)
static bool update(RzCrypto *cry, const ut8 *buf, int len)
static void cps2_crypt(int dir, const ut16 *rom, ut16 *dec, int length, const ut32 *master_key, ut32 upper_limit)
static const int fn1_groupA[8]
static int get_key_size(RzCrypto *cry)
static const struct sbox fn2_rz_boxes[4]
static const struct sbox fn1_r1_boxes[4]
static const struct sbox fn2_r3_boxes[4]
static const struct sbox fn1_r3_boxes[4]
static bool cps2_use(const char *algo)
RzCryptoPlugin rz_crypto_plugin_cps2
static void optimise_sboxes(struct optimised_sbox *out, const struct sbox *in)
static const int fn2_groupA[8]
RZ_API RzLibStruct rizin_plugin
static const struct sbox fn2_r4_boxes[4]
static void expand_2nd_key(ut32 *dstkey, const ut32 *srckey)
static void expand_1st_key(ut32 *dstkey, const ut32 *srckey)
static bool cps2_fini(RzCrypto *cry)
static bool cps2_init(RzCrypto *cry)
static void expand_subkey(ut32 *subkey, ut16 seed)
static int extract_inputs(ut32 val, const int *inputs)
RzCryptoPlugin rz_crypto_plugin_rol
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
static const ut32 sbox3[64]
static const ut32 sbox1[64]
static const ut32 sbox4[64]
static const ut32 sbox2[64]
RZ_API void Ht_() free(HtName_(Ht) *ht)
int main(int argc, char **argv)
#define rz_return_val_if_fail(expr, val)
static ut16 rz_read_at_le16(const void *src, size_t offset)
static void rz_write_at_le16(void *dest, ut16 val, size_t offset)
static ut32 rz_read_be32(const void *src)
if(dbg->bits==RZ_SYS_BITS_64)
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)