Reece Wilson
8a2947ffc5
[*] Refactor bcrypt api: HashPW[Ex] -> HashPassword[Ex] [+] ByteBuffer::GetOrAllocateLinearWriteable [+] ByteBuffer::Can[Read/Write](n) [+] ByteBuffer::GetLinear[Read/Writable]able(n) [*] Split RNG.cpp into two files [+] EHashType::eSHA2_48 (_32, _64 was already in place. missed 48/384 bit) [+] AuCrypto::HMAC and IHMACContext (AuHashing) [+] EHashType::eSHA3_28 [+] EHashType::eSHA3_32 [+] EHashType::eSHA3_48 [+] EHashType::eSHA3_64 (AuCrypto) [+] EHashType::eSHA2_48_384 [+] EHashType::eSHA2_64_512 [+] EHashType::eSHA3_28_224 [+] EHashType::eSHA3_32_256 [+] EHashType::eSHA3_48_384 [+] EHashType::eSHA3_64_512 [*] (IRandomDevice) class -> struct [*] Bugfix: cast in Promise<SuccessValue_t, ErrorValue_t>::WriteIntoError [+] Missing AuHashing namespace alias [*] Time util: pad ms when fraction of a second to 3 digits
88 lines
2.1 KiB
C++
88 lines
2.1 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: HMAC.cpp
|
|
Date: 2022-9-18
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include <Source/Crypto/Crypto.hpp>
|
|
#include "HMAC.hpp"
|
|
|
|
namespace Aurora::Crypto::HMAC
|
|
{
|
|
HMACContext::HMACContext(EHashType type) :
|
|
type_(type)
|
|
{
|
|
}
|
|
|
|
void HMACContext::Ingest(const Memory::MemoryViewRead &input)
|
|
{
|
|
SysAssert(::hmac_process(&this->state_, (const unsigned char *)input.ptr, input.length) != CRYPT_OK);
|
|
}
|
|
|
|
Memory::MemoryViewRead HMACContext::Finalize()
|
|
{
|
|
unsigned long outLength {};
|
|
if (::hmac_done(&this->state_, this->buffer_, &outLength) != CRYPT_OK)
|
|
{
|
|
SysPushErrorCrypt();
|
|
return {};
|
|
}
|
|
|
|
this->bFinished_ = true;
|
|
|
|
return { this->buffer_, outLength };
|
|
}
|
|
|
|
void HMACContext::Reset()
|
|
{
|
|
AuMemcpy(&this->state_, &this->referenceState_, sizeof(this->state_));
|
|
this->bFinished_ = false;
|
|
}
|
|
|
|
bool HMACContext::Init(const Memory::MemoryViewRead &input)
|
|
{
|
|
int iRet {};
|
|
|
|
int hash = HashMethodToId(this->type_);
|
|
if (hash == 0xFF)
|
|
{
|
|
SysPushErrorCrypt("invalid hash {}", this->type_);
|
|
return false;
|
|
}
|
|
|
|
iRet = ::hmac_init(&this->referenceState_, hash, (const unsigned char *)input.ptr, input.length);
|
|
if (iRet != CRYPT_OK)
|
|
{
|
|
SysPushErrorCrypt("error {}", iRet);
|
|
return false;
|
|
}
|
|
|
|
Reset();
|
|
return true;
|
|
}
|
|
|
|
AUKN_SYM IHMACContext *HMACNew(EHashType algorithm,
|
|
const Memory::MemoryViewRead &sharedSecret)
|
|
{
|
|
auto pContext = _new HMACContext(algorithm);
|
|
if (!pContext)
|
|
{
|
|
SysPushErrorMemory();
|
|
return {};
|
|
}
|
|
|
|
if (!pContext->Init(sharedSecret))
|
|
{
|
|
return {};
|
|
}
|
|
|
|
return pContext;
|
|
}
|
|
|
|
AUKN_SYM void HMACRelease(IHMACContext *pContext)
|
|
{
|
|
AuSafeDelete<HMACContext *>(pContext);
|
|
}
|
|
} |