Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/keccak.cpp
Line
Count
Source
1
// keccak.cpp - modified by Wei Dai from Ronny Van Keer's public domain
2
//              sha3-simple.c. All modifications here are placed in the
3
//              public domain by Wei Dai.
4
//              Keccack core function moved to keccakc.cpp in AUG 2018
5
//              by Jeffrey Walton. Separating the core file allows both
6
//              SHA3 and Keccack to share the core implementation.
7
8
/*
9
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
10
Michael Peeters and Gilles Van Assche. For more information, feedback or
11
questions, please refer to our website: http://keccak.noekeon.org/
12
13
Implementation by Ronny Van Keer, hereby denoted as "the implementer".
14
15
To the extent possible under law, the implementer has waived all copyright
16
and related or neighboring rights to the source code in this file.
17
http://creativecommons.org/publicdomain/zero/1.0/
18
*/
19
20
#include "pch.h"
21
#include "keccak.h"
22
23
NAMESPACE_BEGIN(CryptoPP)
24
25
// The Keccak core function
26
extern void KeccakF1600(word64 *state);
27
28
void Keccak::Update(const byte *input, size_t length)
29
192k
{
30
192k
    CRYPTOPP_ASSERT(!(input == NULLPTR && length != 0));
31
192k
    if (length == 0) { return; }
32
33
127k
    size_t spaceLeft;
34
1.02M
    while (length >= (spaceLeft = r() - m_counter))
35
900k
    {
36
900k
        if (spaceLeft)
37
900k
            xorbuf(m_state.BytePtr() + m_counter, input, spaceLeft);
38
900k
        KeccakF1600(m_state);
39
900k
        input += spaceLeft;
40
900k
        length -= spaceLeft;
41
900k
        m_counter = 0;
42
900k
    }
43
44
127k
    if (length)
45
83.7k
        xorbuf(m_state.BytePtr() + m_counter, input, length);
46
127k
    m_counter += (unsigned int)length;
47
127k
}
48
49
void Keccak::Restart()
50
48.1k
{
51
48.1k
    std::memset(m_state, 0, m_state.SizeInBytes());
52
48.1k
    m_counter = 0;
53
48.1k
}
54
55
void Keccak::TruncatedFinal(byte *hash, size_t size)
56
47.3k
{
57
47.3k
    CRYPTOPP_ASSERT(hash != NULLPTR);
58
47.3k
    ThrowIfInvalidTruncatedSize(size);
59
60
47.3k
    m_state.BytePtr()[m_counter] ^= 0x01;
61
47.3k
    m_state.BytePtr()[r()-1] ^= 0x80;
62
47.3k
    KeccakF1600(m_state);
63
47.3k
    std::memcpy(hash, m_state, size);
64
47.3k
    Restart();
65
47.3k
}
66
67
NAMESPACE_END