rc4.bend checks
raw source on the hub · import bend-osdev-legacy-ciphers@0.1.0.0/rc4.bend as Rc4
RC4 (ARC4), the stream cipher, as RFC 6229 ("Test Vectors for the Stream Cipher RC4", 2011) describes it: a key-scheduling algorithm that permutes the 256-byte state S under a key of 1 to 256 bytes, then an output algorithm that yields one keystream byte per step, XORed into the data.
RC4 is broken: RFC 7465 (2015) forbids it in TLS, and its keystream is biased. It is here only to read and write data that old formats and protocols already hold (for compatibility); never use it to protect new data.
The state (S, i, j) is Data: S is a 256-entry table (a tree read and written by the bits of an index), so the cipher is a pure function of its state and the laws can quantify over states. Data arrives in pieces: LcRc4.update takes the state and a piece and answers the next state and the piece encrypted (or decrypted: it is the same operation).
3 imports
import Base import bend-osdev-bytes@0.1.0.0/bytes.bend as B import ./legacy_ciphers.bend as L
Types
type LcRc4State source · line 78 · raw
Data
The state (opaque): S, i and j
LcRc4State@s:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @i:U32 -> @j:U32 -> LcRc4State
type LcRc4Step source · line 92 · raw
Data
One step of the output algorithm: the next state and keystream byte
LcRc4Step@st:LcRc4State -> @k:U32 -> LcRc4Step
type LcRc4Out source · line 134 · raw
Data
A piece of data through the cipher: the state after it, and its bytes encrypted (or decrypted). Open: a caller may match it.
LcRc4Out@state:LcRc4State -> @data:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> LcRc4Out
type LcRc4Run source · line 147 · raw
Data
LcRc4Run@st:LcRc4State -> @out:List<&2, U32> -> LcRc4Run
Definitions
def rc4.path source · line 23 · raw
@d:Nat -> @+i:U32 -> List<&2, Bool>
the bits of i (below 2^d) that pick its entry, most significant first
def rc4.get source · line 30 · raw
@t:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @path:List<&2, Bool> -> U32
def rc4.set source · line 45 · raw
@t:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @path:List<&2, Bool> -> @v:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab
def rc4.at source · line 60 · raw
@+s:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+i:U32 -> U32
def rc4.swap source · line 64 · raw
@+s:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+i:U32 -> @+j:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab
S with entries i and j exchanged
def rc4.ident source · line 70 · raw
@n:Nat -> @+i:U32 -> List<&2, U32>
the identity permutation 0 .. 255
def rc4.ksa source · line 83 · raw
@n:Nat -> @+key:List<&2, U32> -> @+klen:Nat -> @+s:0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab -> @+i:U32 -> @+j:U32 -> 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcTab
The key-scheduling algorithm: for i = 0 .. 255, j = j + S[i] + key[i mod keylength]; swap S[i] and S[j]
def rc4.next source · line 96 · raw
@st:LcRc4State -> LcRc4Step
i = i + 1; j = j + S[i]; swap S[i] and S[j]; the byte S[S[i] + S[j]]
def rc4.step.st source · line 104 · raw
@r:LcRc4Step -> LcRc4State
def rc4.step.k source · line 109 · raw
@r:LcRc4Step -> U32
def rc4.xs source · line 117 · raw
@xs:List<&2, U32> -> @st:LcRc4State -> List<&2, U32>
def rc4.after source · line 125 · raw
@xs:List<&2, U32> -> @st:LcRc4State -> LcRc4State
def rc4.rev_onto source · line 140 · raw
@xs:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>
def rc4.go source · line 150 · raw
@xs:List<&2, U32> -> @st:LcRc4State -> @acc:List<&2, U32> -> LcRc4Run
def rc4.out source · line 158 · raw
@r:LcRc4Run -> LcRc4Out
def rc4.drop source · line 164 · raw
@n:Nat -> @st:LcRc4State -> LcRc4State
drop n keystream bytes
def rc4.start.pick source · line 171 · raw
@ok:Bool -> @+key:List<&2, U32> -> @+klen:Nat -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcRc4State>
def LcRc4.start source · line 181 · raw
@key:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcRc4State>
the state after the key-scheduling algorithm, for a key of 1 to 256 bytes
def LcRc4.start_drop source · line 188 · raw
@key:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @n:Nat -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, LcRc4State>
the state after dropping the first n keystream bytes (RC4-drop[n]; RFC 4345 drops 1536 for SSH's arcfour128 and arcfour256)
def LcRc4.update source · line 192 · raw
@st:LcRc4State -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> LcRc4Out
the next piece of data, encrypted or decrypted, and the state after it
def LcRc4.out_state source · line 195 · raw
@o:LcRc4Out -> LcRc4State
def LcRc4.out_data source · line 200 · raw
@o:LcRc4Out -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets
def LcRc4.crypt source · line 206 · raw
@key:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @m:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> Result<&2, &2, 0x7d16ec09f99b830428c8e2852af315f6/legacy_ciphers.LcError, 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets>
the data under a key, in one call
def LcRc4.keystream source · line 210 · raw
@st:LcRc4State -> @n:Nat -> LcRc4Out
the next n keystream bytes (the data n zero bytes), and the state after them