1*fe753764Sderaadt /* $OpenBSD: arc4.c,v 1.1 2019/10/29 02:51:17 deraadt Exp $ */
2*fe753764Sderaadt /*
3*fe753764Sderaadt * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
4*fe753764Sderaadt *
5*fe753764Sderaadt * Permission to use, copy, modify, and distribute this software for any
6*fe753764Sderaadt * purpose with or without fee is hereby granted, provided that the above
7*fe753764Sderaadt * copyright notice and this permission notice appear in all copies.
8*fe753764Sderaadt *
9*fe753764Sderaadt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*fe753764Sderaadt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*fe753764Sderaadt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*fe753764Sderaadt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*fe753764Sderaadt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*fe753764Sderaadt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*fe753764Sderaadt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*fe753764Sderaadt */
17*fe753764Sderaadt
18*fe753764Sderaadt #include <sys/types.h>
19*fe753764Sderaadt
20*fe753764Sderaadt #include <lib/libsa/arc4.h>
21*fe753764Sderaadt
22*fe753764Sderaadt #define RC4SWAP(x,y) \
23*fe753764Sderaadt do { \
24*fe753764Sderaadt u_int8_t t = ctx->state[x]; \
25*fe753764Sderaadt ctx->state[x] = ctx->state[y]; \
26*fe753764Sderaadt ctx->state[y] = t; \
27*fe753764Sderaadt } while(0)
28*fe753764Sderaadt
29*fe753764Sderaadt void
rc4_keysetup(struct rc4_ctx * ctx,u_char * key,u_int32_t klen)30*fe753764Sderaadt rc4_keysetup(struct rc4_ctx *ctx, u_char *key, u_int32_t klen)
31*fe753764Sderaadt {
32*fe753764Sderaadt u_int8_t x, y;
33*fe753764Sderaadt u_int32_t i;
34*fe753764Sderaadt
35*fe753764Sderaadt x = y = 0;
36*fe753764Sderaadt for (i = 0; i < RC4STATE; i++)
37*fe753764Sderaadt ctx->state[i] = i;
38*fe753764Sderaadt for (i = 0; i < RC4STATE; i++) {
39*fe753764Sderaadt y = (key[x] + ctx->state[i] + y) & (RC4STATE - 1);
40*fe753764Sderaadt RC4SWAP(i, y);
41*fe753764Sderaadt x = (x + 1) % klen;
42*fe753764Sderaadt }
43*fe753764Sderaadt ctx->x = ctx->y = 0;
44*fe753764Sderaadt }
45*fe753764Sderaadt
46*fe753764Sderaadt #ifdef notneeded
47*fe753764Sderaadt void
rc4_crypt(struct rc4_ctx * ctx,u_char * src,u_char * dst,u_int32_t len)48*fe753764Sderaadt rc4_crypt(struct rc4_ctx *ctx, u_char *src, u_char *dst,
49*fe753764Sderaadt u_int32_t len)
50*fe753764Sderaadt {
51*fe753764Sderaadt u_int32_t i;
52*fe753764Sderaadt
53*fe753764Sderaadt for (i = 0; i < len; i++) {
54*fe753764Sderaadt ctx->x = (ctx->x + 1) & (RC4STATE - 1);
55*fe753764Sderaadt ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1);
56*fe753764Sderaadt RC4SWAP(ctx->x, ctx->y);
57*fe753764Sderaadt dst[i] = src[i] ^ ctx->state[
58*fe753764Sderaadt (ctx->state[ctx->x] + ctx->state[ctx->y]) & (RC4STATE - 1)];
59*fe753764Sderaadt }
60*fe753764Sderaadt }
61*fe753764Sderaadt #endif
62*fe753764Sderaadt
63*fe753764Sderaadt void
rc4_getbytes(struct rc4_ctx * ctx,u_char * dst,u_int32_t len)64*fe753764Sderaadt rc4_getbytes(struct rc4_ctx *ctx, u_char *dst, u_int32_t len)
65*fe753764Sderaadt {
66*fe753764Sderaadt u_int32_t i;
67*fe753764Sderaadt
68*fe753764Sderaadt for (i = 0; i < len; i++) {
69*fe753764Sderaadt ctx->x = (ctx->x + 1) & (RC4STATE - 1);
70*fe753764Sderaadt ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1);
71*fe753764Sderaadt RC4SWAP(ctx->x, ctx->y);
72*fe753764Sderaadt dst[i] = ctx->state[
73*fe753764Sderaadt (ctx->state[ctx->x] + ctx->state[ctx->y]) & (RC4STATE - 1)];
74*fe753764Sderaadt }
75*fe753764Sderaadt }
76*fe753764Sderaadt
77*fe753764Sderaadt void
rc4_skip(struct rc4_ctx * ctx,u_int32_t len)78*fe753764Sderaadt rc4_skip(struct rc4_ctx *ctx, u_int32_t len)
79*fe753764Sderaadt {
80*fe753764Sderaadt for (; len > 0; len--) {
81*fe753764Sderaadt ctx->x = (ctx->x + 1) & (RC4STATE - 1);
82*fe753764Sderaadt ctx->y = (ctx->state[ctx->x] + ctx->y) & (RC4STATE - 1);
83*fe753764Sderaadt RC4SWAP(ctx->x, ctx->y);
84*fe753764Sderaadt }
85*fe753764Sderaadt }
86