Botan  1.11.15
Public Types | Public Member Functions | Protected Member Functions
Botan::ChaCha Class Reference

#include <chacha.h>

Inheritance diagram for Botan::ChaCha:
Botan::StreamCipher Botan::SymmetricAlgorithm

List of all members.

Public Types

typedef SCAN_Name Spec

Public Member Functions

void cipher (const byte in[], byte out[], size_t length)
void cipher1 (byte buf[], size_t len)
void clear ()
StreamCipherclone () const
template<typename Alloc >
void decrypt (std::vector< byte, Alloc > &inout)
template<typename Alloc >
void encipher (std::vector< byte, Alloc > &inout)
template<typename Alloc >
void encrypt (std::vector< byte, Alloc > &inout)
Key_Length_Specification key_spec () const
size_t maximum_keylength () const
size_t minimum_keylength () const
std::string name () const
void set_iv (const byte iv[], size_t iv_len)
void set_key (const SymmetricKey &key)
template<typename Alloc >
void set_key (const std::vector< byte, Alloc > &key)
void set_key (const byte key[], size_t length)
bool valid_iv_length (size_t iv_len) const
bool valid_keylength (size_t length) const

Protected Member Functions

virtual void chacha (byte output[64], const u32bit input[16])

Detailed Description

DJB's ChaCha (http://cr.yp.to/chacha.html)

Definition at line 18 of file chacha.h.


Member Typedef Documentation

typedef SCAN_Name Botan::StreamCipher::Spec [inherited]

Definition at line 73 of file stream_cipher.h.


Member Function Documentation

void Botan::ChaCha::chacha ( byte  output[64],
const u32bit  input[16] 
) [protected, virtual]

Definition at line 15 of file chacha.cpp.

References CHACHA_QUARTER_ROUND, and Botan::store_le().

Referenced by cipher(), and set_iv().

   {
   u32bit x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
          x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
          x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
          x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];

#define CHACHA_QUARTER_ROUND(a, b, c, d)   \
   do {                                    \
   a += b; d ^= a; d = rotate_left(d, 16); \
   c += d; b ^= c; b = rotate_left(b, 12); \
   a += b; d ^= a; d = rotate_left(d, 8);  \
   c += d; b ^= c; b = rotate_left(b, 7);  \
   } while(0)

   for(size_t i = 0; i != 10; ++i)
      {
      CHACHA_QUARTER_ROUND(x00, x04, x08, x12);
      CHACHA_QUARTER_ROUND(x01, x05, x09, x13);
      CHACHA_QUARTER_ROUND(x02, x06, x10, x14);
      CHACHA_QUARTER_ROUND(x03, x07, x11, x15);

      CHACHA_QUARTER_ROUND(x00, x05, x10, x15);
      CHACHA_QUARTER_ROUND(x01, x06, x11, x12);
      CHACHA_QUARTER_ROUND(x02, x07, x08, x13);
      CHACHA_QUARTER_ROUND(x03, x04, x09, x14);
      }

#undef CHACHA_QUARTER_ROUND

   store_le(x00 + input[ 0], output + 4 *  0);
   store_le(x01 + input[ 1], output + 4 *  1);
   store_le(x02 + input[ 2], output + 4 *  2);
   store_le(x03 + input[ 3], output + 4 *  3);
   store_le(x04 + input[ 4], output + 4 *  4);
   store_le(x05 + input[ 5], output + 4 *  5);
   store_le(x06 + input[ 6], output + 4 *  6);
   store_le(x07 + input[ 7], output + 4 *  7);
   store_le(x08 + input[ 8], output + 4 *  8);
   store_le(x09 + input[ 9], output + 4 *  9);
   store_le(x10 + input[10], output + 4 * 10);
   store_le(x11 + input[11], output + 4 * 11);
   store_le(x12 + input[12], output + 4 * 12);
   store_le(x13 + input[13], output + 4 * 13);
   store_le(x14 + input[14], output + 4 * 14);
   store_le(x15 + input[15], output + 4 * 15);
   }
void Botan::ChaCha::cipher ( const byte  in[],
byte  out[],
size_t  len 
) [virtual]

Encrypt or decrypt a message

Parameters:
inthe plaintext
outthe byte array to hold the output, i.e. the ciphertext
lenthe length of both in and out in bytes

Implements Botan::StreamCipher.

Definition at line 66 of file chacha.cpp.

References chacha(), and Botan::xor_buf().

   {
   while(length >= m_buffer.size() - m_position)
      {
      xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
      length -= (m_buffer.size() - m_position);
      in += (m_buffer.size() - m_position);
      out += (m_buffer.size() - m_position);
      chacha(&m_buffer[0], &m_state[0]);

      ++m_state[12];
      m_state[13] += (m_state[12] == 0);

      m_position = 0;
      }

   xor_buf(out, in, &m_buffer[m_position], length);

   m_position += length;
   }
void Botan::StreamCipher::cipher1 ( byte  buf[],
size_t  len 
) [inline, inherited]

Encrypt or decrypt a message

Parameters:
bufthe plaintext / ciphertext
lenthe length of buf in bytes

Definition at line 36 of file stream_cipher.h.

