Coverage Report

Created: 2026-01-22 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/3rdparty/sha3/KeccakSponge.c
Line
Count
Source
1
/*
2
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
3
Michaƫl Peeters and Gilles Van Assche. For more information, feedback or
4
questions, please refer to our website: http://keccak.noekeon.org/
5
6
Implementation by the designers,
7
hereby denoted as "the implementer".
8
9
To the extent possible under law, the implementer has waived all copyright
10
and related or neighboring rights to the source code in this file.
11
http://creativecommons.org/publicdomain/zero/1.0/
12
*/
13
14
#include <string.h>
15
#include "KeccakSponge.h"
16
#include "KeccakF-1600-interface.h"
17
#ifdef KeccakReference
18
#include "displayIntermediateValues.h"
19
#endif
20
21
static int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
22
0
{
23
0
    if (rate+capacity != 1600)
24
0
        return 1;
25
0
    if ((rate <= 0) || (rate >= 1600) || ((rate % 64) != 0))
26
0
        return 1;
27
0
    KeccakInitialize();
28
0
    state->rate = rate;
29
0
    state->capacity = capacity;
30
0
    state->fixedOutputLength = 0;
31
0
    KeccakInitializeState(state->state);
32
0
    memset(state->dataQueue, 0, KeccakMaximumRateInBytes);
33
0
    state->bitsInQueue = 0;
34
0
    state->squeezing = 0;
35
0
    state->bitsAvailableForSqueezing = 0;
36
37
0
    return 0;
38
0
}
39
40
static void AbsorbQueue(spongeState *state)
41
0
{
42
    // state->bitsInQueue is assumed to be equal to state->rate
43
    #ifdef KeccakReference
44
    displayBytes(1, "Block to be absorbed", state->dataQueue, state->rate/8);
45
    #endif
46
0
#ifdef ProvideFast576
47
0
    if (state->rate == 576)
48
0
        KeccakAbsorb576bits(state->state, state->dataQueue);
49
0
    else 
50
0
#endif
51
0
#ifdef ProvideFast832
52
0
    if (state->rate == 832)
53
0
        KeccakAbsorb832bits(state->state, state->dataQueue);
54
0
    else 
55
0
#endif
56
0
#ifdef ProvideFast1024
57
0
    if (state->rate == 1024)
58
0
        KeccakAbsorb1024bits(state->state, state->dataQueue);
59
0
    else 
60
0
#endif
61
0
#ifdef ProvideFast1088
62
0
    if (state->rate == 1088)
63
0
        KeccakAbsorb1088bits(state->state, state->dataQueue);
64
0
    else
65
0
#endif
66
0
#ifdef ProvideFast1152
67
0
    if (state->rate == 1152)
68
0
        KeccakAbsorb1152bits(state->state, state->dataQueue);
69
0
    else 
70
0
#endif
71
0
#ifdef ProvideFast1344
72
0
    if (state->rate == 1344)
73
0
        KeccakAbsorb1344bits(state->state, state->dataQueue);
74
0
    else 
75
0
#endif
76
0
        KeccakAbsorb(state->state, state->dataQueue, state->rate/64);
77
0
    state->bitsInQueue = 0;
78
0
}
79
80
static int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen)
81
0
{
82
0
    unsigned long long i, j, wholeBlocks;
83
0
    unsigned int partialBlock, partialByte;
84
0
    const unsigned char *curData;
85
86
0
    if ((state->bitsInQueue % 8) != 0)
87
0
        return 1; // Only the last call may contain a partial byte
88
0
    if (state->squeezing)
89
0
        return 1; // Too late for additional input
90
91
0
    i = 0;
92
0
    while(i < databitlen) {
93
0
        if ((state->bitsInQueue == 0) && (databitlen >= state->rate) && (i <= (databitlen-state->rate))) {
94
0
            wholeBlocks = (databitlen-i)/state->rate;
95
0
            curData = data+i/8;
96
0
#ifdef ProvideFast576
97
0
            if (state->rate == 576) {
98
0
                for(j=0; j<wholeBlocks; j++, curData+=576/8) {
99
                    #ifdef KeccakReference
100
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
101
                    #endif
102
0
                    KeccakAbsorb576bits(state->state, curData);
103
0
                }
104
0
            }
105
0
            else
106
0
#endif
107
0
#ifdef ProvideFast832
108
0
            if (state->rate == 832) {
109
0
                for(j=0; j<wholeBlocks; j++, curData+=832/8) {
110
                    #ifdef KeccakReference
111
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
112
                    #endif
113
0
                    KeccakAbsorb832bits(state->state, curData);
114
0
                }
115
0
            }
116
0
            else
117
0
#endif
118
0
#ifdef ProvideFast1024
119
0
            if (state->rate == 1024) {
120
0
                for(j=0; j<wholeBlocks; j++, curData+=1024/8) {
121
                    #ifdef KeccakReference
122
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
123
                    #endif
124
0
                    KeccakAbsorb1024bits(state->state, curData);
125
0
                }
126
0
            }
127
0
            else
128
0
#endif
129
0
#ifdef ProvideFast1088
130
0
            if (state->rate == 1088) {
131
0
                for(j=0; j<wholeBlocks; j++, curData+=1088/8) {
132
                    #ifdef KeccakReference
133
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
134
                    #endif
135
0
                    KeccakAbsorb1088bits(state->state, curData);
136
0
                }
137
0
            }
138
0
            else
139
0
#endif
140
0
#ifdef ProvideFast1152
141
0
            if (state->rate == 1152) {
142
0
                for(j=0; j<wholeBlocks; j++, curData+=1152/8) {
143
                    #ifdef KeccakReference
144
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
145
                    #endif
146
0
                    KeccakAbsorb1152bits(state->state, curData);
147
0
                }
148
0
            }
149
0
            else
150
0
#endif
151
0
#ifdef ProvideFast1344
152
0
            if (state->rate == 1344) {
153
0
                for(j=0; j<wholeBlocks; j++, curData+=1344/8) {
154
                    #ifdef KeccakReference
155
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
156
                    #endif
157
0
                    KeccakAbsorb1344bits(state->state, curData);
158
0
                }
159
0
            }
160
0
            else
161
0
#endif
162
0
            {
163
0
                for(j=0; j<wholeBlocks; j++, curData+=state->rate/8) {
164
                    #ifdef KeccakReference
165
                    displayBytes(1, "Block to be absorbed", curData, state->rate/8);
166
                    #endif
167
0
                    KeccakAbsorb(state->state, curData, state->rate/64);
168
0
                }
169
0
            }
170
0
            i += wholeBlocks*state->rate;
171
0
        }
172
0
        else {
173
0
            if (databitlen-i > state->rate - state->bitsInQueue)
174
0
                partialBlock = state->rate-state->bitsInQueue;
175
0
            else
176
0
                partialBlock = (unsigned int)(databitlen - i);
177
0
            partialByte = partialBlock % 8;
178
0
            partialBlock -= partialByte;
179
0
            memcpy(state->dataQueue+state->bitsInQueue/8, data+i/8, partialBlock/8);
180
0
            state->bitsInQueue += partialBlock;
181
0
            i += partialBlock;
182
0
            if (state->bitsInQueue == state->rate)
183
0
                AbsorbQueue(state);
184
0
            if (partialByte > 0) {
185
0
                unsigned char mask = (1 << partialByte)-1;
186
0
                state->dataQueue[state->bitsInQueue/8] = data[i/8] & mask;
187
0
                state->bitsInQueue += partialByte;
188
0
                i += partialByte;
189
0
            }
190
0
        }
191
0
    }
192
0
    return 0;
193
0
}
194
195
static void PadAndSwitchToSqueezingPhase(spongeState *state)
196
0
{
197
    // Note: the bits are numbered from 0=LSB to 7=MSB
198
0
    if (state->bitsInQueue + 1 == state->rate) {
199
0
        state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
200
0
        AbsorbQueue(state);
201
0
        memset(state->dataQueue, 0, state->rate/8);
202
0
    }
203
0
    else {
204
0
        memset(state->dataQueue + (state->bitsInQueue+7)/8, 0, state->rate/8 - (state->bitsInQueue+7)/8);
205
0
        state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
206
0
    }
207
0
    state->dataQueue[(state->rate-1)/8] |= 1 << ((state->rate-1) % 8);
208
0
    AbsorbQueue(state);
209
210
    #ifdef KeccakReference
211
    displayText(1, "--- Switching to squeezing phase ---");
212
    #endif
213
0
#ifdef ProvideFast1024
214
0
    if (state->rate == 1024) {
215
0
        KeccakExtract1024bits(state->state, state->dataQueue);
216
0
        state->bitsAvailableForSqueezing = 1024;
217
0
    }
218
0
    else
219
0
#endif
220
0
    {
221
0
        KeccakExtract(state->state, state->dataQueue, state->rate/64);
222
0
        state->bitsAvailableForSqueezing = state->rate;
223
0
    }
224
    #ifdef KeccakReference
225
    displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
226
    #endif
227
0
    state->squeezing = 1;
228
0
}
229
230
static int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength)
231
0
{
232
0
    unsigned long long i;
233
0
    unsigned int partialBlock;
234
235
0
    if (!state->squeezing)
236
0
        PadAndSwitchToSqueezingPhase(state);
237
0
    if ((outputLength % 8) != 0)
238
0
        return 1; // Only multiple of 8 bits are allowed, truncation can be done at user level
239
240
0
    i = 0;
241
0
    while(i < outputLength) {
242
0
        if (state->bitsAvailableForSqueezing == 0) {
243
0
            KeccakPermutation(state->state);
244
0
#ifdef ProvideFast1024
245
0
            if (state->rate == 1024) {
246
0
                KeccakExtract1024bits(state->state, state->dataQueue);
247
0
                state->bitsAvailableForSqueezing = 1024;
248
0
            }
249
0
            else
250
0
#endif
251
0
            {
252
0
                KeccakExtract(state->state, state->dataQueue, state->rate/64);
253
0
                state->bitsAvailableForSqueezing = state->rate;
254
0
            }
255
            #ifdef KeccakReference
256
            displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
257
            #endif
258
0
        }
259
0
        partialBlock = state->bitsAvailableForSqueezing;
260
0
        if ((unsigned long long)partialBlock > outputLength - i)
261
0
            partialBlock = (unsigned int)(outputLength - i);
262
0
        memcpy(output+i/8, state->dataQueue+(state->rate-state->bitsAvailableForSqueezing)/8, partialBlock/8);
263
0
        state->bitsAvailableForSqueezing -= partialBlock;
264
0
        i += partialBlock;
265
0
    }
266
0
    return 0;
267
0
}