libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
SecureVector.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef LIBHOMEGEAR_BASE_SECUREVECTOR_H
32 #define LIBHOMEGEAR_BASE_SECUREVECTOR_H
33 
34 #include <cstdint>
35 #include <vector>
36 
37 namespace BaseLib {
38 namespace Security {
39 
44 template<typename T>
45 class SecureVector : public std::vector<T> {
46  public:
47  SecureVector() : std::vector<T>() {};
48  explicit SecureVector(size_t count) : std::vector<T>(count) {}
49  explicit SecureVector(size_t count, const T &value) : std::vector<T>(count, value) {}
50  template<class InputIt> SecureVector(InputIt first, InputIt last) : std::vector<T>(first, last) {}
51  SecureVector(const SecureVector &) = delete; //Copy constructor
52  SecureVector(SecureVector &&) noexcept = default; //Move constructor
53  SecureVector &operator=(const SecureVector &) = delete; //Copy assignment operator
54 
56  std::fill(this->begin(), this->end(), 0);
57  }
58 
59  void secureResize(size_t count) {
60  if (count <= this->capacity()) {
61  this->resize(count);
62  return;
63  }
64 
65  std::vector<uint8_t> newVector;
66  newVector.resize(count);
67  std::copy(this->begin(), this->end(), newVector.begin());
68  this->swap(newVector);
69  std::fill(newVector.begin(), newVector.end(), 0);
70  }
71 
72  void secureResize(size_t count, const T &value) {
73  if (count <= this->capacity()) {
74  this->resize(count, value);
75  return;
76  }
77 
78  std::vector<uint8_t> newVector;
79  newVector.resize(count, value);
80  std::copy(this->begin(), this->end(), newVector.begin());
81  this->swap(newVector);
82  std::fill(newVector.begin(), newVector.end(), 0);
83  }
84 
85  void securePrepend(const SecureVector &other) {
86  std::vector<uint8_t> newVector;
87  newVector.resize(this->size() + other.size());
88  std::copy(other.begin(), other.end(), newVector.begin());
89  std::copy(this->begin(), this->end(), newVector.begin() + other.size());
90  this->swap(newVector);
91  std::fill(newVector.begin(), newVector.end(), 0);
92  }
93 
94  void secureAppend(const SecureVector &other) {
95  std::vector<uint8_t> newVector;
96  newVector.resize(this->size() + other.size());
97  std::copy(this->begin(), this->end(), newVector.begin());
98  std::copy(other.begin(), other.end(), newVector.begin() + this->size());
99  this->swap(newVector);
100  std::fill(newVector.begin(), newVector.end(), 0);
101  }
102 };
103 
104 }
105 }
106 
107 #endif //LIBHOMEGEAR_BASE_SECUREVECTOR_H
SecureVector(InputIt first, InputIt last)
Definition: SecureVector.h:50
SecureVector()
Definition: SecureVector.h:47
The class only makes sure that the vector is not copyable and the data is zeroed on destruction...
Definition: Io.h:40
Definition: BaseLib.cpp:34
void secureAppend(const SecureVector &other)
Definition: SecureVector.h:94
PVariable value
Definition: UiElements.h:217
SecureVector & operator=(const SecureVector &)=delete
void secureResize(size_t count, const T &value)
Definition: SecureVector.h:72
void securePrepend(const SecureVector &other)
Definition: SecureVector.h:85
void secureResize(size_t count)
Definition: SecureVector.h:59
~SecureVector()
Definition: SecureVector.h:55
SecureVector(size_t count)
Definition: SecureVector.h:48
SecureVector(size_t count, const T &value)
Definition: SecureVector.h:49