AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
sha2.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-crypto-export.hpp"
23
24#include "crypto/hash/hash.hpp"
25
26namespace anch::crypto {
27
47 template<std::size_t O, std::size_t B, typename W, uint32_t R, const std::array<W,8>& I>
48 class ANCH_CRYPTO_EXPORT SHA2: public Hash<O,B> {
49
50 protected:
56 typedef union {
58 uint8_t bytes[B * sizeof(W)];
59
61 W words[B];
62 } Chunk;
63
69 template<const std::array<W,8>& Init>
70 struct Context {
72 std::array<W,8> state;
73
75 uint64_t size;
76
78 uint8_t buffer[B];
79
81 std::array<uint8_t,O> digest;
82
87
91 void reset();
92
93 };
94
95 // Attributes +
96 protected:
99 // Attributes -
100
101
102 // Constructors +
103 public:
108 // Constructors -
109
110 public:
111 // Destructor +
115 virtual ~SHA2();
116 // Destructor -
117
118
119 // Methods +
120 public:
126 virtual const std::array<uint8_t,O>& digest() const override;
127
132 using Hash<O,B>::digest;
133
134 protected:
141 static constexpr inline W shiftRight(uint8_t bits, W word) {
142 return (word >> bits);
143 }
144
151 static constexpr inline W rotateLeft(uint8_t bits, W word) {
152 return ((word << bits) | (word >> (sizeof(W) * 8 - bits)));
153 }
154
161 static constexpr inline W rotateRight(uint8_t bits, W word) {
162 return ((word >> bits) | (word << (sizeof(W) * 8 - bits)));
163 }
164
165 private:
169 virtual void reset() override;
170
177 virtual void addData(const uint8_t* data, std::size_t len) override;
178
182 virtual void finalize() override;
183
189 virtual const std::array<W,R>& getTranslationArray() const = 0;
190
196 void transform(const uint8_t* buffer);
197
204 static void bytesSwap(W* buf, uint8_t count);
205
213 static W ch(W x, W y, W z);
214
222 static W maj(W x, W y, W z);
223
229 virtual W SIGMA0(W word) const = 0;
230
236 virtual W SIGMA1(W word) const = 0;
237
243 virtual W sigma0(W word) const = 0;
244
250 virtual W sigma1(W word) const = 0;
251 // Methods -
252
253 };
254
255}
256
257#include "crypto/hash/impl/sha2.hpp"
Hash algorithm abstract class.
Definition hash.hpp:40
static constexpr W rotateRight(uint8_t bits, W word)
Definition sha2.hpp:161
Context< I > _context
Definition sha2.hpp:98
virtual const std::array< uint8_t, O > & digest() const override
static constexpr W shiftRight(uint8_t bits, W word)
Definition sha2.hpp:141
static constexpr W rotateLeft(uint8_t bits, W word)
Definition sha2.hpp:151
Cryptography namespace.
Definition base64.hpp:30
Definition sha2.hpp:70
std::array< W, 8 > state
Definition sha2.hpp:72
uint64_t size
Definition sha2.hpp:75
uint8_t buffer[B]
Definition sha2.hpp:78
std::array< uint8_t, O > digest
Definition sha2.hpp:81
Definition sha2.hpp:56
uint8_t bytes[B *sizeof(W)]
Definition sha2.hpp:58
W words[B]
Definition sha2.hpp:61