fork/slippc

git clone https://git.julia.blue/fork/slippc

Patrick Crainadded picohash library, getter for parser replay data, and more tests3956dc7

0.8.0
28.1 KiB751 linesraw
1/*
2 * The code is placed under public domain by Kazuho Oku <kazuhooku@gmail.com>.
3 *
4 * The MD5 implementation is based on a public domain implementation written by
5 * Solar Designer <solar@openwall.com> in 2001, which is used by Dovecot.
6 *
7 * The SHA1 implementation is based on a public domain implementation written
8 * by Wei Dai and other contributors for libcrypt, used also in liboauth.
9 *
10 * The SHA224/SHA256 implementation is based on a public domain implementation
11 * by Sam Hocevar <sam@hocevar.net> for LibTomCrypt.
12 */
13#ifndef _picohash_h_
14#define _picohash_h_
15
16#include <assert.h>
17#include <inttypes.h>
18#include <string.h>
19
20#ifdef __BIG_ENDIAN__
21#define _PICOHASH_BIG_ENDIAN
22#elif defined __LITTLE_ENDIAN__
23/* override */
24#elif defined __BYTE_ORDER
25#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
26#define _PICOHASH_BIG_ENDIAN
27#endif
28#else               // ! defined __LITTLE_ENDIAN__
29#include <endian.h> // machine/endian.h
30#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
31#define _PICOHASH_BIG_ENDIAN
32#endif
33#endif
34
35#define PICOHASH_MD5_BLOCK_LENGTH 64
36#define PICOHASH_MD5_DIGEST_LENGTH 16
37
38typedef struct {
39    uint_fast32_t lo, hi;
40    uint_fast32_t a, b, c, d;
41    unsigned char buffer[64];
42    uint_fast32_t block[PICOHASH_MD5_DIGEST_LENGTH];
43} _picohash_md5_ctx_t;
44
45static void _picohash_md5_init(_picohash_md5_ctx_t *ctx);
46static void _picohash_md5_update(_picohash_md5_ctx_t *ctx, const void *data, size_t size);
47static void _picohash_md5_final(_picohash_md5_ctx_t *ctx, void *digest);
48
49#define PICOHASH_SHA1_BLOCK_LENGTH 64
50#define PICOHASH_SHA1_DIGEST_LENGTH 20
51
52typedef struct {
53    uint32_t buffer[PICOHASH_SHA1_BLOCK_LENGTH / 4];
54    uint32_t state[PICOHASH_SHA1_DIGEST_LENGTH / 4];
55    uint64_t byteCount;
56    uint8_t bufferOffset;
57} _picohash_sha1_ctx_t;
58
59static void _picohash_sha1_init(_picohash_sha1_ctx_t *ctx);
60static void _picohash_sha1_update(_picohash_sha1_ctx_t *ctx, const void *input, size_t len);
61static void _picohash_sha1_final(_picohash_sha1_ctx_t *ctx, void *digest);
62
63#define PICOHASH_SHA256_BLOCK_LENGTH 64
64#define PICOHASH_SHA256_DIGEST_LENGTH 32
65#define PICOHASH_SHA224_BLOCK_LENGTH PICOHASH_SHA256_BLOCK_LENGTH
66#define PICOHASH_SHA224_DIGEST_LENGTH 28
67
68typedef struct {
69    uint64_t length;
70    uint32_t state[PICOHASH_SHA256_DIGEST_LENGTH / 4];
71    uint32_t curlen;
72    unsigned char buf[PICOHASH_SHA256_BLOCK_LENGTH];
73} _picohash_sha256_ctx_t;
74
75static void _picohash_sha256_init(_picohash_sha256_ctx_t *ctx);
76static void _picohash_sha256_update(_picohash_sha256_ctx_t *ctx, const void *data, size_t len);
77static void _picohash_sha256_final(_picohash_sha256_ctx_t *ctx, void *digest);
78static void _picohash_sha224_init(_picohash_sha256_ctx_t *ctx);
79static void _picohash_sha224_final(_picohash_sha256_ctx_t *ctx, void *digest);
80
81#define PICOHASH_MAX_BLOCK_LENGTH 64
82#define PICOHASH_MAX_DIGEST_LENGTH 32
83
84typedef struct {
85    union {
86        _picohash_md5_ctx_t _md5;
87        _picohash_sha1_ctx_t _sha1;
88        _picohash_sha256_ctx_t _sha256;
89    };
90    size_t block_length;
91    size_t digest_length;
92    void (*_reset)(void *ctx);
93    void (*_update)(void *ctx, const void *input, size_t len);
94    void (*_final)(void *ctx, void *digest);
95    struct {
96        unsigned char key[PICOHASH_MAX_BLOCK_LENGTH];
97        void (*hash_reset)(void *ctx);
98        void (*hash_final)(void *ctx, void *digest);
99    } _hmac;
100} picohash_ctx_t;
101
102static void picohash_init_md5(picohash_ctx_t *ctx);
103static void picohash_init_sha1(picohash_ctx_t *ctx);
104static void picohash_init_sha224(picohash_ctx_t *ctx);
105static void picohash_init_sha256(picohash_ctx_t *ctx);
106static void picohash_update(picohash_ctx_t *ctx, const void *input, size_t len);
107static void picohash_final(picohash_ctx_t *ctx, void *digest);
108static void picohash_reset(picohash_ctx_t *ctx);
109
110static void picohash_init_hmac(picohash_ctx_t *ctx, void (*initf)(picohash_ctx_t *), const void *key, size_t key_len);
111
112/* following are private definitions */
113
114/*
115 * The basic MD5 functions.
116 *
117 * F is optimized compared to its RFC 1321 definition just like in Colin
118 * Plumb's implementation.
119 */
120#define _PICOHASH_MD5_F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
121#define _PICOHASH_MD5_G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
122#define _PICOHASH_MD5_H(x, y, z) ((x) ^ (y) ^ (z))
123#define _PICOHASH_MD5_I(x, y, z) ((y) ^ ((x) | ~(z)))
124
125/*
126 * The MD5 transformation for all four rounds.
127 */
128#define _PICOHASH_MD5_STEP(f, a, b, c, d, x, t, s)                                                                                 \
129    (a) += f((b), (c), (d)) + (x) + (t);                                                                                           \
130    (a) = (((a) << (s)) | (((a)&0xffffffff) >> (32 - (s))));                                                                       \
131    (a) += (b);
132
133/*
134 * SET reads 4 input bytes in little-endian byte order and stores them
135 * in a properly aligned word in host byte order.
136 *
137 * The check for little-endian architectures which tolerate unaligned
138 * memory accesses is just an optimization.  Nothing will break if it
139 * doesn't work.
140 */
141#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
142#define _PICOHASH_MD5_SET(n) (*(const uint32_t *)&ptr[(n)*4])
143#define _PICOHASH_MD5_GET(n) _PICOHASH_MD5_SET(n)
144#else
145#define _PICOHASH_MD5_SET(n)                                                                                                       \
146    (ctx->block[(n)] = (uint_fast32_t)ptr[(n)*4] | ((uint_fast32_t)ptr[(n)*4 + 1] << 8) | ((uint_fast32_t)ptr[(n)*4 + 2] << 16) |  \
147                       ((uint_fast32_t)ptr[(n)*4 + 3] << 24))
148#define _PICOHASH_MD5_GET(n) (ctx->block[(n)])
149#endif
150
151/*
152 * This processes one or more 64-byte data blocks, but does NOT update
153 * the bit counters.  There're no alignment requirements.
154 */
155static const void *_picohash_md5_body(_picohash_md5_ctx_t *ctx, const void *data, size_t size)
156{
157    const unsigned char *ptr;
158    uint_fast32_t a, b, c, d;
159    uint_fast32_t saved_a, saved_b, saved_c, saved_d;
160
161    ptr = (const unsigned char*)data;
162
163    a = ctx->a;
164    b = ctx->b;
165    c = ctx->c;
166    d = ctx->d;
167
168    do {
169        saved_a = a;
170        saved_b = b;
171        saved_c = c;
172        saved_d = d;
173
174        /* Round 1 */
175        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, a, b, c, d, _PICOHASH_MD5_SET(0), 0xd76aa478, 7)
176        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, d, a, b, c, _PICOHASH_MD5_SET(1), 0xe8c7b756, 12)
177        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, c, d, a, b, _PICOHASH_MD5_SET(2), 0x242070db, 17)
178        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, b, c, d, a, _PICOHASH_MD5_SET(3), 0xc1bdceee, 22)
179        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, a, b, c, d, _PICOHASH_MD5_SET(4), 0xf57c0faf, 7)
180        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, d, a, b, c, _PICOHASH_MD5_SET(5), 0x4787c62a, 12)
181        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, c, d, a, b, _PICOHASH_MD5_SET(6), 0xa8304613, 17)
182        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, b, c, d, a, _PICOHASH_MD5_SET(7), 0xfd469501, 22)
183        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, a, b, c, d, _PICOHASH_MD5_SET(8), 0x698098d8, 7)
184        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, d, a, b, c, _PICOHASH_MD5_SET(9), 0x8b44f7af, 12)
185        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, c, d, a, b, _PICOHASH_MD5_SET(10), 0xffff5bb1, 17)
186        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, b, c, d, a, _PICOHASH_MD5_SET(11), 0x895cd7be, 22)
187        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, a, b, c, d, _PICOHASH_MD5_SET(12), 0x6b901122, 7)
188        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, d, a, b, c, _PICOHASH_MD5_SET(13), 0xfd987193, 12)
189        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, c, d, a, b, _PICOHASH_MD5_SET(14), 0xa679438e, 17)
190        _PICOHASH_MD5_STEP(_PICOHASH_MD5_F, b, c, d, a, _PICOHASH_MD5_SET(15), 0x49b40821, 22)
191
192        /* Round 2 */
193        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, a, b, c, d, _PICOHASH_MD5_GET(1), 0xf61e2562, 5)
194        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, d, a, b, c, _PICOHASH_MD5_GET(6), 0xc040b340, 9)
195        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, c, d, a, b, _PICOHASH_MD5_GET(11), 0x265e5a51, 14)
196        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, b, c, d, a, _PICOHASH_MD5_GET(0), 0xe9b6c7aa, 20)
197        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, a, b, c, d, _PICOHASH_MD5_GET(5), 0xd62f105d, 5)
198        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, d, a, b, c, _PICOHASH_MD5_GET(10), 0x02441453, 9)
199        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, c, d, a, b, _PICOHASH_MD5_GET(15), 0xd8a1e681, 14)
200        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, b, c, d, a, _PICOHASH_MD5_GET(4), 0xe7d3fbc8, 20)
201        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, a, b, c, d, _PICOHASH_MD5_GET(9), 0x21e1cde6, 5)
202        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, d, a, b, c, _PICOHASH_MD5_GET(14), 0xc33707d6, 9)
203        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, c, d, a, b, _PICOHASH_MD5_GET(3), 0xf4d50d87, 14)
204        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, b, c, d, a, _PICOHASH_MD5_GET(8), 0x455a14ed, 20)
205        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, a, b, c, d, _PICOHASH_MD5_GET(13), 0xa9e3e905, 5)
206        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, d, a, b, c, _PICOHASH_MD5_GET(2), 0xfcefa3f8, 9)
207        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, c, d, a, b, _PICOHASH_MD5_GET(7), 0x676f02d9, 14)
208        _PICOHASH_MD5_STEP(_PICOHASH_MD5_G, b, c, d, a, _PICOHASH_MD5_GET(12), 0x8d2a4c8a, 20)
209
210        /* Round 3 */
211        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, a, b, c, d, _PICOHASH_MD5_GET(5), 0xfffa3942, 4)
212        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, d, a, b, c, _PICOHASH_MD5_GET(8), 0x8771f681, 11)
213        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, c, d, a, b, _PICOHASH_MD5_GET(11), 0x6d9d6122, 16)
214        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, b, c, d, a, _PICOHASH_MD5_GET(14), 0xfde5380c, 23)
215        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, a, b, c, d, _PICOHASH_MD5_GET(1), 0xa4beea44, 4)
216        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, d, a, b, c, _PICOHASH_MD5_GET(4), 0x4bdecfa9, 11)
217        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, c, d, a, b, _PICOHASH_MD5_GET(7), 0xf6bb4b60, 16)
218        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, b, c, d, a, _PICOHASH_MD5_GET(10), 0xbebfbc70, 23)
219        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, a, b, c, d, _PICOHASH_MD5_GET(13), 0x289b7ec6, 4)
220        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, d, a, b, c, _PICOHASH_MD5_GET(0), 0xeaa127fa, 11)
221        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, c, d, a, b, _PICOHASH_MD5_GET(3), 0xd4ef3085, 16)
222        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, b, c, d, a, _PICOHASH_MD5_GET(6), 0x04881d05, 23)
223        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, a, b, c, d, _PICOHASH_MD5_GET(9), 0xd9d4d039, 4)
224        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, d, a, b, c, _PICOHASH_MD5_GET(12), 0xe6db99e5, 11)
225        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, c, d, a, b, _PICOHASH_MD5_GET(15), 0x1fa27cf8, 16)
226        _PICOHASH_MD5_STEP(_PICOHASH_MD5_H, b, c, d, a, _PICOHASH_MD5_GET(2), 0xc4ac5665, 23)
227
228        /* Round 4 */
229        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, a, b, c, d, _PICOHASH_MD5_GET(0), 0xf4292244, 6)
230        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, d, a, b, c, _PICOHASH_MD5_GET(7), 0x432aff97, 10)
231        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, c, d, a, b, _PICOHASH_MD5_GET(14), 0xab9423a7, 15)
232        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, b, c, d, a, _PICOHASH_MD5_GET(5), 0xfc93a039, 21)
233        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, a, b, c, d, _PICOHASH_MD5_GET(12), 0x655b59c3, 6)
234        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, d, a, b, c, _PICOHASH_MD5_GET(3), 0x8f0ccc92, 10)
235        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, c, d, a, b, _PICOHASH_MD5_GET(10), 0xffeff47d, 15)
236        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, b, c, d, a, _PICOHASH_MD5_GET(1), 0x85845dd1, 21)
237        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, a, b, c, d, _PICOHASH_MD5_GET(8), 0x6fa87e4f, 6)
238        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, d, a, b, c, _PICOHASH_MD5_GET(15), 0xfe2ce6e0, 10)
239        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, c, d, a, b, _PICOHASH_MD5_GET(6), 0xa3014314, 15)
240        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, b, c, d, a, _PICOHASH_MD5_GET(13), 0x4e0811a1, 21)
241        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, a, b, c, d, _PICOHASH_MD5_GET(4), 0xf7537e82, 6)
242        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, d, a, b, c, _PICOHASH_MD5_GET(11), 0xbd3af235, 10)
243        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, c, d, a, b, _PICOHASH_MD5_GET(2), 0x2ad7d2bb, 15)
244        _PICOHASH_MD5_STEP(_PICOHASH_MD5_I, b, c, d, a, _PICOHASH_MD5_GET(9), 0xeb86d391, 21)
245
246        a += saved_a;
247        b += saved_b;
248        c += saved_c;
249        d += saved_d;
250
251        ptr += 64;
252    } while (size -= 64);
253
254    ctx->a = a;
255    ctx->b = b;
256    ctx->c = c;
257    ctx->d = d;
258
259    return ptr;
260}
261
262inline void _picohash_md5_init(_picohash_md5_ctx_t *ctx)
263{
264    ctx->a = 0x67452301;
265    ctx->b = 0xefcdab89;
266    ctx->c = 0x98badcfe;
267    ctx->d = 0x10325476;
268
269    ctx->lo = 0;
270    ctx->hi = 0;
271}
272
273inline void _picohash_md5_update(_picohash_md5_ctx_t *ctx, const void *data, size_t size)
274{
275    uint_fast32_t saved_lo;
276    unsigned long used, free;
277
278    saved_lo = ctx->lo;
279    if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
280        ctx->hi++;
281    ctx->hi += size >> 29;
282
283    used = saved_lo & 0x3f;
284
285    if (used) {
286        free = 64 - used;
287
288        if (size < free) {
289            memcpy(&ctx->buffer[used], data, size);
290            return;
291        }
292
293        memcpy(&ctx->buffer[used], data, free);
294        data = (const unsigned char *)data + free;
295        size -= free;
296        _picohash_md5_body(ctx, ctx->buffer, 64);
297    }
298
299    if (size >= 64) {
300        data = _picohash_md5_body(ctx, data, size & ~(unsigned long)0x3f);
301        size &= 0x3f;
302    }
303
304    memcpy(ctx->buffer, data, size);
305}
306
307inline void _picohash_md5_final(_picohash_md5_ctx_t *ctx, void *_digest)
308{
309    unsigned char *digest = (unsigned char*)_digest;
310    unsigned long used, free;
311
312    used = ctx->lo & 0x3f;
313
314    ctx->buffer[used++] = 0x80;
315
316    free = 64 - used;
317
318    if (free < 8) {
319        memset(&ctx->buffer[used], 0, free);
320        _picohash_md5_body(ctx, ctx->buffer, 64);
321        used = 0;
322        free = 64;
323    }
324
325    memset(&ctx->buffer[used], 0, free - 8);
326
327    ctx->lo <<= 3;
328    ctx->buffer[56] = ctx->lo;
329    ctx->buffer[57] = ctx->lo >> 8;
330    ctx->buffer[58] = ctx->lo >> 16;
331    ctx->buffer[59] = ctx->lo >> 24;
332    ctx->buffer[60] = ctx->hi;
333    ctx->buffer[61] = ctx->hi >> 8;
334    ctx->buffer[62] = ctx->hi >> 16;
335    ctx->buffer[63] = ctx->hi >> 24;
336
337    _picohash_md5_body(ctx, ctx->buffer, 64);
338
339    digest[0] = ctx->a;
340    digest[1] = ctx->a >> 8;
341    digest[2] = ctx->a >> 16;
342    digest[3] = ctx->a >> 24;
343    digest[4] = ctx->b;
344    digest[5] = ctx->b >> 8;
345    digest[6] = ctx->b >> 16;
346    digest[7] = ctx->b >> 24;
347    digest[8] = ctx->c;
348    digest[9] = ctx->c >> 8;
349    digest[10] = ctx->c >> 16;
350    digest[11] = ctx->c >> 24;
351    digest[12] = ctx->d;
352    digest[13] = ctx->d >> 8;
353    digest[14] = ctx->d >> 16;
354    digest[15] = ctx->d >> 24;
355
356    memset(ctx, 0, sizeof(*ctx));
357}
358
359#define _PICOHASH_SHA1_K0 0x5a827999
360#define _PICOHASH_SHA1_K20 0x6ed9eba1
361#define _PICOHASH_SHA1_K40 0x8f1bbcdc
362#define _PICOHASH_SHA1_K60 0xca62c1d6
363
364static inline uint32_t _picohash_sha1_rol32(uint32_t number, uint8_t bits)
365{
366    return ((number << bits) | (number >> (32 - bits)));
367}
368
369static inline void _picohash_sha1_hash_block(_picohash_sha1_ctx_t *s)
370{
371    uint8_t i;
372    uint32_t a, b, c, d, e, t;
373
374    a = s->state[0];
375    b = s->state[1];
376    c = s->state[2];
377    d = s->state[3];
378    e = s->state[4];
379    for (i = 0; i < 80; i++) {
380        if (i >= 16) {
381            t = s->buffer[(i + 13) & 15] ^ s->buffer[(i + 8) & 15] ^ s->buffer[(i + 2) & 15] ^ s->buffer[i & 15];
382            s->buffer[i & 15] = _picohash_sha1_rol32(t, 1);
383        }
384        if (i < 20) {
385            t = (d ^ (b & (c ^ d))) + _PICOHASH_SHA1_K0;
386        } else if (i < 40) {
387            t = (b ^ c ^ d) + _PICOHASH_SHA1_K20;
388        } else if (i < 60) {
389            t = ((b & c) | (d & (b | c))) + _PICOHASH_SHA1_K40;
390        } else {
391            t = (b ^ c ^ d) + _PICOHASH_SHA1_K60;
392        }
393        t += _picohash_sha1_rol32(a, 5) + e + s->buffer[i & 15];
394        e = d;
395        d = c;
396        c = _picohash_sha1_rol32(b, 30);
397        b = a;
398        a = t;
399    }
400    s->state[0] += a;
401    s->state[1] += b;
402    s->state[2] += c;
403    s->state[3] += d;
404    s->state[4] += e;
405}
406
407static inline void _picohash_sha1_add_uncounted(_picohash_sha1_ctx_t *s, uint8_t data)
408{
409    uint8_t *const b = (uint8_t *)s->buffer;
410#ifdef _PICOHASH_BIG_ENDIAN
411    b[s->bufferOffset] = data;
412#else
413    b[s->bufferOffset ^ 3] = data;
414#endif
415    s->bufferOffset++;
416    if (s->bufferOffset == PICOHASH_SHA1_BLOCK_LENGTH) {
417        _picohash_sha1_hash_block(s);
418        s->bufferOffset = 0;
419    }
420}
421
422inline void _picohash_sha1_init(_picohash_sha1_ctx_t *s)
423{
424    s->state[0] = 0x67452301;
425    s->state[1] = 0xefcdab89;
426    s->state[2] = 0x98badcfe;
427    s->state[3] = 0x10325476;
428    s->state[4] = 0xc3d2e1f0;
429    s->byteCount = 0;
430    s->bufferOffset = 0;
431}
432
433inline void _picohash_sha1_update(_picohash_sha1_ctx_t *s, const void *_data, size_t len)
434{
435    const uint8_t *data = (const uint8_t*)_data;
436    for (; len != 0; --len) {
437        ++s->byteCount;
438        _picohash_sha1_add_uncounted(s, *data++);
439    }
440}
441
442inline void _picohash_sha1_final(_picohash_sha1_ctx_t *s, void *digest)
443{
444    // Pad with 0x80 followed by 0x00 until the end of the block
445    _picohash_sha1_add_uncounted(s, 0x80);
446    while (s->bufferOffset != 56)
447        _picohash_sha1_add_uncounted(s, 0x00);
448
449    // Append length in the last 8 bytes
450    _picohash_sha1_add_uncounted(s, s->byteCount >> 53); // Shifting to multiply by 8
451    _picohash_sha1_add_uncounted(s, s->byteCount >> 45); // as SHA-1 supports bitstreams as well as
452    _picohash_sha1_add_uncounted(s, s->byteCount >> 37); // byte.
453    _picohash_sha1_add_uncounted(s, s->byteCount >> 29);
454    _picohash_sha1_add_uncounted(s, s->byteCount >> 21);
455    _picohash_sha1_add_uncounted(s, s->byteCount >> 13);
456    _picohash_sha1_add_uncounted(s, s->byteCount >> 5);
457    _picohash_sha1_add_uncounted(s, s->byteCount << 3);
458
459#ifndef SHA_BIG_ENDIAN
460    { // Swap byte order back
461        int i;
462        for (i = 0; i < 5; i++) {
463            s->state[i] = (((s->state[i]) << 24) & 0xff000000) | (((s->state[i]) << 8) & 0x00ff0000) |
464                          (((s->state[i]) >> 8) & 0x0000ff00) | (((s->state[i]) >> 24) & 0x000000ff);
465        }
466    }
467#endif
468
469    memcpy(digest, s->state, sizeof(s->state));
470}
471
472#define _picohash_sha256_ch(x, y, z) (z ^ (x & (y ^ z)))
473#define _picohash_sha256_maj(x, y, z) (((x | y) & z) | (x & y))
474#define _picohash_sha256_s(x, y)                                                                                                   \
475    (((((uint32_t)(x)&0xFFFFFFFFUL) >> (uint32_t)((y)&31)) | ((uint32_t)(x) << (uint32_t)(32 - ((y)&31)))) & 0xFFFFFFFFUL)
476#define _picohash_sha256_r(x, n) (((x)&0xFFFFFFFFUL) >> (n))
477#define _picohash_sha256_sigma0(x) (_picohash_sha256_s(x, 2) ^ _picohash_sha256_s(x, 13) ^ _picohash_sha256_s(x, 22))
478#define _picohash_sha256_sigma1(x) (_picohash_sha256_s(x, 6) ^ _picohash_sha256_s(x, 11) ^ _picohash_sha256_s(x, 25))
479#define _picohash_sha256_gamma0(x) (_picohash_sha256_s(x, 7) ^ _picohash_sha256_s(x, 18) ^ _picohash_sha256_r(x, 3))
480#define _picohash_sha256_gamma1(x) (_picohash_sha256_s(x, 17) ^ _picohash_sha256_s(x, 19) ^ _picohash_sha256_r(x, 10))
481#define _picohash_sha256_rnd(a, b, c, d, e, f, g, h, i)                                                                            \
482    t0 = h + _picohash_sha256_sigma1(e) + _picohash_sha256_ch(e, f, g) + K[i] + W[i];                                              \
483    t1 = _picohash_sha256_sigma0(a) + _picohash_sha256_maj(a, b, c);                                                               \
484    d += t0;                                                                                                                       \
485    h = t0 + t1;
486
487static inline void _picohash_sha256_compress(_picohash_sha256_ctx_t *ctx, unsigned char *buf)
488{
489    static const uint32_t K[64] = {
490        0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
491        0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
492        0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
493        0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
494        0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
495        0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
496        0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
497        0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL};
498    uint32_t S[8], W[64], t, t0, t1;
499    int i;
500
501    /* copy state into S */
502    for (i = 0; i < 8; i++)
503        S[i] = ctx->state[i];
504
505    /* copy the state into 512-bits into W[0..15] */
506    for (i = 0; i < 16; i++)
507        W[i] =
508            (uint32_t)buf[4 * i] << 24 | (uint32_t)buf[4 * i + 1] << 16 | (uint32_t)buf[4 * i + 2] << 8 | (uint32_t)buf[4 * i + 3];
509
510    /* fill W[16..63] */
511    for (i = 16; i < 64; i++)
512        W[i] = _picohash_sha256_gamma1(W[i - 2]) + W[i - 7] + _picohash_sha256_gamma0(W[i - 15]) + W[i - 16];
513
514    /* Compress */
515    for (i = 0; i < 64; ++i) {
516        _picohash_sha256_rnd(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
517        t = S[7];
518        S[7] = S[6];
519        S[6] = S[5];
520        S[5] = S[4];
521        S[4] = S[3];
522        S[3] = S[2];
523        S[2] = S[1];
524        S[1] = S[0];
525        S[0] = t;
526    }
527
528    /* feedback */
529    for (i = 0; i < 8; i++)
530        ctx->state[i] = ctx->state[i] + S[i];
531}
532
533static inline void _picohash_sha256_do_final(_picohash_sha256_ctx_t *ctx, void *digest, size_t len)
534{
535    unsigned char *out = (unsigned char*)digest;
536    size_t i;
537
538    /* increase the length of the message */
539    ctx->length += ctx->curlen * 8;
540
541    /* append the '1' bit */
542    ctx->buf[ctx->curlen++] = (unsigned char)0x80;
543
544    /* if the length is currently above 56 bytes we append zeros
545     * then compress.  Then we can fall back to padding zeros and length
546     * encoding like normal.
547     */
548    if (ctx->curlen > 56) {
549        while (ctx->curlen < 64) {
550            ctx->buf[ctx->curlen++] = (unsigned char)0;
551        }
552        _picohash_sha256_compress(ctx, ctx->buf);
553        ctx->curlen = 0;
554    }
555
556    /* pad upto 56 bytes of zeroes */
557    while (ctx->curlen < 56) {
558        ctx->buf[ctx->curlen++] = (unsigned char)0;
559    }
560
561    /* store length */
562    for (i = 0; i != 8; ++i)
563        ctx->buf[56 + i] = ctx->length >> (56 - 8 * i);
564    _picohash_sha256_compress(ctx, ctx->buf);
565
566    /* copy output */
567    for (i = 0; i != len / 4; ++i) {
568        out[i * 4] = ctx->state[i] >> 24;
569        out[i * 4 + 1] = ctx->state[i] >> 16;
570        out[i * 4 + 2] = ctx->state[i] >> 8;
571        out[i * 4 + 3] = ctx->state[i];
572    }
573}
574
575inline void _picohash_sha256_init(_picohash_sha256_ctx_t *ctx)
576{
577    ctx->curlen = 0;
578    ctx->length = 0;
579    ctx->state[0] = 0x6A09E667UL;
580    ctx->state[1] = 0xBB67AE85UL;
581    ctx->state[2] = 0x3C6EF372UL;
582    ctx->state[3] = 0xA54FF53AUL;
583    ctx->state[4] = 0x510E527FUL;
584    ctx->state[5] = 0x9B05688CUL;
585    ctx->state[6] = 0x1F83D9ABUL;
586    ctx->state[7] = 0x5BE0CD19UL;
587}
588
589inline void _picohash_sha256_update(_picohash_sha256_ctx_t *ctx, const void *data, size_t len)
590{
591    const unsigned char *in = (const unsigned char*)data;
592    size_t n;
593
594    while (len > 0) {
595        if (ctx->curlen == 0 && len >= PICOHASH_SHA256_BLOCK_LENGTH) {
596            _picohash_sha256_compress(ctx, (unsigned char *)in);
597            ctx->length += PICOHASH_SHA256_BLOCK_LENGTH * 8;
598            in += PICOHASH_SHA256_BLOCK_LENGTH;
599            len -= PICOHASH_SHA256_BLOCK_LENGTH;
600        } else {
601            n = PICOHASH_SHA256_BLOCK_LENGTH - ctx->curlen;
602            if (n > len)
603                n = len;
604            memcpy(ctx->buf + ctx->curlen, in, (size_t)n);
605            ctx->curlen += n;
606            in += n;
607            len -= n;
608            if (ctx->curlen == 64) {
609                _picohash_sha256_compress(ctx, ctx->buf);
610                ctx->length += 8 * PICOHASH_SHA256_BLOCK_LENGTH;
611                ctx->curlen = 0;
612            }
613        }
614    }
615}
616
617inline void _picohash_sha256_final(_picohash_sha256_ctx_t *ctx, void *digest)
618{
619    _picohash_sha256_do_final(ctx, digest, PICOHASH_SHA256_DIGEST_LENGTH);
620}
621
622inline void _picohash_sha224_init(_picohash_sha256_ctx_t *ctx)
623{
624    ctx->curlen = 0;
625    ctx->length = 0;
626    ctx->state[0] = 0xc1059ed8UL;
627    ctx->state[1] = 0x367cd507UL;
628    ctx->state[2] = 0x3070dd17UL;
629    ctx->state[3] = 0xf70e5939UL;
630    ctx->state[4] = 0xffc00b31UL;
631    ctx->state[5] = 0x68581511UL;
632    ctx->state[6] = 0x64f98fa7UL;
633    ctx->state[7] = 0xbefa4fa4UL;
634}
635
636inline void _picohash_sha224_final(_picohash_sha256_ctx_t *ctx, void *digest)
637{
638    _picohash_sha256_do_final(ctx, digest, PICOHASH_SHA224_DIGEST_LENGTH);
639}
640
641inline void picohash_init_md5(picohash_ctx_t *ctx)
642{
643    ctx->block_length = PICOHASH_MD5_BLOCK_LENGTH;
644    ctx->digest_length = PICOHASH_MD5_DIGEST_LENGTH;
645    ctx->_reset = (void (*)(void*))_picohash_md5_init;
646    ctx->_update = (void (*)(void*, const void*, long unsigned int))_picohash_md5_update;
647    ctx->_final = (void (*)(void*,void*))_picohash_md5_final;
648
649    _picohash_md5_init(&ctx->_md5);
650}
651
652inline void picohash_init_sha1(picohash_ctx_t *ctx)
653{
654    ctx->block_length = PICOHASH_SHA1_BLOCK_LENGTH;
655    ctx->digest_length = PICOHASH_SHA1_DIGEST_LENGTH;
656    ctx->_reset = (void (*)(void*))_picohash_sha1_init;
657    ctx->_update = (void (*)(void*, const void*, long unsigned int))_picohash_sha1_update;
658    ctx->_final = (void (*)(void*,void*))_picohash_sha1_final;
659    _picohash_sha1_init(&ctx->_sha1);
660}
661
662inline void picohash_init_sha224(picohash_ctx_t *ctx)
663{
664    ctx->block_length = PICOHASH_SHA224_BLOCK_LENGTH;
665    ctx->digest_length = PICOHASH_SHA224_DIGEST_LENGTH;
666    ctx->_reset = (void (*)(void*))_picohash_sha224_init;
667    ctx->_update = (void (*)(void*, const void*, long unsigned int))_picohash_sha256_update;
668    ctx->_final = (void (*)(void*,void*))_picohash_sha224_final;
669    _picohash_sha224_init(&ctx->_sha256);
670}
671
672inline void picohash_init_sha256(picohash_ctx_t *ctx)
673{
674    ctx->block_length = PICOHASH_SHA256_BLOCK_LENGTH;
675    ctx->digest_length = PICOHASH_SHA256_DIGEST_LENGTH;
676    ctx->_reset = (void (*)(void*))_picohash_sha256_init;
677    ctx->_update = (void (*)(void*, const void*, long unsigned int))_picohash_sha256_update;
678    ctx->_final = (void (*)(void*,void*))_picohash_sha256_final;
679    _picohash_sha256_init(&ctx->_sha256);
680}
681
682inline void picohash_update(picohash_ctx_t *ctx, const void *input, size_t len)
683{
684    ctx->_update(ctx, input, len);
685}
686
687inline void picohash_final(picohash_ctx_t *ctx, void *digest)
688{
689    ctx->_final(ctx, digest);
690}
691
692inline void picohash_reset(picohash_ctx_t *ctx)
693{
694    ctx->_reset(ctx);
695}
696
697static inline void _picohash_hmac_apply_key(picohash_ctx_t *ctx, unsigned char delta)
698{
699    size_t i;
700    for (i = 0; i != ctx->block_length; ++i)
701        ctx->_hmac.key[i] ^= delta;
702    picohash_update(ctx, ctx->_hmac.key, ctx->block_length);
703    for (i = 0; i != ctx->block_length; ++i)
704        ctx->_hmac.key[i] ^= delta;
705}
706
707static void _picohash_hmac_final(picohash_ctx_t *ctx, void *digest)
708{
709    unsigned char inner_digest[PICOHASH_MAX_DIGEST_LENGTH];
710
711    ctx->_hmac.hash_final(ctx, inner_digest);
712
713    ctx->_hmac.hash_reset(ctx);
714    _picohash_hmac_apply_key(ctx, 0x5c);
715    picohash_update(ctx, inner_digest, ctx->digest_length);
716    memset(inner_digest, 0, ctx->digest_length);
717
718    ctx->_hmac.hash_final(ctx, digest);
719}
720
721static inline void _picohash_hmac_reset(picohash_ctx_t *ctx)
722{
723    ctx->_hmac.hash_reset(ctx);
724    _picohash_hmac_apply_key(ctx, 0x36);
725}
726
727inline void picohash_init_hmac(picohash_ctx_t *ctx, void (*initf)(picohash_ctx_t *), const void *key, size_t key_len)
728{
729    initf(ctx);
730
731    memset(ctx->_hmac.key, 0, ctx->block_length);
732    if (key_len > ctx->block_length) {
733        /* hash the key if it is too long */
734        picohash_update(ctx, key, key_len);
735        picohash_final(ctx, ctx->_hmac.key);
736        ctx->_hmac.hash_reset(ctx);
737    } else {
738        memcpy(ctx->_hmac.key, key, key_len);
739    }
740
741    /* replace reset and final function */
742    ctx->_hmac.hash_reset = ctx->_reset;
743    ctx->_hmac.hash_final = ctx->_final;
744    ctx->_reset = (void (*)(void*))_picohash_hmac_reset;
745    ctx->_final = (void (*)(void*,void*))_picohash_hmac_final;
746
747    /* start calculating the inner hash */
748    _picohash_hmac_apply_key(ctx, 0x36);
749}
750
751#endif