Honeycomb  0.1
Component-Model Framework
Classes | Public Types | Public Member Functions | List of all members
honey::Chacha Class Reference

ChaCha8, a cryptographically secure pseudo random number generator. More...

#include <Chacha.h>

Inheritance diagram for honey::Chacha:
Inheritance graph
[legend]
Collaboration diagram for honey::Chacha:
Collaboration graph
[legend]

Classes

struct  State
 Generator State. More...
 

Public Types

typedef ByteArray< 32 > Key
 256-bit Cryptographic Key More...
 
typedef ByteArray< 8 > Iv
 64-bit Cryptographic IV More...
 
typedef ByteArray< 40 > Seed
 320-bit Seed (Key+IV) More...
 

Public Member Functions

 Chacha ()
 
 ~Chacha ()
 
void setSeed ()
 Set the seed using a default method that gathers entropy from the device. More...
 
void setSeed (const Seed &seed)
 Set the random number generator seed. More...
 
virtual uint64 next ()
 Generate random number between 0 and 2^64-1 inclusive. More...
 
void setKey (const Key &key)
 Init generator with a cryptographic key. More...
 
void setIv (const Iv &iv)
 Set initialization vector into generator. Call for every message between encrypt/decrypt calls to produce unique streams. More...
 
void encrypt (ByteBufConst msg, ByteBuf cipher)
 Encrypt a message. Result is stored in cipher. More...
 
void decrypt (ByteBufConst cipher, ByteBuf msg)
 Decrypt a cipher. Result is stored in msg. More...
 
void setState (const State &state)
 Set the state of the generator. More...
 
StategetState ()
 Get the current state of the generator. Pass result into setState() to restore a state. More...
 

Detailed Description

ChaCha8, a cryptographically secure pseudo random number generator.

This random generator produces high quality randomness and is also a stream cipher. This class can be used for encryption/decryption because the randomness is generated in a way that a listener to the encrypted message is unable to deduce the initial seed (key+iv).

The generator accepts a 256-bit cryptographic key and a 64-bit IV (initialization vector). These can also be combined together to be understood as one 320-bit seed. The entire state of the generator is 512 bits. The period of the generator is 2^70 random integers for each IV.

Random Number Generation

Use setSeed() and next().
By default the seed is filled with entropy from the host device.

Cryptography

Use setKey(), setIv() and encrypt() / decrypt().
The key should be exchanged and set up once, and the IV should be randomized for each message.
A unique IV for each message makes the stream more difficult to decipher.
A separate instance of this class seeded with device entropy can be used to generate keys and IVs.

Sender:                                         Receiver:

key = rand.next(); iv = rand.next();                                        //Generate initial key and IV, use separate Chacha instance
send(key,iv);                                   receive(key,iv);            //Exchange key and IV between sender/receiver
setKey(key); setIv(iv);                         setKey(key); setIv(iv);     //Initialize generator
encrypt(msg,c,len);                             decrypt(c,msg,len);         //Encrypt messages
encrypt(msg2,c,len2);                           decrypt(c,msg2,len2);
iv = rand.next(); encrypt(iv,c,8);              decrypt(c,iv,8);            //Append next IV to cipher
setIv(iv);                                      setIv(iv);                  //Change the IV for the next message
encrypt(msg3,c,len3);                           decrypt(c,msg3,len3);

Member Typedef Documentation

64-bit Cryptographic IV

256-bit Cryptographic Key

320-bit Seed (Key+IV)

Constructor & Destructor Documentation

honey::Chacha::Chacha ( )
inline
honey::Chacha::~Chacha ( )
inline

Member Function Documentation

void honey::Chacha::decrypt ( ByteBufConst  cipher,
ByteBuf  msg 
)

Decrypt a cipher. Result is stored in msg.

void honey::Chacha::encrypt ( ByteBufConst  msg,
ByteBuf  cipher 
)

Encrypt a message. Result is stored in cipher.

State& honey::Chacha::getState ( )
inline

Get the current state of the generator. Pass result into setState() to restore a state.

uint64 honey::Chacha::next ( )
virtual

Generate random number between 0 and 2^64-1 inclusive.

Implements honey::RandomGen.

void honey::Chacha::setIv ( const Iv iv)

Set initialization vector into generator. Call for every message between encrypt/decrypt calls to produce unique streams.

void honey::Chacha::setKey ( const Key key)

Init generator with a cryptographic key.

void honey::Chacha::setSeed ( )

Set the seed using a default method that gathers entropy from the device.

void honey::Chacha::setSeed ( const Seed seed)

Set the random number generator seed.

void honey::Chacha::setState ( const State state)
inline

Set the state of the generator.


The documentation for this class was generated from the following files: