scclib
Stable Cloud Computing C++ Library
hash.h
Go to the documentation of this file.
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 #ifndef _CRYPTO_HASH_H
32 #define _CRYPTO_HASH_H
33 
34 #include <vector>
35 #include <string>
36 #include <memory>
37 #include <crypto/secvec.h>
38 #include <util/iobase.h>
39 
40 // forward declarations
41 class HashBase;
42 class HmacBase;
43 
44 namespace scc::crypto {
45 
87 class Hash
88 {
89 public:
91  enum Algorithm
92  {
93  md5_type =1001, // Note: the MD5 algorithm is not considered secure for cryptographic purposes, but has been included to support legacy services
94  sha1_type =1002, // Note: SHA-1 is also considered insecure, SHA-2 (SHA-256) is suggested as an alternative.
95  sha224_type =1003,
96  sha256_type =1004,
97  sha384_type =1005,
98  sha512_type =1006,
99  sha512_224_type =1007, // 224 bit hash based on 512 bit blocks (performance better on 64 bit machines)
100  sha512_256_type =1008, // 256 bit hash based on 512 bit blocks (performance better on 64 bit machines)
101  sm3_type =1009, // 256 bit hash based on 512 bit blocks (performance better on 64 bit machines)
102  };
103 private:
104  std::unique_ptr<HashBase> m_ptr;
105  Algorithm m_alg;
106 public:
107  static int alg_size(Algorithm alg)
108  {
109  switch (alg)
110  {
111  case md5_type: return 16;
112  case sha1_type: return 20;
113  case sha224_type: return 28;
114  case sha256_type: return 32;
115  case sha384_type: return 48;
116  case sha512_type: return 64;
117  case sha512_224_type: return 28;
118  case sha512_256_type: return 32;
119  case sm3_type: return 32;
120  };
121  return 0;
122  }
123 
131  virtual ~Hash();
132 
133  Hash(const Hash&) = delete;
134  Hash& operator=(const Hash&) = delete;
135 
137  Hash(Hash&& other);
139  Hash& operator=(Hash&& other);
140 
142  static bool supported(Algorithm);
143 
145  void reset();
146 
152  void update(const void*, int);
154  void update(const std::vector<char>& v)
155  {
156  update(v.data(), v.size());
157  }
159  void update(const std::string v)
160  {
161  update(v.data(), v.size());
162  }
163 
174  int get_tag(void*, int);
176  int get_tag(std::vector<char>& v)
177  {
178  v.resize(size());
179  return get_tag(v.data(), v.size());
180  }
184  int get_tag(std::vector<char>& v, int sz)
185  {
186  if (sz >= 0) v.resize(sz);
187  return get_tag(v.data(), v.size());
188  }
189 
197  int final(void*, int);
199  int final(std::vector<char>& v)
200  {
201  v.resize(size());
202  return final(v.data(), v.size());
203  }
204 
206  int alg() const { return m_alg; }
207 
209  int size() const { return alg_size(m_alg); }
210 };
211 
215 {
216  scc::util::Reader& m_rd;
217  Hash& m_chk;
218  SecVecChar m_buf;
219 public:
220  HashReader(scc::util::Reader& rd, Hash& chk) : m_rd(rd), m_chk(chk) {}
221  virtual ~HashReader() {}
222  virtual size_t read(void* loc, size_t len)
223  {
224  m_buf.resize(len);
225  int got = m_rd.read(m_buf.data(), len);
226  if (got == 0)
227  {
228  return 0;
229  }
230  m_chk.update((char*)m_buf.data(), got);
231  memcpy(loc, (char*)m_buf.data(), got);
232  return got;
233  }
234 };
235 
239 {
240  scc::util::Writer& m_wr;
241  Hash& m_chk;
242 public:
243  HashWriter(scc::util::Writer& wr, Hash& chk) : m_wr(wr), m_chk(chk) {}
244  virtual ~HashWriter() {}
245  virtual size_t write(const void* loc, size_t len)
246  {
247  int put = m_wr.write(loc, len);
248  if (put == 0)
249  {
250  return 0;
251  }
252  m_chk.update(loc, put);
253  return put;
254  }
255  virtual void shutdown() {}
256 };
257 
264 class Hmac
265 {
266  std::unique_ptr<HmacBase> m_ptr;
267  Hash::Algorithm m_alg;
268 public:
274  Hmac(const void*, int, Hash::Algorithm);
279  Hmac(const std::vector<char>& key, Hash::Algorithm alg) : Hmac(key.data(), key.size(), alg) {}
284  Hmac(const std::string& key, Hash::Algorithm alg) : Hmac(key.data(), key.size(), alg) {}
285  virtual ~Hmac();
286  Hmac(const Hmac&) = delete; // no copy
287  Hmac& operator=(const Hmac&) = delete;
289  Hmac(Hmac&& other);
291  Hmac& operator=(Hmac&& other);
292 
294  static bool supported(int);
295 
300  void init(const void*, int);
302  void init(std::vector<char>& v)
303  {
304  init(v.data(), v.size());
305  }
307  void reset();
309  void update(const void*, int);
311  void update(const std::vector<char>& v)
312  {
313  update(v.data(), v.size());
314  }
315  void update(const std::string& v)
316  {
317  update(v.data(), v.size());
318  }
319 
328  int final(void*, int);
330  int final(std::vector<char>& v)
331  {
332  v.resize(size());
333  return final(v.data(), v.size());
334  }
335 
337  int alg() const { return m_alg; }
338 
340  int size() const { return Hash::alg_size(m_alg); }
341 };
342 
346 } // namespace
347 
348 #endif
Helper class to hash an incoming stream.
Definition: hash.h:215
virtual size_t read(void *loc, size_t len)
Read interface.
Definition: hash.h:222
Helper class to hash an outgoing stream.
Definition: hash.h:239
virtual size_t write(const void *loc, size_t len)
Write interface.
Definition: hash.h:245
General one-way hashing algorithms.
Definition: hash.h:88
int get_tag(std::vector< char > &v, int sz)
Get the current hash value using a vector, for length between 1 and size of hash.
Definition: hash.h:184
int size() const
Return the current hash size.
Definition: hash.h:209
static bool supported(Algorithm)
Test if an algorithm type is supported.
Hash(Algorithm)
Initialize with hash type.
void update(const std::string v)
Update using a string.
Definition: hash.h:159
void update(const std::vector< char > &v)
Update using a char vector.
Definition: hash.h:154
int alg() const
Hash algorithm.
Definition: hash.h:206
void update(const void *, int)
Update the hash by adding data.
Hash(Hash &&other)
Move constructor.
Algorithm
Hash type.
Definition: hash.h:92
void reset()
Reset the hash to initial value.
int get_tag(void *, int)
Get the current hash value without resetting the hash.
Hash & operator=(Hash &&other)
Move assign.
int get_tag(std::vector< char > &v)
Get the full current hash value using a secure vector.
Definition: hash.h:176
Hmac, aka hash-based message authentication code.
Definition: hash.h:265
static bool supported(int)
Test if an algorithm type is supported.
void init(std::vector< char > &v)
Initialize the hmac with a new key vector.
Definition: hash.h:302
Hmac(const std::vector< char > &key, Hash::Algorithm alg)
Construct an hmac.
Definition: hash.h:279
int alg() const
Hmac algorithm.
Definition: hash.h:337
void reset()
Reset the hmac, using the same key.
void update(const std::vector< char > &v)
Update the hmac with new data.
Definition: hash.h:311
Hmac & operator=(Hmac &&other)
Move assign.
Hmac(const std::string &key, Hash::Algorithm alg)
Construct an hmac.
Definition: hash.h:284
Hmac(Hmac &&other)
Move constructor.
Hmac(const void *, int, Hash::Algorithm)
Construct an hmac.
void update(const void *, int)
Update the hmac with new data.
void init(const void *, int)
Initialize the hmac with a new key.
int size() const
Hmac size.
Definition: hash.h:340
void resize(typename std::vector< T >::size_type)
Resize the vector.
Definition: secvec.cc:59
Input/output stream base reader/writer interface classes.
Secure vector.
Interface class for objects which can be read.
Definition: iobase.h:67
virtual size_t read(void *, size_t)=0
Read interface.
Interface class for objects which can be written.
Definition: iobase.h:86
virtual size_t write(const void *, size_t)=0
Write interface.