738794acda5bc285184d9071035fb7c3aab7dd7a
[trex.git] /
1
2 /*-
3  * Copyright 2005,2007,2009 Colin Percival
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  */
28
29 #include "api.h"
30 #include "crypto_hash_sha256.h"
31 #include "utils.h"
32
33 #include <sys/types.h>
34
35 #include <limits.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 /* Avoid namespace collisions with BSD <sys/endian.h>. */
41 #define be32dec _sha256_be32dec
42 #define be32enc _sha256_be32enc
43
44 static inline uint32_t
45 be32dec(const void *pp)
46 {
47     const uint8_t *p = (uint8_t const *)pp;
48
49     return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
50             ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
51 }
52
53 static inline void
54 be32enc(void *pp, uint32_t x)
55 {
56     uint8_t * p = (uint8_t *)pp;
57
58     p[3] = x & 0xff;
59     p[2] = (x >> 8) & 0xff;
60     p[1] = (x >> 16) & 0xff;
61     p[0] = (x >> 24) & 0xff;
62 }
63
64 static void
65 be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
66 {
67     size_t i;
68
69     for (i = 0; i < len / 4; i++) {
70         be32enc(dst + i * 4, src[i]);
71     }
72 }
73
74 static void
75 be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
76 {
77     size_t i;
78
79     for (i = 0; i < len / 4; i++) {
80         dst[i] = be32dec(src + i * 4);
81     }
82 }
83
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))
92
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);                      \
96     d += t0;                                        \
97     h  = t0 + t1;
98
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],   \
104         W[i] + k)
105
106 static void
107 SHA256_Transform(uint32_t *state, const unsigned char block[64])
108 {
109     uint32_t W[64];
110     uint32_t S[8];
111     uint32_t t0, t1;
112     int i;
113
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];
117     }
118
119     memcpy(S, state, 32);
120
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);
185
186     for (i = 0; i < 8; i++) {
187         state[i] += S[i];
188     }
189
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);
194 }
195
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
201 };
202
203 static void
204 SHA256_Pad(crypto_hash_sha256_state *state)
205 {
206     unsigned char len[8];
207     uint32_t r, plen;
208
209     be32enc_vect(len, state->count, 8);
210
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);
214
215     crypto_hash_sha256_update(state, len, 8);
216 }
217
218 int
219 crypto_hash_sha256_init(crypto_hash_sha256_state *state)
220 {
221     state->count[0] = state->count[1] = 0;
222
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;
231
232     return 0;
233 }
234
235 int
236 crypto_hash_sha256_update(crypto_hash_sha256_state *state,
237                           const unsigned char *in,
238                           unsigned long long inlen)
239 {
240     uint32_t bitlen[2];
241     uint32_t r;
242
243     r = (state->count[1] >> 3) & 0x3f;
244
245     bitlen[1] = ((uint32_t)inlen) << 3;
246     bitlen[0] = (uint32_t)(inlen >> 29);
247
248     /* LCOV_EXCL_START */
249     if ((state->count[1] += bitlen[1]) < bitlen[1]) {
250         state->count[0]++;
251     }
252     /* LCOV_EXCL_STOP */
253     state->count[0] += bitlen[0];
254
255     if (inlen < 64 - r) {
256         memcpy(&state->buf[r], in, inlen);
257         return 0;
258     }
259     memcpy(&state->buf[r], in, 64 - r);
260     SHA256_Transform(state->state, state->buf);
261     in += 64 - r;
262     inlen -= 64 - r;
263
264     while (inlen >= 64) {
265         SHA256_Transform(state->state, in);
266         in += 64;
267         inlen -= 64;
268     }
269     memcpy(state->buf, in, inlen);
270
271     return 0;
272 }
273
274 int
275 crypto_hash_sha256_final(crypto_hash_sha256_state *state,
276                          unsigned char *out)
277 {
278     SHA256_Pad(state);
279     be32enc_vect(out, state->state, 32);
280     sodium_memzero((void *) state, sizeof *state);
281
282     return 0;
283 }
284
285 int
286 crypto_hash(unsigned char *out, const unsigned char *in,
287             unsigned long long inlen)
288 {
289     crypto_hash_sha256_state state;
290
291     crypto_hash_sha256_init(&state);
292     crypto_hash_sha256_update(&state, in, inlen);
293     crypto_hash_sha256_final(&state, out);
294
295     return 0;
296 }