Rizin
unix-like reverse engineering framework and cli tools
crypto_cps2.c File Reference
#include <rz_lib.h>
#include <rz_crypto.h>
#include <rz_util.h>

Go to the source code of this file.

Classes

struct  sbox
 
struct  optimised_sbox
 

Macros

#define UPPER_LIMIT   0x400000
 
#define BIT(x, n)   (((x) >> (n)) & 1)
 
#define BITSWAP8(val, B7, B6, B5, B4, B3, B2, B1, B0)
 

Functions

static ut8 fn (ut8 in, const struct optimised_sbox *sboxes, ut32 key)
 
static void expand_1st_key (ut32 *dstkey, const ut32 *srckey)
 
static void expand_2nd_key (ut32 *dstkey, const ut32 *srckey)
 
static void expand_subkey (ut32 *subkey, ut16 seed)
 
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 int extract_inputs (ut32 val, const int *inputs)
 
static void optimise_sboxes (struct optimised_sbox *out, const struct sbox *in)
 
static void cps2_crypt (int dir, const ut16 *rom, ut16 *dec, int length, const ut32 *master_key, ut32 upper_limit)
 
static bool set_key (RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
 
static int get_key_size (RzCrypto *cry)
 
static bool cps2_use (const char *algo)
 
static bool update (RzCrypto *cry, const ut8 *buf, int len)
 
static bool cps2_init (RzCrypto *cry)
 
static bool cps2_fini (RzCrypto *cry)
 

Variables

static const int fn1_groupA [8] = { 10, 4, 6, 7, 2, 13, 15, 14 }
 
static const int fn1_groupB [8] = { 0, 1, 3, 5, 8, 9, 11, 12 }
 
static const int fn2_groupA [8] = { 6, 0, 2, 13, 1, 4, 14, 7 }
 
static const int fn2_groupB [8] = { 3, 5, 9, 10, 8, 15, 12, 11 }
 
static const struct sbox fn1_r1_boxes [4]
 
static const struct sbox fn1_rz_boxes [4]
 
static const struct sbox fn1_r3_boxes [4]
 
static const struct sbox fn1_r4_boxes [4]
 
static const struct sbox fn2_r1_boxes [4]
 
static const struct sbox fn2_rz_boxes [4]
 
static const struct sbox fn2_r3_boxes [4]
 
static const struct sbox fn2_r4_boxes [4]
 
RzCryptoPlugin rz_crypto_plugin_cps2
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ BIT

#define BIT (   x,
  n 
)    (((x) >> (n)) & 1)

Definition at line 9 of file crypto_cps2.c.

◆ BITSWAP8

#define BITSWAP8 (   val,
  B7,
  B6,
  B5,
  B4,
  B3,
  B2,
  B1,
  B0 
)
Value:
((BIT(val, B7) << 7) | (BIT(val, B6) << 6) | (BIT(val, B5) << 5) | (BIT(val, B4) << 4) | \
(BIT(val, B3) << 3) | (BIT(val, B2) << 2) | (BIT(val, B1) << 1) | (BIT(val, B0) << 0))
#define B2(x)
Definition: aes-internal.h:82
#define B1(x)
Definition: aes-internal.h:81
#define B3(x)
Definition: aes-internal.h:83
ut16 val
Definition: armass64_const.h:6
#define BIT(x, n)
Definition: crypto_cps2.c:9
#define B4(a, b, c, d)
#define B0
Definition: sftypes.h:920

Definition at line 11 of file crypto_cps2.c.

◆ UPPER_LIMIT

#define UPPER_LIMIT   0x400000

Definition at line 7 of file crypto_cps2.c.

Function Documentation

◆ cps2_crypt()

static void cps2_crypt ( int  dir,
const ut16 rom,
ut16 dec,
int  length,
const ut32 master_key,
ut32  upper_limit 
)
static

Definition at line 2777 of file crypto_cps2.c.

2777  {
2778  int i;
2779  ut32 key1[4];
2780  struct optimised_sbox sboxes1[4 * 4];
2781  struct optimised_sbox sboxes2[4 * 4];
2782 
2783  optimise_sboxes(&sboxes1[0 * 4], fn1_r1_boxes);
2784  optimise_sboxes(&sboxes1[1 * 4], fn1_rz_boxes);
2785  optimise_sboxes(&sboxes1[2 * 4], fn1_r3_boxes);
2786  optimise_sboxes(&sboxes1[3 * 4], fn1_r4_boxes);
2787  optimise_sboxes(&sboxes2[0 * 4], fn2_r1_boxes);
2788  optimise_sboxes(&sboxes2[1 * 4], fn2_rz_boxes);
2789  optimise_sboxes(&sboxes2[2 * 4], fn2_r3_boxes);
2790  optimise_sboxes(&sboxes2[3 * 4], fn2_r4_boxes);
2791 
2792  // expand master key to 1st FN 96-bit key
2793  expand_1st_key(key1, master_key);
2794 
2795  // add extra bits for s-boxes with less than 6 inputs
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;
2803 
2804  for (i = 0; i < 0x10000; i++) {
2805  int a;
2806  ut16 seed;
2807  ut32 subkey[2];
2808  ut32 key2[4];
2809 
2810  if ((i & 0xff) == 0) {
2811  eprintf("Crypting %d%%\r", i * 100 / 0x10000);
2812  }
2813 
2814  // pass the address through FN1
2815  seed = feistel(i, fn1_groupA, fn1_groupB,
2816  &sboxes1[0 * 4], &sboxes1[1 * 4], &sboxes1[2 * 4], &sboxes1[3 * 4],
2817  key1[0], key1[1], key1[2], key1[3]);
2818 
2819  // expand the result to 64-bit
2820  expand_subkey(subkey, seed);
2821 
2822  // XOR with the master key
2823  subkey[0] ^= master_key[0];
2824  subkey[1] ^= master_key[1];
2825 
2826  // expand key to 2nd FN 96-bit key
2827  expand_2nd_key(key2, subkey);
2828 
2829  // add extra bits for s-boxes with less than 6 inputs
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;
2838 
2839  // de/en-crypt the opcodes
2840  for (a = i; a < length && a < upper_limit / 2; a += 0x10000) {
2841  if (dir) {
2842  /* decrypt */
2843  dec[a] = feistel(rom[a], fn2_groupA, fn2_groupB,
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);
2847  } else {
2848  /* encrypt */
2849  dec[a] = (rom[a] << 8) | (rom[a] >> 8);
2850  dec[a] = feistel(dec[a], fn2_groupA, fn2_groupB,
2851  &sboxes2[3 * 4], &sboxes2[2 * 4], &sboxes2[1 * 4], &sboxes2[0 * 4],
2852  key2[3], key2[2], key2[1], key2[0]);
2853  }
2854  }
2855  // copy the unencrypted part
2856  while (a < length) {
2857  dec[a] = (rom[a] << 8) | (rom[a] >> 8);
2858  a += 0x10000;
2859  }
2860  }
2861 }
lzma_index ** i
Definition: index.h:629
static const struct sbox fn1_r4_boxes[4]
Definition: crypto_cps2.c:997
static const struct sbox fn1_rz_boxes[4]
Definition: crypto_cps2.c:439
static const struct sbox fn2_r1_boxes[4]
Definition: crypto_cps2.c:1278
static const int fn1_groupB[8]
Definition: crypto_cps2.c:135
static const int fn2_groupB[8]
Definition: crypto_cps2.c:138
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)
Definition: crypto_cps2.c:2715
static const int fn1_groupA[8]
Definition: crypto_cps2.c:134
static const struct sbox fn2_rz_boxes[4]
Definition: crypto_cps2.c:1557
static const struct sbox fn1_r1_boxes[4]
Definition: crypto_cps2.c:160
static const struct sbox fn2_r3_boxes[4]
Definition: crypto_cps2.c:1836
static const struct sbox fn1_r3_boxes[4]
Definition: crypto_cps2.c:718
static void optimise_sboxes(struct optimised_sbox *out, const struct sbox *in)
Definition: crypto_cps2.c:2755
static const int fn2_groupA[8]
Definition: crypto_cps2.c:137
static const struct sbox fn2_r4_boxes[4]
Definition: crypto_cps2.c:2115
static void expand_2nd_key(ut32 *dstkey, const ut32 *srckey)
Definition: crypto_cps2.c:2523
static void expand_1st_key(ut32 *dstkey, const ut32 *srckey)
Definition: crypto_cps2.c:2410
static void expand_subkey(ut32 *subkey, ut16 seed)
Definition: crypto_cps2.c:2637
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
Definition: sflib.h:133
uint16_t ut16
uint32_t ut32
#define eprintf(x, y...)
Definition: rlcc.c:7
#define a(i)
Definition: sha256.c:41

