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,71 @@
1
+#ifndef SHA3_H
2
+#define SHA3_H
3
+
4
+/* -------------------------------------------------------------------------
5
+ * Works when compiled for either 32-bit or 64-bit targets, optimized for 
6
+ * 64 bit.
7
+ *
8
+ * Canonical implementation of Init/Update/Finalize for SHA-3 byte input. 
9
+ *
10
+ * SHA3-256, SHA3-384, SHA-512 are implemented. SHA-224 can easily be added.
11
+ *
12
+ * Based on code from http://keccak.noekeon.org/ .
13
+ *
14
+ * I place the code that I wrote into public domain, free to use. 
15
+ *
16
+ * I would appreciate if you give credits to this work if you used it to 
17
+ * write or test * your code.
18
+ *
19
+ * Aug 2015. Andrey Jivsov. crypto@brainhub.org
20
+ * ---------------------------------------------------------------------- */
21
+
22
+/* 'Words' here refers to uint64_t */
23
+#define SHA3_KECCAK_SPONGE_WORDS \
24
+	(((1600)/8/*bits to byte*/)/sizeof(uint64_t))
25
+typedef struct sha3_context_ {
26
+    uint64_t saved;             /* the portion of the input message that we
27
+                                 * didn't consume yet */
28
+    union {                     /* Keccak's state */
29
+        uint64_t s[SHA3_KECCAK_SPONGE_WORDS];
30
+        uint8_t sb[SHA3_KECCAK_SPONGE_WORDS * 8];
31
+    };
32
+    unsigned byteIndex;         /* 0..7--the next byte after the set one
33
+                                 * (starts from 0; 0--none are buffered) */
34
+    unsigned wordIndex;         /* 0..24--the next word to integrate input
35
+                                 * (starts from 0) */
36
+    unsigned capacityWords;     /* the double size of the hash output in
37
+                                 * words (e.g. 16 for Keccak 512) */
38
+} sha3_context;
39
+
40
+enum SHA3_FLAGS {
41
+    SHA3_FLAGS_NONE=0,
42
+    SHA3_FLAGS_KECCAK=1
43
+};
44
+
45
+enum SHA3_RETURN {
46
+    SHA3_RETURN_OK=0,
47
+    SHA3_RETURN_BAD_PARAMS=1
48
+};
49
+typedef enum SHA3_RETURN sha3_return_t;
50
+
51
+/* For Init or Reset call these: */
52
+sha3_return_t sha3_Init(void *priv, unsigned bitSize);
53
+
54
+void sha3_Init256(void *priv);
55
+void sha3_Init384(void *priv);
56
+void sha3_Init512(void *priv);
57
+
58
+enum SHA3_FLAGS sha3_SetFlags(void *priv, enum SHA3_FLAGS);
59
+
60
+void sha3_Update(void *priv, void const *bufIn, size_t len);
61
+
62
+void const *sha3_Finalize(void *priv);
63
+
64
+/* Single-call hashing */
65
+sha3_return_t sha3_HashBuffer( 
66
+    unsigned bitSize,   /* 256, 384, 512 */
67
+    enum SHA3_FLAGS flags, /* SHA3_FLAGS_NONE or SHA3_FLAGS_KECCAK */
68
+    const void *in, unsigned inBytes, 
69
+    void *out, unsigned outBytes );     /* up to bitSize/8; truncation OK */
70
+
71
+#endif