28#define ROTL32(v, n) _rotl((v), (n))
29#define ROTL64(v, n) _rotl64((v), (n))
31#define ROTR32(v, n) _rotr((v), (n))
32#define ROTR64(v, n) _rotr64((v), (n))
36#define U8V(v) ((uint8_t)(v)&0xFFU)
37#define U16V(v) ((uint16_t)(v)&0xFFFFU)
38#define U32V(v) ((uint32_t)(v)&0xFFFFFFFFU)
39#define U64V(v) ((uint64_t)(v)&0xFFFFFFFFFFFFFFFFU)
41#define ROTL32(v, n) (U32V((uint32_t)(v) << (n)) | ((uint32_t)(v) >> (32 - (n))))
44#define ROTL64(v, n) (U64V((uint64_t)(v) << (n)) | ((uint64_t)(v) >> (64 - (n))))
46#define ROTR32(v, n) ROTL32(v, 32 - (n))
47#define ROTR64(v, n) ROTL64(v, 64 - (n))
51#define ROTL8(v, n) (U8V((uint8_t)(v) << (n)) | ((uint8_t)(v) >> (8 - (n))))
53#define ROTL16(v, n) (U16V((uint16_t)(v) << (n)) | ((uint16_t)(v) >> (16 - (n))))
55#define ROTR8(v, n) ROTL8(v, 8 - (n))
56#define ROTR16(v, n) ROTL16(v, 16 - (n))
58#define SHA256_DIGEST_SIZE 32
69void sha256_hash(
unsigned char *buf,
const unsigned char *data,
size_t size);
73#define _SHA256_UNROLL2
77 p->
state[0] = 0x6a09e667;
78 p->
state[1] = 0xbb67ae85;
79 p->
state[2] = 0x3c6ef372;
80 p->
state[3] = 0xa54ff53a;
81 p->
state[4] = 0x510e527f;
82 p->
state[5] = 0x9b05688c;
83 p->
state[6] = 0x1f83d9ab;
84 p->
state[7] = 0x5be0cd19;
88#define S0(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22))
89#define S1(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25))
90#define s0(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ (x >> 3))
91#define s1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ (x >> 10))
93#define blk0(i) (W[i] = data[i])
94#define blk2(i) (W[i & 15] += s1(W[(i - 2) & 15]) + W[(i - 7) & 15] + s0(W[(i - 15) & 15]))
96#define Ch(x, y, z) (z ^ (x & (y ^ z)))
97#define Maj(x, y, z) ((x & y) | (z & (x | y)))
99#define a(i) T[(0 - (i)) & 7]
100#define b(i) T[(1 - (i)) & 7]
101#define c(i) T[(2 - (i)) & 7]
102#define d(i) T[(3 - (i)) & 7]
103#define e(i) T[(4 - (i)) & 7]
104#define f(i) T[(5 - (i)) & 7]
105#define g(i) T[(6 - (i)) & 7]
106#define h(i) T[(7 - (i)) & 7]
108#ifdef _SHA256_UNROLL2
110#define R(a, b, c, d, e, f, g, h, i) \
111 h += S1(e) + Ch(e, f, g) + K[i + j] + (j ? blk2(i) : blk0(i)); \
113 h += S0(a) + Maj(a, b, c)
116 R(a, b, c, d, e, f, g, h, i); \
117 R(h, a, b, c, d, e, f, g, (i + 1)); \
118 R(g, h, a, b, c, d, e, f, (i + 2)); \
119 R(f, g, h, a, b, c, d, e, (i + 3)); \
120 R(e, f, g, h, a, b, c, d, (i + 4)); \
121 R(d, e, f, g, h, a, b, c, (i + 5)); \
122 R(c, d, e, f, g, h, a, b, (i + 6)); \
123 R(b, c, d, e, f, g, h, a, (i + 7))
128 h(i) += S1(e(i)) + Ch(e(i), f(i), g(i)) + K[i + j] + (j ? blk2(i) : blk0(i)); \
130 h(i) += S0(a(i)) + Maj(a(i), b(i), c(i))
148static const uint32_t
K[64] = {
149 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
150 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
151 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
152 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
153 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
154 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
155 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
156 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
160 uint32_t W[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U};
162#ifdef _SHA256_UNROLL2
174 for (j = 0; j < 8; j++)
178 for (j = 0; j < 64; j += 16) {
179#if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2)
184 for (i = 0; i < 16; i++) {
190#ifdef _SHA256_UNROLL2
200 for (j = 0; j < 8; j++)
218 for (i = 0; i < 16; i++)
219 data32[i] = ((uint32_t)(p->
buffer[i * 4]) << 24) + ((uint32_t)(p->
buffer[i * 4 + 1]) << 16) +
220 ((uint32_t)(p->
buffer[i * 4 + 2]) << 8) + ((uint32_t)(p->
buffer[i * 4 + 3]));
226 uint32_t curBufferPos = (uint32_t)p->
count & 0x3F;
228 p->
buffer[curBufferPos++] = *data++;
231 if (curBufferPos == 64) {
240 uint64_t lenInBits = (p->
count << 3);
241 uint32_t curBufferPos = (uint32_t)p->
count & 0x3F;
243 p->
buffer[curBufferPos++] = 0x80;
244 while (curBufferPos != (64 - 8)) {
245 curBufferPos &= 0x3F;
246 if (curBufferPos == 0)
248 p->
buffer[curBufferPos++] = 0;
250 for (i = 0; i < 8; i++) {
251 p->
buffer[curBufferPos++] = (
unsigned char)(lenInBits >> 56);
256 for (i = 0; i < 8; i++) {
257 *digest++ = (
unsigned char)(p->
state[i] >> 24);
258 *digest++ = (
unsigned char)(p->
state[i] >> 16);
259 *digest++ = (
unsigned char)(p->
state[i] >> 8);
260 *digest++ = (
unsigned char)(p->
state[i]);
#define R(a, b, c, d, e, f, g, h, i)
unsigned long long ULong64_t
void sha256_update(sha256_t *p, const unsigned char *data, size_t size)
static void sha256_transform(uint32_t *state, const uint32_t *data)
static void sha256_write_byte_block(sha256_t *p)
static const uint32_t K[64]
void sha256_final(sha256_t *p, unsigned char *digest)
void sha256_init(sha256_t *p)
struct ROOT::Internal::SHA256::sha256_t sha256_t
void sha256_hash(unsigned char *buf, const unsigned char *data, size_t size)
void Sha256(const unsigned char *data, int len, ULong64_t *fDigest)