3 * Copyright 2005,2007,2009 Colin Percival
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "crypto_hash_sha256.h"
33 #include <sys/types.h>
40 /* Avoid namespace collisions with BSD <sys/endian.h>. */
41 #define be32dec _sha256_be32dec
42 #define be32enc _sha256_be32enc
44 static inline uint32_t
45 be32dec(const void *pp)
47 const uint8_t *p = (uint8_t const *)pp;
49 return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
50 ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
54 be32enc(void *pp, uint32_t x)
56 uint8_t * p = (uint8_t *)pp;
59 p[2] = (x >> 8) & 0xff;
60 p[1] = (x >> 16) & 0xff;
61 p[0] = (x >> 24) & 0xff;
65 be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
69 for (i = 0; i < len / 4; i++) {
70 be32enc(dst + i * 4, src[i]);
75 be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
79 for (i = 0; i < len / 4; i++) {
80 dst[i] = be32dec(src + i * 4);
84 #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
85 #define Maj(x, y, z) ((x & (y | z)) | (y & z))
86 #define SHR(x, n) (x >> n)
87 #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
88 #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
89 #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
90 #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
91 #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
93 #define RND(a, b, c, d, e, f, g, h, k) \
94 t0 = h + S1(e) + Ch(e, f, g) + k; \
95 t1 = S0(a) + Maj(a, b, c); \
99 #define RNDr(S, W, i, k) \
100 RND(S[(64 - i) % 8], S[(65 - i) % 8], \
101 S[(66 - i) % 8], S[(67 - i) % 8], \
102 S[(68 - i) % 8], S[(69 - i) % 8], \
103 S[(70 - i) % 8], S[(71 - i) % 8], \
107 SHA256_Transform(uint32_t *state, const unsigned char block[64])
114 be32dec_vect(W, block, 64);
115 for (i = 16; i < 64; i++) {
116 W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
119 memcpy(S, state, 32);
121 RNDr(S, W, 0, 0x428a2f98);
122 RNDr(S, W, 1, 0x71374491);
123 RNDr(S, W, 2, 0xb5c0fbcf);
124 RNDr(S, W, 3, 0xe9b5dba5);
125 RNDr(S, W, 4, 0x3956c25b);
126 RNDr(S, W, 5, 0x59f111f1);
127 RNDr(S, W, 6, 0x923f82a4);
128 RNDr(S, W, 7, 0xab1c5ed5);
129 RNDr(S, W, 8, 0xd807aa98);
130 RNDr(S, W, 9, 0x12835b01);
131 RNDr(S, W, 10, 0x243185be);
132 RNDr(S, W, 11, 0x550c7dc3);
133 RNDr(S, W, 12, 0x72be5d74);
134 RNDr(S, W, 13, 0x80deb1fe);
135 RNDr(S, W, 14, 0x9bdc06a7);
136 RNDr(S, W, 15, 0xc19bf174);
137 RNDr(S, W, 16, 0xe49b69c1);
138 RNDr(S, W, 17, 0xefbe4786);
139 RNDr(S, W, 18, 0x0fc19dc6);
140 RNDr(S, W, 19, 0x240ca1cc);
141 RNDr(S, W, 20, 0x2de92c6f);
142 RNDr(S, W, 21, 0x4a7484aa);
143 RNDr(S, W, 22, 0x5cb0a9dc);
144 RNDr(S, W, 23, 0x76f988da);
145 RNDr(S, W, 24, 0x983e5152);
146 RNDr(S, W, 25, 0xa831c66d);
147 RNDr(S, W, 26, 0xb00327c8);
148 RNDr(S, W, 27, 0xbf597fc7);
149 RNDr(S, W, 28, 0xc6e00bf3);
150 RNDr(S, W, 29, 0xd5a79147);
151 RNDr(S, W, 30, 0x06ca6351);
152 RNDr(S, W, 31, 0x14292967);
153 RNDr(S, W, 32, 0x27b70a85);
154 RNDr(S, W, 33, 0x2e1b2138);
155 RNDr(S, W, 34, 0x4d2c6dfc);
156 RNDr(S, W, 35, 0x53380d13);
157 RNDr(S, W, 36, 0x650a7354);
158 RNDr(S, W, 37, 0x766a0abb);
159 RNDr(S, W, 38, 0x81c2c92e);
160 RNDr(S, W, 39, 0x92722c85);
161 RNDr(S, W, 40, 0xa2bfe8a1);
162 RNDr(S, W, 41, 0xa81a664b);
163 RNDr(S, W, 42, 0xc24b8b70);
164 RNDr(S, W, 43, 0xc76c51a3);
165 RNDr(S, W, 44, 0xd192e819);
166 RNDr(S, W, 45, 0xd6990624);
167 RNDr(S, W, 46, 0xf40e3585);
168 RNDr(S, W, 47, 0x106aa070);
169 RNDr(S, W, 48, 0x19a4c116);
170 RNDr(S, W, 49, 0x1e376c08);
171 RNDr(S, W, 50, 0x2748774c);
172 RNDr(S, W, 51, 0x34b0bcb5);
173 RNDr(S, W, 52, 0x391c0cb3);
174 RNDr(S, W, 53, 0x4ed8aa4a);
175 RNDr(S, W, 54, 0x5b9cca4f);
176 RNDr(S, W, 55, 0x682e6ff3);
177 RNDr(S, W, 56, 0x748f82ee);
178 RNDr(S, W, 57, 0x78a5636f);
179 RNDr(S, W, 58, 0x84c87814);
180 RNDr(S, W, 59, 0x8cc70208);
181 RNDr(S, W, 60, 0x90befffa);
182 RNDr(S, W, 61, 0xa4506ceb);
183 RNDr(S, W, 62, 0xbef9a3f7);
184 RNDr(S, W, 63, 0xc67178f2);
186 for (i = 0; i < 8; i++) {
190 sodium_memzero((void *) W, sizeof W);
191 sodium_memzero((void *) S, sizeof S);
192 sodium_memzero((void *) &t0, sizeof t0);
193 sodium_memzero((void *) &t1, sizeof t1);
196 static unsigned char PAD[64] = {
197 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
199 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
204 SHA256_Pad(crypto_hash_sha256_state *state)
206 unsigned char len[8];
209 be32enc_vect(len, state->count, 8);
211 r = (state->count[1] >> 3) & 0x3f;
212 plen = (r < 56) ? (56 - r) : (120 - r);
213 crypto_hash_sha256_update(state, PAD, (unsigned long long) plen);
215 crypto_hash_sha256_update(state, len, 8);
219 crypto_hash_sha256_init(crypto_hash_sha256_state *state)
221 state->count[0] = state->count[1] = 0;
223 state->state[0] = 0x6A09E667;
224 state->state[1] = 0xBB67AE85;
225 state->state[2] = 0x3C6EF372;
226 state->state[3] = 0xA54FF53A;
227 state->state[4] = 0x510E527F;
228 state->state[5] = 0x9B05688C;
229 state->state[6] = 0x1F83D9AB;
230 state->state[7] = 0x5BE0CD19;
236 crypto_hash_sha256_update(crypto_hash_sha256_state *state,
237 const unsigned char *in,
238 unsigned long long inlen)
243 r = (state->count[1] >> 3) & 0x3f;
245 bitlen[1] = ((uint32_t)inlen) << 3;
246 bitlen[0] = (uint32_t)(inlen >> 29);
248 /* LCOV_EXCL_START */
249 if ((state->count[1] += bitlen[1]) < bitlen[1]) {
253 state->count[0] += bitlen[0];
255 if (inlen < 64 - r) {
256 memcpy(&state->buf[r], in, inlen);
259 memcpy(&state->buf[r], in, 64 - r);
260 SHA256_Transform(state->state, state->buf);
264 while (inlen >= 64) {
265 SHA256_Transform(state->state, in);
269 memcpy(state->buf, in, inlen);
275 crypto_hash_sha256_final(crypto_hash_sha256_state *state,
279 be32enc_vect(out, state->state, 32);
280 sodium_memzero((void *) state, sizeof *state);
286 crypto_hash(unsigned char *out, const unsigned char *in,
287 unsigned long long inlen)
289 crypto_hash_sha256_state state;
291 crypto_hash_sha256_init(&state);
292 crypto_hash_sha256_update(&state, in, inlen);
293 crypto_hash_sha256_final(&state, out);