MyNixOS website logo
Description

Implementation of CipherSaber2 RC4 cryptography.

This package implements CipherSaber-2, a standard for RC4 encryption. See the project website for details.

ciphersaber2

Copyright © 2015 Bart Massey

This package provides a Haskell library and driver program implementing CipherSaber-2(CS2) stream encryption based on the RC4 stream encryption algorithm. This implementation has been tested against and is compatible with existing CipherSaber implementations.

CS2

The documentation for CS2 is a bit out-of-date and scattered.

History

CS2 is based on the RC4 stream cipher. Wikipedia has a nice history of RC4 as well as current reports on its cryptanalysis.

In 1999, Arnold Reinhold suggested using RC4 as the basis for citizens to learn to build their own encryption software, along the lines of Jedi Light Sabers. Reinhold proposed a stream protocol for RC4 ciphertext that he called CipherSaber (Note that the CipherSaber website is mostly abandoned and in some state of disrepair.)

In 2003, after cryptographic attacks were found against RC4 as used in CipherSaber, Reinhold modified the CipherSaber protocol to produce a new parameterized family of protocols known as CS2: the original CipherSaber is a special case of CS2, and is often referred to as CipherSaber-1.

Algorithm

Pseudocode for CS2 is available from a variety of places. The pseudocode given here attempts to be clear and normative.

CS2 encryption and decryption both require an RC4 implementation that has been modified to iterate the key schedule a given number of times.

-- Produce an RC4 keystream of length n with
-- r rounds of key scheduling given key k
rc4(nrk):
l ← lengthk
-- Initialize the array.
S ← zero-based array of 256 bytes
foriin 0..255
S[i] ← i
-- Do key scheduling.
j ← 0
repeatrtimes
foriin 0..255
j ← (j + S[i] + k[imodl]) mod 256
S[i] ↔ S[j]
-- Finally, produce the stream.
keystream ← zero-based array of n bytes
j ← 0
foriin 0..n-1
i' ← (i + 1) mod 256
j ← (j + S[i']) mod 256
S[i'] ↔ S[j]
keystream[i] ← S[(S[i'] + S[j]) mod 256]
returnkeystream

CS2 encryption requires a plaintext message (treated as a bytestream), a key, and an "initial value" (IV) of 10 bytes. The key should be no more than 53 bytes, to ensure good mixing during key scheduling.

The IV is a nonce that must be different for each message sent: it should be chosen randomly if possible, but may be chosen pseudo-randomly or even just counted if necessary. CS2 appends the IV to the CS2 key to produce an RC4 key. RC4 uses only the first 256 RC4 key bytes. Thus, the CS2 key must be no more than 246 bytes: a longer CS2 key would cause some or all of the IV to not be used by RC4.

-- Ciphersaber-2 encrypt message m with key k and
-- r rounds of key scheduling
encrypt(mrk):
n ← lengthm
iv ← appropriately-chosen 10-byte IV
k' ← prepend ktoiv
keystream ← rc4(nr, k')
ciphertext ← zero-based array of n + 10 bytes
foriin 0..9
ciphertext[i] ← iv[i]
foriin 0..n
ciphertext[i + 10] ← m[ixorkeystream[i]
return ciphertext

CS2 decryption requires ciphertext and the encryption key used to produce the ciphertext.

-- Ciphersaber-2 decrypt ciphertext m with key k and
-- r rounds of key scheduling
decrypt(mrk):
n ← lengthm
iv ← m[0..9]
delete the first 10 characters of m
k' ← prepend ktoiv
keystream ← rc4(n - 10, r, k')
plaintext ← zero-based array of n - 10 bytes
foriin 0..n-10
plaintext[i] ← m[ixorkeystream[i]
return plaintext

Library

The CipherSaber2 library provides a relatively straightforward ByteString interface. See the haddock documentation for details.

Driver

The program cs2 uses the CipherSaber2 library to encrypt or decrypt stdin to stdout.

cs2 is written in Haskell, so you will need a Haskell installation to run it. It depends on the package [parseargs](http://hackage.haskell.org/package/parseargs) from Hackage, as well as the bytestring package that should probably have come with your distribution but may need to be installed. Say "cabal install parseargs" and "cabal install bytestring" to get things set up. (This in turn may require a cabal-install package from your Linux distribution or thereabouts.)

Say "runghc cs2.hs --help" for usage information.

Say "runghc cs2.hs -e whee <f.txt >g.cs2" to encrypt the file f.txt with key whee. An IV will be automatically chosen.

Say "runghc cs2.hs -d whee <g.cs2 >ff.txt" to decrypt the file g.cs2 with key whee.

Test Instances

The test directory include a bunch of plaintext/ciphertext pairs taken from the original CS2 materials. Please see README.md in that directory for more information.

License

This work is licensed under the "MIT License". Please see the file LICENSE in the source distribution of this software for license terms.

Metadata

Version

0.1.1.2

License

Executables (1)

  • bin/cs2

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows