PKUOS - Pintos
Pintos source browser for PKU Operating System course
arc4.c
Go to the documentation of this file.
1#include <stdint.h>
2#include "tests/arc4.h"
3
4/** Swap bytes. */
5static inline void
7{
8 uint8_t t = *a;
9 *a = *b;
10 *b = t;
11}
12
13void
14arc4_init (struct arc4 *arc4, const void *key_, size_t size)
15{
16 const uint8_t *key = key_;
17 size_t key_idx;
18 uint8_t *s;
19 int i, j;
20
21 s = arc4->s;
22 arc4->i = arc4->j = 0;
23 for (i = 0; i < 256; i++)
24 s[i] = i;
25 for (key_idx = 0, i = j = 0; i < 256; i++)
26 {
27 j = (j + s[i] + key[key_idx]) & 255;
28 swap_byte (s + i, s + j);
29 if (++key_idx >= size)
30 key_idx = 0;
31 }
32}
33
34void
35arc4_crypt (struct arc4 *arc4, void *buf_, size_t size)
36{
37 uint8_t *buf = buf_;
38 uint8_t *s;
39 uint8_t i, j;
40
41 s = arc4->s;
42 i = arc4->i;
43 j = arc4->j;
44 while (size-- > 0)
45 {
46 i += 1;
47 j += s[i];
48 swap_byte (s + i, s + j);
49 *buf++ ^= s[(s[i] + s[j]) & 255];
50 }
51 arc4->i = i;
52 arc4->j = j;
53}
void arc4_init(struct arc4 *arc4, const void *key_, size_t size)
Definition: arc4.c:14
void arc4_crypt(struct arc4 *arc4, void *buf_, size_t size)
tests/arc4.h
Definition: arc4.c:35
static void swap_byte(uint8_t *a, uint8_t *b)
Swap bytes.
Definition: arc4.c:6
static char buf[BUF_SIZE]
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).
Definition: random.c:17
unsigned char uint8_t
Definition: stdint.h:20
Alleged RC4 algorithm encryption state.
Definition: arc4.h:9
uint8_t s[256]
Definition: arc4.h:10
uint8_t i
Definition: arc4.h:11
uint8_t j
Definition: arc4.h:11