Coverage Report

Created: 2023-09-25 06:34

/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
10.2k
std::unique_ptr<StreamCipher> StreamCipher::create(std::string_view algo_spec, std::string_view provider) {
40
10.2k
#if defined(BOTAN_HAS_SHAKE_CIPHER)
41
10.2k
   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
10.2k
   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
10.2k
#endif
53
54
10.2k
#if defined(BOTAN_HAS_CHACHA)
55
10.2k
   if(algo_spec == "ChaCha20") {
56
0
      if(provider.empty() || provider == "base") {
57
0
         return std::make_unique<ChaCha>(20);
58
0
      }
59
0
   }
60
10.2k
#endif
61
62
10.2k
#if defined(BOTAN_HAS_SALSA20)
63
10.2k
   if(algo_spec == "Salsa20") {
64
0
      if(provider.empty() || provider == "base") {
65
0
         return std::make_unique<Salsa20>();
66
0
      }
67
0
   }
68
10.2k
#endif
69
70
10.2k
   const SCAN_Name req(algo_spec);
71
72
10.2k
#if defined(BOTAN_HAS_CTR_BE)
73
10.2k
   if((req.algo_name() == "CTR-BE" || req.algo_name() == "CTR") && req.arg_count_between(1, 2)) {
74
231
      if(provider.empty() || provider == "base") {
75
231
         auto cipher = BlockCipher::create(req.arg(0));
76
231
         if(cipher) {
77
231
            size_t ctr_size = req.arg_as_integer(1, cipher->block_size());
78
231
            return std::make_unique<CTR_BE>(std::move(cipher), ctr_size);
79
231
         }
80
231
      }
81
231
   }
82
9.98k
#endif
83
84
9.98k
#if defined(BOTAN_HAS_CHACHA)
85
9.98k
   if(req.algo_name() == "ChaCha") {
86
1.11k
      if(provider.empty() || provider == "base") {
87
1.11k
         return std::make_unique<ChaCha>(req.arg_as_integer(0, 20));
88
1.11k
      }
89
1.11k
   }
90
8.87k
#endif
91
92
8.87k
#if defined(BOTAN_HAS_OFB)
93
8.87k
   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
8.87k
#endif
101
102
8.87k
#if defined(BOTAN_HAS_RC4)
103
104
8.87k
   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
8.87k
#endif
113
114
8.87k
   BOTAN_UNUSED(req);
115
8.87k
   BOTAN_UNUSED(provider);
116
117
8.87k
   return nullptr;
118
8.87k
}
119
120
//static
121
0
std::unique_ptr<StreamCipher> StreamCipher::create_or_throw(std::string_view algo, std::string_view provider) {
122
0
   if(auto sc = StreamCipher::create(algo, provider)) {
123
0
      return sc;
124
0
   }
125
0
   throw Lookup_Error("Stream cipher", algo, provider);
126
0
}
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 {
133
0
   return 0;
134
0
}
135
136
}  // namespace Botan