Referenced by Botan::SIV_Encryption::finish().

         { cipher(buf, buf, len); }
void Botan::ChaCha::clear ( ) [virtual]

Implements Botan::SymmetricAlgorithm.

Definition at line 154 of file chacha.cpp.

References Botan::zap().

   {
   zap(m_state);
   zap(m_buffer);
   m_position = 0;
   }
StreamCipher* Botan::ChaCha::clone ( ) const [inline, virtual]

Get a new object representing the same algorithm as *this

Implements Botan::StreamCipher.

Definition at line 36 of file chacha.h.

{ return new ChaCha; }
template<typename Alloc >
void Botan::StreamCipher::decrypt ( std::vector< byte, Alloc > &  inout) [inline, inherited]

Definition at line 48 of file stream_cipher.h.

         { cipher(&inout[0], &inout[0], inout.size()); }
template<typename Alloc >
void Botan::StreamCipher::encipher ( std::vector< byte, Alloc > &  inout) [inline, inherited]

Definition at line 40 of file stream_cipher.h.

         { cipher(&inout[0], &inout[0], inout.size()); }
template<typename Alloc >
void Botan::StreamCipher::encrypt ( std::vector< byte, Alloc > &  inout) [inline, inherited]

Definition at line 44 of file stream_cipher.h.

         { cipher(&inout[0], &inout[0], inout.size()); }
Returns:
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 28 of file chacha.h.

         {
         return Key_Length_Specification(16, 32, 16);
         }
size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const [inline, inherited]
Returns:
minimum allowed key length

Definition at line 36 of file sym_algo.h.

References Botan::Key_Length_Specification::maximum_keylength().

         {
         return key_spec().maximum_keylength();
         }
size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const [inline, inherited]
Returns:
maxmium allowed key length

Definition at line 44 of file sym_algo.h.

         {
         return key_spec().minimum_keylength();
         }
std::string Botan::ChaCha::name ( ) const [inline, virtual]

Implements Botan::SymmetricAlgorithm.

Definition at line 34 of file chacha.h.

Referenced by set_iv().

{ return "ChaCha"; }
void Botan::ChaCha::set_iv ( const byte  [],
size_t  iv_len 
) [virtual]

Resync the cipher using the IV

Parameters:
ivthe initialization vector
iv_lenthe length of the IV in bytes

Reimplemented from Botan::StreamCipher.

Definition at line 127 of file chacha.cpp.

References chacha(), Botan::load_le< u32bit >(), name(), and valid_iv_length().

   {
   if(!valid_iv_length(length))
      throw Invalid_IV_Length(name(), length);

   m_state[12] = 0;
   m_state[13] = 0;

   if(length == 8)
      {
      m_state[14] = load_le<u32bit>(iv, 0);
      m_state[15] = load_le<u32bit>(iv, 1);
      }
   else if(length == 12)
      {
      m_state[13] = load_le<u32bit>(iv, 0);
      m_state[14] = load_le<u32bit>(iv, 1);
      m_state[15] = load_le<u32bit>(iv, 2);
      }

   chacha(&m_buffer[0], &m_state[0]);
   ++m_state[12];
   m_state[13] += (m_state[12] == 0);

   m_position = 0;
   }
void Botan::SymmetricAlgorithm::set_key ( const SymmetricKey key) [inline, inherited]

Set the symmetric key of this object.

Parameters:
keythe SymmetricKey to be set.

Definition at line 63 of file sym_algo.h.

References Botan::OctetString::begin(), and Botan::OctetString::length().

Referenced by Botan::aont_package(), Botan::aont_unpackage(), botan_mac_set_key(), Botan::TLS::Session::decrypt(), Botan::TLS::Session::encrypt(), and Botan::pbkdf2().

         {
         set_key(key.begin(), key.length());
         }
template<typename Alloc >
void Botan::SymmetricAlgorithm::set_key ( const std::vector< byte, Alloc > &  key) [inline, inherited]

Definition at line 69 of file sym_algo.h.

         {
         set_key(&key[0], key.size());
         }
void Botan::SymmetricAlgorithm::set_key ( const byte  key[],
size_t  length 
) [inline, inherited]

Set the symmetric key of this object.

Parameters:
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 79 of file sym_algo.h.

         {
         if(!valid_keylength(length))
            throw Invalid_Key_Length(name(), length);
         key_schedule(key, length);
         }
bool Botan::ChaCha::valid_iv_length ( size_t  iv_len) const [inline, virtual]
Parameters:
iv_lenthe length of the IV in bytes
Returns:
if the length is valid for this algorithm

Reimplemented from Botan::StreamCipher.

Definition at line 25 of file chacha.h.

Referenced by set_iv().

         { return (iv_len == 8 || iv_len == 12); }
bool Botan::SymmetricAlgorithm::valid_keylength ( size_t  length) const [inline, inherited]

Check whether a given key length is valid for this algorithm.

Parameters:
lengththe key length to be checked.
Returns:
true if the key length is valid.

Definition at line 54 of file sym_algo.h.

Referenced by Botan::aont_package(), and Botan::aont_unpackage().

         {
         return key_spec().valid_keylength(length);
         }

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