Skip to content

StreamCipher

Stephen Berquet edited this page Feb 4, 2015 · 3 revisions

Stream ciphers

The stream cipher is one of the two major categories of symmetric ciphers. Unlike block ciphers, in a stream cipher each plaintext digit is encrypted one at a time with the corresponding digit of the key, to give a digit of the ciphertext stream.

A cipher is said "symmetric" when it uses only one key : the same key is used to encrypt and decrypt data.

Usage

Stream cipher classes implements the Cryptopp\StreamCipherInterface interface.

$o = new Cryptopp\StreamCipherSosemanuk();

// returns the name of the stream cipher
$o->getName();

// returns the block size
$o->getBlockSize();

// indicates if a given key length is valid
$o->isValidKeyLength(10);

// indicates if a given initialization vector length is valid
$o->isValidIvLength(10);

Encrypt/decrypt data

In addition to the cipher key, a stream cipher needs an initialization vector (shortened IV), which is more or less a second key. The main difference between the key and the initialization vector is that the key has to be kept secret, whereas the initialization vector does not need to. Also, the initialization vector must be used once and only once with a given key (a key/initialization vector association must not be used twice, never).

$o = new Cryptopp\StreamCipherSosemanuk();
$o->setKey("your secret key");
$o->setIv("your initialization vector");

$ciphertext = $o->encrypt("data to encrypt");

$plaintext = $o->decrypt($ciphertext);

The encryption/decryption process is incremental. If you encrypt the same string twice without a call to the restart() method, the resulting ciphertext (the encrypted data) will be different.

// the following example is equivalent to the previous one
$ciphertext2  = $o->encrypt("data");
$ciphertext2 .= $o->encrypt(" to ");
$ciphertext2 .= $o->encrypt("encr");
$ciphertext2 .= $o->encrypt("ypt");

// without a call to restart, the resulting ciphertext will be different
$ciphertext3 = $o->encrypt("data to encrypt");

// when you call restart(), the state is resetted
// $ciphertext4 == $ciphertext2
$o->restart();
$ciphertext4 = $o->encrypt("data to encrypt");

Stream ciphers are usable with Cryptopp\SymmetricTransformationFilter (see the cipher mode page).

Extending

Extending a stream cipher class is possible to add some methods, but existing methods are not overwritable. Also, you are required to call the parent constructor.

class MySosemanuk extends Cryptopp\StreamCipherSosemanuk
{
    public function __construct()
    {
        parent::__construct();
    }

    public function myMethod()
    {
        // ...
    }
}

$o = new MySosemanuk();

Create a stream cipher class

You can create your own stream cipher class by implementing the Cryptopp\StreamCipherInterface interface. The following methods are required :

Method signature Descritption
getName() Returns the cipher name
getBlockSize() Returns the block size (in bytes)
encrypt($data) Encrypts data
decrypt($data) Decrypts data
restart() Resets encryption/decryption state
setKey($key) Sets the key
setIv($iv) Sets the initialization vector
getKey() Returns the key
getIv() Returns the initialization vector
isValidKeyLength($length) Indicates if a given key length is valid for this algorithm
isValidIvLength($length) Indicates if a given initialization vector length is valid for this algorithm

Stream cipher classes

Sosemanuk

Sosemanuk is a stream cipher developed by Come Berbain, Olivier Billet, Anne Canteaut, Nicolas Courtois, Henri Gilbert, Louis Goubin, Aline Gouget, Louis Granboulan, Cédric Lauradoux, Marine Minier, Thomas Pornin and Hervé Sibert.

  • Class : Cryptopp\StreamCipherSosemanuk
  • Key size : 1 to 32 bytes
    • Recommended key length is 128 bits (16 bytes) as the guaranteed security is only 128 bits
  • IV size : 128 bits (16 bytes)

Panama

Panama is a cryptography primitive which can be used both as a hash function and a stream cipher. It was designed by Joan Daemen and Craig Clapp.

  • Class : Cryptopp\StreamCipherPanama
  • Key size : 256 bits (32 bytes)
  • IV size : 256 bits (32 bytes)

Salsa20

Salsa20 is a stream cipher submitted by Daniel J. Bernstein.

  • Class : Cryptopp\StreamCipherSalsa20
  • Key size : 128 bits (16 bytes) or 256 bits (32 bytes)
  • IV size : 64 bits (8 bytes)

XSalsa20

XSalsa20 is a stream cipher based upon Salsa20 but with a much longer IV : 192 bits instead of 64 bits.

  • Class : Cryptopp\StreamCipherXSalsa20
  • Key size : 256 bits (32 bytes)
  • IV size : 192 bits (24 bytes)

Clone this wiki locally