Rizin
unix-like reverse engineering framework and cli tools
crypto_ror.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_lib.h>
5 #include <rz_crypto.h>
6 #include <rz_util.h>
7 
8 #define NAME "ror"
9 
10 enum { MAX_ror_KEY_SIZE = 32768 };
11 
12 struct ror_state {
14  int key_size;
15 };
16 
17 static bool ror_init_state(struct ror_state *const state, const ut8 *key, int keylen) {
18  if (!state || !key || keylen < 1 || keylen > MAX_ror_KEY_SIZE) {
19  return false;
20  }
21  int i;
22  state->key_size = keylen;
23  for (i = 0; i < keylen; i++) {
24  state->key[i] = key[i];
25  }
26  return true;
27 }
28 
29 static void ror_crypt(struct ror_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen) {
30  int i;
31  for (i = 0; i < buflen; i++) {
32  ut8 count = state->key[i % state->key_size] & 7;
33  ut8 inByte = inbuf[i];
34  outbuf[i] = (inByte >> count) | (inByte << ((8 - count) & 7));
35  }
36 }
37 
38 static bool ror_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
39  rz_return_val_if_fail(cry->user && key, false);
40  struct ror_state *st = (struct ror_state *)cry->user;
41 
42  cry->dir = direction;
43  return ror_init_state(st, key, keylen);
44 }
45 
46 static int ror_get_key_size(RzCrypto *cry) {
47  rz_return_val_if_fail(cry->user, 0);
48  struct ror_state *st = (struct ror_state *)cry->user;
49 
50  return st->key_size;
51 }
52 
53 static bool ror_use(const char *algo) {
54  return !strcmp(algo, NAME);
55 }
56 
57 static bool update(RzCrypto *cry, const ut8 *buf, int len) {
58  rz_return_val_if_fail(cry->user, false);
59  struct ror_state *st = (struct ror_state *)cry->user;
60 
61  if (cry->dir) {
62  eprintf("Use ROL algorithm to decrypt\n");
63  return false;
64  }
65  ut8 *obuf = calloc(1, len);
66  if (!obuf) {
67  return false;
68  }
69  ror_crypt(st, buf, obuf, len);
70  rz_crypto_append(cry, obuf, len);
71  free(obuf);
72  return true;
73 }
74 
75 static bool ror_init(RzCrypto *cry) {
76  rz_return_val_if_fail(cry, false);
77 
78  cry->user = RZ_NEW0(struct ror_state);
79  return cry->user != NULL;
80 }
81 
82 static bool ror_fini(RzCrypto *cry) {
83  rz_return_val_if_fail(cry, false);
84 
85  free(cry->user);
86  return true;
87 }
88 
90  .name = NAME,
91  .author = "pancake",
92  .license = "LGPL-3",
93  .set_key = ror_set_key,
94  .get_key_size = ror_get_key_size,
95  .use = ror_use,
96  .update = update,
97  .final = update,
98  .init = ror_init,
99  .fini = ror_fini,
100 };
101 
102 #ifndef RZ_PLUGIN_INCORE
105  .data = &rz_crypto_plugin_ror,
106  .version = RZ_VERSION
107 };
108 #endif
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
static int ror_get_key_size(RzCrypto *cry)
Definition: crypto_ror.c:46
static bool ror_fini(RzCrypto *cry)
Definition: crypto_ror.c:82
#define NAME
Definition: crypto_ror.c:8
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_ror.c:57
@ MAX_ror_KEY_SIZE
Definition: crypto_ror.c:10
static bool ror_use(const char *algo)
Definition: crypto_ror.c:53
RZ_API RzLibStruct rizin_plugin
Definition: crypto_ror.c:103
RzCryptoPlugin rz_crypto_plugin_ror
Definition: crypto_ror.c:89
static bool ror_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_ror.c:38
static bool ror_init_state(struct ror_state *const state, const ut8 *key, int keylen)
Definition: crypto_ror.c:17
static bool ror_init(RzCrypto *cry)
Definition: crypto_ror.c:75
static void ror_crypt(struct ror_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_ror.c:29
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 count
Definition: sflib.h:98
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
unsigned char outbuf[SIZE]
Definition: gun.c:162
unsigned char inbuf[SIZE]
Definition: gun.c:161
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_LIB_TYPE_CRYPTO
Definition: rz_lib.h:81
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
ut8 key[MAX_ror_KEY_SIZE]
Definition: crypto_ror.c:13
int key_size
Definition: crypto_ror.c:14
const char * name
Definition: rz_crypto.h:41
void * user
Definition: rz_crypto.h:36
Definition: dis.h:43
ut64 buflen
Definition: core.c:76
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static unsigned char * obuf
Definition: z80asm.c:36