Coverage Report

Created: 2023-02-22 06:14

/src/botan/build/include/botan/sym_algo.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Symmetric Algorithm Base Class
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_SYMMETRIC_ALGORITHM_H_
9
#define BOTAN_SYMMETRIC_ALGORITHM_H_
10
11
#include <botan/symkey.h>
12
#include <botan/types.h>
13
14
namespace Botan {
15
16
/**
17
* Represents the length requirements on an algorithm key
18
*/
19
class BOTAN_PUBLIC_API(2,0) Key_Length_Specification final
20
   {
21
   public:
22
      /**
23
      * Constructor for fixed length keys
24
      * @param keylen the supported key length
25
      */
26
      explicit Key_Length_Specification(size_t keylen) :
27
         m_min_keylen(keylen),
28
         m_max_keylen(keylen),
29
         m_keylen_mod(1)
30
518
         {
31
518
         }
32
33
      /**
34
      * Constructor for variable length keys
35
      * @param min_k the smallest supported key length
36
      * @param max_k the largest supported key length
37
      * @param k_mod the number of bytes the key must be a multiple of
38
      */
39
      Key_Length_Specification(size_t min_k,
40
                               size_t max_k,
41
                               size_t k_mod = 1) :
42
         m_min_keylen(min_k),
43
         m_max_keylen(max_k ? max_k : min_k),
44
         m_keylen_mod(k_mod)
45
2.01M
         {
46
2.01M
         }
47
48
      /**
49
      * @param length is a key length in bytes
50
      * @return true iff this length is a valid length for this algo
51
      */
52
      bool valid_keylength(size_t length) const
53
2.01M
         {
54
2.01M
         return ((length >= m_min_keylen) &&
55
2.01M
                 (length <= m_max_keylen) &&
56
2.01M
                 (length % m_keylen_mod == 0));
57
2.01M
         }
58
59
      /**
60
      * @return minimum key length in bytes
61
      */
62
      size_t minimum_keylength() const
63
0
         {
64
0
         return m_min_keylen;
65
0
         }
66
67
      /**
68
      * @return maximum key length in bytes
69
      */
70
      size_t maximum_keylength() const
71
0
         {
72
0
         return m_max_keylen;
73
0
         }
74
75
      /**
76
      * @return key length multiple in bytes
77
      */
78
      size_t keylength_multiple() const
79
0
         {
80
0
         return m_keylen_mod;
81
0
         }
82
83
      /*
84
      * Multiplies all length requirements with the given factor
85
      * @param n the multiplication factor
86
      * @return a key length specification multiplied by the factor
87
      */
88
      Key_Length_Specification multiple(size_t n) const
89
1.16k
         {
90
1.16k
         return Key_Length_Specification(n * m_min_keylen,
91
1.16k
                                         n * m_max_keylen,
92
1.16k
                                         n * m_keylen_mod);
93
1.16k
         }
94
95
   private:
96
      size_t m_min_keylen, m_max_keylen, m_keylen_mod;
97
   };
98
99
/**
100
* This class represents a symmetric algorithm object.
101
*/
102
class BOTAN_PUBLIC_API(2,0) SymmetricAlgorithm
103
   {
104
   public:
105
29.6k
      virtual ~SymmetricAlgorithm() = default;
106
107
      /**
108
      * Reset the internal state. This includes not just the key, but
109
      * any partial message that may have been in process.
110
      */
111
      virtual void clear() = 0;
112
113
      /**
114
      * @return object describing limits on key size
115
      */
116
      virtual Key_Length_Specification key_spec() const = 0;
117
118
      /**
119
      * @return maximum allowed key length
120
      */
121
      size_t maximum_keylength() const
122
0
         {
123
0
         return key_spec().maximum_keylength();
124
0
         }
125
126
      /**
127
      * @return minimum allowed key length
128
      */
129
      size_t minimum_keylength() const
130
0
         {
131
0
         return key_spec().minimum_keylength();
132
0
         }
133
134
      /**
135
      * Check whether a given key length is valid for this algorithm.
136
      * @param length the key length to be checked.
137
      * @return true if the key length is valid.
138
      */
139
      bool valid_keylength(size_t length) const
140
2.01M
         {
141
2.01M
         return key_spec().valid_keylength(length);
142
2.01M
         }
143
144
      /**
145
      * Set the symmetric key of this object.
146
      * @param key the SymmetricKey to be set.
147
      */
148
      void set_key(const SymmetricKey& key)
149
3.66k
         {
150
3.66k
         set_key(key.begin(), key.length());
151
3.66k
         }
152
153
      template<typename Alloc>
154
      void set_key(const std::vector<uint8_t, Alloc>& key)
155
2.35k
         {
156
2.35k
         set_key(key.data(), key.size());
157
2.35k
         }
void Botan::SymmetricAlgorithm::set_key<std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Line
Count
Source
155
1.53k
         {
156
1.53k
         set_key(key.data(), key.size());
157
1.53k
         }
void Botan::SymmetricAlgorithm::set_key<Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&)
Line
Count
Source
155
821
         {
156
821
         set_key(key.data(), key.size());
157
821
         }
158
159
      /**
160
      * Set the symmetric key of this object.
161
      * @param key the to be set as a byte array.
162
      * @param length in bytes of key param
163
      */
164
      void set_key(const uint8_t key[], size_t length);
165
166
      /**
167
      * @return the algorithm name
168
      */
169
      virtual std::string name() const = 0;
170
171
      /**
172
      * @return true if a key has been set on this object
173
      */
174
      virtual bool has_keying_material() const = 0;
175
176
   protected:
177
      void assert_key_material_set() const
178
3.68M
         {
179
3.68M
         assert_key_material_set(has_keying_material());
180
3.68M
         }
181
182
      void assert_key_material_set(bool predicate) const
183
3.68M
         {
184
3.68M
         if(!predicate)
185
0
            throw_key_not_set_error();
186
3.68M
         }
187
188
   private:
189
      void throw_key_not_set_error() const;
190
191
      /**
192
      * Run the key schedule
193
      * @param key the key
194
      * @param length of key
195
      */
196
      virtual void key_schedule(const uint8_t key[], size_t length) = 0;
197
   };
198
199
}
200
201
#endif