Browse code

Change sha3 implementation used. Initial unsaved data API.

Dario Rodriguez authored on 02/03/2019 16:12:12
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,328 @@
1
+/* -------------------------------------------------------------------------
2
+ * Works when compiled for either 32-bit or 64-bit targets, optimized for 
3
+ * 64 bit.
4
+ *
5
+ * Canonical implementation of Init/Update/Finalize for SHA-3 byte input. 
6
+ *
7
+ * SHA3-256, SHA3-384, SHA-512 are implemented. SHA-224 can easily be added.
8
+ *
9
+ * Based on code from http://keccak.noekeon.org/ .
10
+ *
11
+ * I place the code that I wrote into public domain, free to use. 
12
+ *
13
+ * I would appreciate if you give credits to this work if you used it to 
14
+ * write or test * your code.
15
+ *
16
+ * Aug 2015. Andrey Jivsov. crypto@brainhub.org
17
+ * ---------------------------------------------------------------------- */
18
+
19
+#include <stdio.h>
20
+#include <stdint.h>
21
+#include <string.h>
22
+
23
+#include "sha3.h"
24
+
25
+#define SHA3_ASSERT( x )
26
+#if defined(_MSC_VER)
27
+#define SHA3_TRACE( format, ...)
28
+#define SHA3_TRACE_BUF( format, buf, l, ...)
29
+#else
30
+#define SHA3_TRACE(format, args...)
31
+#define SHA3_TRACE_BUF(format, buf, l, args...)
32
+#endif
33
+
34
+/* 
35
+ * This flag is used to configure "pure" Keccak, as opposed to NIST SHA3.
36
+ */
37
+#define SHA3_USE_KECCAK_FLAG 0x80000000
38
+#define SHA3_CW(x) ((x) & (~SHA3_USE_KECCAK_FLAG))
39
+
40
+
41
+#if defined(_MSC_VER)
42
+#define SHA3_CONST(x) x
43
+#else
44
+#define SHA3_CONST(x) x##L
45
+#endif
46
+
47
+#ifndef SHA3_ROTL64
48
+#define SHA3_ROTL64(x, y) \
49
+	(((x) << (y)) | ((x) >> ((sizeof(uint64_t)*8) - (y))))
50
+#endif
51
+
52
+static const uint64_t keccakf_rndc[24] = {
53
+    SHA3_CONST(0x0000000000000001UL), SHA3_CONST(0x0000000000008082UL),
54
+    SHA3_CONST(0x800000000000808aUL), SHA3_CONST(0x8000000080008000UL),
55
+    SHA3_CONST(0x000000000000808bUL), SHA3_CONST(0x0000000080000001UL),
56
+    SHA3_CONST(0x8000000080008081UL), SHA3_CONST(0x8000000000008009UL),
57
+    SHA3_CONST(0x000000000000008aUL), SHA3_CONST(0x0000000000000088UL),
58
+    SHA3_CONST(0x0000000080008009UL), SHA3_CONST(0x000000008000000aUL),
59
+    SHA3_CONST(0x000000008000808bUL), SHA3_CONST(0x800000000000008bUL),
60
+    SHA3_CONST(0x8000000000008089UL), SHA3_CONST(0x8000000000008003UL),
61
+    SHA3_CONST(0x8000000000008002UL), SHA3_CONST(0x8000000000000080UL),
62
+    SHA3_CONST(0x000000000000800aUL), SHA3_CONST(0x800000008000000aUL),
63
+    SHA3_CONST(0x8000000080008081UL), SHA3_CONST(0x8000000000008080UL),
64
+    SHA3_CONST(0x0000000080000001UL), SHA3_CONST(0x8000000080008008UL)
65
+};
66
+
67
+static const unsigned keccakf_rotc[24] = {
68
+    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62,
69
+    18, 39, 61, 20, 44
70
+};
71
+
72
+static const unsigned keccakf_piln[24] = {
73
+    10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20,
74
+    14, 22, 9, 6, 1
75
+};
76
+
77
+/* generally called after SHA3_KECCAK_SPONGE_WORDS-ctx->capacityWords words 
78
+ * are XORed into the state s 
79
+ */
80
+static void
81
+keccakf(uint64_t s[25])
82
+{
83
+    int i, j, round;
84
+    uint64_t t, bc[5];
85
+#define KECCAK_ROUNDS 24
86
+
87
+    for(round = 0; round < KECCAK_ROUNDS; round++) {
88
+
89
+        /* Theta */
90
+        for(i = 0; i < 5; i++)
91
+            bc[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20];
92
+
93
+        for(i = 0; i < 5; i++) {
94
+            t = bc[(i + 4) % 5] ^ SHA3_ROTL64(bc[(i + 1) % 5], 1);
95
+            for(j = 0; j < 25; j += 5)
96
+                s[j + i] ^= t;
97
+        }
98
+
99
+        /* Rho Pi */
100
+        t = s[1];
101
+        for(i = 0; i < 24; i++) {
102
+            j = keccakf_piln[i];
103
+            bc[0] = s[j];
104
+            s[j] = SHA3_ROTL64(t, keccakf_rotc[i]);
105
+            t = bc[0];
106
+        }
107
+
108
+        /* Chi */
109
+        for(j = 0; j < 25; j += 5) {
110
+            for(i = 0; i < 5; i++)
111
+                bc[i] = s[j + i];
112
+            for(i = 0; i < 5; i++)
113
+                s[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5];
114
+        }
115
+
116
+        /* Iota */
117
+        s[0] ^= keccakf_rndc[round];
118
+    }
119
+}
120
+
121
+/* *************************** Public Inteface ************************ */
122
+
123
+/* For Init or Reset call these: */
124
+sha3_return_t
125
+sha3_Init(void *priv, unsigned bitSize) {
126
+    sha3_context *ctx = (sha3_context *) priv;
127
+    if( bitSize != 256 && bitSize != 384 && bitSize != 512 )
128
+        return SHA3_RETURN_BAD_PARAMS;
129
+    memset(ctx, 0, sizeof(*ctx));
130
+    ctx->capacityWords = 2 * bitSize / (8 * sizeof(uint64_t));
131
+    return SHA3_RETURN_OK;
132
+}
133
+
134
+void
135
+sha3_Init256(void *priv)
136
+{
137
+    sha3_Init(priv, 256);
138
+}
139
+
140
+void
141
+sha3_Init384(void *priv)
142
+{
143
+    sha3_Init(priv, 384);
144
+}
145
+
146
+void
147
+sha3_Init512(void *priv)
148
+{
149
+    sha3_Init(priv, 512);
150
+}
151
+
152
+enum SHA3_FLAGS
153
+sha3_SetFlags(void *priv, enum SHA3_FLAGS flags)
154
+{
155
+    sha3_context *ctx = (sha3_context *) priv;
156
+    flags &= SHA3_FLAGS_KECCAK;
157
+    ctx->capacityWords |= (flags == SHA3_FLAGS_KECCAK ? SHA3_USE_KECCAK_FLAG : 0);
158
+    return flags;
159
+}
160
+
161
+
162
+void
163
+sha3_Update(void *priv, void const *bufIn, size_t len)
164
+{
165
+    sha3_context *ctx = (sha3_context *) priv;
166
+
167
+    /* 0...7 -- how much is needed to have a word */
168
+    unsigned old_tail = (8 - ctx->byteIndex) & 7;
169
+
170
+    size_t words;
171
+    unsigned tail;
172
+    size_t i;
173
+
174
+    const uint8_t *buf = bufIn;
175
+
176
+    SHA3_TRACE_BUF("called to update with:", buf, len);
177
+
178
+    SHA3_ASSERT(ctx->byteIndex < 8);
179
+    SHA3_ASSERT(ctx->wordIndex < sizeof(ctx->s) / sizeof(ctx->s[0]));
180
+
181
+    if(len < old_tail) {        /* have no complete word or haven't started 
182
+                                 * the word yet */
183
+        SHA3_TRACE("because %d<%d, store it and return", (unsigned)len,
184
+                (unsigned)old_tail);
185
+        /* endian-independent code follows: */
186
+        while (len--)
187
+            ctx->saved |= (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8);
188
+        SHA3_ASSERT(ctx->byteIndex < 8);
189
+        return;
190
+    }
191
+
192
+    if(old_tail) {              /* will have one word to process */
193
+        SHA3_TRACE("completing one word with %d bytes", (unsigned)old_tail);
194
+        /* endian-independent code follows: */
195
+        len -= old_tail;
196
+        while (old_tail--)
197
+            ctx->saved |= (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8);
198
+
199
+        /* now ready to add saved to the sponge */
200
+        ctx->s[ctx->wordIndex] ^= ctx->saved;
201
+        SHA3_ASSERT(ctx->byteIndex == 8);
202
+        ctx->byteIndex = 0;
203
+        ctx->saved = 0;
204
+        if(++ctx->wordIndex ==
205
+                (SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords))) {
206
+            keccakf(ctx->s);
207
+            ctx->wordIndex = 0;
208
+        }
209
+    }
210
+
211
+    /* now work in full words directly from input */
212
+
213
+    SHA3_ASSERT(ctx->byteIndex == 0);
214
+
215
+    words = len / sizeof(uint64_t);
216
+    tail = len - words * sizeof(uint64_t);
217
+
218
+    SHA3_TRACE("have %d full words to process", (unsigned)words);
219
+
220
+    for(i = 0; i < words; i++, buf += sizeof(uint64_t)) {
221
+        const uint64_t t = (uint64_t) (buf[0]) |
222
+                ((uint64_t) (buf[1]) << 8 * 1) |
223
+                ((uint64_t) (buf[2]) << 8 * 2) |
224
+                ((uint64_t) (buf[3]) << 8 * 3) |
225
+                ((uint64_t) (buf[4]) << 8 * 4) |
226
+                ((uint64_t) (buf[5]) << 8 * 5) |
227
+                ((uint64_t) (buf[6]) << 8 * 6) |
228
+                ((uint64_t) (buf[7]) << 8 * 7);
229
+#if defined(__x86_64__ ) || defined(__i386__)
230
+        SHA3_ASSERT(memcmp(&t, buf, 8) == 0);
231
+#endif
232
+        ctx->s[ctx->wordIndex] ^= t;
233
+        if(++ctx->wordIndex ==
234
+                (SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords))) {
235
+            keccakf(ctx->s);
236
+            ctx->wordIndex = 0;
237
+        }
238
+    }
239
+
240
+    SHA3_TRACE("have %d bytes left to process, save them", (unsigned)tail);
241
+
242
+    /* finally, save the partial word */
243
+    SHA3_ASSERT(ctx->byteIndex == 0 && tail < 8);
244
+    while (tail--) {
245
+        SHA3_TRACE("Store byte %02x '%c'", *buf, *buf);
246
+        ctx->saved |= (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8);
247
+    }
248
+    SHA3_ASSERT(ctx->byteIndex < 8);
249
+    SHA3_TRACE("Have saved=0x%016" PRIx64 " at the end", ctx->saved);
250
+}
251
+
252
+/* This is simply the 'update' with the padding block.
253
+ * The padding block is 0x01 || 0x00* || 0x80. First 0x01 and last 0x80 
254
+ * bytes are always present, but they can be the same byte.
255
+ */
256
+void const *
257
+sha3_Finalize(void *priv)
258
+{
259
+    sha3_context *ctx = (sha3_context *) priv;
260
+
261
+    SHA3_TRACE("called with %d bytes in the buffer", ctx->byteIndex);
262
+
263
+    /* Append 2-bit suffix 01, per SHA-3 spec. Instead of 1 for padding we
264
+     * use 1<<2 below. The 0x02 below corresponds to the suffix 01.
265
+     * Overall, we feed 0, then 1, and finally 1 to start padding. Without
266
+     * M || 01, we would simply use 1 to start padding. */
267
+
268
+    uint64_t t;
269
+
270
+    if( ctx->capacityWords & SHA3_USE_KECCAK_FLAG ) {
271
+        /* Keccak version */
272
+        t = (uint64_t)(((uint64_t) 1) << (ctx->byteIndex * 8));
273
+    }
274
+    else {
275
+        /* SHA3 version */
276
+        t = (uint64_t)(((uint64_t)(0x02 | (1 << 2))) << ((ctx->byteIndex) * 8));
277
+    }
278
+
279
+    ctx->s[ctx->wordIndex] ^= ctx->saved ^ t;
280
+
281
+    ctx->s[SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords) - 1] ^=
282
+            SHA3_CONST(0x8000000000000000UL);
283
+    keccakf(ctx->s);
284
+
285
+    /* Return first bytes of the ctx->s. This conversion is not needed for
286
+     * little-endian platforms e.g. wrap with #if !defined(__BYTE_ORDER__)
287
+     * || !defined(__ORDER_LITTLE_ENDIAN__) || __BYTE_ORDER__!=__ORDER_LITTLE_ENDIAN__ 
288
+     *    ... the conversion below ...
289
+     * #endif */
290
+    {
291
+        unsigned i;
292
+        for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
293
+            const unsigned t1 = (uint32_t) ctx->s[i];
294
+            const unsigned t2 = (uint32_t) ((ctx->s[i] >> 16) >> 16);
295
+            ctx->sb[i * 8 + 0] = (uint8_t) (t1);
296
+            ctx->sb[i * 8 + 1] = (uint8_t) (t1 >> 8);
297
+            ctx->sb[i * 8 + 2] = (uint8_t) (t1 >> 16);
298
+            ctx->sb[i * 8 + 3] = (uint8_t) (t1 >> 24);
299
+            ctx->sb[i * 8 + 4] = (uint8_t) (t2);
300
+            ctx->sb[i * 8 + 5] = (uint8_t) (t2 >> 8);
301
+            ctx->sb[i * 8 + 6] = (uint8_t) (t2 >> 16);
302
+            ctx->sb[i * 8 + 7] = (uint8_t) (t2 >> 24);
303
+        }
304
+    }
305
+
306
+    SHA3_TRACE_BUF("Hash: (first 32 bytes)", ctx->sb, 256 / 8);
307
+
308
+    return (ctx->sb);
309
+}
310
+
311
+sha3_return_t sha3_HashBuffer( unsigned bitSize, enum SHA3_FLAGS flags, const void *in, unsigned inBytes, void *out, unsigned outBytes ) {
312
+    sha3_return_t err;
313
+    sha3_context c;
314
+
315
+    err = sha3_Init(&c, bitSize);
316
+    if( err != SHA3_RETURN_OK )
317
+        return err;
318
+    if( sha3_SetFlags(&c, flags) != flags ) {
319
+        return SHA3_RETURN_BAD_PARAMS;
320
+    }
321
+    sha3_Update(&c, in, inBytes);
322
+    const void *h = sha3_Finalize(&c);
323
+
324
+    if(outBytes > bitSize/8)
325
+        outBytes = bitSize/8;
326
+    memcpy(out, h, outBytes);
327
+    return SHA3_RETURN_OK;
328
+}