File: arcfour.c

package info (click to toggle)
wipe 0.24-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 468 kB
  • sloc: ansic: 1,925; makefile: 93; perl: 85; sh: 27
file content (74 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* wipe
 * 
 * by Berke Durak
 *
 * Arcfour implementation, beta.
 *
 */

#include "arcfour.h"

void arcfour_SetupKey (u8 *k, int n, struct arcfour_KeySchedule *ks)
{
  int i, j;
  u8 kt[256];

  for (i = 0, j = 0; i<256; i++) {
    ks->s[i] = i;
    kt[i] = k[j];
    j ++; if (j == n) j = 0;
  }

  for (i = 0, j = 0; i<256; i++) {
    u8 t;

    j = 0xff & (j + ks->s[i] + kt[i]);
    t = ks->s[i];
    ks->s[i] = ks->s[j];
    ks->s[j] = t;
  }

  ks->i = i; ks->j = j;
}

inline u8 arcfour_GetByte (struct arcfour_KeySchedule *ks)
{
  int i, j;
  u8 x;

  i = ks->i; j = ks->j;

  i = 0xff & (i+1); j = (j + ks->s[i]) & 0xff;
  x = ks->s[i];
  ks->s[i] = ks->s[j];
  ks->s[j] = x;
  ks->i = i; ks->j = j;
  return ks->s[(ks->s[i] + ks->s[j]) & 0xff];
}

void arcfour_Fill (struct arcfour_KeySchedule *ks, u8 *b, int n)
{
  while (n--) *(b++) = arcfour_GetByte (ks);
}

#ifdef TEST_ARCFOUR
#define TESTKEY "thisisamoddafokkingtest"
#include <stdio.h>

int main (int argc, char **argv)
{
  int i;
  struct arcfour_KeySchedule ks;

  arcfour_SetupKey (TESTKEY, strlen (TESTKEY), &ks);

  for (i = 0; i<10240; i++) {
    putchar (arcfour_GetByte (&ks));
  }

  return 0;
}
#undef TESTKEY
#endif

/* vim:set sw=4:set ts=8: */