Jamie Reece Wilson
3732352b4e
[+] AuHashing::Blake2S_32 [+] AuHashing::Blake2S_28 [+] AuHashing::Blake2S_20 [+] AuHashing::Blake2S_16 [+] AuHashing::Blake2B_64 [+] AuHashing::Blake2B_48 [+] AuHashing::Blake2B_32 [+] AuHashing::Blake2B_20 [+] AuHashing::GetHashLength [+] AuHashing::GetHashBits [+] AuHashing::IHashStream::GetHashType
293 lines
9.7 KiB
C++
293 lines
9.7 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuDigests.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuDigests.hpp"
|
|
#include <tomcrypt.h>
|
|
|
|
namespace Aurora::Hashing
|
|
{
|
|
#define DIGEST_CHECK(n) SysAssert(n == CRYPT_OK)
|
|
|
|
AUKN_SYM void MD4(const AuMemoryViewRead &read, AuArray<AuUInt8, 16> &md4)
|
|
{
|
|
hash_state md;
|
|
DIGEST_CHECK(md4_init(&md));
|
|
DIGEST_CHECK(md4_process(&md, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(md4_done(&md, md4.data()));
|
|
}
|
|
|
|
AUKN_SYM void MD5(const AuMemoryViewRead &read, AuArray<AuUInt8, 16> &md5)
|
|
{
|
|
hash_state md;
|
|
DIGEST_CHECK(md5_init(&md));
|
|
DIGEST_CHECK(md5_process(&md, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(md5_done(&md, md5.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA1(const AuMemoryViewRead &read, AuArray<AuUInt8, 20> &sha1)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha1_init(&hs));
|
|
DIGEST_CHECK(sha1_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha1_done(&hs, sha1.data()));
|
|
}
|
|
|
|
AUKN_SYM void Tiger(const AuMemoryViewRead &read, AuArray<AuUInt8, 24> &tiger)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(tiger_init(&hs));
|
|
DIGEST_CHECK(tiger_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(tiger_done(&hs, tiger.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA2(const AuMemoryViewRead &read, AuArray<AuUInt8, 32> &sha2)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha256_init(&hs));
|
|
DIGEST_CHECK(sha256_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha256_done(&hs, sha2.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA2_48(const AuMemoryViewRead &read, AuArray<AuUInt8, 48> &sha2)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha384_init(&hs));
|
|
DIGEST_CHECK(sha384_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha384_done(&hs, sha2.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA2_64(const AuMemoryViewRead &read, AuArray<AuUInt8, 64> &sha2)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha512_init(&hs));
|
|
DIGEST_CHECK(sha512_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha512_done(&hs, sha2.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA3_28(const AuMemoryViewRead &read, AuArray<AuUInt8, 28> &sha3)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha3_224_init(&hs));
|
|
DIGEST_CHECK(sha3_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha3_done(&hs, sha3.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA3_32(const AuMemoryViewRead &read, AuArray<AuUInt8, 32> &sha3)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha3_256_init(&hs));
|
|
DIGEST_CHECK(sha3_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha3_done(&hs, sha3.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA3_48(const AuMemoryViewRead &read, AuArray<AuUInt8, 48> &sha3)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha3_384_init(&hs));
|
|
DIGEST_CHECK(sha3_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha3_done(&hs, sha3.data()));
|
|
}
|
|
|
|
AUKN_SYM void SHA3_64(const AuMemoryViewRead &read, AuArray<AuUInt8, 64> &sha3)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(sha3_512_init(&hs));
|
|
DIGEST_CHECK(sha3_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(sha3_done(&hs, sha3.data()));
|
|
}
|
|
|
|
AUKN_SYM void RMD128(const AuMemoryViewRead &read, AuArray<AuUInt8, 16> &rmd128)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(rmd128_init(&hs));
|
|
DIGEST_CHECK(rmd128_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(rmd128_done(&hs, rmd128.data()));
|
|
}
|
|
|
|
AUKN_SYM void RMD160(const AuMemoryViewRead &read, AuArray<AuUInt8, 20> &rmd160)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(rmd160_init(&hs));
|
|
DIGEST_CHECK(rmd160_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(rmd160_done(&hs, rmd160.data()));
|
|
}
|
|
|
|
AUKN_SYM void RMD256(const AuMemoryViewRead &read, AuArray<AuUInt8, 32> &rmd256)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(rmd256_init(&hs));
|
|
DIGEST_CHECK(rmd256_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(rmd256_done(&hs, rmd256.data()));
|
|
}
|
|
|
|
AUKN_SYM void RMD320(const AuMemoryViewRead &read, AuArray<AuUInt8, 40> &rmd320)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(rmd320_init(&hs));
|
|
DIGEST_CHECK(rmd320_process(&hs, reinterpret_cast<const unsigned char *>(read.ptr), read.length));
|
|
DIGEST_CHECK(rmd320_done(&hs, rmd320.data()));
|
|
}
|
|
|
|
AUKN_SYM void Whirlpool(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 64> &whirlpool)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(whirlpool_init(&hs));
|
|
DIGEST_CHECK(whirlpool_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(whirlpool_done(&hs, whirlpool.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2S_32(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 32> &blake2S)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2s_256_init(&hs));
|
|
DIGEST_CHECK(blake2s_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2s_done(&hs, blake2S.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2S_28(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 28> &blake2S)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2s_224_init(&hs));
|
|
DIGEST_CHECK(blake2s_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2s_done(&hs, blake2S.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2S_20(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 20> &blake2S)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2s_160_init(&hs));
|
|
DIGEST_CHECK(blake2s_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2s_done(&hs, blake2S.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2S_16(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 16> &blake2S)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2s_128_init(&hs));
|
|
DIGEST_CHECK(blake2s_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2s_done(&hs, blake2S.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2B_64(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 64> &blake2B)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2b_512_init(&hs));
|
|
DIGEST_CHECK(blake2b_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2b_done(&hs, blake2B.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2B_48(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 48> &blake2B)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2b_384_init(&hs));
|
|
DIGEST_CHECK(blake2b_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2b_done(&hs, blake2B.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2B_32(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 32> &blake2B)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2b_256_init(&hs));
|
|
DIGEST_CHECK(blake2b_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2b_done(&hs, blake2B.data()));
|
|
}
|
|
|
|
AUKN_SYM void Blake2B_20(const Memory::MemoryViewRead &toHash, AuArray<AuUInt8, 20> &blake2B)
|
|
{
|
|
hash_state hs;
|
|
DIGEST_CHECK(blake2b_160_init(&hs));
|
|
DIGEST_CHECK(blake2b_process(&hs, reinterpret_cast<const unsigned char *>(toHash.ptr), toHash.length));
|
|
DIGEST_CHECK(blake2b_done(&hs, blake2B.data()));
|
|
}
|
|
|
|
AUKN_SYM AuUInt8 GetHashLength(EHashType eType)
|
|
{
|
|
switch (eType)
|
|
{
|
|
case EHashType::eMD4:
|
|
return 16;
|
|
|
|
case EHashType::eMD5:
|
|
return 16;
|
|
|
|
case EHashType::eSHA1:
|
|
return 20;
|
|
|
|
case EHashType::eSHA2_48:
|
|
return 48;
|
|
|
|
case EHashType::eSHA2_32:
|
|
return 32;
|
|
|
|
case EHashType::eSHA2_64:
|
|
return 64;
|
|
|
|
case EHashType::eSHA3_28:
|
|
return 28;
|
|
|
|
case EHashType::eSHA3_48:
|
|
return 48;
|
|
|
|
case EHashType::eSHA3_32:
|
|
return 32;
|
|
|
|
case EHashType::eSHA3_64:
|
|
return 64;
|
|
|
|
case EHashType::eTiger:
|
|
return 24;
|
|
|
|
case EHashType::eRMD128:
|
|
return 16;
|
|
|
|
case EHashType::eRMD160:
|
|
return 20;
|
|
|
|
case EHashType::eRMD256:
|
|
return 32;
|
|
|
|
case EHashType::eRMD320:
|
|
return 40;
|
|
|
|
case EHashType::eWhirlpool:
|
|
return 64;
|
|
|
|
case EHashType::eBlake2S_32:
|
|
return 32;
|
|
|
|
case EHashType::eBlake2S_28:
|
|
return 28;
|
|
|
|
case EHashType::eBlake2S_20:
|
|
return 20;
|
|
|
|
case EHashType::eBlake2S_16:
|
|
return 16;
|
|
|
|
case EHashType::eBlake2B_64:
|
|
return 64;
|
|
|
|
case EHashType::eBlake2B_48:
|
|
return 48;
|
|
|
|
case EHashType::eBlake2B_32:
|
|
return 32;
|
|
|
|
case EHashType::eBlake2B_20:
|
|
return 20;
|
|
}
|
|
|
|
SysPushErrorArg("Invalid hash type");
|
|
return 0;
|
|
}
|
|
|
|
AUKN_SYM AuUInt8 GetHashBits(EHashType eType)
|
|
{
|
|
return GetHashLength(eType) * 8;
|
|
}
|
|
} |