Coverage Report

Created: 2025-06-13 06:43

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