Rizin
unix-like reverse engineering framework and cli tools
crypto_xor.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2017 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 MAX_xor_KEY_SIZE 32768
9 
10 struct xor_state {
11  ut8 *key;
12  int key_size;
13 };
14 
15 static bool xor_init_state(struct xor_state *const state, const ut8 *key, int keylen) {
16  if (!state || !key || keylen < 1) { // || keylen > MAX_xor_KEY_SIZE) {
17  return false;
18  }
19  state->key_size = keylen;
20  state->key = malloc(keylen);
21  if (!state->key) {
22  return false;
23  }
24  memcpy(state->key, key, keylen);
25  return true;
26 }
27 
28 // Encrypt/Decrypt xor state buffer using the supplied key
29 static void xor_crypt(struct xor_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen) {
30  int i; // index for input
31  for (i = 0; i < buflen; i++) {
32  outbuf[i] = inbuf[i] ^ state->key[(i % state->key_size)];
33  }
34 }
35 
36 static bool xor_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
37  rz_return_val_if_fail(cry->user, 0);
38  struct xor_state *st = (struct xor_state *)cry->user;
39 
40  return xor_init_state(st, key, keylen);
41 }
42 
43 static int xor_get_key_size(RzCrypto *cry) {
44  rz_return_val_if_fail(cry->user, 0);
45  struct xor_state *st = (struct xor_state *)cry->user;
46 
47  return st->key_size;
48 }
49 
50 static bool xor_use(const char *algo) {
51  return !strcmp(algo, "xor");
52 }
53 
54 static bool update(RzCrypto *cry, const ut8 *buf, int len) {
55  rz_return_val_if_fail(cry->user, false);
56  struct xor_state *st = (struct xor_state *)cry->user;
57 
58  ut8 *obuf = calloc(1, len);
59  if (!obuf) {
60  return false;
61  }
62  xor_crypt(st, buf, obuf, len);
63  rz_crypto_append(cry, obuf, len);
64  free(obuf);
65  return true;
66 }
67 
68 static bool final(RzCrypto *cry, const ut8 *buf, int len) {
69  return update(cry, buf, len);
70 }
71 
72 static bool xor_init(RzCrypto *cry) {
73  rz_return_val_if_fail(cry, false);
74 
75  cry->user = RZ_NEW0(struct xor_state);
76  return cry->user != NULL;
77 }
78 
79 static bool xor_fini(RzCrypto *cry) {
80  rz_return_val_if_fail(cry, false);
81  struct xor_state *state = (struct xor_state *)cry->user;
82 
83  if (state) {
84  free(state->key);
85  }
86  free(cry->user);
87  return true;
88 }
89 
91  .name = "xor",
92  .author = "pancake",
93  .license = "LGPL-3",
94  .set_key = xor_set_key,
95  .get_key_size = xor_get_key_size,
96  .use = xor_use,
97  .update = update,
98  .final = final,
99  .init = xor_init,
100  .fini = xor_fini,
101 };
102 
103 #ifndef RZ_PLUGIN_INCORE
106  .data = &rz_crypto_plugin_xor,
107  .version = RZ_VERSION
108 };
109 #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 bool update(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto_xor.c:54
RZ_API RzLibStruct rizin_plugin
Definition: crypto_xor.c:104
static bool xor_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
Definition: crypto_xor.c:36
RzCryptoPlugin rz_crypto_plugin_xor
Definition: crypto_xor.c:90
static void xor_crypt(struct xor_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
Definition: crypto_xor.c:29
static int xor_get_key_size(RzCrypto *cry)
Definition: crypto_xor.c:43
static bool xor_fini(RzCrypto *cry)
Definition: crypto_xor.c:79
static bool xor_use(const char *algo)
Definition: crypto_xor.c:50
static bool xor_init_state(struct xor_state *const state, const ut8 *key, int keylen)
Definition: crypto_xor.c:15
static bool xor_init(RzCrypto *cry)
Definition: crypto_xor.c:72
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
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#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
const char * name
Definition: rz_crypto.h:41
void * user
Definition: rz_crypto.h:36
Definition: dis.h:43
ut8 * key
Definition: crypto_xor.c:11
int key_size
Definition: crypto_xor.c:12
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