AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
sha1.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_CRYPTO_SHA1_H_
21 #define _ANCH_CRYPTO_SHA1_H_
22 
23 #include "crypto/hash/hash.hpp"
24 
25 
26 namespace anch {
27  namespace crypto {
28 
29  template<typename H> H HMAC(const std::string&, const std::string&);
30 
38  class SHA1: public Hash<20,64> {
39 
40  friend SHA1 anch::crypto::HMAC<SHA1>(const std::string&, const std::string&);
41 
42  private:
48  typedef union {
50  uint8_t bytes[64];
51 
53  uint32_t words[16];
54  } Chunk;
55 
61  struct Context {
63  std::array<uint32_t,5> state;
64 
66  uint64_t size;
67 
69  std::array<uint8_t,64> buffer;
70 
72  std::array<uint8_t,20> digest;
73 
77  Context() {
78  reset();
79  }
80 
84  void reset() {
85  state = { {0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0} };
86  size = 0;
87  }
88  };
89 
90  // Attributes +
91  private:
93  Context _context;
94  // Attributes -
95 
96 
97  // Constructors +
98  public:
102  SHA1();
103 
109  template<class CharT, class Traits, class Allocator>
110  SHA1(const std::basic_string<CharT,Traits,Allocator>& data) {
111  Hash::digest(data);
112  }
113 
119  template<class CharT, class Traits>
120  SHA1(std::basic_istream<CharT,Traits>& stream) {
121  Hash::digest(stream);
122  }
123 
124  private:
131  SHA1(const uint8_t* data, std::size_t len) {
132  Hash::digest(data, len);
133  }
134  // Constructors -
135 
136 
137  // Destructor +
138  public:
142  virtual ~SHA1();
143  // Destructor -
144 
145 
146  // Methods +
147  public:
153  virtual const std::array<uint8_t,20>& digest() const override;
154 
155  protected:
159  virtual void reset() override {
160  _context.reset();
161  }
162 
169  virtual void addData(const uint8_t* data, std::size_t len) override;
170 
174  virtual void finalize() override;
175 
176  private:
182  void transform(const uint8_t* buffer);
183 
190  static inline uint32_t rol32(uint32_t value, unsigned int shift){
191  return ((value << shift) | value >> (32 - shift));
192  }
193 
200  static inline uint32_t word(Chunk& chunk, unsigned int position) {
201  return (chunk.words[position & 0xf] = rol32(chunk.words[(position + 13) & 0xf]
202  ^ chunk.words[(position + 8) & 0xf]
203  ^ chunk.words[(position + 2) & 0xf]
204  ^ chunk.words[(position) & 0xf],
205  1));
206  }
207 
219  static inline void round0(Chunk& chunk,
220  const unsigned int position,
221  uint32_t& v,
222  uint32_t& w,
223  uint32_t& x,
224  uint32_t& y,
225  uint32_t& z){
226  z += ((( w & (x ^ y)) ^ y) + chunk.words[position] + 0x5A827999 + rol32(v, 5));
227  w = rol32(w, 30);
228  }
229 
241  static inline void round1(Chunk& chunk,
242  const unsigned int position,
243  uint32_t& v,
244  uint32_t& w,
245  uint32_t& x,
246  uint32_t& y,
247  uint32_t& z){
248  z += ((( w & (x ^ y)) ^ y) + word(chunk,position) + 0x5A827999 + rol32(v, 5));
249  w = rol32(w, 30);
250  }
251 
263  static inline void round2(Chunk& chunk,
264  const unsigned int position,
265  uint32_t& v,
266  uint32_t& w,
267  uint32_t& x,
268  uint32_t& y,
269  uint32_t& z){
270  z += (( w ^ x ^ y) + word(chunk, position) + 0x6ED9EBA1 + rol32(v, 5));
271  w = rol32(w, 30);
272  }
273 
285  static inline void round3(Chunk& chunk,
286  const unsigned int position,
287  uint32_t& v,
288  uint32_t& w,
289  uint32_t& x,
290  uint32_t& y,
291  uint32_t& z){
292  z += (((( w | x) & y) | (w & x)) + word(chunk, position) + 0x8F1BBCDC + rol32(v, 5));
293  w = rol32(w, 30);
294  }
295 
307  static inline void round4(Chunk& chunk,
308  const unsigned int position,
309  uint32_t& v,
310  uint32_t& w,
311  uint32_t& x,
312  uint32_t& y,
313  uint32_t& z){
314  z += ((w ^ x ^ y) + word(chunk, position) + 0xCA62C1D6 + rol32(v, 5));
315  w = rol32(w, 30);
316  }
317  // Methods -
318 
319  };
320 
321  extern template SHA1 HMAC<SHA1>(const std::string&, const std::string&);
322 
323  }
324 }
325 
326 #endif // _ANCH_CRYPTO_SHA1_H_
virtual const std::array< uint8_t, O > & digest() const =0
H HMAC(const std::string &, const std::string &)
Definition: hmac.hpp:38
virtual ~SHA1()
Definition: sha1.cpp:60
virtual void addData(const uint8_t *data, std::size_t len) override
Definition: sha1.cpp:197
SHA1()
Definition: sha1.cpp:51
AnCH framework base namespace.
Definition: base64.hpp:28
virtual const std::array< uint8_t, 20 > & digest() const override
Definition: sha1.cpp:72
SHA1(const std::basic_string< CharT, Traits, Allocator > &data)
Definition: sha1.hpp:110
Hash algorithm abstract class.
Definition: hash.hpp:41
virtual void reset() override
Definition: sha1.hpp:159
virtual void finalize() override
Definition: sha1.cpp:224
SHA1(std::basic_istream< CharT, Traits > &stream)
Definition: sha1.hpp:120
SHA1 hash algorithm implementation.
Definition: sha1.hpp:38