lsh.h
Go to the documentation of this file.
// lsh.h - written and placed in the public domain by Jeffrey Walton
// Based on the specification and source code provided by
// Korea Internet & Security Agency (KISA) website. Also
// see https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do
// and https://seed.kisa.or.kr/kisa/Board/22/detailView.do.
// We are hitting some sort of GCC bug in the LSH AVX2 code path.
// Clang is OK on the AVX2 code path. We believe it is GCC Issue
// 82735, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82735. It
// makes using zeroupper a little tricky.
/// \file lsh.h
/// \brief Classes for the LSH hash functions
/// \since Crypto++ 8.6
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
#ifndef CRYPTOPP_LSH_H
#define CRYPTOPP_LSH_H
#include "cryptlib.h"
#include "secblock.h"
// Enable SSE2 and AVX2 for 64-bit machines.
// 32-bit machines slow down with SSE2.
#if (CRYPTOPP_BOOL_X32) || (CRYPTOPP_BOOL_X64)
# define CRYPTOPP_ENABLE_64BIT_SSE 1
#endif
NAMESPACE_BEGIN(CryptoPP)
/// \brief LSH-224 and LSH-256 hash base class
/// \details LSH256_Base is the base class for both LSH-224 and LSH-256
/// \since Crypto++ 8.6
class LSH256_Base : public HashTransformation
{
public:
/// \brief Block size, in bytes
/// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
CRYPTOPP_CONSTANT(BLOCKSIZE = 128);
virtual ~LSH256_Base() {}
unsigned int BlockSize() const { return BLOCKSIZE; }
unsigned int DigestSize() const { return m_digestSize; }
unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word32>(); }
void Restart();
void Update(const byte *input, size_t size);
void TruncatedFinal(byte *hash, size_t size);
std::string AlgorithmProvider() const;
protected:
LSH256_Base(unsigned int algType, unsigned int digestSize)
: m_digestSize(digestSize) { m_state[80] = algType; }
protected:
// Working state is:
// * cv_l = 8 32-bit words
// * cv_r = 8 32-bit words
// * submsg_e_l = 8 32-bit words
// * submsg_e_r = 8 32-bit words
// * submsg_o_l = 8 32-bit words
// * submsg_o_r = 8 32-bit words
// * last_block = 32 32-bit words (128 bytes)
// * algType
// * remainingBitLength
FixedSizeSecBlock<word32, 80+2> m_state;
// word32 m_algType, m_remainingBitLength;
word32 m_digestSize;
};
/// \brief LSH-224 hash function
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
/// \since Crypto++ 8.6
class LSH224 : public LSH256_Base
{
public:
/// \brief Digest size, in bytes
/// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 28
CRYPTOPP_CONSTANT(DIGESTSIZE = 28);
/// \brief Block size, in bytes
/// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
/// \brief The algorithm's name
/// \return the standard algorithm name
/// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
/// Some algorithms do not have standard names yet. For example, there is no standard
/// algorithm name for Shoup's ECIES.
/// \note StaticAlgorithmName is not universally implemented yet.
static std::string StaticAlgorithmName() { return "LSH-224"; }
/// \brief Construct a LSH-224
/// \details LSH_TYPE_224 is the magic value 0x000001C defined in lsh.cpp.
LSH224() : LSH256_Base(0x000001C, DIGESTSIZE) { Restart(); }
std::string AlgorithmName() const { return StaticAlgorithmName(); }
};
/// \brief LSH-256 hash function
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
/// \since Crypto++ 8.6
class LSH256 : public LSH256_Base
{
public:
/// \brief Digest size, in bytes
/// \details LSH_256 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32
CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
/// \brief Block size, in bytes
/// \details LSH_256 uses LSH256_MSG_BLK_BYTE_LEN for block size, which is 128
CRYPTOPP_CONSTANT(BLOCKSIZE = LSH256_Base::BLOCKSIZE);
/// \brief The algorithm's name
/// \return the standard algorithm name
/// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
/// Some algorithms do not have standard names yet. For example, there is no standard
/// algorithm name for Shoup's ECIES.
/// \note StaticAlgorithmName is not universally implemented yet.
static std::string StaticAlgorithmName() { return "LSH-256"; }
/// \brief Construct a LSH-256
/// \details LSH_TYPE_256 is the magic value 0x0000020 defined in lsh.cpp.
LSH256() : LSH256_Base(0x0000020, DIGESTSIZE) { Restart(); }
std::string AlgorithmName() const { return StaticAlgorithmName(); }
};
/// \brief LSH-384 and LSH-512 hash base class
/// \details LSH512_Base is the base class for both LSH-384 and LSH-512
/// \since Crypto++ 8.6
class LSH512_Base : public HashTransformation
{
public:
/// \brief Block size, in bytes
/// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
CRYPTOPP_CONSTANT(BLOCKSIZE = 256);
virtual ~LSH512_Base() {}
unsigned int BlockSize() const { return BLOCKSIZE; }
unsigned int DigestSize() const { return m_digestSize; }
unsigned int OptimalDataAlignment() const { return GetAlignmentOf<word64>(); }
void Restart();
void Update(const byte *input, size_t size);
void TruncatedFinal(byte *hash, size_t size);
std::string AlgorithmProvider() const;
protected:
LSH512_Base(unsigned int algType, unsigned int digestSize)
: m_digestSize(digestSize) { m_state[80] = algType; }
protected:
// Working state is:
// * cv_l = 8 64-bit words
// * cv_r = 8 64-bit words
// * submsg_e_l = 8 64-bit words
// * submsg_e_r = 8 64-bit words
// * submsg_o_l = 8 64-bit words
// * submsg_o_r = 8 64-bit words
// * last_block = 32 64-bit words (256 bytes)
// * algType
// * remainingBitLength
FixedSizeSecBlock<word64, 80+2> m_state;
// word32 m_algType, m_remainingBitLength;
word32 m_digestSize;
};
/// \brief LSH-384 hash function
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
/// \since Crypto++ 8.6
class LSH384 : public LSH512_Base
{
public:
/// \brief Digest size, in bytes
/// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 48
CRYPTOPP_CONSTANT(DIGESTSIZE = 48);
/// \brief Block size, in bytes
/// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
/// \brief The algorithm's name
/// \return the standard algorithm name
/// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
/// Some algorithms do not have standard names yet. For example, there is no standard
/// algorithm name for Shoup's ECIES.
/// \note StaticAlgorithmName is not universally implemented yet.
static std::string StaticAlgorithmName() { return "LSH-384"; }
/// \brief Construct a LSH-384
/// \details LSH_TYPE_384 is the magic value 0x0010030 defined in lsh.cpp.
LSH384() : LSH512_Base(0x0010030, DIGESTSIZE) { Restart(); }
std::string AlgorithmName() const { return StaticAlgorithmName(); }
};
/// \brief LSH-512 hash function
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
/// \since Crypto++ 8.6
class LSH512 : public LSH512_Base
{
public:
/// \brief Digest size, in bytes
/// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 64
CRYPTOPP_CONSTANT(DIGESTSIZE = 64);
/// \brief Block size, in bytes
/// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
/// \brief The algorithm's name
/// \return the standard algorithm name
/// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
/// Some algorithms do not have standard names yet. For example, there is no standard
/// algorithm name for Shoup's ECIES.
/// \note StaticAlgorithmName is not universally implemented yet.
static std::string StaticAlgorithmName() { return "LSH-512"; }
/// \brief Construct a LSH-512
/// \details LSH_TYPE_512 is the magic value 0x0010040 defined in lsh.cpp.
LSH512() : LSH512_Base(0x0010040, DIGESTSIZE) { Restart(); }
std::string AlgorithmName() const { return StaticAlgorithmName(); }
};
/// \brief LSH-512-256 hash function
/// \sa <A HREF="https://seed.kisa.or.kr/kisa/algorithm/EgovLSHInfo.do">LSH</A>
/// on the Korea Internet & Security Agency (KISA) website.
/// \since Crypto++ 8.6
class LSH512_256 : public LSH512_Base
{
public:
/// \brief Digest size, in bytes
/// \details LSH_512 uses LSH_GET_HASHBYTE(algType) for digest size, which is 32
CRYPTOPP_CONSTANT(DIGESTSIZE = 32);
/// \brief Block size, in bytes
/// \details LSH_512 uses LSH512_MSG_BLK_BYTE_LEN for block size, which is 256
CRYPTOPP_CONSTANT(BLOCKSIZE = LSH512_Base::BLOCKSIZE);
/// \brief The algorithm's name
/// \return the standard algorithm name
/// \details The standard algorithm name can be a name like <tt>AES</tt> or <tt>AES/GCM</tt>.
/// Some algorithms do not have standard names yet. For example, there is no standard
/// algorithm name for Shoup's ECIES.
/// \note StaticAlgorithmName is not universally implemented yet.
static std::string StaticAlgorithmName() { return "LSH-512-256"; }
/// \brief Construct a LSH-512-256
/// \details LSH_TYPE_512_256 is the magic value 0x0010020 defined in lsh.cpp.
LSH512_256() : LSH512_Base(0x0010020, DIGESTSIZE) { Restart(); }
std::string AlgorithmName() const { return StaticAlgorithmName(); }
};
NAMESPACE_END
#endif // CRYPTOPP_LSH_H
FixedSizeSecBlock
Fixed size stack-based SecBlock.
Definition secblock.h:1246
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition cryptlib.h:1118
LSH224
LSH-224 hash function.
Definition lsh.h:78
LSH224::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:85
LSH224::DIGESTSIZE
static const int DIGESTSIZE
Digest size, in bytes.
Definition lsh.h:82
LSH224::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:93
LSH224::LSH224
LSH224()
Construct a LSH-224.
Definition lsh.h:97
LSH224::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:99
LSH256_Base
LSH-224 and LSH-256 hash base class.
Definition lsh.h:35
LSH256_Base::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
LSH256_Base::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:39
LSH256_Base::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
LSH256_Base::Update
void Update(const byte *input, size_t size)
Updates a hash with additional input.
LSH256_Base::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition lsh.h:45
LSH256_Base::Restart
void Restart()
Restart the hash.
LSH256_Base::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition lsh.h:43
LSH256_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition lsh.h:44
LSH256
LSH-256 hash function.
Definition lsh.h:107
LSH256::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:114
LSH256::DIGESTSIZE
static const int DIGESTSIZE
Digest size, in bytes.
Definition lsh.h:111
LSH256::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:122
LSH256::LSH256
LSH256()
Construct a LSH-256.
Definition lsh.h:126
LSH256::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:128
LSH384
LSH-384 hash function.
Definition lsh.h:178
LSH384::LSH384
LSH384()
Construct a LSH-384.
Definition lsh.h:197
LSH384::DIGESTSIZE
static const int DIGESTSIZE
Digest size, in bytes.
Definition lsh.h:182
LSH384::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:185
LSH384::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:199
LSH384::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:193
LSH512_256
LSH-512-256 hash function.
Definition lsh.h:236
LSH512_256::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:243
LSH512_256::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:257
LSH512_256::LSH512_256
LSH512_256()
Construct a LSH-512-256.
Definition lsh.h:255
LSH512_256::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:251
LSH512_256::DIGESTSIZE
static const int DIGESTSIZE
Digest size, in bytes.
Definition lsh.h:240
LSH512_Base
LSH-384 and LSH-512 hash base class.
Definition lsh.h:135
LSH512_Base::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:139
LSH512_Base::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition lsh.h:145
LSH512_Base::Restart
void Restart()
Restart the hash.
LSH512_Base::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition lsh.h:144
LSH512_Base::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
LSH512_Base::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
LSH512_Base::Update
void Update(const byte *input, size_t size)
Updates a hash with additional input.
LSH512_Base::BlockSize
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition lsh.h:143
LSH512
LSH-512 hash function.
Definition lsh.h:207
LSH512::LSH512
LSH512()
Construct a LSH-512.
Definition lsh.h:226
LSH512::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition lsh.h:228
LSH512::StaticAlgorithmName
static std::string StaticAlgorithmName()
The algorithm's name.
Definition lsh.h:222
LSH512::BLOCKSIZE
static const int BLOCKSIZE
Block size, in bytes.
Definition lsh.h:214
LSH512::DIGESTSIZE
static const int DIGESTSIZE
Digest size, in bytes.
Definition lsh.h:211
word32
unsigned int word32
32-bit unsigned datatype
Definition config_int.h:72
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
CryptoPP
Crypto++ library namespace.
secblock.h
Classes and functions for secure memory allocations.