Botan 3.1.1
Crypto and TLS for C&
stream_cipher.cpp
Go to the documentation of this file.
1/*
2* Stream Ciphers
3* (C) 2015,2016 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/stream_cipher.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/scan_name.h>
12
13#if defined(BOTAN_HAS_CHACHA)
14 #include <botan/internal/chacha.h>
15#endif
16
17#if defined(BOTAN_HAS_SALSA20)
18 #include <botan/internal/salsa20.h>
19#endif
20
21#if defined(BOTAN_HAS_SHAKE_CIPHER)
22 #include <botan/internal/shake_cipher.h>
23#endif
24
25#if defined(BOTAN_HAS_CTR_BE)
26 #include <botan/internal/ctr.h>
27#endif
28
29#if defined(BOTAN_HAS_OFB)
30 #include <botan/internal/ofb.h>
31#endif
32
33#if defined(BOTAN_HAS_RC4)
34 #include <botan/internal/rc4.h>
35#endif
36
37namespace Botan {
38
39std::unique_ptr<StreamCipher> StreamCipher::create(std::string_view algo_spec, std::string_view provider) {
40#if defined(BOTAN_HAS_SHAKE_CIPHER)
41 if(algo_spec == "SHAKE-128" || algo_spec == "SHAKE-128-XOF") {
42 if(provider.empty() || provider == "base") {
43 return std::make_unique<SHAKE_128_Cipher>();
44 }
45 }
46
47 if(algo_spec == "SHAKE-256" || algo_spec == "SHAKE-256-XOF") {
48 if(provider.empty() || provider == "base") {
49 return std::make_unique<SHAKE_256_Cipher>();
50 }
51 }
52#endif
53
54#if defined(BOTAN_HAS_CHACHA)
55 if(algo_spec == "ChaCha20") {
56 if(provider.empty() || provider == "base") {
57 return std::make_unique<ChaCha>(20);
58 }
59 }
60#endif
61
62#if defined(BOTAN_HAS_SALSA20)
63 if(algo_spec == "Salsa20") {
64 if(provider.empty() || provider == "base") {
65 return std::make_unique<Salsa20>();
66 }
67 }
68#endif
69
70 const SCAN_Name req(algo_spec);
71
72#if defined(BOTAN_HAS_CTR_BE)
73 if((req.algo_name() == "CTR-BE" || req.algo_name() == "CTR") && req.arg_count_between(1, 2)) {
74 if(provider.empty() || provider == "base") {
75 auto cipher = BlockCipher::create(req.arg(0));
76 if(cipher) {
77 size_t ctr_size = req.arg_as_integer(1, cipher->block_size());
78 return std::make_unique<CTR_BE>(std::move(cipher), ctr_size);
79 }
80 }
81 }
82#endif
83
84#if defined(BOTAN_HAS_CHACHA)
85 if(req.algo_name() == "ChaCha") {
86 if(provider.empty() || provider == "base") {
87 return std::make_unique<ChaCha>(req.arg_as_integer(0, 20));
88 }
89 }
90#endif
91
92#if defined(BOTAN_HAS_OFB)
93 if(req.algo_name() == "OFB" && req.arg_count() == 1) {
94 if(provider.empty() || provider == "base") {
95 if(auto cipher = BlockCipher::create(req.arg(0))) {
96 return std::make_unique<OFB>(std::move(cipher));
97 }
98 }
99 }
100#endif
101
102#if defined(BOTAN_HAS_RC4)
103
104 if(req.algo_name() == "RC4" || req.algo_name() == "ARC4" || req.algo_name() == "MARK-4") {
105 const size_t skip = (req.algo_name() == "MARK-4") ? 256 : req.arg_as_integer(0, 0);
106
107 if(provider.empty() || provider == "base") {
108 return std::make_unique<RC4>(skip);
109 }
110 }
111
112#endif
113
114 BOTAN_UNUSED(req);
116
117 return nullptr;
118}
119
120//static
121std::unique_ptr<StreamCipher> StreamCipher::create_or_throw(std::string_view algo, std::string_view provider) {
122 if(auto sc = StreamCipher::create(algo, provider)) {
123 return sc;
124 }
125 throw Lookup_Error("Stream cipher", algo, provider);
126}
127
128std::vector<std::string> StreamCipher::providers(std::string_view algo_spec) {
129 return probe_providers_of<StreamCipher>(algo_spec);
130}
131
133 return 0;
134}
135
136} // namespace Botan
#define BOTAN_UNUSED
Definition: assert.h:118
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
std::string arg(size_t i) const
Definition: scan_name.cpp:119
size_t arg_count() const
Definition: scan_name.h:49
const std::string algo_name() const
Definition: scan_name.h:44
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:133
bool arg_count_between(size_t lower, size_t upper) const
Definition: scan_name.h:56
static std::unique_ptr< StreamCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
virtual size_t default_iv_length() const
static std::vector< std::string > providers(std::string_view algo_spec)
void cipher(const uint8_t in[], uint8_t out[], size_t len)
Definition: stream_cipher.h:56
virtual std::string provider() const
Definition: alg_id.cpp:13