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