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