scclib
Stable Cloud Computing C++ Library
secvec.cc
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <crypto/secvec.h>
32 
33 using namespace scc::crypto;
34 
35 template <typename T>
36 SecVec<T>::SecVec(typename std::vector<T>::size_type sz) : std::vector<T>(sz)
37 {
38 }
39 
40 template <typename T>
41 SecVec<T>::SecVec(typename std::vector<T>::size_type sz, const typename std::vector<T>::value_type& value) : std::vector<T>(sz, value)
42 {
43 }
44 
45 template <typename T>
47 {
48  explicit_bzero(std::vector<T>::data(), std::vector<T>::size());
49 }
50 
51 template <typename T>
52 void SecVec<T>::clear() noexcept
53 {
54  explicit_bzero(std::vector<T>::data(), std::vector<T>::size());
55  std::vector<T>::clear();
56 }
57 
58 template <typename T>
59 void SecVec<T>::resize(typename std::vector<T>::size_type count)
60 {
61  if (std::vector<T>::size() > count)
62  {
63  explicit_bzero(std::vector<T>::data()+count, std::vector<T>::size()-count);
64  }
65  std::vector<T>::resize(count);
66 }
67 
68 template <typename T>
69 void SecVec<T>::resize(typename std::vector<T>::size_type count, const typename std::vector<T>::value_type& value)
70 {
71  if (std::vector<T>::size() > count)
72  {
73  explicit_bzero(std::vector<T>::data()+count, std::vector<T>::size()-count);
74  }
75  std::vector<T>::resize(count, value);
76 }
77 
78 template SecVec<unsigned char>::SecVec(typename std::vector<unsigned char>::size_type);
79 template SecVec<unsigned char>::SecVec(typename std::vector<unsigned char>::size_type, const typename std::vector<unsigned char>::value_type&);
81 template void SecVec<unsigned char>::clear() noexcept;
82 template void SecVec<unsigned char>::resize(typename std::vector<unsigned char>::size_type);
83 template void SecVec<unsigned char>::resize(typename std::vector<unsigned char>::size_type, const typename std::vector<unsigned char>::value_type&);
84 
85 template SecVec<char>::SecVec(typename std::vector<char>::size_type);
86 template SecVec<char>::SecVec(typename std::vector<char>::size_type, const typename std::vector<char>::value_type&);
87 template SecVec<char>::~SecVec();
88 template void SecVec<char>::clear() noexcept;
89 template void SecVec<char>::resize(typename std::vector<char>::size_type);
90 template void SecVec<char>::resize(typename std::vector<char>::size_type, const typename std::vector<char>::value_type&);
91 
92 template <typename T>
93 std::istream& operator >>(std::istream& is, scc::crypto::SecVec<T>& sv)
94 {
95  char ch;
96  while (is.get(ch))
97  {
98  sv.push_back(ch);
99  }
100  if (is.fail() && !is.eof())
101  {
102  throw std::runtime_error("secure vector stream read error");
103  }
104  return is;
105 }
106 
107 template std::istream& operator >><unsigned char>(std::istream& is, scc::crypto::SecVec<unsigned char>& sv);
108 template std::istream& operator >><char>(std::istream& is, scc::crypto::SecVec<char>& sv);
109 
110 template <typename T>
111 std::ostream& operator <<(std::ostream& os, const scc::crypto::SecVec<T>& sv)
112 {
113  os.write((const char*)sv.data(), sv.size()*sizeof(T));
114  if (os.fail())
115  {
116  throw std::runtime_error("secure vector stream write error");
117  }
118  return os;
119 }
120 
121 template std::ostream& operator <<<unsigned char>(std::ostream& os, const scc::crypto::SecVec<unsigned char>& sv);
122 template std::ostream& operator <<<char>(std::ostream& os, const scc::crypto::SecVec<char>& sv);
Secure vector helper.
Definition: secvec.h:58
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
Definition: inet.cc:320
Secure vector.