References a, BIT, eprintf, expand_1st_key(), expand_2nd_key(), expand_subkey(), feistel(), fn1_groupA, fn1_groupB, fn1_r1_boxes, fn1_r3_boxes, fn1_r4_boxes, fn1_rz_boxes, fn2_groupA, fn2_groupB, fn2_r1_boxes, fn2_r3_boxes, fn2_r4_boxes, fn2_rz_boxes, i, length, and optimise_sboxes().

Referenced by update().

◆ cps2_fini()

static bool cps2_fini ( RzCrypto cry)
static

Definition at line 2947 of file crypto_cps2.c.

2947  {
2948  rz_return_val_if_fail(cry, false);
2949 
2950  free(cry->user);
2951  return true;
2952 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
void * user
Definition: rz_crypto.h:36

References free(), rz_return_val_if_fail, and rz_crypto_t::user.

◆ cps2_init()

static bool cps2_init ( RzCrypto cry)
static

Definition at line 2940 of file crypto_cps2.c.

2940  {
2941  rz_return_val_if_fail(cry, false);
2942 
2943  cry->user = RZ_NEWS0(ut32, 2);
2944  return cry->user != NULL;
2945 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282

References NULL, RZ_NEWS0, rz_return_val_if_fail, and rz_crypto_t::user.

◆ cps2_use()

static bool cps2_use ( const char *  algo)
static

Definition at line 2908 of file crypto_cps2.c.

2908  {
2909  return !strcmp(algo, "cps2");
2910 }

◆ expand_1st_key()

static void expand_1st_key ( ut32 dstkey,
const ut32 srckey 
)
static

Definition at line 2410 of file crypto_cps2.c.

2410  {
2411  static const int bits[96] = {
2412  33,
2413  58,
2414  49,
2415  36,
2416  0,
2417  31,
2418  22,
2419  30,
2420  3,
2421  16,
2422  5,
2423  53,
2424  10,
2425  41,
2426  23,
2427  19,
2428  27,
2429  39,
2430  43,
2431  6,
2432  34,
2433  12,
2434  61,
2435  21,
2436  48,
2437  13,
2438  32,
2439  35,
2440  6,
2441  42,
2442  43,
2443  14,
2444  21,
2445  41,
2446  52,
2447  25,
2448  18,
2449  47,
2450  46,
2451  37,
2452  57,
2453  53,
2454  20,
2455  8,
2456  55,
2457  54,
2458  59,
2459  60,
2460  27,
2461  33,
2462  35,
2463  18,
2464  8,
2465  15,
2466  63,
2467  1,
2468  50,
2469  44,
2470  16,
2471  46,
2472  5,
2473  4,
2474  45,
2475  51,
2476  38,
2477  25,
2478  13,
2479  11,
2480  62,
2481  29,
2482  48,
2483  2,
2484  59,
2485  61,
2486  62,
2487  56,
2488  51,
2489  57,
2490  54,
2491  9,
2492  24,
2493  63,
2494  22,
2495  7,
2496  26,
2497  42,
2498  45,
2499  40,
2500  23,
2501  14,
2502  2,
2503  31,
2504  52,
2505  28,
2506  44,
2507  17,
2508  };
2509  int i;
2510 
2511  dstkey[0] = 0;
2512  dstkey[1] = 0;
2513  dstkey[2] = 0;
2514  dstkey[3] = 0;
2515 
2516  for (i = 0; i < 96; i++) {
2517  dstkey[i / 24] |= BIT(srckey[bits[i] / 32], bits[i] % 32) << (i % 24);
2518  }
2519 }
int bits(struct state *s, int need)
Definition: blast.c:72

References BIT, bits(), and i.

Referenced by cps2_crypt().

◆ expand_2nd_key()

static void expand_2nd_key ( ut32 dstkey,
const ut32 srckey 
)
static

Definition at line 2523 of file crypto_cps2.c.

2523  {
2524  static const int bits[96] = {
2525  34,
2526  9,
2527  32,
2528  24,
2529  44,
2530  54,
2531  38,
2532  61,
2533  47,
2534  13,
2535  28,
2536  7,
2537  29,
2538  58,
2539  18,
2540  1,
2541  20,
2542  60,
2543  15,
2544  6,
2545  11,
2546  43,
2547  39,
2548  19,
2549  63,
2550  23,
2551  16,
2552  62,
2553  54,
2554  40,
2555  31,
2556  3,
2557  56,
2558  61,
2559  17,
2560  25,
2561  47,
2562  38,
2563  55,
2564  57,
2565  5,
2566  4,
2567  15,
2568  42,
2569  22,
2570  7,
2571  2,
2572  19,
2573  46,
2574  37,
2575  29,
2576  39,
2577  12,
2578  30,
2579  49,
2580  57,
2581  31,
2582  41,
2583  26,
2584  27,
2585  24,
2586  36,
2587  11,
2588  63,
2589  33,
2590  16,
2591  56,
2592  62,
2593  48,
2594  60,
2595  59,
2596  32,
2597  12,
2598  30,
2599  53,
2600  48,
2601  10,
2602  0,
2603  50,
2604  35,
2605  3,
2606  59,
2607  14,
2608  49,
2609  51,
2610  45,
2611  44,
2612  2,
2613  21,
2614  33,
2615  55,
2616  52,
2617  23,
2618  28,
2619  8,
2620  26,
2621  };
2622  int i;
2623 
2624  dstkey[0] = 0;
2625  dstkey[1] = 0;
2626  dstkey[2] = 0;
2627  dstkey[3] = 0;
2628 
2629  for (i = 0; i < 96; i++) {
2630  dstkey[i / 24] |= BIT(srckey[bits[i] / 32], bits[i] % 32) << (i % 24);
2631  }
2632 }

References BIT, bits(), and i.

Referenced by cps2_crypt().

◆ expand_subkey()

static void expand_subkey ( ut32 subkey,
ut16  seed 
)
static

Definition at line 2637 of file crypto_cps2.c.

2637  {
2638  // Note that each row of the table is a permutation of the seed bits.
2639  static const int bits[64] = {
2640  5,
2641  10,
2642  14,
2643  9,
2644  4,
2645  0,
2646  15,
2647  6,
2648  1,
2649  8,
2650  3,
2651  2,
2652  12,
2653  7,
2654  13,
2655  11,
2656  5,
2657  12,
2658  7,
2659  2,
2660  13,
2661  11,
2662  9,
2663  14,
2664  4,
2665  1,
2666  6,
2667  10,
2668  8,
2669  0,
2670  15,
2671  3,
2672  4,
2673  10,
2674  2,
2675  0,
2676  6,
2677  9,
2678  12,
2679  1,
2680  11,
2681  7,
2682  15,
2683  8,
2684  13,
2685  5,
2686  14,
2687  3,
2688  14,
2689  11,
2690  12,
2691  7,
2692  4,
2693  5,
2694  2,
2695  10,
2696  1,
2697  15,
2698  0,
2699  9,
2700  8,
2701  6,
2702  13,
2703  3,
2704  };
2705  int i;
2706 
2707  subkey[0] = 0;
2708  subkey[1] = 0;
2709 
2710  for (i = 0; i < 64; i++) {
2711  subkey[i / 32] |= BIT(seed, bits[i]) << (i % 32);
2712  }
2713 }

References BIT, bits(), and i.

Referenced by cps2_crypt().

◆ extract_inputs()

static int extract_inputs ( ut32  val,
const int inputs 
)
static

Definition at line 2745 of file crypto_cps2.c.

2745  {
2746  int i, res = 0;
2747  for (i = 0; i < 6; i++) {
2748  if (inputs[i] != -1) {
2749  res |= BIT(val, inputs[i]) << i;
2750  }
2751  }
2752  return res;
2753 }

References BIT, i, and val.

Referenced by optimise_sboxes().

◆ feistel()

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 
)
inlinestatic

Definition at line 2715 of file crypto_cps2.c.

2718  {
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]);
2721 
2722  l ^= fn(r, boxes1, key1);
2723  r ^= fn(l, boxes2, key2);
2724  l ^= fn(r, boxes3, key3);
2725  r ^= fn(l, boxes4, key4);
2726 
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]);
2743 }
#define BITSWAP8(val, B7, B6, B5, B4, B3, B2, B1, B0)
Definition: crypto_cps2.c:11
static ut8 fn(ut8 in, const struct optimised_sbox *sboxes, ut32 key)
Definition: crypto_cps2.c:2396
#define r
Definition: crypto_rc6.c:12
uint8_t ut8
Definition: lh5801.h:11

