/src/Botan-3.13.0/build/include/public/botan/symkey.h
Line | Count | Source |
1 | | /* |
2 | | * OctetString |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_SYMKEY_H_ |
9 | | #define BOTAN_SYMKEY_H_ |
10 | | |
11 | | #include <botan/secmem.h> |
12 | | #include <span> |
13 | | #include <string> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | class RandomNumberGenerator; |
18 | | |
19 | | /** |
20 | | * Octet String |
21 | | */ |
22 | | class BOTAN_PUBLIC_API(2, 0) OctetString final { |
23 | | public: |
24 | | /** |
25 | | * Return the length of this octet string |
26 | | * @return size of this octet string in bytes |
27 | | */ |
28 | 0 | size_t length() const { return m_data.size(); } |
29 | | |
30 | | /** |
31 | | * Return the length of this octet string |
32 | | * @return size of this octet string in bytes |
33 | | */ |
34 | 0 | size_t size() const { return m_data.size(); } |
35 | | |
36 | | /** |
37 | | * Test whether this octet string is empty |
38 | | * @return true if this string holds no bytes |
39 | | */ |
40 | 0 | bool empty() const { return m_data.empty(); } |
41 | | |
42 | | /** |
43 | | * Return the contents of this octet string |
44 | | * @return this object as a secure_vector<uint8_t> |
45 | | */ |
46 | 0 | secure_vector<uint8_t> bits_of() const { return m_data; } |
47 | | |
48 | | /** |
49 | | * Return a pointer to the first byte |
50 | | * @return start of this string |
51 | | */ |
52 | 0 | const uint8_t* begin() const { return m_data.data(); } |
53 | | |
54 | | /** |
55 | | * Return a pointer one past the last byte |
56 | | * @return end of this string |
57 | | */ |
58 | 0 | const uint8_t* end() const { return begin() + m_data.size(); } |
59 | | |
60 | | /** |
61 | | * Format this octet string as a hex string |
62 | | * @return this encoded as hex |
63 | | */ |
64 | | std::string to_string() const; |
65 | | |
66 | | /** |
67 | | * XOR the contents of another octet string into this one |
68 | | * @param other octet string |
69 | | * @return reference to this |
70 | | */ |
71 | | OctetString& operator^=(const OctetString& other); |
72 | | |
73 | | /** |
74 | | * Force to have odd parity |
75 | | * |
76 | | * Deprecated. There is no reason to use this outside of interacting with |
77 | | * some very old or weird system which requires DES and also which do not |
78 | | * automatically ignore the parity bits. |
79 | | */ |
80 | | BOTAN_DEPRECATED("Why would you need to do this") void set_odd_parity(); |
81 | | |
82 | | /** |
83 | | * Create a new OctetString |
84 | | * @param str is a hex encoded string |
85 | | */ |
86 | | explicit OctetString(std::string_view str = ""); |
87 | | |
88 | | /** |
89 | | * Create a new random OctetString |
90 | | * @param rng is a random number generator |
91 | | * @param len is the desired length in bytes |
92 | | */ |
93 | | OctetString(RandomNumberGenerator& rng, size_t len); |
94 | | |
95 | | /** |
96 | | * Create a new OctetString |
97 | | * @param in is an array |
98 | | * @param len is the length of in in bytes |
99 | | */ |
100 | | OctetString(const uint8_t in[], size_t len); |
101 | | |
102 | | /** |
103 | | * Create a new OctetString |
104 | | * @param in a bytestring |
105 | | */ |
106 | 0 | explicit OctetString(std::span<const uint8_t> in) : m_data(in.begin(), in.end()) {} |
107 | | |
108 | | /** |
109 | | * Create a new OctetString |
110 | | * @param in a bytestring |
111 | | */ |
112 | 0 | explicit OctetString(secure_vector<uint8_t> in) : m_data(std::move(in)) {} |
113 | | |
114 | | private: |
115 | | secure_vector<uint8_t> m_data; |
116 | | }; |
117 | | |
118 | | /** |
119 | | * Compare two strings |
120 | | * @param x an octet string |
121 | | * @param y an octet string |
122 | | * @return if x is equal to y |
123 | | */ |
124 | | BOTAN_PUBLIC_API(2, 0) bool operator==(const OctetString& x, const OctetString& y); |
125 | | |
126 | | /** |
127 | | * Compare two strings |
128 | | * @param x an octet string |
129 | | * @param y an octet string |
130 | | * @return if x is not equal to y |
131 | | */ |
132 | | BOTAN_PUBLIC_API(2, 0) bool operator!=(const OctetString& x, const OctetString& y); |
133 | | |
134 | | /** |
135 | | * Concatenate two strings |
136 | | * @param x an octet string |
137 | | * @param y an octet string |
138 | | * @return x concatenated with y |
139 | | */ |
140 | | BOTAN_PUBLIC_API(2, 0) OctetString operator+(const OctetString& x, const OctetString& y); |
141 | | |
142 | | /** |
143 | | * XOR two strings |
144 | | * @param x an octet string |
145 | | * @param y an octet string |
146 | | * @return x XORed with y |
147 | | */ |
148 | | BOTAN_PUBLIC_API(2, 0) OctetString operator^(const OctetString& x, const OctetString& y); |
149 | | |
150 | | /** |
151 | | * Alternate name for octet string showing intent to use as a key |
152 | | */ |
153 | | using SymmetricKey = OctetString; |
154 | | |
155 | | /** |
156 | | * Alternate name for octet string showing intent to use as an IV |
157 | | */ |
158 | | using InitializationVector = OctetString; |
159 | | |
160 | | } // namespace Botan |
161 | | |
162 | | #endif |