Botan: Botan::Salsa20 Class Reference

Botan 1.10.16

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

`#include <salsa20.h>`

Inheritance diagram for Botan::Salsa20:

## Public Member Functions
void | cipher (const byte in[], byte out[], size_t length)
void | cipher1 (byte buf[], size_t len)
void | clear ()
StreamCipher * | clone () 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

## ◆ Salsa20()

Botan::Salsa20::Salsa20 | ( |  | ) |
| inline

Definition at line 37 of file salsa20.h.

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

## Member Function Documentation

## ◆ cipher()

void Botan::Salsa20::cipher | ( | const byte | in[],
|  | byte | out[],
|  | size_t | len
| ) |  |
| virtual

Encrypt or decrypt a message

Parameters

  in | the plaintext
  out | the byte array to hold the output, i.e. the ciphertext
  len | the length of both in and out in bytes

Implements Botan::StreamCipher.

Definition at line 104 of file salsa20.cpp.

References clear(), Botan::load_le< u32bit >(), set_iv(), 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 }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21

## ◆ cipher1()

void Botan::StreamCipher::cipher1 | ( | byte | buf[],
|  | size_t | len
| ) |  |
| inlineinherited

Encrypt or decrypt a message

Parameters

  buf | the plaintext / ciphertext
  len | the 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); }
Botan::StreamCipher::cipher
virtual void cipher(const byte in[], byte out[], size_t len)=0

## ◆ clear()

void Botan::Salsa20::clear | ( |  | ) |
| virtual

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 233 of file salsa20.cpp.

References Botan::zeroise().

Referenced by cipher().

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

## ◆ clone()

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; }
Botan::Salsa20::Salsa20
Salsa20()
Definition: salsa20.h:37

## ◆ key_spec()

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 }

## ◆ maximum_keylength()

size_t Botan::SymmetricAlgorithm::maximum_keylength | ( |  | ) | const
| inlineinherited

Returns
  minimum allowed key length

Definition at line 33 of file sym_algo.h.

Referenced by Botan::Cascade_Cipher::decrypt_n(), and Botan::ANSI_X931_RNG::randomize().

34 {
35 return key_spec().maximum_keylength();
36 }
Botan::Key_Length_Specification::maximum_keylength
size_t maximum_keylength() const
Definition: key_spec.h:69
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0

## ◆ minimum_keylength()

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 }
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
Botan::Key_Length_Specification::minimum_keylength
size_t minimum_keylength() const
Definition: key_spec.h:61

## ◆ name()

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 }

## ◆ set_iv()

void Botan::Salsa20::set_iv | ( | const byte | iv[],
|  | size_t | iv_len
| ) |  |
| virtual

Resync the cipher using the IV

Parameters

  iv | the initialization vector
  iv_len | the 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().

Referenced by cipher().

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 }
Botan::load_le< u32bit >
u32bit load_le< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:183
Botan::Salsa20::valid_iv_length
bool valid_iv_length(size_t iv_len) const
Definition: salsa20.h:25
Botan::Salsa20::name
std::string name() const
Definition: salsa20.cpp:225

## ◆ set_key() [1/2]

void Botan::SymmetricAlgorithm::set_key | ( | const SymmetricKey & | key | ) |
| inlineinherited

Set the symmetric key of this object.

Parameters

  key | the SymmetricKey to be set.

Definition at line 60 of file sym_algo.h.

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

Referenced by Botan::aont_unpackage(), Botan::CTR_BE::clear(), Botan::OFB::clear(), Botan::DESX::decrypt_n(), Botan::Cascade_Cipher::decrypt_n(), Botan::Lion::decrypt_n(), Botan::PKCS5_PBKDF2::derive_key(), Botan::ECB_Decryption::ECB_Decryption(), Botan::ECB_Encryption::ECB_Encryption(), Botan::Lion::encrypt_n(), Botan::HMAC_RNG::HMAC_RNG(), Botan::MAC_Filter::MAC_Filter(), Botan::ANSI_X931_RNG::randomize(), Botan::Randpool::randomize(), Botan::HMAC_RNG::reseed(), Botan::EAX_Base::set_key(), Botan::XTS_Encryption::set_key(), Botan::XTS_Decryption::set_key(), Botan::Record_Writer::set_keys(), Botan::Record_Reader::set_keys(), Botan::StreamCipher_Filter::StreamCipher_Filter(), Botan::DLIES_Decryptor::~DLIES_Decryptor(), and Botan::DLIES_Encryptor::~DLIES_Encryptor().

61 { set_key(key.begin(), key.length()); }
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60

## ◆ set_key() [2/2]

void Botan::SymmetricAlgorithm::set_key | ( | const byte | key[],
|  | size_t | length
| ) |  |
| inlineinherited

Set the symmetric key of this object.

Parameters

  key | the to be set as a byte array.
  length | in 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 }
Botan::SymmetricAlgorithm::valid_keylength
bool valid_keylength(size_t length) const
Definition: sym_algo.h:51
Botan::Algorithm::name
virtual std::string name() const =0

## ◆ valid_iv_length()

bool Botan::Salsa20::valid_iv_length | ( | size_t | iv_len | ) | const
| inlinevirtual

Parameters

  iv_len | the 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); }

## ◆ valid_keylength()

bool Botan::SymmetricAlgorithm::valid_keylength | ( | size_t | length | ) | const
| inlineinherited

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

Parameters

  length | the 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_unpackage(), Botan::HMAC_RNG::HMAC_RNG(), Botan::Lion::Lion(), and Botan::Randpool::Randpool().

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

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

- src/stream/salsa20/salsa20.h
- src/stream/salsa20/salsa20.cpp