References BIT, BITSWAP8, fn(), r, and val.

Referenced by cps2_crypt().

◆ fn()

static ut8 fn ( ut8  in,
const struct optimised_sbox sboxes,
ut32  key 
)
static

Definition at line 2396 of file crypto_cps2.c.

2396  {
2397  const struct optimised_sbox *sbox1 = &sboxes[0];
2398  const struct optimised_sbox *sbox2 = &sboxes[1];
2399  const struct optimised_sbox *sbox3 = &sboxes[2];
2400  const struct optimised_sbox *sbox4 = &sboxes[3];
2401 
2402  return sbox1->output[sbox1->input_lookup[in] ^ ((key >> 0) & 0x3f)] |
2403  sbox2->output[sbox2->input_lookup[in] ^ ((key >> 6) & 0x3f)] |
2404  sbox3->output[sbox3->input_lookup[in] ^ ((key >> 12) & 0x3f)] |
2405  sbox4->output[sbox4->input_lookup[in] ^ ((key >> 18) & 0x3f)];
2406 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
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
Definition: sflib.h:118
static const ut32 sbox3[64]
Definition: des.c:37
static const ut32 sbox1[64]
Definition: des.c:15
static const ut32 sbox4[64]
Definition: des.c:48
static const ut32 sbox2[64]
Definition: des.c:26

References in, key, sbox1, sbox2, sbox3, and sbox4.

Referenced by feistel().

◆ get_key_size()

static int get_key_size ( RzCrypto cry)
static

Definition at line 2903 of file crypto_cps2.c.

2903  {
2904  /* 64bit key */
2905  return 8;
2906 }

◆ optimise_sboxes()

static void optimise_sboxes ( struct optimised_sbox out,
const struct sbox in 
)
static

Definition at line 2755 of file crypto_cps2.c.

2755  {
2756  int i, box;
2757 
2758  for (box = 0; box < 4; box++) {
2759  // precalculate the input lookup
2760  for (i = 0; i < 256; i++) {
2761  out[box].input_lookup[i] = extract_inputs(i, in[box].inputs);
2762  }
2763  // precalculate the output masks
2764  for (i = 0; i < 64; i++) {
2765  int o = in[box].table[i];
2766  out[box].output[i] = 0;
2767  if (o & 1) {
2768  out[box].output[i] |= 1 << in[box].outputs[0];
2769  }
2770  if (o & 2) {
2771  out[box].output[i] |= 1 << in[box].outputs[1];
2772  }
2773  }
2774  }
2775 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int extract_inputs(ut32 val, const int *inputs)
Definition: crypto_cps2.c:2745

References extract_inputs(), i, in, and out.

Referenced by cps2_crypt().

◆ set_key()

static bool set_key ( RzCrypto cry,
const ut8 key,
int  keylen,
int  mode,
int  direction 
)
static

Definition at line 2888 of file crypto_cps2.c.

2888  {
2889  rz_return_val_if_fail(cry->user && key, false);
2890  ut32 *cps2key = (ut32 *)cry->user;
2891 
2892  cry->dir = direction;
2893  if (keylen == 8) {
2894  /* fix key endianness */
2895  const ut32 *key32 = (const ut32 *)key;
2896  cps2key[0] = rz_read_be32(key32);
2897  cps2key[1] = rz_read_be32(key32 + 1);
2898  return true;
2899  }
2900  return false;
2901 }
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References rz_crypto_t::dir, if(), key, rz_read_be32(), rz_return_val_if_fail, and rz_crypto_t::user.

◆ update()

static bool update ( RzCrypto cry,
const ut8 buf,
int  len 
)
static

Definition at line 2912 of file crypto_cps2.c.

2912  {
2913  rz_return_val_if_fail(cry->user && len > 0, false);
2914  ut32 *cps2key = (ut32 *)cry->user;
2915 
2916  size_t slen = len / 2;
2917  ut16 *output = RZ_NEWS0(ut16, slen);
2918  if (!output) {
2919  return false;
2920  }
2921  ut16 *input = RZ_NEWS0(ut16, slen);
2922  if (!input) {
2923  free(output);
2924  return false;
2925  }
2926  for (size_t i = 0; i < slen; i++) {
2927  input[i] = rz_read_at_le16(buf, i * 2);
2928  }
2929  /* TODO : control decryption errors */
2930  cps2_crypt(cry->dir, input, output, slen, cps2key, UPPER_LIMIT);
2931  for (size_t i = 0; i < slen; i++) {
2932  rz_write_at_le16((ut8 *)output, output[i], i * 2);
2933  }
2934  rz_crypto_append(cry, (const ut8 *)output, slen * 2);
2935  free(output);
2936  free(input);
2937  return true;
2938 }
size_t len
Definition: 6502dis.c:15
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
static void cps2_crypt(int dir, const ut16 *rom, ut16 *dec, int length, const ut32 *master_key, ut32 upper_limit)
Definition: crypto_cps2.c:2777
#define UPPER_LIMIT
Definition: crypto_cps2.c:7
voidpf void * buf
Definition: ioapi.h:138
static ut16 rz_read_at_le16(const void *src, size_t offset)
Definition: rz_endian.h:214
static void rz_write_at_le16(void *dest, ut16 val, size_t offset)
Definition: rz_endian.h:227
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237

References cps2_crypt(), rz_crypto_t::dir, free(), i, input(), len, output, rz_crypto_append(), RZ_NEWS0, rz_read_at_le16(), rz_return_val_if_fail, rz_write_at_le16(), UPPER_LIMIT, and rz_crypto_t::user.

Variable Documentation

◆ fn1_groupA

const int fn1_groupA[8] = { 10, 4, 6, 7, 2, 13, 15, 14 }
static

Definition at line 134 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn1_groupB

const int fn1_groupB[8] = { 0, 1, 3, 5, 8, 9, 11, 12 }
static

Definition at line 135 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn1_r1_boxes

const struct sbox fn1_r1_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn1_r3_boxes

const struct sbox fn1_r3_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn1_r4_boxes

const struct sbox fn1_r4_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn1_rz_boxes

const struct sbox fn1_rz_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_groupA

const int fn2_groupA[8] = { 6, 0, 2, 13, 1, 4, 14, 7 }
static

Definition at line 137 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_groupB

const int fn2_groupB[8] = { 3, 5, 9, 10, 8, 15, 12, 11 }
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_r1_boxes

const struct sbox fn2_r1_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_r3_boxes

const struct sbox fn2_r3_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_r4_boxes

const struct sbox fn2_r4_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ fn2_rz_boxes

const struct sbox fn2_rz_boxes[4]
static

Definition at line 138 of file crypto_cps2.c.

Referenced by cps2_crypt().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.version = RZ_VERSION
}
RzCryptoPlugin rz_crypto_plugin_rol
Definition: crypto_rol.c:89
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
#define RZ_VERSION
Definition: rz_version.h:8

Definition at line 2967 of file crypto_cps2.c.

◆ rz_crypto_plugin_cps2

RzCryptoPlugin rz_crypto_plugin_cps2
Initial value:
= {
.name = "cps2",
.author = "pancake,esanfelix,pof",
.license = "LGPL-3",
.set_key = set_key,
.get_key_size = get_key_size,
.use = cps2_use,
.update = update,
.init = cps2_init,
.fini = cps2_fini,
}
static bool set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_cps2.c:2888
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_cps2.c:2912
static int get_key_size(RzCrypto *cry)
Definition: crypto_cps2.c:2903
static bool cps2_use(const char *algo)
Definition: crypto_cps2.c:2908
static bool cps2_fini(RzCrypto *cry)
Definition: crypto_cps2.c:2947
static bool cps2_init(RzCrypto *cry)
Definition: crypto_cps2.c:2940

Definition at line 2954 of file crypto_cps2.c.