tlx
Loading...
Searching...
No Matches
sha256.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/digest/sha256.cpp
3 *
4 * Public domain implementation of SHA-256 (SHA-2) processor. Copied from
5 * https://github.com/kalven/sha-2, which is based on LibTomCrypt.
6 *
7 * Part of tlx - http://panthema.net/tlx
8 *
9 * Copyright (C) 2018 Timo Bingmann <tb@panthema.net>
10 *
11 * All rights reserved. Published under the Boost Software License, Version 1.0
12 ******************************************************************************/
13
14#include <tlx/digest/sha256.hpp>
15
16#include <tlx/math/ror.hpp>
18
19#include <algorithm>
20#include <cstdint>
21
22namespace tlx {
23
24/*
25 * LibTomCrypt, modular cryptographic library -- Tom St Denis
26 *
27 * LibTomCrypt is a library that provides various cryptographic algorithms in a
28 * highly modular and flexible manner.
29 *
30 * The library is free for all purposes without any express guarantee it works.
31 */
32
33typedef std::uint32_t u32;
34typedef std::uint64_t u64;
35
36namespace {
37
38static const u32 K[64] = {
39 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
40 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
41 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
42 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
43 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
44 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
45 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
46 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
47 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
48 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
49 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
50 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
51 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
52};
53
54static inline u32 min(u32 x, u32 y) {
55 return x < y ? x : y;
56}
57
58static inline u32 load32(const std::uint8_t* y) {
59 return (u32(y[0]) << 24) | (u32(y[1]) << 16) |
60 (u32(y[2]) << 8) | (u32(y[3]) << 0);
61}
62static inline void store64(u64 x, std::uint8_t* y) {
63 for (int i = 0; i != 8; ++i)
64 y[i] = (x >> ((7 - i) * 8)) & 255;
65}
66static inline void store32(u32 x, std::uint8_t* y) {
67 for (int i = 0; i != 4; ++i)
68 y[i] = (x >> ((3 - i) * 8)) & 255;
69}
70
71static inline u32 Ch(u32 x, u32 y, u32 z) {
72 return z ^ (x & (y ^ z));
73}
74static inline u32 Maj(u32 x, u32 y, u32 z) {
75 return ((x | y) & z) | (x & y);
76}
77static inline u32 Sh(u32 x, u32 n) {
78 return x >> n;
79}
80static inline u32 Sigma0(u32 x) {
81 return ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22);
82}
83static inline u32 Sigma1(u32 x) {
84 return ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25);
85}
86static inline u32 Gamma0(u32 x) {
87 return ror32(x, 7) ^ ror32(x, 18) ^ Sh(x, 3);
88}
89static inline u32 Gamma1(u32 x) {
90 return ror32(x, 17) ^ ror32(x, 19) ^ Sh(x, 10);
91}
92
93static void sha256_compress(std::uint32_t state[8], const std::uint8_t* buf) {
94 u32 S[8], W[64], t0, t1, t;
95
96 // Copy state into S
97 for (size_t i = 0; i < 8; i++)
98 S[i] = state[i];
99
100 // Copy the state into 512-bits into W[0..15]
101 for (size_t i = 0; i < 16; i++)
102 W[i] = load32(buf + (4 * i));
103
104 // Fill W[16..63]
105 for (size_t i = 16; i < 64; i++)
106 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
107
108 // Compress
109 auto RND =
110 [&](u32 a, u32 b, u32 c, u32& d, u32 e, u32 f, u32 g, u32& h, u32 i)
111 {
112 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];
113 t1 = Sigma0(a) + Maj(a, b, c);
114 d += t0;
115 h = t0 + t1;
116 };
117
118 for (size_t i = 0; i < 64; ++i)
119 {
120 RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
121 t = S[7], S[7] = S[6], S[6] = S[5], S[5] = S[4],
122 S[4] = S[3], S[3] = S[2], S[2] = S[1], S[1] = S[0], S[0] = t;
123 }
124
125 // Feedback
126 for (size_t i = 0; i < 8; i++)
127 state[i] = state[i] + S[i];
128}
129
130} // namespace
131
133 curlen_ = 0;
134 length_ = 0;
135 state_[0] = 0x6A09E667UL;
136 state_[1] = 0xBB67AE85UL;
137 state_[2] = 0x3C6EF372UL;
138 state_[3] = 0xA54FF53AUL;
139 state_[4] = 0x510E527FUL;
140 state_[5] = 0x9B05688CUL;
141 state_[6] = 0x1F83D9ABUL;
142 state_[7] = 0x5BE0CD19UL;
143}
144
145SHA256::SHA256(const void* data, std::uint32_t size)
146 : SHA256() {
147 process(data, size);
148}
149
150SHA256::SHA256(const std::string& str)
151 : SHA256() {
152 process(str);
153}
154
155void SHA256::process(const void* data, u32 size) {
156 const u32 block_size = sizeof(SHA256::buf_);
157 auto in = static_cast<const std::uint8_t*>(data);
158
159 while (size > 0)
160 {
161 if (curlen_ == 0 && size >= block_size)
162 {
163 sha256_compress(state_, in);
164 length_ += block_size * 8;
165 in += block_size;
166 size -= block_size;
167 }
168 else
169 {
170 u32 n = min(size, (block_size - curlen_));
171 std::copy(in, in + n, buf_ + curlen_);
172 curlen_ += n;
173 in += n;
174 size -= n;
175
176 if (curlen_ == block_size)
177 {
178 sha256_compress(state_, buf_);
179 length_ += 8 * block_size;
180 curlen_ = 0;
181 }
182 }
183 }
184}
185
186void SHA256::process(const std::string& str) {
187 return process(str.data(), str.size());
188}
189
191 // Increase the length of the message
192 length_ += curlen_ * 8;
193
194 // Append the '1' bit
195 buf_[curlen_++] = static_cast<std::uint8_t>(0x80);
196
197 // If the length_ is currently above 56 bytes we append zeros then
198 // sha256_compress(). Then we can fall back to padding zeros and length
199 // encoding like normal.
200 if (curlen_ > 56)
201 {
202 while (curlen_ < 64)
203 buf_[curlen_++] = 0;
204 sha256_compress(state_, buf_);
205 curlen_ = 0;
206 }
207
208 // Pad up to 56 bytes of zeroes
209 while (curlen_ < 56)
210 buf_[curlen_++] = 0;
211
212 // Store length
213 store64(length_, buf_ + 56);
214 sha256_compress(state_, buf_);
215
216 // Copy output
217 for (size_t i = 0; i < 8; i++)
218 store32(state_[i], static_cast<std::uint8_t*>(digest) + (4 * i));
219}
220
221std::string SHA256::digest() {
222 std::string out(kDigestLength, '0');
223 finalize(const_cast<char*>(out.data()));
224 return out;
225}
226
227std::string SHA256::digest_hex() {
228 std::uint8_t digest[kDigestLength];
231}
232
234 std::uint8_t digest[kDigestLength];
237}
238
239std::string sha256_hex(const void* data, std::uint32_t size) {
240 return SHA256(data, size).digest_hex();
241}
242
243std::string sha256_hex(const std::string& str) {
244 return SHA256(str).digest_hex();
245}
246
247std::string sha256_hex_uc(const void* data, std::uint32_t size) {
248 return SHA256(data, size).digest_hex_uc();
249}
250
251std::string sha256_hex_uc(const std::string& str) {
252 return SHA256(str).digest_hex_uc();
253}
254
255} // namespace tlx
256
257/******************************************************************************/
SHA-256 processor without external dependencies.
Definition sha256.hpp:29
std::uint32_t state_[8]
Definition sha256.hpp:58
void finalize(void *digest)
finalize computation and output 32 byte (256 bit) digest
Definition sha256.cpp:190
std::string digest_hex()
finalize computation and return 32 byte (256 bit) digest hex encoded
Definition sha256.cpp:227
std::string digest()
finalize computation and return 32 byte (256 bit) digest
Definition sha256.cpp:221
std::string digest_hex_uc()
finalize computation and return 32 byte (256 bit) digest upper-case hex
Definition sha256.cpp:233
std::uint32_t curlen_
Definition sha256.hpp:59
static constexpr size_t kDigestLength
digest length in bytes
Definition sha256.hpp:44
std::uint8_t buf_[64]
Definition sha256.hpp:60
void process(const void *data, std::uint32_t size)
process more data
Definition sha256.cpp:155
SHA256()
construct empty object.
Definition sha256.cpp:132
std::uint64_t length_
Definition sha256.hpp:57
std::string sha256_hex_uc(const void *data, std::uint32_t size)
process data and return 32 byte (256 bit) digest upper-case hex encoded
Definition sha256.cpp:247
std::string sha256_hex(const void *data, std::uint32_t size)
process data and return 32 byte (256 bit) digest hex encoded
Definition sha256.cpp:239
static std::uint32_t ror32(const std::uint32_t &x, int i)
ror32 - generic
Definition ror.hpp:55
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
Definition hexdump.cpp:96
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
Definition hexdump.cpp:22
std::uint32_t u32
Definition sha256.cpp:33
std::uint64_t u64
Definition sha256.cpp:34