AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
sha2.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#pragma once
21
22#include "crypto/hash/hash.hpp"
23
24namespace anch::crypto {
25
45 template<std::size_t O, std::size_t B, typename W, uint32_t R, const std::array<W,8>& I>
46 class SHA2: public Hash<O,B> {
47
48 protected:
54 typedef union {
56 uint8_t bytes[B * sizeof(W)];
57
59 W words[B];
60 } Chunk;
61
67 template<const std::array<W,8>& Init>
68 struct Context {
70 std::array<W,8> state;
71
73 uint64_t size;
74
76 uint8_t buffer[B];
77
79 std::array<uint8_t,O> digest;
80
85
89 void reset();
90
91 };
92
93 // Attributes +
94 protected:
97 // Attributes -
98
99
100 // Constructors +
101 public:
106 // Constructors -
107
108 public:
109 // Destructor +
113 virtual ~SHA2();
114 // Destructor -
115
116
117 // Methods +
118 public:
124 virtual const std::array<uint8_t,O>& digest() const override;
125
126 protected:
133 static constexpr inline W shiftRight(uint8_t bits, W word) {
134 return (word >> bits);
135 }
136
143 static constexpr inline W rotateLeft(uint8_t bits, W word) {
144 return ((word << bits) | (word >> (sizeof(W) * 8 - bits)));
145 }
146
153 static constexpr inline W rotateRight(uint8_t bits, W word) {
154 return ((word >> bits) | (word << (sizeof(W) * 8 - bits)));
155 }
156
157 private:
161 virtual void reset() override;
162
169 virtual void addData(const uint8_t* data, std::size_t len) override;
170
174 virtual void finalize() override;
175
181 virtual const std::array<W,R>& getTranslationArray() const = 0;
182
188 void transform(const uint8_t* buffer);
189
196 static void bytesSwap(W* buf, uint8_t count);
197
205 static W ch(W x, W y, W z);
206
214 static W maj(W x, W y, W z);
215
221 virtual W SIGMA0(W word) const = 0;
222
228 virtual W SIGMA1(W word) const = 0;
229
235 virtual W sigma0(W word) const = 0;
236
242 virtual W sigma1(W word) const = 0;
243 // Methods -
244
245 };
246
247}
248
249#include "crypto/hash/impl/sha2.hpp"
Hash algorithm abstract class.
Definition hash.hpp:38
static constexpr W rotateRight(uint8_t bits, W word)
Definition sha2.hpp:153
Context< I > _context
Definition sha2.hpp:96
virtual const std::array< uint8_t, O > & digest() const override
static constexpr W shiftRight(uint8_t bits, W word)
Definition sha2.hpp:133
static constexpr W rotateLeft(uint8_t bits, W word)
Definition sha2.hpp:143
Cryptography namespace.
Definition base64.hpp:28
Definition sha2.hpp:68
std::array< W, 8 > state
Definition sha2.hpp:70
uint64_t size
Definition sha2.hpp:73
uint8_t buffer[B]
Definition sha2.hpp:76
std::array< uint8_t, O > digest
Definition sha2.hpp:79
Definition sha2.hpp:54
uint8_t bytes[B *sizeof(W)]
Definition sha2.hpp:56
W words[B]
Definition sha2.hpp:59