»Core Development>Code coverage>Modules/_sha3/kcp/KeccakHash.c

Python code coverage for Modules/_sha3/kcp/KeccakHash.c

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