CS318 - Pintos
Pintos source browser for JHU CS318 course
src
tests
arc4.c
Go to the documentation of this file.
1
#include <
stdint.h
>
2
#include "
tests/arc4.h
"
3
4
/** Swap bytes. */
5
static
inline
void
6
swap_byte
(
uint8_t
*a,
uint8_t
*b)
7
{
8
uint8_t
t = *a;
9
*a = *b;
10
*b = t;
11
}
12
13
void
14
arc4_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
34
void
35
arc4_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
}
uint8_t
unsigned char uint8_t
Definition:
stdint.h:20
arc4
Alleged RC4 algorithm encryption state.
Definition:
arc4.h:8
ustar_header::size
char size[12]
File size in bytes as octal string.
Definition:
ustar.c:16
buf
static char buf[BUF_SIZE]
Definition:
child-syn-read.c:16
arc4_init
void arc4_init(struct arc4 *arc4, const void *key_, size_t size)
Definition:
arc4.c:14
swap_byte
static void swap_byte(uint8_t *a, uint8_t *b)
Swap bytes.
Definition:
arc4.c:6
arc4_crypt
void arc4_crypt(struct arc4 *arc4, void *buf_, size_t size)
tests/arc4.h
Definition:
arc4.c:35
arc4.h
arc4::i
uint8_t i
Definition:
arc4.h:11
stdint.h
s
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).
Definition:
random.c:17
arc4::j
uint8_t j
Definition:
arc4.h:11
arc4::s
uint8_t s[256]
Definition:
arc4.h:10
Generated on Thu Aug 22 2019 10:19:15 for CS318 - Pintos by
1.8.16