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

Go to the source code of this file.

Classes

struct  serpent_state
 

Macros

#define DW_BY_BLOCK   4
 
#define DW_BY_USERKEY   8
 
#define NB_ROUNDS   32
 
#define NB_SUBKEYS   33
 
#define NIBBLES_BY_SUBKEY   32
 

Typedefs

typedef struct serpent_state serpent_state_t
 

Functions

void serpent_encrypt (serpent_state_t *st, ut32 in[DW_BY_BLOCK], ut32 out[DW_BY_BLOCK])
 
void serpent_decrypt (serpent_state_t *st, ut32 in[DW_BY_BLOCK], ut32 out[DW_BY_BLOCK])
 
void serpent_keyschedule (const serpent_state_t *st, ut32 subkeys[NB_SUBKEYS *DW_BY_BLOCK])
 

Macro Definition Documentation

◆ DW_BY_BLOCK

#define DW_BY_BLOCK   4

Definition at line 9 of file crypto_serpent_algo.h.

◆ DW_BY_USERKEY

#define DW_BY_USERKEY   8

Definition at line 10 of file crypto_serpent_algo.h.

◆ NB_ROUNDS

#define NB_ROUNDS   32

Definition at line 11 of file crypto_serpent_algo.h.

◆ NB_SUBKEYS

#define NB_SUBKEYS   33

Definition at line 12 of file crypto_serpent_algo.h.

◆ NIBBLES_BY_SUBKEY

#define NIBBLES_BY_SUBKEY   32

Definition at line 13 of file crypto_serpent_algo.h.

Typedef Documentation

◆ serpent_state_t

Function Documentation

◆ serpent_decrypt()

void serpent_decrypt ( serpent_state_t st,
ut32  in[DW_BY_BLOCK],
ut32  out[DW_BY_BLOCK] 
)

Definition at line 235 of file crypto_serpent_algo.c.

236  {
237  int i;
238  ut32 subkeys[DW_BY_BLOCK * NB_SUBKEYS] = { 0 };
239  ut32 tmp_block[DW_BY_BLOCK] = { 0 };
240 
241  serpent_keyschedule(st, subkeys);
242 
243  apply_IP(in, tmp_block);
244  for (i = NB_ROUNDS - 1; i >= 0; i--) {
245  apply_round_inv(i, tmp_block, subkeys);
246  }
247  apply_FP(tmp_block, out);
248 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
void apply_round_inv(int round, ut32 block[DW_BY_BLOCK], ut32 subkeys[DW_BY_BLOCK *NB_SUBKEYS])
void apply_IP(ut32 in[DW_BY_BLOCK], ut32 out[DW_BY_BLOCK])
void serpent_keyschedule(const serpent_state_t *st, ut32 subkeys[NB_SUBKEYS *DW_BY_BLOCK])
void apply_FP(ut32 in[DW_BY_BLOCK], ut32 out[DW_BY_BLOCK])
#define NB_SUBKEYS
#define NB_ROUNDS
#define DW_BY_BLOCK
uint32_t ut32

References apply_FP(), apply_IP(), apply_round_inv(), DW_BY_BLOCK, i, in, NB_ROUNDS, NB_SUBKEYS, out, and serpent_keyschedule().

Referenced by update().

◆ serpent_encrypt()

void serpent_encrypt ( serpent_state_t st,
ut32  in[DW_BY_BLOCK],
ut32  out[DW_BY_BLOCK] 
)

Definition at line 220 of file crypto_serpent_algo.c.

221  {
222  int i;
223  ut32 subkeys[DW_BY_BLOCK * NB_SUBKEYS] = { 0 };
224  ut32 tmp_block[DW_BY_BLOCK] = { 0 };
225 
226  serpent_keyschedule(st, subkeys);
227 
228  apply_IP(in, tmp_block);
229  for (i = 0; i < NB_ROUNDS; i++) {
230  apply_round(i, tmp_block, subkeys);
231  }
232  apply_FP(tmp_block, out);
233 }
void apply_round(int round, ut32 block[DW_BY_BLOCK], ut32 subkeys[DW_BY_BLOCK *NB_SUBKEYS])

References apply_FP(), apply_IP(), apply_round(), DW_BY_BLOCK, i, in, NB_ROUNDS, NB_SUBKEYS, out, and serpent_keyschedule().

Referenced by update().

◆ serpent_keyschedule()

void serpent_keyschedule ( const serpent_state_t st,
ut32  subkeys[NB_SUBKEYS *DW_BY_BLOCK] 
)

Definition at line 93 of file crypto_serpent_algo.c.

93  {
94  rz_return_if_fail((st->key_size == 128) || (st->key_size == 192) || (st->key_size == 256));
95 
96  ut32 tmpkeys[DW_BY_BLOCK * NB_SUBKEYS + DW_BY_USERKEY] = { 0 };
97  const ut32 phi = 0x9e3779b9;
98  int si;
99  ut8 in, out;
100  int i, j, l;
101 
102  for (i = 0; i < st->key_size / 32; i++) {
103  tmpkeys[i] = st->key[i];
104  }
105 
106  // Padding key
107  if (st->key_size != 256) {
108  tmpkeys[st->key_size / 32] = 1;
109  }
110 
112  tmpkeys[i] = tmpkeys[i - 8] ^ tmpkeys[i - 5] ^ tmpkeys[i - 3] ^ tmpkeys[i - 1] ^ phi ^ (i - 8);
113  rotl(tmpkeys + i, 11);
114  }
115 
116  // Applying sbox for subkey i
117  for (i = 0; i < NB_SUBKEYS; i++) {
118  si = (32 + 3 - i) % 8;
119 
120  // Iterates over all nibbles of the subkey i
121  for (j = 0; j < NIBBLES_BY_SUBKEY; j++) {
122  in = get_bit(j, tmpkeys[0 + DW_BY_BLOCK * i + DW_BY_USERKEY]) | get_bit(j, tmpkeys[1 + DW_BY_BLOCK * i + DW_BY_USERKEY]) << 1 | get_bit(j, tmpkeys[2 + DW_BY_BLOCK * i + DW_BY_USERKEY]) << 2 | get_bit(j, tmpkeys[3 + DW_BY_BLOCK * i + DW_BY_USERKEY]) << 3;
123  out = apply_sbox(si, in);
124  for (l = 0; l < DW_BY_BLOCK; l++) {
125  subkeys[l + DW_BY_BLOCK * i] |= get_bit(l, (ut32)out) << j;
126  }
127  }
128  }
129 
130  // Apply IP on every subkey
131  for (i = 0; i < NB_SUBKEYS; i++) {
132  apply_IP(&subkeys[i * DW_BY_BLOCK], &tmpkeys[DW_BY_USERKEY + i * DW_BY_BLOCK]);
133  }
134 
135  memcpy(subkeys, tmpkeys + DW_BY_USERKEY, 132 * sizeof(ut32));
136 }
si
static ut8 apply_sbox(int si, ut8 x)
static ut8 get_bit(int i, ut32 input)
static void rotl(ut32 *x, int s)
#define DW_BY_USERKEY
#define NIBBLES_BY_SUBKEY
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References apply_IP(), apply_sbox(), DW_BY_BLOCK, DW_BY_USERKEY, get_bit(), i, in, serpent_state::key, serpent_state::key_size, memcpy(), NB_SUBKEYS, NIBBLES_BY_SUBKEY, out, rotl(), rz_return_if_fail, and si.

Referenced by serpent_decrypt(), and serpent_encrypt().