Coverage Report

Created: 2023-06-07 07:00

/src/botan/src/lib/stream/stream_cipher.cpp
Line
Count
Source (jump to first uncovered line)
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
37
namespace Botan {
38
39
1
std::unique_ptr<StreamCipher> StreamCipher::create(std::string_view algo_spec, std::string_view provider) {
40
1
#if defined(BOTAN_HAS_SHAKE_CIPHER)
41
1
   if(algo_spec == "SHAKE-128" || algo_spec == "SHAKE-128-XOF") {
42
0
      if(provider.empty() || provider == "base") {
43
0
         return std::make_unique<SHAKE_128_Cipher>();
44
0
      }
45
0
   }
46
47
1
   if(algo_spec == "SHAKE-256" || algo_spec == "SHAKE-256-XOF") {
48
0
      if(provider.empty() || provider == "base") {
49
0
         return std::make_unique<SHAKE_256_Cipher>();
50
0
      }
51
0
   }
52
1
#endif
53
54
1
#if defined(BOTAN_HAS_CHACHA)
55
1
   if(algo_spec == "ChaCha20") {
56
0
      if(provider.empty() || provider == "base") {
57
0
         return std::make_unique<ChaCha>(20);
58
0
      }
59
0
   }
60
1
#endif
61
62
1
#if defined(BOTAN_HAS_SALSA20)
63
1
   if(algo_spec == "Salsa20") {
64
0
      if(provider.empty() || provider == "base") {
65
0
         return std::make_unique<Salsa20>();
66
0
      }
67
0
   }
68
1
#endif
69
70
1
   const SCAN_Name req(algo_spec);
71
72
1
#if defined(BOTAN_HAS_CTR_BE)
73
1
   if((req.algo_name() == "CTR-BE" || req.algo_name() == "CTR") && req.arg_count_between(1, 2)) {
74
0
      if(provider.empty() || provider == "base") {
75
0
         auto cipher = BlockCipher::create(req.arg(0));
76
0
         if(cipher) {
77
0
            size_t ctr_size = req.arg_as_integer(1, cipher->block_size());
78
0
            return std::make_unique<CTR_BE>(std::move(cipher), ctr_size);
79
0
         }
80
0
      }
81
0
   }
82
1
#endif
83
84
1
#if defined(BOTAN_HAS_CHACHA)
85
1
   if(req.algo_name() == "ChaCha") {
86
1
      if(provider.empty() || provider == "base") {
87
1
         return std::make_unique<ChaCha>(req.arg_as_integer(0, 20));
88
1
      }
89
1
   }
90
0
#endif
91
92
0
#if defined(BOTAN_HAS_OFB)
93
0
   if(req.algo_name() == "OFB" && req.arg_count() == 1) {
94
0
      if(provider.empty() || provider == "base") {
95
0
         if(auto cipher = BlockCipher::create(req.arg(0))) {
96
0
            return std::make_unique<OFB>(std::move(cipher));
97
0
         }
98
0
      }
99
0
   }
100
0
#endif
101
102
0
#if defined(BOTAN_HAS_RC4)
103
104
0
   if(req.algo_name() == "RC4" || req.algo_name() == "ARC4" || req.algo_name() == "MARK-4") {
105
0
      const size_t skip = (req.algo_name() == "MARK-4") ? 256 : req.arg_as_integer(0, 0);
106
107
0
      if(provider.empty() || provider == "base") {
108
0
         return std::make_unique<RC4>(skip);
109
0
      }
110
0
   }
111
112
0
#endif
113
114
0
   BOTAN_UNUSED(req);
115
0
   BOTAN_UNUSED(provider);
116
117
0
   return nullptr;
118
0
}
119
120
//static
121
1
std::unique_ptr<StreamCipher> StreamCipher::create_or_throw(std::string_view algo, std::string_view provider) {
122
1
   if(auto sc = StreamCipher::create(algo, provider)) {
123
1
      return sc;
124
1
   }
125
0
   throw Lookup_Error("Stream cipher", algo, provider);
126
1
}
127
128
0
std::vector<std::string> StreamCipher::providers(std::string_view algo_spec) {
129
0
   return probe_providers_of<StreamCipher>(algo_spec);
130
0
}
131
132
0
size_t StreamCipher::default_iv_length() const { return 0; }
133
134
}  // namespace Botan