Coverage Report

Created: 2021-06-10 10:30

/src/botan/build/include/botan/stream_cipher.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Stream Cipher
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_STREAM_CIPHER_H_
9
#define BOTAN_STREAM_CIPHER_H_
10
11
#include <botan/sym_algo.h>
12
#include <string>
13
#include <memory>
14
#include <vector>
15
16
namespace Botan {
17
18
/**
19
* Base class for all stream ciphers
20
*/
21
class BOTAN_PUBLIC_API(2,0) StreamCipher : public SymmetricAlgorithm
22
   {
23
   public:
24
      virtual ~StreamCipher() = default;
25
26
      /**
27
      * Create an instance based on a name
28
      * If provider is empty then best available is chosen.
29
      * @param algo_spec algorithm name
30
      * @param provider provider implementation to use
31
      * @return a null pointer if the algo/provider combination cannot be found
32
      */
33
      static std::unique_ptr<StreamCipher>
34
         create(const std::string& algo_spec,
35
                const std::string& provider = "");
36
37
      /**
38
      * Create an instance based on a name
39
      * If provider is empty then best available is chosen.
40
      * @param algo_spec algorithm name
41
      * @param provider provider implementation to use
42
      * Throws a Lookup_Error if the algo/provider combination cannot be found
43
      */
44
      static std::unique_ptr<StreamCipher>
45
         create_or_throw(const std::string& algo_spec,
46
                         const std::string& provider = "");
47
48
      /**
49
      * @return list of available providers for this algorithm, empty if not available
50
      */
51
      static std::vector<std::string> providers(const std::string& algo_spec);
52
53
      /**
54
      * Encrypt or decrypt a message
55
      * @param in the plaintext
56
      * @param out the byte array to hold the output, i.e. the ciphertext
57
      * @param len the length of both in and out in bytes
58
      */
59
      virtual void cipher(const uint8_t in[], uint8_t out[], size_t len) = 0;
60
61
      /**
62
      * Write keystream bytes to a buffer
63
      * @param out the byte array to hold the keystream
64
      * @param len the length of out in bytes
65
      */
66
      virtual void write_keystream(uint8_t out[], size_t len)
67
0
         {
68
0
         clear_mem(out, len);
69
0
         cipher1(out, len);
70
0
         }
71
72
      /**
73
      * Encrypt or decrypt a message
74
      * The message is encrypted/decrypted in place.
75
      * @param buf the plaintext / ciphertext
76
      * @param len the length of buf in bytes
77
      */
78
      void cipher1(uint8_t buf[], size_t len)
79
55
         { cipher(buf, buf, len); }
80
81
      /**
82
      * Encrypt a message
83
      * The message is encrypted/decrypted in place.
84
      * @param inout the plaintext / ciphertext
85
      */
86
      template<typename Alloc>
87
         void encipher(std::vector<uint8_t, Alloc>& inout)
88
1.25k
         { cipher(inout.data(), inout.data(), inout.size()); }
89
90
      /**
91
      * Encrypt a message
92
      * The message is encrypted in place.
93
      * @param inout the plaintext / ciphertext
94
      */
95
      template<typename Alloc>
96
         void encrypt(std::vector<uint8_t, Alloc>& inout)
97
0
         { cipher(inout.data(), inout.data(), inout.size()); }
98
99
      /**
100
      * Decrypt a message in place
101
      * The message is decrypted in place.
102
      * @param inout the plaintext / ciphertext
103
      */
104
      template<typename Alloc>
105
         void decrypt(std::vector<uint8_t, Alloc>& inout)
106
         { cipher(inout.data(), inout.data(), inout.size()); }
107
108
      /**
109
      * Resync the cipher using the IV
110
      * @param iv the initialization vector
111
      * @param iv_len the length of the IV in bytes
112
      */
113
      virtual void set_iv(const uint8_t iv[], size_t iv_len) = 0;
114
115
      /**
116
      * Return the default (preferred) nonce length
117
      * If this function returns 0, then this cipher does not support nonces
118
      */
119
0
      virtual size_t default_iv_length() const { return 0; }
120
121
      /**
122
      * @param iv_len the length of the IV in bytes
123
      * @return if the length is valid for this algorithm
124
      */
125
0
      virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
126
127
      /**
128
      * @return a new object representing the same algorithm as *this
129
      */
130
      StreamCipher* clone() const
131
0
         {
132
0
         return this->new_object().release();
133
0
         }
134
135
      /**
136
      * @return new object representing the same algorithm as *this
137
      */
138
      virtual std::unique_ptr<StreamCipher> new_object() const = 0;
139
140
      /**
141
      * Set the offset and the state used later to generate the keystream
142
      * @param offset the offset where we begin to generate the keystream
143
      */
144
      virtual void seek(uint64_t offset) = 0;
145
146
      /**
147
      * @return provider information about this implementation. Default is "base",
148
      * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
149
      */
150
0
      virtual std::string provider() const { return "base"; }
151
   };
152
153
}
154
155
#endif