Botan 3.14.1
Crypto and TLS for C&
salsa20.h
Go to the documentation of this file.
1/*
2* Salsa20 / XSalsa20
3* (C) 1999-2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_SALSA20_H_
9#define BOTAN_SALSA20_H_
10
11#include <botan/stream_cipher.h>
12
13namespace Botan {
14
15/**
16* DJB's Salsa20 (and XSalsa20)
17*/
18class Salsa20 final : public StreamCipher {
19 public:
20 std::string provider() const override;
21 bool valid_iv_length(size_t iv_len) const override;
22 size_t default_iv_length() const override;
23 Key_Length_Specification key_spec() const override;
24 void clear() override;
25 std::string name() const override;
26 std::unique_ptr<StreamCipher> new_object() const override;
27 bool has_keying_material() const override;
28 void seek(uint64_t offset) override;
29
30 /**
31 * Seek to the start of the specified 64-byte block. Unlike seek, this
32 * can reach the entire 64-bit counter space.
33 */
34 void seek_block(uint64_t block);
35
36 bool supports_seek() const override { return true; }
37
38 // Salsa uses a 64-bit counter which we consider sufficient
39 std::optional<uint64_t> remaining_keystream_bytes() const override { return {}; }
40
41 size_t buffer_size() const override;
42
43 // For internal use only
44 static void salsa_core(uint8_t output[64], const uint32_t input[16], size_t rounds);
45
46 // For internal use only
47 static void hsalsa20(uint32_t output[8], const uint32_t input[16]);
48
49 protected:
50 void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override;
51 void generate_keystream(uint8_t out[], size_t len) override;
52 void set_iv_bytes(const uint8_t iv[], size_t iv_len) override;
53
54 private:
55 void key_schedule(std::span<const uint8_t> key) override;
56
57 void initialize_state();
58
59 static size_t parallelism();
60
61 static void salsa20(uint8_t output[], size_t output_blocks, uint32_t state[16], size_t rounds);
62
63#if defined(BOTAN_HAS_SALSA20_SIMD32)
64 static void salsa20_simd32_x4(uint8_t output[64 * 4], uint32_t state[16], size_t rounds);
65#endif
66
67#if defined(BOTAN_HAS_SALSA20_SIMD8X32)
68 static void salsa20_simd8x32_x8(uint8_t output[64 * 8], uint32_t state[16], size_t rounds);
69#endif
70
71#if defined(BOTAN_HAS_SALSA20_AVX512)
72 static void salsa20_avx512_x16(uint8_t output[64 * 16], uint32_t state[16], size_t rounds);
73#endif
74
78 size_t m_position = 0;
79};
80
81} // namespace Botan
82
83#endif
void seek(uint64_t offset) override
Definition salsa20.cpp:396
bool has_keying_material() const override
Definition salsa20.cpp:294
void clear() override
Definition salsa20.cpp:389
void seek_block(uint64_t block)
Definition salsa20.cpp:401
size_t buffer_size() const override
Definition salsa20.cpp:298
bool supports_seek() const override
Definition salsa20.h:36
std::optional< uint64_t > remaining_keystream_bytes() const override
Definition salsa20.h:39
void set_iv_bytes(const uint8_t iv[], size_t iv_len) override
Definition salsa20.cpp:320
void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override
Definition salsa20.cpp:214
static void hsalsa20(uint32_t output[8], const uint32_t input[16])
Definition salsa20.cpp:35
std::string provider() const override
Definition salsa20.cpp:145
bool valid_iv_length(size_t iv_len) const override
Definition salsa20.cpp:366
size_t default_iv_length() const override
Definition salsa20.cpp:370
static void salsa_core(uint8_t output[64], const uint32_t input[16], size_t rounds)
Definition salsa20.cpp:79
std::unique_ptr< StreamCipher > new_object() const override
Definition salsa20.cpp:378
std::string name() const override
Definition salsa20.cpp:382
Key_Length_Specification key_spec() const override
Definition salsa20.cpp:374
void generate_keystream(uint8_t out[], size_t len) override
Definition salsa20.cpp:235
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128