16
18
/* SHA-224 and SHA-256 constants for 64 rounds. These words represent
17
19
* the first 32 bits of the fractional parts of the cube
18
20
* roots of the first 64 prime numbers. */
19
static const unsigned K256[64] = {
20
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
21
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
22
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
23
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
24
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
25
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
26
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
27
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
28
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
29
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
30
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
21
static const unsigned rhash_k256[64] = {
22
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
23
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
24
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
25
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
26
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
27
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
28
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
29
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
30
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
31
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
32
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
33
35
/* The SHA256/224 functions defined by FIPS 180-3, 4.1.2 */
44
46
/* Recalculate element n-th of circular buffer W using formula
45
47
* W[n] = sigma1(W[n-2]) + W[n-7] + sigma0(W[n-15]) + W[n-16]; */
46
48
#define RECALCULATE_W(W,n) \
47
(W[n] += (sigma1(W[(n-2) & 15]) + W[(n-7) & 15] + sigma0(W[(n-15) & 15])))
49
(W[n] += (sigma1(W[(n-2) & 15]) + W[(n-7) & 15] + sigma0(W[(n-15) & 15])))
49
51
#define ROUND(a,b,c,d,e,f,g,h,k,data) { \
50
unsigned T1 = h + Sigma1(e) + Ch(e,f,g) + k + (data); \
51
d += T1, h = T1 + Sigma0(a) + Maj(a,b,c); }
52
unsigned T1 = h + Sigma1(e) + Ch(e,f,g) + k + (data); \
53
d += T1, h = T1 + Sigma0(a) + Maj(a,b,c); }
52
54
#define ROUND_1_16(a,b,c,d,e,f,g,h,n) \
53
ROUND(a,b,c,d,e,f,g,h, K256[n], W[n] = be2me_32(block[n]))
55
ROUND(a,b,c,d,e,f,g,h, rhash_k256[n], W[n] = be2me_32(block[n]))
54
56
#define ROUND_17_64(a,b,c,d,e,f,g,h,n) \
55
ROUND(a,b,c,d,e,f,g,h, k[n], RECALCULATE_W(W, n))
57
ROUND(a,b,c,d,e,f,g,h, k[n], RECALCULATE_W(W, n))
58
60
* Initialize context before calculaing hash.
60
62
* @param ctx context to initalize
62
void sha256_init(sha256_ctx *ctx)
64
void rhash_sha256_init(sha256_ctx *ctx)
64
/* Initial values. These words were obtained by taking the first 32
65
* bits of the fractional parts of the square roots of the first
66
* eight prime numbers. */
67
static const unsigned SHA256_H0[8] = {
68
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
69
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
73
ctx->digest_length = sha256_hash_size;
75
/* initialize algorithm state */
76
memcpy(ctx->hash, SHA256_H0, sizeof(ctx->hash));
66
/* Initial values. These words were obtained by taking the first 32
67
* bits of the fractional parts of the square roots of the first
68
* eight prime numbers. */
69
static const unsigned SHA256_H0[8] = {
70
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
71
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
75
ctx->digest_length = sha256_hash_size;
77
/* initialize algorithm state */
78
memcpy(ctx->hash, SHA256_H0, sizeof(ctx->hash));
103
105
* @param hash algorithm state
104
106
* @param block the message block to process
106
static void sha256_process_block(unsigned hash[8], unsigned block[16])
108
static void rhash_sha256_process_block(unsigned hash[8], unsigned block[16])
108
unsigned A, B, C, D, E, F, G, H;
113
A = hash[0], B = hash[1], C = hash[2], D = hash[3];
114
E = hash[4], F = hash[5], G = hash[6], H = hash[7];
116
/* Compute SHA using alternate Method: FIPS 180-3 6.1.3 */
117
ROUND_1_16(A, B, C, D, E, F, G, H, 0);
118
ROUND_1_16(H, A, B, C, D, E, F, G, 1);
119
ROUND_1_16(G, H, A, B, C, D, E, F, 2);
120
ROUND_1_16(F, G, H, A, B, C, D, E, 3);
121
ROUND_1_16(E, F, G, H, A, B, C, D, 4);
122
ROUND_1_16(D, E, F, G, H, A, B, C, 5);
123
ROUND_1_16(C, D, E, F, G, H, A, B, 6);
124
ROUND_1_16(B, C, D, E, F, G, H, A, 7);
125
ROUND_1_16(A, B, C, D, E, F, G, H, 8);
126
ROUND_1_16(H, A, B, C, D, E, F, G, 9);
127
ROUND_1_16(G, H, A, B, C, D, E, F, 10);
128
ROUND_1_16(F, G, H, A, B, C, D, E, 11);
129
ROUND_1_16(E, F, G, H, A, B, C, D, 12);
130
ROUND_1_16(D, E, F, G, H, A, B, C, 13);
131
ROUND_1_16(C, D, E, F, G, H, A, B, 14);
132
ROUND_1_16(B, C, D, E, F, G, H, A, 15);
134
for(i = 16, k = &K256[16]; i < 64; i += 16, k += 16) {
135
ROUND_17_64(A, B, C, D, E, F, G, H, 0);
136
ROUND_17_64(H, A, B, C, D, E, F, G, 1);
137
ROUND_17_64(G, H, A, B, C, D, E, F, 2);
138
ROUND_17_64(F, G, H, A, B, C, D, E, 3);
139
ROUND_17_64(E, F, G, H, A, B, C, D, 4);
140
ROUND_17_64(D, E, F, G, H, A, B, C, 5);
141
ROUND_17_64(C, D, E, F, G, H, A, B, 6);
142
ROUND_17_64(B, C, D, E, F, G, H, A, 7);
143
ROUND_17_64(A, B, C, D, E, F, G, H, 8);
144
ROUND_17_64(H, A, B, C, D, E, F, G, 9);
145
ROUND_17_64(G, H, A, B, C, D, E, F, 10);
146
ROUND_17_64(F, G, H, A, B, C, D, E, 11);
147
ROUND_17_64(E, F, G, H, A, B, C, D, 12);
148
ROUND_17_64(D, E, F, G, H, A, B, C, 13);
149
ROUND_17_64(C, D, E, F, G, H, A, B, 14);
150
ROUND_17_64(B, C, D, E, F, G, H, A, 15);
153
hash[0] += A, hash[1] += B, hash[2] += C, hash[3] += D;
154
hash[4] += E, hash[5] += F, hash[6] += G, hash[7] += H;
110
unsigned A, B, C, D, E, F, G, H;
115
A = hash[0], B = hash[1], C = hash[2], D = hash[3];
116
E = hash[4], F = hash[5], G = hash[6], H = hash[7];
118
/* Compute SHA using alternate Method: FIPS 180-3 6.1.3 */
119
ROUND_1_16(A, B, C, D, E, F, G, H, 0);
120
ROUND_1_16(H, A, B, C, D, E, F, G, 1);
121
ROUND_1_16(G, H, A, B, C, D, E, F, 2);
122
ROUND_1_16(F, G, H, A, B, C, D, E, 3);
123
ROUND_1_16(E, F, G, H, A, B, C, D, 4);
124
ROUND_1_16(D, E, F, G, H, A, B, C, 5);
125
ROUND_1_16(C, D, E, F, G, H, A, B, 6);
126
ROUND_1_16(B, C, D, E, F, G, H, A, 7);
127
ROUND_1_16(A, B, C, D, E, F, G, H, 8);
128
ROUND_1_16(H, A, B, C, D, E, F, G, 9);
129
ROUND_1_16(G, H, A, B, C, D, E, F, 10);
130
ROUND_1_16(F, G, H, A, B, C, D, E, 11);
131
ROUND_1_16(E, F, G, H, A, B, C, D, 12);
132
ROUND_1_16(D, E, F, G, H, A, B, C, 13);
133
ROUND_1_16(C, D, E, F, G, H, A, B, 14);
134
ROUND_1_16(B, C, D, E, F, G, H, A, 15);
136
for(i = 16, k = &rhash_k256[16]; i < 64; i += 16, k += 16) {
137
ROUND_17_64(A, B, C, D, E, F, G, H, 0);
138
ROUND_17_64(H, A, B, C, D, E, F, G, 1);
139
ROUND_17_64(G, H, A, B, C, D, E, F, 2);
140
ROUND_17_64(F, G, H, A, B, C, D, E, 3);
141
ROUND_17_64(E, F, G, H, A, B, C, D, 4);
142
ROUND_17_64(D, E, F, G, H, A, B, C, 5);
143
ROUND_17_64(C, D, E, F, G, H, A, B, 6);
144
ROUND_17_64(B, C, D, E, F, G, H, A, 7);
145
ROUND_17_64(A, B, C, D, E, F, G, H, 8);
146
ROUND_17_64(H, A, B, C, D, E, F, G, 9);
147
ROUND_17_64(G, H, A, B, C, D, E, F, 10);
148
ROUND_17_64(F, G, H, A, B, C, D, E, 11);
149
ROUND_17_64(E, F, G, H, A, B, C, D, 12);
150
ROUND_17_64(D, E, F, G, H, A, B, C, 13);
151
ROUND_17_64(C, D, E, F, G, H, A, B, 14);
152
ROUND_17_64(B, C, D, E, F, G, H, A, 15);
155
hash[0] += A, hash[1] += B, hash[2] += C, hash[3] += D;
156
hash[4] += E, hash[5] += F, hash[6] += G, hash[7] += H;