mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:47:34 +00:00
LibCrypto+LibTLS: Reformat everything
I have no idea how I'll squash _this_ one...
This commit is contained in:
parent
a1e1570552
commit
05e2c7d9cf
14 changed files with 1434 additions and 1426 deletions
|
@ -33,147 +33,148 @@ static constexpr u8 zeros[] { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|||
namespace Crypto {
|
||||
namespace PK {
|
||||
|
||||
template <typename HashFunction, size_t SaltSize>
|
||||
class EMSA_PSS : public Code<HashFunction> {
|
||||
public:
|
||||
template <typename... Args>
|
||||
EMSA_PSS(Args... args)
|
||||
: Code<HashFunction>(args...)
|
||||
{
|
||||
m_buffer = ByteBuffer::wrap(m_data_buffer, sizeof(m_data_buffer));
|
||||
template<typename HashFunction, size_t SaltSize>
|
||||
class EMSA_PSS : public Code<HashFunction> {
|
||||
public:
|
||||
template<typename... Args>
|
||||
EMSA_PSS(Args... args)
|
||||
: Code<HashFunction>(args...)
|
||||
{
|
||||
m_buffer = ByteBuffer::wrap(m_data_buffer, sizeof(m_data_buffer));
|
||||
}
|
||||
|
||||
static constexpr auto SaltLength = SaltSize;
|
||||
|
||||
virtual void encode(const ByteBuffer& in, ByteBuffer& out, size_t em_bits) override
|
||||
{
|
||||
// FIXME: we're supposed to check if in.size() > HashFunction::input_limitation
|
||||
// however, all of our current hash functions can hash unlimited blocks
|
||||
auto& hash_fn = this->hasher();
|
||||
hash_fn.update(in);
|
||||
auto message_hash = hash_fn.digest();
|
||||
auto hash_length = hash_fn.DigestSize;
|
||||
auto em_length = (em_bits + 7) / 8;
|
||||
u8 salt[SaltLength];
|
||||
|
||||
arc4random_buf(salt, SaltLength);
|
||||
|
||||
if (em_length < hash_length + SaltLength + 2) {
|
||||
dbg() << "Ooops...encoding error";
|
||||
return;
|
||||
}
|
||||
|
||||
static constexpr auto SaltLength = SaltSize;
|
||||
m_buffer.overwrite(0, zeros, 8);
|
||||
m_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
|
||||
m_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
|
||||
|
||||
virtual void encode(const ByteBuffer& in, ByteBuffer& out, size_t em_bits) override
|
||||
{
|
||||
// FIXME: we're supposed to check if in.size() > HashFunction::input_limitation
|
||||
// however, all of our current hash functions can hash unlimited blocks
|
||||
auto& hash_fn = this->hasher();
|
||||
hash_fn.update(in);
|
||||
auto message_hash = hash_fn.digest();
|
||||
auto hash_length = hash_fn.DigestSize;
|
||||
auto em_length = (em_bits + 7) / 8;
|
||||
u8 salt[SaltLength];
|
||||
hash_fn.update(m_buffer);
|
||||
auto hash = hash_fn.digest();
|
||||
|
||||
arc4random_buf(salt, SaltLength);
|
||||
u8 DB_data[em_length - HashFunction::DigestSize - 1];
|
||||
auto DB = ByteBuffer::wrap(DB_data, em_length - HashFunction::DigestSize - 1);
|
||||
auto DB_offset = 0;
|
||||
|
||||
if (em_length < hash_length + SaltLength + 2) {
|
||||
dbg() << "Ooops...encoding error";
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < em_length - SaltLength - HashFunction::DigestSize - 2; ++i)
|
||||
DB[DB_offset++] = 0;
|
||||
|
||||
m_buffer.overwrite(0, zeros, 8);
|
||||
m_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
|
||||
m_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
|
||||
DB[DB_offset++] = 0x01;
|
||||
|
||||
hash_fn.update(m_buffer);
|
||||
auto hash = hash_fn.digest();
|
||||
DB.overwrite(DB_offset, salt, SaltLength);
|
||||
|
||||
u8 DB_data[em_length - HashFunction::DigestSize - 1];
|
||||
auto DB = ByteBuffer::wrap(DB_data, em_length - HashFunction::DigestSize - 1);
|
||||
auto DB_offset = 0;
|
||||
auto mask_length = em_length - HashFunction::DigestSize - 1;
|
||||
|
||||
for (size_t i = 0; i < em_length - SaltLength - HashFunction::DigestSize - 2; ++i)
|
||||
DB[DB_offset++] = 0;
|
||||
u8 DB_mask[mask_length];
|
||||
auto DB_mask_buffer = ByteBuffer::wrap(DB_mask, mask_length);
|
||||
// FIXME: we should probably allow reading from u8*
|
||||
auto hash_buffer = ByteBuffer::wrap(hash.data, HashFunction::DigestSize);
|
||||
MGF1(hash_buffer, mask_length, DB_mask_buffer);
|
||||
|
||||
DB[DB_offset++] = 0x01;
|
||||
for (size_t i = 0; i < DB.size(); ++i)
|
||||
DB_data[i] ^= DB_mask[i];
|
||||
|
||||
DB.overwrite(DB_offset, salt, SaltLength);
|
||||
auto count = (8 - (em_length * 8 - em_bits));
|
||||
DB_data[0] &= (0xff >> count) << count;
|
||||
|
||||
auto mask_length = em_length - HashFunction::DigestSize - 1;
|
||||
out.overwrite(0, DB.data(), DB.size());
|
||||
out.overwrite(DB.size(), hash.data, hash_fn.DigestSize);
|
||||
out[DB.size() + hash_fn.DigestSize] = 0xbc;
|
||||
}
|
||||
|
||||
u8 DB_mask[mask_length];
|
||||
auto DB_mask_buffer = ByteBuffer::wrap(DB_mask, mask_length);
|
||||
// FIXME: we should probably allow reading from u8*
|
||||
auto hash_buffer = ByteBuffer::wrap(hash.data, HashFunction::DigestSize);
|
||||
MGF1(hash_buffer, mask_length, DB_mask_buffer);
|
||||
virtual VerificationConsistency verify(const ByteBuffer& msg, const ByteBuffer& emsg, size_t em_bits) override
|
||||
{
|
||||
auto& hash_fn = this->hasher();
|
||||
hash_fn.update(msg);
|
||||
auto message_hash = hash_fn.digest();
|
||||
|
||||
for (size_t i = 0; i < DB.size(); ++i)
|
||||
DB_data[i] ^= DB_mask[i];
|
||||
if (emsg.size() < HashFunction::DigestSize + SaltLength + 2)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
auto count = (8 - (em_length * 8 - em_bits));
|
||||
DB_data[0] &= (0xff >> count) << count;
|
||||
if (emsg[emsg.size() - 1] != 0xbc)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
out.overwrite(0, DB.data(), DB.size());
|
||||
out.overwrite(DB.size(), hash.data, hash_fn.DigestSize);
|
||||
out[DB.size() + hash_fn.DigestSize] = 0xbc;
|
||||
auto mask_length = emsg.size() - HashFunction::DigestSize - 1;
|
||||
auto masked_DB = emsg.slice_view(0, mask_length);
|
||||
auto H = emsg.slice_view(mask_length, HashFunction::DigestSize);
|
||||
|
||||
auto length_to_check = 8 * emsg.size() - em_bits;
|
||||
auto octet = masked_DB[0];
|
||||
for (size_t i = 0; i < length_to_check; ++i)
|
||||
if ((octet >> (8 - i)) & 0x01)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
u8 DB_mask[mask_length];
|
||||
auto DB_mask_buffer = ByteBuffer::wrap(DB_mask, mask_length);
|
||||
MGF1(H, mask_length, DB_mask_buffer);
|
||||
|
||||
u8 DB[mask_length];
|
||||
|
||||
for (size_t i = 0; i < mask_length; ++i)
|
||||
DB[i] = masked_DB[i] ^ DB_mask[i];
|
||||
|
||||
DB[0] &= 0xff >> (8 - length_to_check);
|
||||
|
||||
auto check_octets = emsg.size() - HashFunction::DigestSize - SaltLength - 2;
|
||||
for (size_t i = 0; i < check_octets; ++i) {
|
||||
if (DB[i])
|
||||
return VerificationConsistency::Inconsistent;
|
||||
}
|
||||
|
||||
virtual VerificationConsistency verify(const ByteBuffer& msg, const ByteBuffer& emsg, size_t em_bits) override
|
||||
{
|
||||
auto& hash_fn = this->hasher();
|
||||
hash_fn.update(msg);
|
||||
auto message_hash = hash_fn.digest();
|
||||
if (DB[check_octets + 1] != 0x01)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
if (emsg.size() < HashFunction::DigestSize + SaltLength + 2)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
auto* salt = DB + mask_length - SaltLength;
|
||||
u8 m_prime[8 + HashFunction::DigestSize + SaltLength] { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
if (emsg[emsg.size() - 1] != 0xbc)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
auto m_prime_buffer = ByteBuffer::wrap(m_prime, sizeof(m_prime));
|
||||
|
||||
auto mask_length = emsg.size() - HashFunction::DigestSize - 1;
|
||||
auto masked_DB = emsg.slice_view(0, mask_length);
|
||||
auto H = emsg.slice_view(mask_length, HashFunction::DigestSize);
|
||||
m_prime_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
|
||||
m_prime_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
|
||||
|
||||
auto length_to_check = 8 * emsg.size() - em_bits;
|
||||
auto octet = masked_DB[0];
|
||||
for (size_t i = 0; i < length_to_check; ++i)
|
||||
if ((octet >> (8 - i)) & 0x01)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
hash_fn.update(m_prime_buffer);
|
||||
auto H_prime = hash_fn.digest();
|
||||
|
||||
u8 DB_mask[mask_length];
|
||||
auto DB_mask_buffer = ByteBuffer::wrap(DB_mask, mask_length);
|
||||
MGF1(H, mask_length, DB_mask_buffer);
|
||||
if (__builtin_memcmp(message_hash.data, H_prime.data, HashFunction::DigestSize))
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
u8 DB[mask_length];
|
||||
return VerificationConsistency::Consistent;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < mask_length; ++i)
|
||||
DB[i] = masked_DB[i] ^ DB_mask[i];
|
||||
|
||||
DB[0] &= 0xff >> (8 - length_to_check);
|
||||
|
||||
auto check_octets = emsg.size() - HashFunction::DigestSize - SaltLength - 2;
|
||||
for (size_t i = 0; i < check_octets; ++i)
|
||||
if (DB[i])
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
if (DB[check_octets + 1] != 0x01)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
auto* salt = DB + mask_length - SaltLength;
|
||||
u8 m_prime[8 + HashFunction::DigestSize + SaltLength] { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
auto m_prime_buffer = ByteBuffer::wrap(m_prime, sizeof(m_prime));
|
||||
|
||||
m_prime_buffer.overwrite(8, message_hash.data, HashFunction::DigestSize);
|
||||
m_prime_buffer.overwrite(8 + HashFunction::DigestSize, salt, SaltLength);
|
||||
|
||||
hash_fn.update(m_prime_buffer);
|
||||
auto H_prime = hash_fn.digest();
|
||||
|
||||
if (__builtin_memcmp(message_hash.data, H_prime.data, HashFunction::DigestSize))
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
||||
return VerificationConsistency::Consistent;
|
||||
void MGF1(const ByteBuffer& seed, size_t length, ByteBuffer& out)
|
||||
{
|
||||
auto& hash_fn = this->hasher();
|
||||
ByteBuffer T = ByteBuffer::create_zeroed(0);
|
||||
for (size_t counter = 0; counter < length / HashFunction::DigestSize - 1; ++counter) {
|
||||
hash_fn.update(seed);
|
||||
hash_fn.update((u8*)&counter, 4);
|
||||
T.append(hash_fn.digest().data, HashFunction::DigestSize);
|
||||
}
|
||||
out.overwrite(0, T.data(), length);
|
||||
}
|
||||
|
||||
void MGF1(const ByteBuffer& seed, size_t length, ByteBuffer& out)
|
||||
{
|
||||
auto& hash_fn = this->hasher();
|
||||
ByteBuffer T = ByteBuffer::create_zeroed(0);
|
||||
for (size_t counter = 0; counter < length / HashFunction::DigestSize - 1; ++counter) {
|
||||
hash_fn.update(seed);
|
||||
hash_fn.update((u8*)&counter, 4);
|
||||
T.append(hash_fn.digest().data, HashFunction::DigestSize);
|
||||
}
|
||||
out.overwrite(0, T.data(), length);
|
||||
}
|
||||
|
||||
private:
|
||||
u8 m_data_buffer[8 + HashFunction::DigestSize + SaltLength];
|
||||
ByteBuffer m_buffer;
|
||||
};
|
||||
private:
|
||||
u8 m_data_buffer[8 + HashFunction::DigestSize + SaltLength];
|
||||
ByteBuffer m_buffer;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue