digestpp 1.0
C++11 header-only message digest library
Loading...
Searching...
No Matches
sha1_provider.hpp
Go to the documentation of this file.
1/*
2This code is written by kerukuro and released into public domain.
3*/
4
5#ifndef DIGESTPP_PROVIDERS_SHA1_HPP
6#define DIGESTPP_PROVIDERS_SHA1_HPP
7
11#include <array>
12
13namespace digestpp
14{
15
16namespace detail
17{
18
19namespace sha1_functions
20{
21 static inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
22 {
23 return (x & y) ^ (~x & z);
24 }
25
26 static inline uint32_t Parity(uint32_t x, uint32_t y, uint32_t z)
27 {
28 return x ^ y ^ z;
29 }
30
31 static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
32 {
33 return (x & y) ^ (x & z) ^ (y & z);
34 }
35}
36
38{
39public:
40 static const bool is_xof = false;
41
43 {
44 }
45
47 {
48 clear();
49 }
50
51 inline void init()
52 {
53 H[0] = 0x67452301;
54 H[1] = 0xefcdab89;
55 H[2] = 0x98badcfe;
56 H[3] = 0x10325476;
57 H[4] = 0xc3d2e1f0;
58 pos = 0;
59 total = 0;
60 }
61
62 inline void update(const unsigned char* data, size_t len)
63 {
64 detail::absorb_bytes(data, len, 64, 64, m.data(), pos, total,
65 [this](const unsigned char* data, size_t len) { transform(data, len); });
66 }
67
68 inline void final(unsigned char* hash)
69 {
70 total += pos * 8;
71 m[pos++] = 0x80;
72 if (pos > 56) {
73 if (pos != 64)
74 memset(&m[pos], 0, 64 - pos);
75 transform(&m[0], 1);
76 pos = 0;
77 }
78 memset(&m[0] + pos, 0, 56 - pos);
79 uint64_t mlen = byteswap(total);
80 memcpy(&m[0] + (64 - 8), &mlen, 64 / 8);
81 transform(&m[0], 1);
82 for (int i = 0; i < 5; i++)
83 H[i] = byteswap(H[i]);
84 memcpy(hash, H.data(), 160/8);
85 }
86
87 inline void clear()
88 {
89 zero_memory(H);
90 zero_memory(m);
91 }
92
93 inline size_t hash_size() const { return 160; }
94
95private:
96 inline void transform(const unsigned char* data, size_t num_blks)
97 {
98 for (uint64_t blk = 0; blk < num_blks; blk++)
99 {
100 uint32_t M[16];
101 for (uint32_t i = 0; i < 64 / 4; i++)
102 M[i] = byteswap((reinterpret_cast<const uint32_t*>(data)[blk * 16 + i]));
103
104 uint32_t W[80];
105 for (int t = 0; t <= 15; t++)
106 W[t] = M[t];
107 for (int t = 16; t <= 79; t++)
108 W[t] = rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
109
110 uint32_t a = H[0];
111 uint32_t b = H[1];
112 uint32_t c = H[2];
113 uint32_t d = H[3];
114 uint32_t e = H[4];
115
116 uint32_t K = sha1_constants<void>::K[0];
117 auto f = sha1_functions::Ch;
118 for (int t = 0; t <= 79; t++)
119 {
120 uint32_t T = rotate_left(a, 5) + f(b, c, d) + e + K + W[t];
121 e = d;
122 d = c;
123 c = rotate_left(b, 30);
124 b = a;
125 a = T;
126
127 if (t == 19)
128 {
131 }
132 else if (t == 39)
133 {
136 }
137 else if (t == 59)
138 {
141 }
142
143 }
144 H[0] += a;
145 H[1] += b;
146 H[2] += c;
147 H[3] += d;
148 H[4] += e;
149 }
150 }
151
152 std::array<uint32_t, 5> H;
153 std::array<unsigned char, 64> m;
154 size_t pos;
155 uint64_t total;
156};
157
158} // namespace detail
159
160} // namespace digestpp
161
162#endif
Definition sha1_provider.hpp:38
sha1_provider()
Definition sha1_provider.hpp:42
~sha1_provider()
Definition sha1_provider.hpp:46
void init()
Definition sha1_provider.hpp:51
void update(const unsigned char *data, size_t len)
Definition sha1_provider.hpp:62
size_t hash_size() const
Definition sha1_provider.hpp:93
void clear()
Definition sha1_provider.hpp:87
static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
Definition sha1_provider.hpp:31
static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
Definition sha1_provider.hpp:21
static uint32_t Parity(uint32_t x, uint32_t y, uint32_t z)
Definition sha1_provider.hpp:26
uint16_t byteswap(uint16_t val)
Definition functions.hpp:16
uint32_t rotate_left(uint32_t x, unsigned n)
Definition functions.hpp:67
void zero_memory(void *v, size_t n)
Definition functions.hpp:85
void absorb_bytes(const unsigned char *data, size_t len, size_t bs, size_t bschk, unsigned char *m, size_t &pos, T &total, TF transform)
Definition absorb_data.hpp:16
digestpp namespace
Definition ascon.hpp:14
Definition traits.hpp:17
Definition sha1_constants.hpp:16
static const uint32_t K[4]
Definition sha1_constants.hpp:17