1 /*-
2 * Copyright (c) 2010
3 * Thorsten Glaser <tg@mirbsd.org>
4 *
5 * Provided that these terms and disclaimer and all copyright notices
6 * are retained or reproduced in an accompanying document, permission
7 * is granted to deal in this work without restriction, including un-
8 * limited rights to use, publicly perform, distribute, sell, modify,
9 * merge, give away, or sublicence.
10 *
11 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12 * the utmost extent permitted by applicable law, neither express nor
13 * implied; without malicious intent or gross negligence. In no event
14 * may a licensor, author or contributor be held liable for indirect,
15 * direct, other damage, loss, or other issues arising in any way out
16 * of dealing in the work, even if advised of the possibility of such
17 * damage or existence of a defect, except proven that it results out
18 * of said person's immediate fault when using the work as intended.
19 *-
20 * Arcfour cipher re-implementation from (alledged) spec description.
21 * Base cipher operation: initialise state and get byte of keystream.
22 */
23
24 #include <libckern.h>
25
26 __RCSID("$MirOS: src/kern/c/arcfour_base.c,v 1.1 2010/09/12 17:10:49 tg Exp $");
27
28 void
arcfour_init(struct arcfour_status * c)29 arcfour_init(struct arcfour_status *c)
30 {
31 register uint8_t i = 0;
32
33 do {
34 c->S[i] = i;
35 } while (++i);
36
37 c->i = c->j = 0;
38 }
39
40 uint8_t
arcfour_byte(struct arcfour_status * c)41 arcfour_byte(struct arcfour_status *c)
42 {
43 register uint8_t i, j, si, sj;
44
45 i = ++(c->i);
46 si = c->S[i];
47 j = (c->j += si);
48 sj = c->S[j];
49 c->S[i] = sj;
50 c->S[j] = si;
51 return (c->S[(uint8_t)(si + sj)]);
52 }
53