Rizin
unix-like reverse engineering framework and cli tools
crypto_rol.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 "rol"
9 
10 enum { MAX_rol_KEY_SIZE = 32768 };
11 
12 struct rol_state {
14  int key_size;
15 };
16 
17 static bool rol_init_state(struct rol_state *const state, const ut8 *key, int keylen) {
18  if (!state || !key || keylen < 1 || keylen > MAX_rol_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 rol_crypt(struct rol_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 rol_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 rol_state *st = (struct rol_state *)cry->user;
41 
42  cry->dir = direction;
43  return rol_init_state(st, key, keylen);
44 }
45 
46 static int rol_get_key_size(RzCrypto *cry) {
47  rz_return_val_if_fail(cry->user, 0);
48  struct rol_state *st = (struct rol_state *)cry->user;
49 
50  return st->key_size;
51 }
52 
53 static bool rol_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 rol_state *st = (struct rol_state *)cry->user;
60 
61  if (cry->dir) {
62  eprintf("Use ROR algorithm to decrypt\n");
63  return false;
64  }
65  ut8 *obuf = calloc(1, len);
66  if (!obuf) {
67  return false;
68  }
69  rol_crypt(st, buf, obuf, len);
70  rz_crypto_append(cry, obuf, len);
71  free(obuf);
72  return true;
73 }
74 
75 static bool rol_init(RzCrypto *cry) {
76  rz_return_val_if_fail(cry, false);
77 
78  cry->user = RZ_NEW0(struct rol_state);
79  return cry->user != NULL;
80 }
81 
82 static bool rol_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 = rol_set_key,
94  .get_key_size = rol_get_key_size,
95  .use = rol_use,
96  .update = update,
97  .final = update,
98  .init = rol_init,
99  .fini = rol_fini,
100 };
101 
102 #ifndef RZ_PLUGIN_INCORE
105  .data = &rz_crypto_plugin_rol,
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
@ MAX_rol_KEY_SIZE
Definition: crypto_rol.c:10
static bool rol_init(RzCrypto *cry)
Definition: crypto_rol.c:75
RzCryptoPlugin rz_crypto_plugin_rol
Definition: crypto_rol.c:89
#define NAME
Definition: crypto_rol.c:8
static bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_rol.c:57
static bool rol_use(const char *algo)
Definition: crypto_rol.c:53
RZ_API RzLibStruct rizin_plugin
Definition: crypto_rol.c:103
static bool rol_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_rol.c:38
static bool rol_init_state(struct rol_state *const state, const ut8 *key, int keylen)
Definition: crypto_rol.c:17
static bool rol_fini(RzCrypto *cry)
Definition: crypto_rol.c:82
static int rol_get_key_size(RzCrypto *cry)
Definition: crypto_rol.c:46
static void rol_crypt(struct rol_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_rol.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_rol_KEY_SIZE]
Definition: crypto_rol.c:13
int key_size
Definition: crypto_rol.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