Botan 3.14.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/mem_ops.h>
12#include <botan/internal/algorithm_spec.h>
13#include <botan/internal/probe_providers.h>
14
15#if defined(BOTAN_HAS_CHACHA)
16 #include <botan/internal/chacha.h>
17#endif
18
19#if defined(BOTAN_HAS_SALSA20)
20 #include <botan/internal/salsa20.h>
21#endif
22
23#if defined(BOTAN_HAS_SHAKE_CIPHER)
24 #include <botan/internal/shake_cipher.h>
25#endif
26
27#if defined(BOTAN_HAS_CTR_BE)
28 #include <botan/internal/ctr.h>
29#endif
30
31#if defined(BOTAN_HAS_OFB)
32 #include <botan/internal/ofb.h>
33#endif
34
35#if defined(BOTAN_HAS_RC4)
36 #include <botan/internal/rc4.h>
37#endif
38
39namespace Botan {
40
41std::unique_ptr<StreamCipher> StreamCipher::create(std::string_view algo_spec, std::string_view provider) {
42#if defined(BOTAN_HAS_SHAKE_CIPHER)
43 if(algo_spec == "SHAKE-128" || algo_spec == "SHAKE-128-XOF") {
44 if(provider.empty() || provider == "base") {
45 return std::make_unique<SHAKE_128_Cipher>();
46 }
47 }
48
49 if(algo_spec == "SHAKE-256" || algo_spec == "SHAKE-256-XOF") {
50 if(provider.empty() || provider == "base") {
51 return std::make_unique<SHAKE_256_Cipher>();
52 }
53 }
54#endif
55
56#if defined(BOTAN_HAS_CHACHA)
57 if(algo_spec == "ChaCha20") {
58 if(provider.empty() || provider == "base") {
59 return std::make_unique<ChaCha>(20);
60 }
61 }
62#endif
63
64#if defined(BOTAN_HAS_SALSA20)
65 if(algo_spec == "Salsa20") {
66 if(provider.empty() || provider == "base") {
67 return std::make_unique<Salsa20>();
68 }
69 }
70#endif
71
72 const AlgorithmSpec req(algo_spec);
73
74#if defined(BOTAN_HAS_CTR_BE)
75 if(auto m = req.match("CTR-BE|CTR({cipher},{ctr_size:int?})")) {
76 if(provider.empty() || provider == "base") {
77 if(auto cipher = BlockCipher::create(m->str("cipher"))) {
78 const size_t ctr_size = m->has("ctr_size") ? m->integer("ctr_size") : cipher->block_size();
79 return std::make_unique<CTR_BE>(std::move(cipher), ctr_size);
80 }
81 }
82 }
83#endif
84
85#if defined(BOTAN_HAS_CHACHA)
86 if(auto m = req.match("ChaCha({rounds:int=20})")) {
87 if(provider.empty() || provider == "base") {
88 return std::make_unique<ChaCha>(m->integer("rounds"));
89 }
90 }
91#endif
92
93#if defined(BOTAN_HAS_OFB)
94 if(auto m = req.match("OFB({cipher})")) {
95 if(provider.empty() || provider == "base") {
96 if(auto cipher = BlockCipher::create(m->str("cipher"))) {
97 return std::make_unique<OFB>(std::move(cipher));
98 }
99 }
100 }
101#endif
102
103#if defined(BOTAN_HAS_RC4)
104 if(auto m = req.match("RC4|ARC4({skip:int=0})")) {
105 if(provider.empty() || provider == "base") {
106 return std::make_unique<RC4>(m->integer("skip"));
107 }
108 }
109
110 if(req.matches("MARK-4")) {
111 if(provider.empty() || provider == "base") {
112 return std::make_unique<RC4>(256);
113 }
114 }
115#endif
116
117 BOTAN_UNUSED(req);
119
120 return nullptr;
121}
122
123//static
124std::unique_ptr<StreamCipher> StreamCipher::create_or_throw(std::string_view algo, std::string_view provider) {
125 if(auto sc = StreamCipher::create(algo, provider)) {
126 return sc;
127 }
128 throw Lookup_Error("Stream cipher", algo, provider);
129}
130
131std::vector<std::string> StreamCipher::providers(std::string_view algo_spec) {
132 return probe_providers_of<StreamCipher>(algo_spec);
133}
134
135void StreamCipher::cipher(std::span<const uint8_t> in, std::span<uint8_t> out) {
136 BOTAN_ARG_CHECK(in.size() <= out.size(), "Output buffer of stream cipher must be at least as long as input buffer");
137 cipher_bytes(in.data(), out.data(), in.size());
138}
139
141 return 0;
142}
143
144void StreamCipher::generate_keystream(uint8_t out[], size_t len) {
145 clear_mem(out, len);
146 cipher1(out, len);
147}
148
149} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
void cipher1(uint8_t buf[], size_t len)
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
virtual void generate_keystream(uint8_t out[], size_t len)
virtual void cipher_bytes(const uint8_t in[], uint8_t out[], size_t len)=0
static std::vector< std::string > providers(std::string_view algo_spec)
void cipher(const uint8_t in[], uint8_t out[], size_t len)
virtual std::string provider() const
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118