Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/keccak.h
Line
Count
Source (jump to first uncovered line)
1
// keccak.h - originally written and placed in the public domain by Wei Dai
2
3
/// \file keccak.h
4
/// \brief Classes for Keccak message digests
5
/// \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
6
///   FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
7
/// \details Keccak will likely change in the future to accommodate extensibility of the
8
///   round function and the XOF functions.
9
/// \sa <a href="http://en.wikipedia.org/wiki/Keccak">Keccak</a>
10
/// \since Crypto++ 5.6.4
11
12
#ifndef CRYPTOPP_KECCAK_H
13
#define CRYPTOPP_KECCAK_H
14
15
#include "cryptlib.h"
16
#include "secblock.h"
17
18
NAMESPACE_BEGIN(CryptoPP)
19
20
/// \brief Keccak message digest base class
21
/// \details The Crypto++ Keccak implementation uses F1600 with XOF d=0x01.
22
///   FIPS 202 conformance (XOF d=0x06) is available in SHA3 classes.
23
/// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
24
///   Library users should instantiate a derived class, and only use Keccak
25
///   as a base class reference or pointer.
26
/// \details Keccak will likely change in the future to accommodate extensibility of the
27
///   round function and the XOF functions.
28
/// \details Perform the following to specify a different digest size. The class will use F1600,
29
///   XOF d=0x01, and a new value for <tt>r()</tt> (which will be <tt>200-2*24 = 152</tt>).
30
///   <pre>  Keccack_192 : public Keccack
31
///   {
32
///     public:
33
///       CRYPTOPP_CONSTANT(DIGESTSIZE = 24);
34
///       Keccack_192() : Keccack(DIGESTSIZE) {}
35
///   };
36
///   </pre>
37
///
38
/// \sa SHA3, Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
39
/// \since Crypto++ 5.6.4
40
class Keccak : public HashTransformation
41
{
42
protected:
43
    /// \brief Construct a Keccak
44
    /// \param digestSize the digest size, in bytes
45
    /// \details Keccak is the base class for Keccak_224, Keccak_256, Keccak_384 and Keccak_512.
46
    ///   Library users should instantiate a derived class, and only use Keccak
47
    ///   as a base class reference or pointer.
48
    /// \details This constructor was moved to protected at Crypto++ 8.1
49
    ///   because users were attempting to create Keccak objects with it.
50
    /// \since Crypto++ 5.6.4
51
867
    Keccak(unsigned int digestSize) : m_digestSize(digestSize) {Restart();}
52
53
public:
54
136k
    unsigned int DigestSize() const {return m_digestSize;}
55
0
    unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word64>();}
56
57
    void Update(const byte *input, size_t length);
58
    void Restart();
59
    void TruncatedFinal(byte *hash, size_t size);
60
61
protected:
62
1.07M
    inline unsigned int r() const {return BlockSize();}
63
64
    FixedSizeSecBlock<word64, 25> m_state;
65
    unsigned int m_digestSize, m_counter;
66
};
67
68
/// \brief Keccak message digest template
69
/// \tparam T_DigestSize the size of the digest, in bytes
70
/// \since Crypto++ 6.0
71
template<unsigned int T_DigestSize>
72
class Keccak_Final : public Keccak
73
{
74
public:
75
    CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize);
76
    CRYPTOPP_CONSTANT(BLOCKSIZE = 200 - 2 * DIGESTSIZE);
77
    static std::string StaticAlgorithmName()
78
12
        { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
CryptoPP::Keccak_Final<28u>::StaticAlgorithmName()
Line
Count
Source
78
4
        { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
CryptoPP::Keccak_Final<32u>::StaticAlgorithmName()
Line
Count
Source
78
5
        { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
CryptoPP::Keccak_Final<48u>::StaticAlgorithmName()
Line
Count
Source
78
2
        { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
CryptoPP::Keccak_Final<64u>::StaticAlgorithmName()
Line
Count
Source
78
1
        { return "Keccak-" + IntToString(DIGESTSIZE * 8); }
79
80
    /// \brief Construct a Keccak-X message digest
81
867
    Keccak_Final() : Keccak(DIGESTSIZE) {}
CryptoPP::Keccak_Final<28u>::Keccak_Final()
Line
Count
Source
81
161
    Keccak_Final() : Keccak(DIGESTSIZE) {}
CryptoPP::Keccak_Final<32u>::Keccak_Final()
Line
Count
Source
81
231
    Keccak_Final() : Keccak(DIGESTSIZE) {}
CryptoPP::Keccak_Final<48u>::Keccak_Final()
Line
Count
Source
81
186
    Keccak_Final() : Keccak(DIGESTSIZE) {}
CryptoPP::Keccak_Final<64u>::Keccak_Final()
Line
Count
Source
81
289
    Keccak_Final() : Keccak(DIGESTSIZE) {}
82
83
    /// \brief Provides the block size of the compression function
84
    /// \return block size of the compression function, in bytes
85
    /// \details BlockSize() will return 0 if the hash is not block based
86
    ///   or does not have an equivalent block size. For example, Keccak
87
    ///   and SHA-3 do not have a block size, but they do have an equivalent
88
    ///   block size called rate expressed as <tt>r</tt>.
89
1.23M
    unsigned int BlockSize() const { return BLOCKSIZE; }
CryptoPP::Keccak_Final<28u>::BlockSize() const
Line
Count
Source
89
203k
    unsigned int BlockSize() const { return BLOCKSIZE; }
CryptoPP::Keccak_Final<32u>::BlockSize() const
Line
Count
Source
89
279k
    unsigned int BlockSize() const { return BLOCKSIZE; }
CryptoPP::Keccak_Final<48u>::BlockSize() const
Line
Count
Source
89
284k
    unsigned int BlockSize() const { return BLOCKSIZE; }
CryptoPP::Keccak_Final<64u>::BlockSize() const
Line
Count
Source
89
471k
    unsigned int BlockSize() const { return BLOCKSIZE; }
90
91
0
    std::string AlgorithmName() const { return StaticAlgorithmName(); }
Unexecuted instantiation: CryptoPP::Keccak_Final<28u>::AlgorithmName() const
Unexecuted instantiation: CryptoPP::Keccak_Final<32u>::AlgorithmName() const
Unexecuted instantiation: CryptoPP::Keccak_Final<48u>::AlgorithmName() const
Unexecuted instantiation: CryptoPP::Keccak_Final<64u>::AlgorithmName() const
92
93
private:
94
#if !defined(__BORLANDC__)
95
    // ensure there was no underflow in the math
96
    CRYPTOPP_COMPILE_ASSERT(BLOCKSIZE < 200);
97
#endif
98
};
99
100
/// \brief Keccak-224 message digest
101
/// \since Crypto++ 5.6.4
102
DOCUMENTED_TYPEDEF(Keccak_Final<28>, Keccak_224);
103
104
/// \brief Keccak-256 message digest
105
/// \since Crypto++ 5.6.4
106
DOCUMENTED_TYPEDEF(Keccak_Final<32>, Keccak_256);
107
108
/// \brief Keccak-384 message digest
109
/// \since Crypto++ 5.6.4
110
DOCUMENTED_TYPEDEF(Keccak_Final<48>, Keccak_384);
111
112
/// \brief Keccak-512 message digest
113
/// \since Crypto++ 5.6.4
114
DOCUMENTED_TYPEDEF(Keccak_Final<64>, Keccak_512);
115
116
NAMESPACE_END
117
118
#endif