|
| 1 | +/* |
| 2 | +The eXtended Keccak Code Package (XKCP) |
| 3 | +https://github.com/XKCP/XKCP |
| 4 | +
|
| 5 | +Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche. |
| 6 | +
|
| 7 | +Implementation by the designers, hereby denoted as "the implementer". |
| 8 | +
|
| 9 | +For more information, feedback or questions, please refer to the Keccak Team website: |
| 10 | +https://keccak.team/ |
| 11 | +
|
| 12 | +To the extent possible under law, the implementer has waived all copyright |
| 13 | +and related or neighboring rights to the source code in this file. |
| 14 | +http://creativecommons.org/publicdomain/zero/1.0/ |
| 15 | +*/ |
| 16 | + |
| 17 | +#ifndef _KeccakHashInterface_h_ |
| 18 | +#define _KeccakHashInterface_h_ |
| 19 | + |
| 20 | +#include "config.h" |
| 21 | +#ifdef XKCP_has_KeccakP1600 |
| 22 | + |
| 23 | +#include <stdint.h> |
| 24 | +#include <string.h> |
| 25 | +#include "KeccakSponge.h" |
| 26 | + |
| 27 | +#ifndef _Keccak_BitTypes_ |
| 28 | +#define _Keccak_BitTypes_ |
| 29 | +typedef uint8_t BitSequence; |
| 30 | + |
| 31 | +typedef size_t BitLength; |
| 32 | +#endif |
| 33 | + |
| 34 | +typedef enum { KECCAK_SUCCESS = 0, KECCAK_FAIL = 1, KECCAK_BAD_HASHLEN = 2 } HashReturn; |
| 35 | + |
| 36 | +typedef struct { |
| 37 | + KeccakWidth1600_SpongeInstance sponge; |
| 38 | + unsigned int fixedOutputLength; |
| 39 | + unsigned char delimitedSuffix; |
| 40 | +} Keccak_HashInstance; |
| 41 | + |
| 42 | +/** |
| 43 | + * Function to initialize the Keccak[r, c] sponge function instance used in sequential hashing mode. |
| 44 | + * @param hashInstance Pointer to the hash instance to be initialized. |
| 45 | + * @param rate The value of the rate r. |
| 46 | + * @param capacity The value of the capacity c. |
| 47 | + * @param hashbitlen The desired number of output bits, |
| 48 | + * or 0 for an arbitrarily-long output. |
| 49 | + * @param delimitedSuffix Bits that will be automatically appended to the end |
| 50 | + * of the input message, as in domain separation. |
| 51 | + * This is a byte containing from 0 to 7 bits |
| 52 | + * formatted like the @a delimitedData parameter of |
| 53 | + * the Keccak_SpongeAbsorbLastFewBits() function. |
| 54 | + * @pre One must have r+c=1600 and the rate a multiple of 8 bits in this implementation. |
| 55 | + * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise. |
| 56 | + */ |
| 57 | +HashReturn Keccak_HashInitialize(Keccak_HashInstance *hashInstance, unsigned int rate, unsigned int capacity, unsigned int hashbitlen, unsigned char delimitedSuffix); |
| 58 | + |
| 59 | +/** Macro to initialize a SHAKE128 instance as specified in the FIPS 202 standard. |
| 60 | + */ |
| 61 | +#define Keccak_HashInitialize_SHAKE128(hashInstance) Keccak_HashInitialize(hashInstance, 1344, 256, 0, 0x1F) |
| 62 | + |
| 63 | +/** Macro to initialize a SHAKE256 instance as specified in the FIPS 202 standard. |
| 64 | + */ |
| 65 | +#define Keccak_HashInitialize_SHAKE256(hashInstance) Keccak_HashInitialize(hashInstance, 1088, 512, 0, 0x1F) |
| 66 | + |
| 67 | +/** Macro to initialize a SHA3-224 instance as specified in the FIPS 202 standard. |
| 68 | + */ |
| 69 | +#define Keccak_HashInitialize_SHA3_224(hashInstance) Keccak_HashInitialize(hashInstance, 1152, 448, 224, 0x06) |
| 70 | + |
| 71 | +/** Macro to initialize a SHA3-256 instance as specified in the FIPS 202 standard. |
| 72 | + */ |
| 73 | +#define Keccak_HashInitialize_SHA3_256(hashInstance) Keccak_HashInitialize(hashInstance, 1088, 512, 256, 0x06) |
| 74 | + |
| 75 | +/** Macro to initialize a SHA3-384 instance as specified in the FIPS 202 standard. |
| 76 | + */ |
| 77 | +#define Keccak_HashInitialize_SHA3_384(hashInstance) Keccak_HashInitialize(hashInstance, 832, 768, 384, 0x06) |
| 78 | + |
| 79 | +/** Macro to initialize a SHA3-512 instance as specified in the FIPS 202 standard. |
| 80 | + */ |
| 81 | +#define Keccak_HashInitialize_SHA3_512(hashInstance) Keccak_HashInitialize(hashInstance, 576, 1024, 512, 0x06) |
| 82 | + |
| 83 | +/** |
| 84 | + * Function to give input data to be absorbed. |
| 85 | + * @param hashInstance Pointer to the hash instance initialized by Keccak_HashInitialize(). |
| 86 | + * @param data Pointer to the input data. |
| 87 | + * When @a databitLen is not a multiple of 8, the last bits of data must be |
| 88 | + * in the least significant bits of the last byte (little-endian convention). |
| 89 | + * In this case, the (8 - @a databitLen mod 8) most significant bits |
| 90 | + * of the last byte are ignored. |
| 91 | + * @param databitLen The number of input bits provided in the input data. |
| 92 | + * @pre In the previous call to Keccak_HashUpdate(), databitlen was a multiple of 8. |
| 93 | + * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise. |
| 94 | + */ |
| 95 | +HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequence *data, BitLength databitlen); |
| 96 | + |
| 97 | +/** |
| 98 | + * Function to call after all input blocks have been input and to get |
| 99 | + * output bits if the length was specified when calling Keccak_HashInitialize(). |
| 100 | + * @param hashInstance Pointer to the hash instance initialized by Keccak_HashInitialize(). |
| 101 | + * If @a hashbitlen was not 0 in the call to Keccak_HashInitialize(), the number of |
| 102 | + * output bits is equal to @a hashbitlen. |
| 103 | + * If @a hashbitlen was 0 in the call to Keccak_HashInitialize(), the output bits |
| 104 | + * must be extracted using the Keccak_HashSqueeze() function. |
| 105 | + * @param hashval Pointer to the buffer where to store the output data. |
| 106 | + * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise. |
| 107 | + */ |
| 108 | +HashReturn Keccak_HashFinal(Keccak_HashInstance *hashInstance, BitSequence *hashval); |
| 109 | + |
| 110 | + /** |
| 111 | + * Function to squeeze output data. |
| 112 | + * @param hashInstance Pointer to the hash instance initialized by Keccak_HashInitialize(). |
| 113 | + * @param data Pointer to the buffer where to store the output data. |
| 114 | + * @param databitlen The number of output bits desired (must be a multiple of 8). |
| 115 | + * @pre Keccak_HashFinal() must have been already called. |
| 116 | + * @pre @a databitlen is a multiple of 8. |
| 117 | + * @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise. |
| 118 | + */ |
| 119 | +HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data, BitLength databitlen); |
| 120 | + |
| 121 | +#else |
| 122 | +#error This requires an implementation of Keccak-p[1600] |
| 123 | +#endif |
| 124 | + |
| 125 | +#endif |
0 commit comments