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

Go to the source code of this file.

Classes

struct  blowfish_state
 

Macros

#define BLOCK_SIZE   8
 

Functions

static void swap (ut32 *left, ut32 *right)
 
static ut32 F (struct blowfish_state *const state, const ut32 inbuf)
 
static void blowfish_crypt (struct blowfish_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
 
static void blowfish_decrypt (struct blowfish_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
 
static bool blowfish_init_state (struct blowfish_state *const state, const ut8 *key, int keylen)
 
static bool blowfish_set_key (RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
 
static int blowfish_get_key_size (RzCrypto *cry)
 
static bool blowfish_use (const char *algo)
 
static bool update (RzCrypto *cry, const ut8 *buf, int len)
 
static bool final (RzCrypto *cry, const ut8 *buf, int len)
 
static bool blowfish_init (RzCrypto *cry)
 
static bool blowfish_fini (RzCrypto *cry)
 

Variables

static const ut32 p [18]
 
static const ut32 s [4][256]
 
RzCryptoPlugin rz_crypto_plugin_blowfish
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ BLOCK_SIZE

#define BLOCK_SIZE   8

Definition at line 14 of file crypto_blowfish.c.

Function Documentation

◆ blowfish_crypt()

static void blowfish_crypt ( struct blowfish_state *const  state,
const ut8 inbuf,
ut8 outbuf,
int  buflen 
)
static

Definition at line 176 of file crypto_blowfish.c.

176  {
177  ut32 left, right;
178  int index1, index2;
179 
180  if (!state || !inbuf || !outbuf || buflen < 0 || buflen % 8 != 0) {
181  // let user deal with padding
182  if (buflen % 8 != 0) {
183  eprintf("Invalid input length %d. Expected length is multiple of 8 bytes.\n", buflen);
184  }
185  return;
186  }
187 
188  for (index1 = 0; index1 < buflen; index1 += 8) {
189  left = (inbuf[index1 + 0] << 24 | inbuf[index1 + 1] << 16 | inbuf[index1 + 2] << 8 | inbuf[index1 + 3]);
190  right = (inbuf[index1 + 4] << 24 | inbuf[index1 + 5] << 16 | inbuf[index1 + 6] << 8 | inbuf[index1 + 7]);
191 
192  for (index2 = 0; index2 < 16; index2 += 1) {
193  left ^= state->p[index2];
194  right ^= F(state, left);
195  swap(&left, &right);
196  }
197  /* Undo the last swap. */
198  swap(&left, &right);
199  right ^= state->p[16];
200  left ^= state->p[17];
201 
202  outbuf[index1 + 0] = left >> 24;
203  outbuf[index1 + 1] = left >> 16;
204  outbuf[index1 + 2] = left >> 8;
205  outbuf[index1 + 3] = left;
206  outbuf[index1 + 4] = right >> 24;
207  outbuf[index1 + 5] = right >> 16;
208  outbuf[index1 + 6] = right >> 8;
209  outbuf[index1 + 7] = right;
210  }
211 }
static void swap(ut32 *left, ut32 *right)
static ut32 F(struct blowfish_state *const state, const ut32 inbuf)
uint32_t ut32
unsigned char outbuf[SIZE]
Definition: gun.c:162
unsigned char inbuf[SIZE]
Definition: gun.c:161
#define eprintf(x, y...)
Definition: rlcc.c:7
Definition: dis.h:43
ut64 buflen
Definition: core.c:76

References buflen, eprintf, F(), inbuf, outbuf, and swap().

Referenced by blowfish_init_state(), and update().

◆ blowfish_decrypt()

static void blowfish_decrypt ( struct blowfish_state *const  state,
const ut8 inbuf,
ut8 outbuf,
int  buflen 
)
static

Definition at line 213 of file crypto_blowfish.c.

213  {
214  ut32 left, right;
215  int index1, index2;
216 
217  if (!state || !inbuf || !outbuf || buflen < 0 || buflen % 8 != 0) {
218  // length of encrypted output of blowfish is multiple of 8 bytes.
219  if ((buflen % 8) != 0) {
220  eprintf("Invalid input length %d. Expected length is multiple of 8 bytes.\n", buflen);
221  }
222  return;
223  }
224 
225  for (index1 = 0; index1 < buflen; index1 += 8) {
226  left = (inbuf[index1 + 0] << 24 | inbuf[index1 + 1] << 16 | inbuf[index1 + 2] << 8 | inbuf[index1 + 3]);
227  right = (inbuf[index1 + 4] << 24 | inbuf[index1 + 5] << 16 | inbuf[index1 + 6] << 8 | inbuf[index1 + 7]);
228 
229  for (index2 = 17; index2 > 1; index2 -= 1) {
230  left ^= state->p[index2];
231  right ^= F(state, left);
232  swap(&left, &right);
233  }
234  /* Undo the last swap. */
235  swap(&left, &right);
236  right ^= state->p[1];
237  left ^= state->p[0];
238 
239  outbuf[index1 + 0] = left >> 24;
240  outbuf[index1 + 1] = left >> 16;
241  outbuf[index1 + 2] = left >> 8;
242  outbuf[index1 + 3] = left;
243  outbuf[index1 + 4] = right >> 24;
244  outbuf[index1 + 5] = right >> 16;
245  outbuf[index1 + 6] = right >> 8;
246  outbuf[index1 + 7] = right;
247  }
248 }

References buflen, eprintf, F(), inbuf, outbuf, and swap().

Referenced by update().

◆ blowfish_fini()

static bool blowfish_fini ( RzCrypto cry)
static

Definition at line 341 of file crypto_blowfish.c.

341  {
342  rz_return_val_if_fail(cry, false);
343 
344  free(cry->user);
345  return true;
346 }
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.

◆ blowfish_get_key_size()

static int blowfish_get_key_size ( RzCrypto cry)
static

Definition at line 298 of file crypto_blowfish.c.

298  {
299  rz_return_val_if_fail(cry->user, 0);
300  struct blowfish_state *st = (struct blowfish_state *)cry->user;
301 
302  return st->key_size;
303 }

References blowfish_state::key_size, rz_return_val_if_fail, and rz_crypto_t::user.

◆ blowfish_init()

static bool blowfish_init ( RzCrypto cry)
static

Definition at line 334 of file crypto_blowfish.c.

334  {
335  rz_return_val_if_fail(cry, false);
336 
337  cry->user = RZ_NEW0(struct blowfish_state);
338  return cry->user != NULL;
339 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW0(x)
Definition: rz_types.h:284

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

◆ blowfish_init_state()

static bool blowfish_init_state ( struct blowfish_state *const  state,
const ut8 key,
int  keylen 
)
static

Definition at line 250 of file crypto_blowfish.c.

250  {
251  if (!state || !key || keylen > 56) {
252  return false;
253  }
254 
255  ut8 block[8];
256  int index1, index2;
257 
258  memset(block, 0, 8);
259  memcpy(state->p, p, sizeof(ut32) * 18);
260  memcpy(state->s, s, sizeof(ut32) * 1024);
261 
262  state->key_size = keylen;
263 
264  /* Subkeys are calculated in terms of user key.
265  * P0 is XORed with first 32 bits of key, P1 with next 32 bits, and so on.
266  * Key bits are reused when length is exceeded.
267  * */
268  for (index1 = 0, index2 = 0; index1 < 18; index1 += 1, index2 += 4) {
269  state->p[index1] ^= ((key[index2 % keylen] << 24) | (key[(index2 + 1) % keylen] << 16) | (key[(index2 + 2) % keylen] << 8) | (key[(index2 + 3) % keylen]));
270  }
271 
272  /* Recalculating P-boxes */
273  for (index1 = 0; index1 < 18; index1 += 2) {
274  blowfish_crypt(state, block, block, 8);
275  state->p[index1] = (block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3]);
276  state->p[index1 + 1] = (block[4] << 24 | block[5] << 16 | block[6] << 8 | block[7]);
277  }
278 
279  /* Recalculating S-boxes */
280  for (index1 = 0; index1 < 4; index1 += 1) {
281  for (index2 = 0; index2 < 256; index2 += 2) {
282  blowfish_crypt(state, block, block, 8);
283  state->s[index1][index2] = (block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3]);
284  state->s[index1][index2 + 1] = (block[4] << 24 | block[5] << 16 | block[6] << 8 | block[7]);
285  }
286  }
287  return true;
288 }
static const ut32 s[4][256]
static void blowfish_crypt(struct blowfish_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
static const ut32 p[18]
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
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References blowfish_crypt(), key, memcpy(), memset(), p, and s.

Referenced by blowfish_set_key().

◆ blowfish_set_key()

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

Definition at line 290 of file crypto_blowfish.c.

290  {
291  rz_return_val_if_fail(cry->user && key, false);
292  struct blowfish_state *st = (struct blowfish_state *)cry->user;
293 
294  cry->dir = direction;
295  return blowfish_init_state(st, key, keylen);
296 }
static bool blowfish_init_state(struct blowfish_state *const state, const ut8 *key, int keylen)

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

◆ blowfish_use()

static bool blowfish_use ( const char *  algo)
static

Definition at line 305 of file crypto_blowfish.c.

305  {
306  return !strcmp(algo, "blowfish");
307 }

◆ F()

static ut32 F ( struct blowfish_state *const  state,
const ut32  inbuf 
)
static

Definition at line 168 of file crypto_blowfish.c.

168  {
169  ut8 a = (inbuf >> 24) & 0xff;
170  ut8 b = (inbuf >> 16) & 0xff;
171  ut8 c = (inbuf >> 8) & 0xff;
172  ut8 d = (inbuf)&0xff;
173  return ((state->s[0][a] + state->s[1][b]) ^ state->s[2][c]) + state->s[3][d];
174 }
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41

References a, b, c, d, and inbuf.

Referenced by blowfish_crypt(), and blowfish_decrypt().

◆ final()

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

Definition at line 330 of file crypto_blowfish.c.

330  {
331  return update(cry, buf, len);
332 }
size_t len
Definition: 6502dis.c:15
static bool update(RzCrypto *cry, const ut8 *buf, int len)
voidpf void * buf
Definition: ioapi.h:138

References len, and update().

◆ swap()

static void swap ( ut32 left,
ut32 right 
)
static

Definition at line 160 of file crypto_blowfish.c.

160  {
161  ut32 temp;
162 
163  temp = *right;
164  *right = *left;
165  *left = temp;
166 }

Referenced by blowfish_crypt(), and blowfish_decrypt().

◆ update()

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

Definition at line 309 of file crypto_blowfish.c.

309  {
310  rz_return_val_if_fail(cry->user, false);
311  struct blowfish_state *st = (struct blowfish_state *)cry->user;
312 
313  if (!buf || len < 1) {
314  return false;
315  }
316  ut8 *obuf = calloc(1, len);
317  if (!obuf) {
318  return false;
319  }
320  if (cry->dir == RZ_CRYPTO_DIR_ENCRYPT) {
321  blowfish_crypt(st, buf, obuf, len);
322  } else {
323  blowfish_decrypt(st, buf, obuf, len);
324  }
325  rz_crypto_append(cry, obuf, len);
326  free(obuf);
327  return true;
328 }
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len)
Definition: crypto.c:175
static void blowfish_decrypt(struct blowfish_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen)
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
@ RZ_CRYPTO_DIR_ENCRYPT
Definition: rz_crypto.h:23
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static unsigned char * obuf
Definition: z80asm.c:36

References blowfish_crypt(), blowfish_decrypt(), calloc(), rz_crypto_t::dir, free(), if(), len, obuf, rz_crypto_append(), RZ_CRYPTO_DIR_ENCRYPT, rz_return_val_if_fail, and rz_crypto_t::user.

Referenced by final().

Variable Documentation

◆ p

const ut32 p[18]
static
Initial value:
= {
0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x082EFA98,
0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD,
0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B
}

Definition at line 16 of file crypto_blowfish.c.

Referenced by blowfish_init_state().

◆ rizin_plugin

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

Definition at line 362 of file crypto_blowfish.c.

◆ rz_crypto_plugin_blowfish

RzCryptoPlugin rz_crypto_plugin_blowfish
Initial value:
= {
.name = "blowfish",
.license = "LGPL3",
.author = "kishorbhat",
.set_key = blowfish_set_key,
.get_key_size = blowfish_get_key_size,
.use = blowfish_use,
.update = update,
.final = final,
.init = blowfish_init,
.fini = blowfish_fini,
}
static bool blowfish_init(RzCrypto *cry)
static bool blowfish_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction)
static bool blowfish_use(const char *algo)
static int blowfish_get_key_size(RzCrypto *cry)
static bool blowfish_fini(RzCrypto *cry)

Definition at line 348 of file crypto_blowfish.c.

◆ s

const ut32 s[4][256]
static

Definition at line 22 of file crypto_blowfish.c.

Referenced by blowfish_init_state().