Botan 1.10.17
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 byte key[], size_t length)
void set_key (const SymmetricKey &key)
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) {}

Referenced by clone().

Member Function Documentation

◆ cipher()

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.

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 }
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21

References Botan::xor_buf().

◆ cipher1()

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.

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

References cipher().

◆ clear()

void Botan::Salsa20::clear ( )
virtual

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 233 of file salsa20.cpp.

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

References Botan::zeroise().

◆ 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; }

References Salsa20().

◆ 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 }
Key_Length_Specification(size_t keylen)
Definition key_spec.h:25

◆ maximum_keylength()

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

References key_spec().

◆ 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 }
size_t minimum_keylength() const
Definition key_spec.h:61

References key_spec().

◆ name()

std::string Botan::Salsa20::name ( ) const
virtual
Returns
name of this algorithm

Implements Botan::Algorithm.

Definition at line 225 of file salsa20.cpp.

226 {
227 return "Salsa20";
228 }

Referenced by set_iv().

◆ set_iv()

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.

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 }
std::string name() const
Definition salsa20.cpp:225
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
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition exceptn.h:80

References Botan::Invalid_IV_Length::Invalid_IV_Length(), Botan::load_le(), name(), and valid_iv_length().

◆ set_key() [1/2]

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 }
virtual std::string name() const =0
bool valid_keylength(size_t length) const
Definition sym_algo.h:51
Invalid_Key_Length(const std::string &name, size_t length)
Definition exceptn.h:57

References Botan::Algorithm::name(), and valid_keylength().

◆ set_key() [2/2]

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

◆ valid_iv_length()

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.

26 { return (iv_len == 8 || iv_len == 24); }

Referenced by set_iv().

◆ valid_keylength()

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.

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

References key_spec().

Referenced by Botan::aont_package(), Botan::aont_unpackage(), set_key(), and Botan::EAX_Base::valid_keylength().


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