Skip to content

Instantly share code, notes, and snippets.

@loreb
Created September 22, 2013 16:40
Show Gist options
  • Save loreb/6661660 to your computer and use it in GitHub Desktop.
Save loreb/6661660 to your computer and use it in GitHub Desktop.
OpenBSD's arc4.c rewritten in striictly POSIX /bin/sh, mostly to survive some **loud** TV show (I'll go to hell for this :) TODO wtf is up with zsh?
#! /bin/sh
# Ispirato dal papa in Sardegna XD
# -- per la cronaca, ha finito prima lui, ma di poco.
#/* $OpenBSD: arc4.c,v 1.3 2007/09/11 12:07:05 djm Exp $ */
#/*
# * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
# *
# * Permission to use, copy, modify, and distribute this software for any
# * purpose with or without fee is hereby granted, provided that the above
# * copyright notice and this permission notice appear in all copies.
# *
# * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# */
#/* $OpenBSD/sys/crypto/arc4.c */
#define RC4SWAP(x,y)
RC4SWAP() {
local x=$1
local y=$2
local t
eval "t=\$_rc4_state$x"
eval "_rc4_state$x=\$_rc4_state$y";
eval "_rc4_state$y=\$t"
}
#void rc4_keysetup(struct rc4_ctx *ctx, u_char *key, u_int32_t klen)
rc4_keysetup()
{
local key="$*"
local klen=${#key}
if [ $klen -lt 3 ] ; then
echo>&2 "RC4 key too short"
return 100
fi
local x=0
local y=0
local i
# key[x] => eval "key$x=..."
i=0
# extract one character: printf %s "$string"
# pop one character: ${HOME#?} => home/$user
# echo A | od -td1 => _ 41 _
while [ $i -lt $klen ] ; do
local c="$(printf %c "$key")"
key="${key#?}"
local byte
byte=$(printf %s "$c" | od -td1 | while read crap value yadda ; do echo $value; exit; done)
eval "local key$i=\$byte"
i=$(($i + 1))
done
# RC4 setup
i=0
while [ $i -lt 256 ] ; do
eval "_rc4_state$i=$i"
i=$(($i + 1))
done
i=0
while [ $i -lt 256 ] ; do
eval "y=\$(( (\$key$x + \$_rc4_state$i + \$y) & 0xFF))"
RC4SWAP $i $y
x=$(( ($x + 1) % $klen))
i=$(($i+1))
done
_rc4_x=0
_rc4_y=0
}
#void rc4_crypt(struct rc4_ctx *ctx, u_char *src, u_char *dst, u_int32_t len)
#void rc4_getbytes(struct rc4_ctx *ctx, u_char *dst, u_int32_t len)
# Arrays are clumsy => one byte at a time;
# returning an integer in [0,255] is ok.
rc4_getbyte()
{
_rc4_x=$(( ($_rc4_x + 1) & 0xFF))
eval "local rx=\$_rc4_state$_rc4_x"
_rc4_y=$(( ($rx + $_rc4_y) & 0xFF))
RC4SWAP $_rc4_x $_rc4_y
local idx
eval "idx=\$(( (\$_rc4_state$_rc4_x + \$_rc4_state$_rc4_y) & 0xFF))"
local rv
eval "rv=\$_rc4_state$idx"
return $rv
}
#void rc4_skip(struct rc4_ctx *ctx, u_int32_t len)
rc4skip()
{
local i=0
while [ $i -lt $1 ] ; do
rc4_getbyte
i=$(($i+1))
done
}
rc4test()
{
local key="$1"; shift
local rv=0
rc4_keysetup "$key"
for i ; do
want=$((0 + $i)) || exit 123
rc4_getbyte
got=$?
if [ $want != $got ] ; then
echo>&2 "RC4($key): want $want, got $got"
rv=123
fi
done
return $rv
}
# Wikipedia's test vectors
rc4test "Key" 0xEB 0x9F 0x77 0x81 0xB7 0x34 0xCA 0x72 0xA7 0x19
rc4test "Wiki" 0x60 0x44 0xDB 0x6D 0x41 0xB7
rc4test "Secret" 0x04 0xD4 0x6B 0x05 0x3C 0xA8 0x7B 0x59
echo >&2 "No output => RC4 tested ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment