Coverage Report

Created: 2023-02-22 06:14

/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
      *
64
      * The contents of @p out are ignored/overwritten
65
      *
66
      * @param out the byte array to hold the keystream
67
      * @param len the length of out in bytes
68
      */
69
      virtual void write_keystream(uint8_t out[], size_t len)
70
0
         {
71
0
         clear_mem(out, len);
72
0
         cipher1(out, len);
73
0
         }
74
75
      /**
76
      * Encrypt or decrypt a message
77
      * The message is encrypted/decrypted in place.
78
      * @param buf the plaintext / ciphertext
79
      * @param len the length of buf in bytes
80
      */
81
      void cipher1(uint8_t buf[], size_t len)
82
2.22k
         { cipher(buf, buf, len); }
83
84
      /**
85
      * Encrypt a message
86
      * The message is encrypted/decrypted in place.
87
      * @param inout the plaintext / ciphertext
88
      */
89
      template<typename Alloc>
90
         void encipher(std::vector<uint8_t, Alloc>& inout)
91
655
         { cipher(inout.data(), inout.data(), inout.size()); }
92
93
      /**
94
      * Encrypt a message
95
      * The message is encrypted in place.
96
      * @param inout the plaintext / ciphertext
97
      */
98
      template<typename Alloc>
99
         void encrypt(std::vector<uint8_t, Alloc>& inout)
100
         { cipher(inout.data(), inout.data(), inout.size()); }
101
102
      /**
103
      * Decrypt a message in place
104
      * The message is decrypted in place.
105
      * @param inout the plaintext / ciphertext
106
      */
107
      template<typename Alloc>
108
         void decrypt(std::vector<uint8_t, Alloc>& inout)
109
         { cipher(inout.data(), inout.data(), inout.size()); }
110
111
      /**
112
      * Resync the cipher using the IV
113
      * @param iv the initialization vector
114
      * @param iv_len the length of the IV in bytes
115
      */
116
      virtual void set_iv(const uint8_t iv[], size_t iv_len) = 0;
117
118
      /**
119
      * Return the default (preferred) nonce length
120
      * If this function returns 0, then this cipher does not support nonces
121
      */
122
0
      virtual size_t default_iv_length() const { return 0; }
123
124
      /**
125
      * @param iv_len the length of the IV in bytes
126
      * @return if the length is valid for this algorithm
127
      */
128
0
      virtual bool valid_iv_length(size_t iv_len) const { return (iv_len == 0); }
129
130
      /**
131
      * @return a new object representing the same algorithm as *this
132
      */
133
      StreamCipher* clone() const
134
0
         {
135
0
         return this->new_object().release();
136
0
         }
137
138
      /**
139
      * @return new object representing the same algorithm as *this
140
      */
141
      virtual std::unique_ptr<StreamCipher> new_object() const = 0;
142
143
      /**
144
      * Set the offset and the state used later to generate the keystream
145
      * @param offset the offset where we begin to generate the keystream
146
      */
147
      virtual void seek(uint64_t offset) = 0;
148
149
      /**
150
      * @return provider information about this implementation. Default is "base",
151
      * might also return "sse2", "avx2" or some other arbitrary string.
152
      */
153
0
      virtual std::string provider() const { return "base"; }
154
   };
155
156
}
157
158
#endif