Botan  1.10.9
Public Member Functions | List of all members
Botan::Salsa20 Class Reference

#include <salsa20.h>

Inheritance diagram for Botan::Salsa20:
Botan::StreamCipher Botan::SymmetricAlgorithm Botan::Algorithm

Public Member Functions

void cipher (const byte in[], byte out[], size_t length)
 
void cipher1 (byte buf[], size_t len)
 
void clear ()
 
StreamCipherclone () const
 
Key_Length_Specification key_spec () const
 
size_t maximum_keylength () const
 
size_t minimum_keylength () const
 
std::string name () const
 
 Salsa20 ()
 
void set_iv (const byte iv[], size_t iv_len)
 
void set_key (const SymmetricKey &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
 

Detailed Description

DJB's Salsa20 (and XSalsa20)

Definition at line 18 of file salsa20.h.

Constructor & Destructor Documentation

Botan::Salsa20::Salsa20 ( )
inline

Definition at line 37 of file salsa20.h.

37 : state(16), buffer(64), position(0) {}

Member Function Documentation

void Botan::Salsa20::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 104 of file salsa20.cpp.

References Botan::MemoryRegion< T >::size(), and Botan::xor_buf().

105  {
106  while(length >= buffer.size() - position)
107  {
108  xor_buf(out, in, &buffer[position], buffer.size() - position);
109  length -= (buffer.size() - position);
110  in += (buffer.size() - position);
111  out += (buffer.size() - position);
112  salsa20(&buffer[0], &state[0]);
113 
114  ++state[8];
115  if(!state[8]) // if overflow in state[8]
116  ++state[9]; // carry to state[9]
117 
118  position = 0;
119  }
120 
121  xor_buf(out, in, &buffer[position], length);
122 
123  position += length;
124  }
size_t size() const
Definition: secmem.h:29
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
void Botan::StreamCipher::cipher1 ( byte  buf[],
size_t  len 
)
inlineinherited

Encrypt or decrypt a message

Parameters
bufthe plaintext / ciphertext
lenthe length of buf in bytes

Definition at line 34 of file stream_cipher.h.

Referenced by Botan::Lion::decrypt_n(), and Botan::Lion::encrypt_n().

35  { cipher(buf, buf, len); }
virtual void cipher(const byte in[], byte out[], size_t len)=0
void Botan::Salsa20::clear ( )
virtual

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 233 of file salsa20.cpp.

References Botan::zeroise().

234  {
235  zeroise(state);
236  zeroise(buffer);
237  position = 0;
238  }
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
StreamCipher* Botan::Salsa20::clone ( ) const
inlinevirtual

Get a new object representing the same algorithm as *this

Implements Botan::StreamCipher.

Definition at line 35 of file salsa20.h.

35 { return new Salsa20; }
Key_Length_Specification Botan::Salsa20::key_spec ( ) const
inlinevirtual
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 28 of file salsa20.h.

29  {
30  return Key_Length_Specification(16, 32, 16);
31  }
size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 33 of file sym_algo.h.

34  {
35  return key_spec().maximum_keylength();
36  }
size_t maximum_keylength() const
Definition: key_spec.h:69
virtual Key_Length_Specification key_spec() const =0
size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
maxmium allowed key length

Definition at line 41 of file sym_algo.h.

42  {
43  return key_spec().minimum_keylength();
44  }
size_t minimum_keylength() const
Definition: key_spec.h:61
virtual Key_Length_Specification key_spec() const =0
std::string Botan::Salsa20::name ( ) const
virtual
Returns
name of this algorithm

Implements Botan::Algorithm.

Definition at line 225 of file salsa20.cpp.

Referenced by set_iv().

226  {
227  return "Salsa20";
228  }
void Botan::Salsa20::set_iv ( const byte  iv[],
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 177 of file salsa20.cpp.

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

178  {
179  if(!valid_iv_length(length))
180  throw Invalid_IV_Length(name(), length);
181 
182  if(length == 8)
183  {
184  // Salsa20
185  state[6] = load_le<u32bit>(iv, 0);
186  state[7] = load_le<u32bit>(iv, 1);
187  }
188  else
189  {
190  // XSalsa20
191  state[6] = load_le<u32bit>(iv, 0);
192  state[7] = load_le<u32bit>(iv, 1);
193  state[8] = load_le<u32bit>(iv, 2);
194  state[9] = load_le<u32bit>(iv, 3);
195 
196  SecureVector<u32bit> hsalsa(8);
197  hsalsa20(&hsalsa[0], &state[0]);
198 
199  state[ 1] = hsalsa[0];
200  state[ 2] = hsalsa[1];
201  state[ 3] = hsalsa[2];
202  state[ 4] = hsalsa[3];
203  state[ 6] = load_le<u32bit>(iv, 4);
204  state[ 7] = load_le<u32bit>(iv, 5);
205  state[11] = hsalsa[4];
206  state[12] = hsalsa[5];
207  state[13] = hsalsa[6];
208  state[14] = hsalsa[7];
209  }
210 
211  state[8] = 0;
212  state[9] = 0;
213 
214  salsa20(&buffer[0], &state[0]);
215  ++state[8];
216  if(!state[8]) // if overflow in state[8]
217  ++state[9]; // carry to state[9]
218 
219  position = 0;
220  }
bool valid_iv_length(size_t iv_len) const
Definition: salsa20.h:25
u32bit load_le< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:183
std::string name() const
Definition: salsa20.cpp:225
void Botan::SymmetricAlgorithm::set_key ( const SymmetricKey key)
inlineinherited
void Botan::SymmetricAlgorithm::set_key ( const byte  key[],
size_t  length 
)
inlineinherited

Set the symmetric key of this object.

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

Definition at line 68 of file sym_algo.h.

69  {
70  if(!valid_keylength(length))
71  throw Invalid_Key_Length(name(), length);
72  key_schedule(key, length);
73  }
bool valid_keylength(size_t length) const
Definition: sym_algo.h:51
virtual std::string name() const =0
bool Botan::Salsa20::valid_iv_length ( size_t  iv_len) const
inlinevirtual
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 salsa20.h.

Referenced by set_iv().

26  { return (iv_len == 8 || iv_len == 24); }
bool Botan::SymmetricAlgorithm::valid_keylength ( size_t  length) const
inlineinherited

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 51 of file sym_algo.h.

Referenced by Botan::aont_package(), Botan::aont_unpackage(), Botan::HMAC_RNG::HMAC_RNG(), Botan::Lion::Lion(), Botan::Randpool::Randpool(), and Botan::EAX_Base::valid_keylength().

52  {
53  return key_spec().valid_keylength(length);
54  }
bool valid_keylength(size_t length) const
Definition: key_spec.h:51
virtual Key_Length_Specification key_spec() const =0

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