1 | n/a | /* |
---|
2 | n/a | Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, |
---|
3 | n/a | Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby |
---|
4 | n/a | denoted as "the implementer". |
---|
5 | n/a | |
---|
6 | n/a | For more information, feedback or questions, please refer to our websites: |
---|
7 | n/a | http://keccak.noekeon.org/ |
---|
8 | n/a | http://keyak.noekeon.org/ |
---|
9 | n/a | http://ketje.noekeon.org/ |
---|
10 | n/a | |
---|
11 | n/a | To the extent possible under law, the implementer has waived all copyright |
---|
12 | n/a | and related or neighboring rights to the source code in this file. |
---|
13 | n/a | http://creativecommons.org/publicdomain/zero/1.0/ |
---|
14 | n/a | */ |
---|
15 | n/a | |
---|
16 | n/a | #include <string.h> |
---|
17 | n/a | #include "KeccakHash.h" |
---|
18 | n/a | |
---|
19 | n/a | /* ---------------------------------------------------------------- */ |
---|
20 | n/a | |
---|
21 | n/a | HashReturn Keccak_HashInitialize(Keccak_HashInstance *instance, unsigned int rate, unsigned int capacity, unsigned int hashbitlen, unsigned char delimitedSuffix) |
---|
22 | n/a | { |
---|
23 | n/a | HashReturn result; |
---|
24 | n/a | |
---|
25 | n/a | if (delimitedSuffix == 0) |
---|
26 | n/a | return FAIL; |
---|
27 | n/a | result = (HashReturn)KeccakWidth1600_SpongeInitialize(&instance->sponge, rate, capacity); |
---|
28 | n/a | if (result != SUCCESS) |
---|
29 | n/a | return result; |
---|
30 | n/a | instance->fixedOutputLength = hashbitlen; |
---|
31 | n/a | instance->delimitedSuffix = delimitedSuffix; |
---|
32 | n/a | return SUCCESS; |
---|
33 | n/a | } |
---|
34 | n/a | |
---|
35 | n/a | /* ---------------------------------------------------------------- */ |
---|
36 | n/a | |
---|
37 | n/a | HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *data, DataLength databitlen) |
---|
38 | n/a | { |
---|
39 | n/a | if ((databitlen % 8) == 0) |
---|
40 | n/a | return (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8); |
---|
41 | n/a | else { |
---|
42 | n/a | HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8); |
---|
43 | n/a | if (ret == SUCCESS) { |
---|
44 | n/a | /* The last partial byte is assumed to be aligned on the least significant bits */ |
---|
45 | n/a | |
---|
46 | n/a | unsigned char lastByte = data[databitlen/8]; |
---|
47 | n/a | /* Concatenate the last few bits provided here with those of the suffix */ |
---|
48 | n/a | |
---|
49 | n/a | unsigned short delimitedLastBytes = (unsigned short)((unsigned short)lastByte | ((unsigned short)instance->delimitedSuffix << (databitlen % 8))); |
---|
50 | n/a | if ((delimitedLastBytes & 0xFF00) == 0x0000) { |
---|
51 | n/a | instance->delimitedSuffix = delimitedLastBytes & 0xFF; |
---|
52 | n/a | } |
---|
53 | n/a | else { |
---|
54 | n/a | unsigned char oneByte[1]; |
---|
55 | n/a | oneByte[0] = delimitedLastBytes & 0xFF; |
---|
56 | n/a | ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, oneByte, 1); |
---|
57 | n/a | instance->delimitedSuffix = (delimitedLastBytes >> 8) & 0xFF; |
---|
58 | n/a | } |
---|
59 | n/a | } |
---|
60 | n/a | return ret; |
---|
61 | n/a | } |
---|
62 | n/a | } |
---|
63 | n/a | |
---|
64 | n/a | /* ---------------------------------------------------------------- */ |
---|
65 | n/a | |
---|
66 | n/a | HashReturn Keccak_HashFinal(Keccak_HashInstance *instance, BitSequence *hashval) |
---|
67 | n/a | { |
---|
68 | n/a | HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorbLastFewBits(&instance->sponge, instance->delimitedSuffix); |
---|
69 | n/a | if (ret == SUCCESS) |
---|
70 | n/a | return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, hashval, instance->fixedOutputLength/8); |
---|
71 | n/a | else |
---|
72 | n/a | return ret; |
---|
73 | n/a | } |
---|
74 | n/a | |
---|
75 | n/a | /* ---------------------------------------------------------------- */ |
---|
76 | n/a | |
---|
77 | n/a | HashReturn Keccak_HashSqueeze(Keccak_HashInstance *instance, BitSequence *data, DataLength databitlen) |
---|
78 | n/a | { |
---|
79 | n/a | if ((databitlen % 8) != 0) |
---|
80 | n/a | return FAIL; |
---|
81 | n/a | return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, data, databitlen/8); |
---|
82 | n/a | } |
---|