This commit is contained in:
2025-11-24 14:19:51 +05:30
commit f5c1412b28
6734 changed files with 1527575 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
HMAC-MD5 is still secure despite the ease of producing collisions in MD5.
See Mihir Bellare paper "New Proofs for NMAC and HMAC: Security without Collision-Resistance" (June 2006)
Using HMAC construction:
HMAC(x) = h(k || p1 || h(k || p2 || x))
h() = MD5 hash
p1,p2 = distinct padding to bring k up to the block size
p1 = 0x36 repeated, p2 = 0x5c repeated
Diverges from usual implementation by using little-endian rather than big-endian input
*/
#ifndef HMAC_MD5_HPP
#define HMAC_MD5_HPP
#include <cat/crypt/hash/ICryptHash.hpp>
namespace cat {
class CAT_EXPORT HMAC_MD5 : public ICryptHash
{
protected:
static const int DIGEST_BYTES = 16;
static const int WORK_BYTES = 64; // bytes in one block
static const int WORK_WORDS = WORK_BYTES / sizeof(u32);
u32 CachedInitialState[4]; // Cached state for H(K||inner padding)
u32 CachedFinalState[4]; // Cached state for H(K||outer padding)
u64 byte_counter;
u32 State[4];
u8 Work[WORK_BYTES];
int used_bytes;
void HashComputation(const void *message, int blocks, u32 *NextState);
// Unsupported modes
bool BeginKey(int /*bits*/) { return false; }
bool BeginKDF() { return false; }
bool BeginPRNG() { return false; }
public:
~HMAC_MD5();
bool SetKey(ICryptHash *parent);
void RekeyFromMD5(HMAC_MD5 *parent);
bool BeginMAC();
void Crunch(const void *message, int bytes);
void End();
// TODO: Strengthening is not supported right now
void Generate(void *out, int bytes, int strengthening_rounds = 0);
};
} // namespace cat
#endif // HMAC_MD5_HPP

View File

@ -0,0 +1,79 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
// 06/15/09 began
#ifndef CAT_I_CRYPT_HASH_HPP
#define CAT_I_CRYPT_HASH_HPP
#include <cat/Platform.hpp>
#include <cstring>
namespace cat {
// Cryptographic hash functions of any size will derive from ICryptoHash and implement its public methods
class CAT_EXPORT ICryptHash
{
protected:
int digest_bytes;
public:
virtual ~ICryptHash() {}
// Returns the number of bytes in a message digest produced by this hash
CAT_INLINE int GetDigestByteCount() { return digest_bytes; }
CAT_INLINE void CrunchString(const char *s) { Crunch(s, (int)std::strlen(s) + 1); }
public:
// Begin a new key
virtual bool BeginKey(int bits) = 0;
// Start from an existing key
virtual bool SetKey(ICryptHash *parent) = 0;
// Begin hash function in MAC, KDF, or PRNG mode
virtual bool BeginMAC() = 0;
virtual bool BeginKDF() = 0;
virtual bool BeginPRNG() = 0;
// Crunch some message bytes
virtual void Crunch(const void *message, int bytes) = 0;
// Finalize the hash and prepare to generate output
virtual void End() = 0;
// Extended hash output mode
virtual void Generate(void *out, int bytes, int strengthening_rounds = 0) = 0;
};
} // namespace cat
#endif // CAT_I_CRYPT_HASH_HPP

View File

@ -0,0 +1,108 @@
/*
Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Bruce Schneier's SHA-3 candidate Skein hash function
http://www.skein-hash.info/
*/
#ifndef CAT_SKEIN_HPP
#define CAT_SKEIN_HPP
#include <cat/crypt/hash/ICryptHash.hpp>
namespace cat {
// Base class for various versions of Skein
class CAT_EXPORT Skein : public ICryptHash
{
protected:
// Tweak word 1 bit field starting positions
static const int T1_POS_TREE_LVL = 112-64; // bits 112..118 : level in hash tree
static const int T1_POS_BIT_PAD = 119-64; // bit 119 : partial final input byte
static const int T1_POS_BLK_TYPE = 120-64; // bits 120..125 : type field
static const int T1_POS_FIRST = 126-64; // bits 126 : first block flag
static const int T1_POS_FINAL = 127-64; // bit 127 : final block flag
// Tweak word 1 bit field masks
static const u64 T1_MASK_FIRST = (u64)1 << T1_POS_FIRST;
static const u64 T1_MASK_FINAL = (u64)1 << T1_POS_FINAL;
static const u64 T1_MASK_BIT_PAD = (u64)1 << T1_POS_BIT_PAD;
static const u64 T1_MASK_TREE_LVL = (u64)0x7F << T1_POS_TREE_LVL;
static const u64 T1_MASK_BLK_TYPE = (u64)63 << T1_POS_BLK_TYPE;
static const int BLK_TYPE_KEY = 0; // key, for MAC and KDF
static const int BLK_TYPE_CFG = 4; // configuration block
static const int BLK_TYPE_PERS = 8; // personalization string
static const int BLK_TYPE_PK = 12; // public key (for digital signature hashing)
static const int BLK_TYPE_KDF = 16; // key identifier for KDF
static const int BLK_TYPE_NONCE = 20; // nonce for PRNG
static const int BLK_TYPE_MSG = 48; // message processing
static const int BLK_TYPE_OUT = 63; // output stage
static const u32 ID_STRING_LE = 0x33414853;
static const u32 SKEIN_VERSION = 1;
static const u64 SCHEMA_VER = ((u64)SKEIN_VERSION << 32) | ID_STRING_LE;
static const int MAX_BITS = 512;
static const int MAX_WORDS = MAX_BITS / 64;
static const int MAX_BYTES = MAX_BITS / 8;
u64 Tweak[2];
u64 State[MAX_WORDS];
u8 Work[MAX_BYTES];
int used_bytes, digest_words;
u64 output_block_counter;
bool output_prng_mode;
typedef void (Skein::*HashComputation)(const void *message, int blocks, u32 byte_count, u64 *NextState);
void HashComputation256(const void *message, int blocks, u32 byte_count, u64 *NextState);
void HashComputation512(const void *message, int blocks, u32 byte_count, u64 *NextState);
HashComputation hash_func;
void GenerateInitialState(int bits);
public:
~Skein();
bool BeginKey(int bits);
bool SetKey(ICryptHash *parent);
bool BeginMAC();
bool BeginKDF();
bool BeginPRNG();
void Crunch(const void *message, int bytes);
void End();
void Generate(void *out, int bytes, int strengthening_rounds = 0);
};
} // namespace cat
#endif // CAT_SKEIN_HPP